1.0.5 • Published 1 year ago

@multiple-transaction-manager/redis v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@multiple-transaction-manager/redis

Redis context implementation for multiple-transaction-manager library.

Important Note: Redis transactions are not executed immediately like relational DB transactions. Instead, redis piles up a set of functions, and execute all of them at the end of the transaction at once. Therefore it may not be possible to use the result function of the RedisTask until the context is committed.

API

Classes

RedisContext

constructor(txnMngr, client)

  • txnMngr: {MultiTxnMngr} The multiple transaction manager to to bind with the context.
  • client: {RedisClientType} The Redis client.
  • Returns: {RedisContext} The created RedisContext instance.

addFunctionTask(execFunc)

Adds a task to the transaction manager.

  • execFunc: { (client: RedisClientType, txn: RedisClientMultiCommandType, task: Task) => RedisClientMultiCommandType} The function to be executes in promise. Redis client and current MultiCommand are provided to the function.
  • Returns: {RedisTask} Returns the created RedisTask instance.

RedisTask

constructor(context, execFunc)

  • context: {RedisContext} The RedisContext to to bind with the task.
  • execFunc: {(client: RedisClientType, txn: RedisClientMultiCommandType, task: Task) => RedisClientMultiCommandType} The function to be executes in promise. Redis client and current MultiCommand are provided to the function.
  • Returns: {RedisTask} The created RedisTask instance.

Example

https://github.com/kaplanke/mtxn-redis/blob/master/test/mtxn.redis.test.ts

    // init manager & context
    const txnMngr: MultiTxnMngr = new MultiTxnMngr();
    const redisContext = new RedisContext(txnMngr, client);

    // Add first step
    redisContext.addFunctionTask((_client, txn, _task) => txn.set("theKey1", "theValue1"));

    // Add second step
    redisContext.addFunctionTask((_client, txn, _task) => txn.set("theKey2", "theValue2"));

    // Add control step
    const controlTask: Task = redisContext.addFunctionTask((_client, txn, _task) => txn.keys("*"));

    await txnMngr.exec();

    expect(controlTask.getResult().length).toEqual(2);