{"_id":"@balmychan/nestjs-simple-redis-lock","name":"@balmychan/nestjs-simple-redis-lock","dist-tags":{"latest":"9.0.0"},"versions":{"9.0.0":{"name":"@balmychan/nestjs-simple-redis-lock","version":"9.0.0","description":"Distributed lock with single redis instance, simple and easy to use for Nestjs","main":"index.js","scripts":{"build":"rm -rf dist && tsc -p tsconfig.json","test":"echo \"Error: no test specified\" && exit 1"},"keywords":["nestjs","redis","redis lock","distributed lock"],"repository":{"type":"git","url":"git+ssh://git@github.com/balmychan/nestjs-simple-redis-lock.git"},"author":{"name":"balmychan"},"license":"MIT","homepage":"https://github.com/balmychan/nestjs-simple-redis-lock#readme","dependencies":{"@nestjs/common":"^9.0.11","@nestjs/core":"^9.0.11","@nestjs/terminus":"^8.0.6","debug":"^4.3.4","ioredis":"^5.2.3","rxjs":"^7.5.5"},"devDependencies":{"@types/debug":"^4.1.7","@types/node":"^16.11.26","typescript":"^4.6.3"},"types":"./index.d.ts","gitHead":"c145d345bbc332fd47d4ac8d5d777d34bea71f02","bugs":{"url":"https://github.com/balmychan/nestjs-simple-redis-lock/issues"},"_id":"@balmychan/nestjs-simple-redis-lock@9.0.0","_nodeVersion":"16.14.2","_npmVersion":"8.5.0","dist":{"integrity":"sha512-XiIt4lmrj1b40/vZ/tqI2ya3lRRaaXDWw7BCRMnN+V0OQgWPuacEbiD0oorHxtBBAJ5GfyOaXhUHJc3BoMS58Q==","shasum":"7b83b3c70ba3eeb80848a32ee6652bd1f580447d","tarball":"https://registry.npmjs.org/@balmychan/nestjs-simple-redis-lock/-/nestjs-simple-redis-lock-9.0.0.tgz","fileCount":27,"unpackedSize":108956,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCsdqJkL2gxVn6VSf/7/6hvfDJSnZXqDPLIBzyjmHYxigIhAIgFtaCg2JGLk+KLkT0HFb3lmh73TciXZNMu7Vk+yfYf"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjJBwgACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmrfrg/+PS1EaaHK6kqzAqUh7QfiPNkyLNRYtmd48AanCaZ+oHtmX7Th\r\nV8ffc0VQgxpbvaSj018x9FOJqDUuXANsx7cqQ6v+/ZXmw9x9biSUKzE+8L6c\r\nhbygv9nInhilPh+ytXSISccBjRp8MczrnNH3AiIU3UyvePQkW72RWww/Btk0\r\nMX+qb+UaBdHM1ZIiAxKaUcT8qQlTuUGHqFknMaEfI84xR8nGLGKYdMi+HTHV\r\nJY0bafPjRBq62mjuHExyqhiZghvaPERBAxDqzHYfjL0HSaXOwioZ1oVq1ani\r\nmkldQc/SwvKZHkbr5qBPafDHCNcbOuKSxAU9+c+1fScStALCDqQTY3D2t0lQ\r\nUENgsDB8lx/96DTuH2WcaFN0AjhFI5uPUI9LkUf6zdV9pHNdNa5TUAu0pPAE\r\n82NS2btNgiHtioH04Innvn0IO8VTdAE8h5rWy3wMbsIXmjb1nb1EoZiSDJzB\r\nCx2hFGxXUuzUsAHL6W4U3mwCpEYNY8emw1PZiroEJJt6r40xiKhmmBTzM2Yx\r\nNPmsuwlz0HEHaYMtS7oHjBjr8Nu1UCYEchAfNWVmSk45qA9cCCWJGvoZAZXH\r\nxFgsiBy7rKllG8bDQqu/SPqK6D7rPI+m5dzmQnNrnLG/VD8ZjEDhhpZxDaOI\r\nK2Sk9R1w6OSYAITFLdARxe1S/g9Ij6AJid0=\r\n=Prts\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"balmychan","email":"ayumi.goto.0212@gmail.com"},"directories":{},"maintainers":[{"name":"balmychan","email":"ayumi.goto.0212@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-simple-redis-lock_9.0.0_1663310879855_0.9985193592607922"},"_hasShrinkwrap":false}},"time":{"created":"2022-09-16T06:47:59.813Z","9.0.0":"2022-09-16T06:48:00.091Z","modified":"2022-09-16T06:48:00.265Z"},"maintainers":[{"name":"balmychan","email":"ayumi.goto.0212@gmail.com"}],"description":"Distributed lock with single redis instance, simple and easy to use for Nestjs","homepage":"https://github.com/balmychan/nestjs-simple-redis-lock#readme","keywords":["nestjs","redis","redis lock","distributed lock"],"repository":{"type":"git","url":"git+ssh://git@github.com/balmychan/nestjs-simple-redis-lock.git"},"author":{"name":"balmychan"},"bugs":{"url":"https://github.com/balmychan/nestjs-simple-redis-lock/issues"},"license":"MIT","readme":"# @huangang/nestjs-simple-redis-lock\r\nDistributed lock with single redis instance, simple and easy to use for [Nestjs](https://github.com/nestjs/nest)\r\n\r\n## Installation\r\n```\r\nnpm install @huangang/nestjs-simple-redis-lock\r\n```\r\n\r\n## Usage\r\nYou must install [nestjs-redis](https://github.com/liaoliaots/nestjs-redis), and use in Nest. This package use it to access redis:\r\n```JavaScript\r\n// app.ts\r\nimport { RedisLockModule } from '@huangang/nestjs-simple-redis-lock';\r\n\r\n@Module({\r\n  imports: [\r\n    ...\r\n    RedisModule.forRootAsync({ // import RedisModule before RedisLockModule\r\n      imports: [ConfigModule],\r\n      useFactory: (config: ConfigService) => ({\r\n        host: config.get('REDIS_HOST'),\r\n        port: config.get('REDIS_PORT'),\r\n        db: parseInt(config.get('REDIS_DB'), 10),\r\n        password: config.get('REDIS_PASSWORD'),\r\n        keyPrefix: config.get('REDIS_KEY_PREFIX'),\r\n      }),\r\n      inject: [ConfigService],\r\n    }),\r\n   RedisLockModule.registerAsync({\r\n      useFactory: async (redisManager: RedisManager) => {\r\n        return { prefix: ':lock:', client: redisManager.getClient() }\r\n      },\r\n      inject: [RedisManager]\r\n    }), // import RedisLockModule, use default configuration\r\n  ]\r\n})\r\nexport class AppModule {}\r\n```\r\n### 1. Simple example\r\n```TypeScript\r\nimport { RedisLockService } from '@huangang/nestjs-simple-redis-lock';\r\n\r\nexport class FooService {\r\n  constructor(\r\n    protected readonly lockService: RedisLockService, // inject RedisLockService \r\n  ) {}\r\n\r\n  async test1() {\r\n    try {\r\n      /**\r\n       * Get a lock by name\r\n       * Automatically unlock after 1min\r\n       * Try again after 100ms\r\n       * The max times to retry is 36000, about 1h\r\n       */\r\n      await this.lockService.lock('test1');\r\n      // Do somethings\r\n    } finally { // use 'finally' to ensure unlocking\r\n      this.lockService.unlock('test1'); // unlock\r\n      // Or: await this.lockService.unlock('test1'); wait for the unlocking\r\n    }\r\n  }\r\n  \r\n  async test2() {\r\n    /**\r\n     * Automatically unlock after 2min\r\n     * Try again after 50ms if failed\r\n     * The max times to retry is 100\r\n     */\r\n    await this.lockService.lock('test1', 2 * 60 * 1000, 50, 100);\r\n    // Do somethings\r\n    await this.lockService.setTTL('test1', 60000); // Renewal the lock when the program is very time consuming, avoiding automatically unlock\r\n    this.lockService.unlock('test1');\r\n  }\r\n}\r\n```\r\n\r\n### 2. Example by using decorator\r\nUsing `@huangang/nestjs-simple-redis-lock` by decorator, the locking and unlocking will be very easy.\r\nSimple example with constant lock name:\r\n```TypeScript\r\nimport { RedisLockService, RedisLock } from '@huangang/nestjs-simple-redis-lock';\r\n\r\nexport class FooService {\r\n  constructor(\r\n    protected readonly lockService: RedisLockService, // inject RedisLockService \r\n  ) {}\r\n\r\n  /**\r\n   * Wrap the method, starting with getting a lock, ending with unlocking\r\n   * The first parameter is lock name\r\n   * By default, automatically unlock after 1min.\r\n   * By default, try again after 100ms if failed\r\n   * By default, the max times to retry is 36000, about 1h\r\n   */\r\n  @RedisLock('test2')\r\n  async test1() {\r\n    // Do somethings\r\n    return 'some values';\r\n  }\r\n\r\n  /**\r\n   * Automatically unlock after 2min\r\n   * Try again after 50ms if failed\r\n   * The max times to retry is 100\r\n   */ \r\n  @RedisLock('test2', 2 * 60 * 1000, 50, 100)\r\n  async test2() {\r\n    // Do somethings\r\n    return 'some values';\r\n  }\r\n}\r\n```\r\n\r\nThe first parameter of this decorator is a powerful function. It can use to determinate lock name by many ways. \r\nSimple example with dynamic lock name:\r\n```TypeScript\r\nimport { RedisLockService, RedisLock } from '@huangang/nestjs-simple-redis-lock';\r\n\r\nexport class FooService {\r\n  lockName = 'test3';\r\n\r\n  constructor(\r\n    protected readonly lockService: RedisLockService, // inject RedisLockService \r\n  ) {}\r\n\r\n  /**\r\n   * Determinate lock name from 'this'\r\n   * The first parameter is 'this', so you can access any member in 'this' for create a dynamic lock name.\r\n   */\r\n  @RedisLock((target) => target.lockName)\r\n  async test1() {\r\n    // Do somethings\r\n    return 'some values';\r\n  }\r\n\r\n  /**\r\n   * Determinate lock name from the parameters of the method\r\n   * The original parameters also pass to the function, so you can determinate the lock name by the parameters.\r\n   */\r\n  @RedisLock((target, param1, param2) => param1 + param2)\r\n  async test2(param1, param2) {\r\n    // Do somethings\r\n    return 'some values';\r\n  }\r\n}\r\n```\r\n\r\n## Configuration\r\n* Register:*\r\n```TypeScript\r\n@Module({\r\n  imports: [\r\n    RedisLockModule.register({\r\n      clientName: 'client_name', // the Redis client name in nestjs-redis, to use specific Redis client. Default to use default client\r\n      prefix: 'my_lock:', // By default, the prefix is 'lock:'\r\n    })\r\n  ]\r\n})\r\n```\r\n*Async register:*\r\n```TypeScript\r\n@Module({\r\n  imports: [\r\n    RedisLockModule.registerAsync({\r\n          imports: [ConfigModule],\r\n          useFactory: async (config: ConfigService) => ({\r\n            clientName: config.get('REDIS_LOCK_CLIENT_NAME')\r\n          }),\r\n          inject: [ConfigService],\r\n        }),\r\n  ]\r\n})\r\n```\r\n\r\n## Debug\r\nAdd a environment variable `DEBUG=nestjs-simple-redis-lock` when start application to check log:\r\n```json\r\n// package.json\r\n{\r\n  \"scripts\": {\r\n    \"start:dev\": \"DEBUG=nestjs-simple-redis-lock tsc-watch -p tsconfig.build.json --onSuccess \\\"node dist/main.js\\\"\",\r\n  }\r\n}\r\n```\r\n","readmeFilename":"README.md"}