@comparaonline/idempotence-lib
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

SetUp

Initialize redis with the provided utility

import { RedisConfig } from '@comparaonline/idempotence-lib/redis-config';
import { initializeIdempotenceRedis } from '@comparaonline/idempotence-lib/initialize-idempotence-redis';

const config: RedisConfig = {
  host: 'localhost'
};

initializeIdempotenceRedis(config);

Usage

executeWithStringKey

Signature:

executeWithStringKey<T = any>(key: string, fn: () => T, expireTime: number): Promise<T | undefined>
Param Type Description
key string The key to use to verify uniqueness.
fn function The function to execute in case us the first time the key is used.
expireTime number The expiration time in seconds to keep the result asociated to the key. By default is 10 days.

The result will be undefined if the function is called again with the same key and it didn't obtain the result for the first call yet.

Use example:

import { IdempotenceManager } from '@comparaonline/idempotence-lib/idempotence-manager';

const myFn = async (event: any) => {
  const result = IdempotenceManager.executeWithStringKey(event.id, () => {
    // process Event
    return Math.random();
  });
  console.log(result);
};

executeWithObjectKey

Signature:

executeWithObjectKey<T = any>(objToHash: any, fn: () => T, expireTime: number): Promise<T | undefined>
Param Type Description
objToHash any The object that will be hashed to a unique key.
fn function The function to execute in case us the first time the key is used.
expireTime number The expiration time in seconds to keep the result asociated to the key. By default is 10 days.

The result will be undefined if the function is called again with the same key and it didn't obtain the result for the first call yet.

Use example:

import { IdempotenceManager } from '@comparaonline/idempotence-lib/idempotence-manager';

const myFn = async (event: any) => {
  const result = IdempotenceManager.executeWithObjectKey(event, () => {
    // process Event
    return Math.random();
  });
  console.log(result);
};

removeStrokesByObjectKey

Signature:

removeStrokesByObjectKey(objToHash: any): Promise<void>
Param Type Description
objToHash any The object that will be hashed to a unique key.

The function removes the record associated with a given object from the Redis database.

Use example:

import { IdempotenceManager } from '@comparaonline/idempotence-lib';

const myFn = async (event: any) => {
  IdempotenceManager.removeStrokesByObjectKey(event);
};

Readme

Keywords

Package Sidebar

Install

npm i @comparaonline/idempotence-lib

Weekly Downloads

14

Version

1.1.1

License

MIT

Unpacked Size

33.6 kB

Total Files

32

Last publish

Collaborators

  • gnavarro
  • dsuarez_compa
  • pfariaz
  • ricardo.sosa
  • suarezcumare
  • fverag
  • comparaonline-dev
  • comparaonlineprivate
  • matotias
  • javierlara1989
  • smurua
  • eseceve
  • pablocompara