prime-sdk
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

Instalando pacote

yarn add prime-sdk

or

npm install prime-sdk

Queue (Sqs)

Install the module

@Module({
  imports: [
    QueueModule.forRoot({
      credentials: {
        accessKeyId: '000000000',
        secretAccessKey: '000000000',
      },
      endpoint: 'http://localhost:4566/',
      region: 'us-east-1',
    }),
  ],
  providers: [AppService],
  exports: [],
})
export class AppModule {}

example of usage

@Injectable()
export class AppService implements IConsumer {
  constructor(private readonly queueHandler: Queue) {
    this.queueHandler.handler({
      QueueUrl: `${'teste'}`,
      context: this,
      bulk: 1,
      waitTimeSeconds: 20,
    });
  }

  handleMessage(message: Message | Message[]): Promise<void> {
    console.log(message);
    return;
  }

  handleError(error: unknown): Promise<void> {
    console.log(error);
    return;
  }
}

Events (Sns)

Install the module

@Module({
  imports: [
    EventModule.forRoot({
      credentials: {
        accessKeyId: '000000000',
        secretAccessKey: '000000000',
      },
      endpoint: 'http://localhost:4566/',
      region: 'us-east-1',
    }),
  ],
  providers: [AppService],
  exports: [],
})
export class AppModule {}

example of usage

@Injectable()
export class AppService implements IConsumer {
  constructor(private readonly event: Events) {}

  public async trigger() {
    this.event.triggerEvent({
      topic: "arn:aws:sns:us-east-1:000000000000:teste-topic",
      message: 'teste',
    });
  }
}

Storage (S3)

Install the module

@Module({
  imports: [
    StorageModule.forRoot({
      credentials: {
        accessKeyId: '000000000',
        secretAccessKey: '000000000',
      },
      endpoint: 'http://localhost:4566/sample-bucket',
      region: 'us-east-1',
    })
  ],
  providers: [AppService],
  exports: [],
})
export class AppModule {}

Example of usage

@Injectable()
export class AppService implements IConsumer {
  constructor(private readonly storage: Storage) {}

  public async trigger() {

    const data = JSON.stringify({ teste: new Date().toISOString() });

    const file = Buffer.from(data, 'utf-8');

    this.storage.uploadFile({
      bucket: 'sample-bucket',
      file: file,
      nameFile: 'teste.txt',
      path: 'prime-sdk',
    });
  }
}

Cache (Redis)

  1. Install the module

  2. Define whether you are working with a Redis cluster (e.g., ElasticCache) or a simple Redis (e.g., Docker).

  3. Example of basic configuration :

3.1. Simple Redis:

CacheModule.forRoot({
      type: TypeRedis.SIMPLE,
      connection: {
        host: 'http://redis-service',
        port: 6379,
      },
      redisOptions: {
        password: '000000',
      },
    }),

3.2. Cluster Redis:

    CacheModule.forRoot({
      type: TypeRedis.CLUSTER,
      nodes: [
        {
          host: 'clustercfg.000-00000-cache.0000.use1.cache.amazonaws.com',
          port: 6379,
        },
      ],
      clusterOptions: {
        slotsRefreshTimeout: 3000,
        dnsLookup: (address, callback) => callback(null, address),
        redisOptions: {
          password: '00000',
          showFriendlyErrorStack: true,
          tls: {},
        },
      },
    }),

  1. Interfaces and Types:
enum TypeRedis {
  SIMPLE = 'simple',
  CLUSTER = 'cluster',
}

type Connection = {
  port: number;
  host: string;
};

type SimpleRedisSetup = {
  type: TypeRedis.SIMPLE;
  connection: Connection;
  redisOptions?: RedisOptions;
};

type ClusterRedisSetup = {
  type: TypeRedis.CLUSTER;
  nodes: Connection[];
  clusterOptions?: ClusterOptions;
};

interface ICache {
  set(
    key: string,
    value: Date | number | Buffer | string,
    ttl?: number,
  ): Promise<void>;

  get(key: string): Promise<string>;

  delete(key: string): Promise<void>;
}

For more information about RedisOptions and ClusterOptions, refer to the ioredis documentation:

https://www.npmjs.com/package/ioredis

  1. Example of usage
@Injectable()
export class AppService {
  constructor(@Inject(ICache) private readonly cache: ICache) {}

  async set( key: string,
    value: string | number | Date | Buffer,
    ttl?: number): Promise<void> {
    await this.cache.set(key, value, ttl);
  }

  async get(key: string): Promise<string> {
    return await this.cache.get(key);
  }

  async del(key: string): Promise<void> {
    return await this.cache.delete(key);
  }
}

Next steps

  • Interface for Class to facilite test

Readme

Keywords

none

Package Sidebar

Install

npm i prime-sdk

Weekly Downloads

88

Version

2.0.1

License

MIT

Unpacked Size

415 kB

Total Files

180

Last publish

Collaborators

  • emanuel_augusto
  • ruanvieira