prime-sdk
v1.4.1
Published
swiss army knife
Downloads
365
Readme
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)
Install the module
Define whether you are working with a Redis cluster (e.g., ElasticCache) or a simple Redis (e.g., Docker).
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: {},
},
},
}),
- 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
- 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