npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@onivoro/server-aws-redshift

v24.33.1

Published

AWS Redshift Data API integration for NestJS applications.

Readme

@onivoro/server-aws-redshift

AWS Redshift Data API integration for NestJS applications.

Installation

npm install @onivoro/server-aws-redshift

Overview

This library provides AWS Redshift Data API integration for NestJS applications, offering basic database operations, user management, and schema permissions.

Module Setup

import { Module } from '@nestjs/common';
import { ServerAwsRedshiftModule } from '@onivoro/server-aws-redshift';

@Module({
  imports: [
    ServerAwsRedshiftModule.configure()
  ]
})
export class AppModule {}

Configuration

The module uses environment-based configuration:

export class ServerAwsRedshiftConfig {
  AWS_REGION: string;
  AWS_PROFILE?: string;  // Optional AWS profile
  AWS_REDSHIFT_CLUSTER_IDENTIFIER: string;
  AWS_REDSHIFT_DATABASE: string;
  AWS_REDSHIFT_USER: string;
  AWS_REDSHIFT_WORKGROUP?: string;
  AWS_S3_BUCKET?: string;  // For data operations
}

Service

RedshiftDataService

The service provides Redshift database operations:

import { Injectable } from '@nestjs/common';
import { RedshiftDataService } from '@onivoro/server-aws-redshift';

@Injectable()
export class DataWarehouseService {
  constructor(private readonly redshiftService: RedshiftDataService) {}

  // Execute a query
  async executeQuery(sql: string) {
    const result = await this.redshiftService.query(sql);
    return result;
  }

  // Create a new user
  async createAnalystUser(username: string, password: string) {
    await this.redshiftService.createUser(username, password);
  }

  // Create a user group
  async createAnalystGroup(groupName: string) {
    await this.redshiftService.createGroup(groupName);
  }

  // Add user to group
  async addUserToGroup(username: string, groupName: string) {
    await this.redshiftService.addUserToGroup(username, groupName);
  }

  // Grant schema access
  async grantSchemaAccess(groupName: string, schemaName: string) {
    await this.redshiftService.grantSchemaPermissions(groupName, schemaName);
  }
}

Available Methods

Query Execution

  • query(sql: string) - Execute a SQL query and return results

User Management

  • createUser(username: string, password: string) - Create a new Redshift user
  • createGroup(groupName: string) - Create a new user group
  • addUserToGroup(username: string, groupName: string) - Add user to a group
  • grantSchemaPermissions(groupName: string, schemaName: string) - Grant schema access to a group

Workgroup Operations

  • getWorkgroupEndpoint(workgroupName: string) - Get the endpoint for a workgroup (used internally)

Direct Client Access

The service exposes the underlying Redshift Data client:

import { 
  ListDatabasesCommand,
  ListTablesCommand,
  DescribeTableCommand,
  GetStatementResultCommand
} from '@aws-sdk/client-redshift-data';

@Injectable()
export class AdvancedRedshiftService {
  constructor(private readonly redshiftService: RedshiftDataService) {}

  // List all databases
  async listDatabases() {
    const command = new ListDatabasesCommand({
      ClusterIdentifier: process.env.AWS_REDSHIFT_CLUSTER_IDENTIFIER,
      Database: process.env.AWS_REDSHIFT_DATABASE,
      DbUser: process.env.AWS_REDSHIFT_USER
    });
    
    return await this.redshiftService.redshiftDataApiClient.send(command);
  }

  // List tables in a schema
  async listTables(schemaName: string) {
    const command = new ListTablesCommand({
      ClusterIdentifier: process.env.AWS_REDSHIFT_CLUSTER_IDENTIFIER,
      Database: process.env.AWS_REDSHIFT_DATABASE,
      DbUser: process.env.AWS_REDSHIFT_USER,
      SchemaPattern: schemaName
    });
    
    return await this.redshiftService.redshiftDataApiClient.send(command);
  }
}

Example: Data Warehouse Operations

import { Module, Injectable } from '@nestjs/common';
import { ServerAwsRedshiftModule, RedshiftDataService } from '@onivoro/server-aws-redshift';

@Module({
  imports: [ServerAwsRedshiftModule.configure()],
  providers: [AnalyticsService]
})
export class AnalyticsModule {}

@Injectable()
export class AnalyticsService {
  constructor(private readonly redshiftService: RedshiftDataService) {}

  async setupAnalyticsUser(email: string) {
    const username = email.split('@')[0].replace(/[^a-zA-Z0-9]/g, '_');
    const tempPassword = `Temp123!${Math.random().toString(36).slice(-4)}`;
    
    try {
      // Create user
      await this.redshiftService.createUser(username, tempPassword);
      
      // Create or use existing analyst group
      const groupName = 'analysts';
      await this.redshiftService.createGroup(groupName);
      
      // Add user to group
      await this.redshiftService.addUserToGroup(username, groupName);
      
      // Grant permissions to analytics schema
      await this.redshiftService.grantSchemaPermissions(groupName, 'analytics');
      
      return {
        username,
        tempPassword,
        message: 'User created successfully. Please change password on first login.'
      };
    } catch (error) {
      console.error('Failed to setup user:', error);
      throw error;
    }
  }

  async runAnalyticsQuery(query: string) {
    // Validate query is read-only
    if (query.toLowerCase().includes('drop') || 
        query.toLowerCase().includes('delete') || 
        query.toLowerCase().includes('update')) {
      throw new Error('Only SELECT queries are allowed');
    }
    
    return await this.redshiftService.query(query);
  }
}

Environment Variables

# Required
AWS_REGION=us-east-1
AWS_REDSHIFT_CLUSTER_IDENTIFIER=my-redshift-cluster
AWS_REDSHIFT_DATABASE=mydb
AWS_REDSHIFT_USER=admin

# Optional
AWS_PROFILE=my-profile
AWS_REDSHIFT_WORKGROUP=my-workgroup
AWS_S3_BUCKET=my-data-bucket

Limitations

  • Basic query execution only (no advanced features like prepared statements)
  • Limited user management capabilities
  • No built-in connection pooling or query optimization
  • No support for advanced Redshift features (materialized views, stored procedures)
  • For advanced operations, use the exposed redshiftDataApiClient directly

Best Practices

  1. Security: Use least-privilege database users
  2. Query Validation: Always validate user input before executing queries
  3. Error Handling: Implement proper error handling for database operations
  4. Performance: Use appropriate cluster sizing and distribution keys
  5. Monitoring: Monitor query performance using Redshift console

License

MIT