{"_id":"@boyanstanchev/nestjs-simple-redis-lock","_rev":"1-1ff6f8e6724ccd7aeb2ec9dec61a3b59","name":"@boyanstanchev/nestjs-simple-redis-lock","dist-tags":{"latest":"1.0.2"},"versions":{"1.0.1":{"name":"@boyanstanchev/nestjs-simple-redis-lock","version":"1.0.1","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/boyanstanchev/nestjs-simple-redis-lock.git"},"author":{"name":"Alex Chen"},"license":"MIT","homepage":"https://github.com/boyanstanchev/nestjs-simple-redis-lock#readme","dependencies":{"@nestjs/common":"^10.0.0","@nestjs/core":"^10.0.0","@types/debug":"^4.1.5","debug":"^4.1.1","reflect-metadata":"^0.1.13","rxjs":"^7.1.0","ioredis":"^5.3.2","@songkeys/nestjs-redis":"^10.0.0"},"devDependencies":{"@types/node":"^18.11.9","typescript":"^4.6.3"},"types":"./index.d.ts","gitHead":"998754de45c87660bfab94f6905017c9297e4b84","bugs":{"url":"https://github.com/boyanstanchev/nestjs-simple-redis-lock/issues"},"_id":"@boyanstanchev/nestjs-simple-redis-lock@1.0.1","_nodeVersion":"18.12.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-jqX1ccHVKyatIlvSTMb6YaNuyh126zYXw+Y4b1vMQkIJGkxXzyYE8aDbajN9EfxSZTJ203ur6HB7sVjjVSNnHQ==","shasum":"44852e2ee6d288856588474bcee22f71284c9d92","tarball":"https://registry.npmjs.org/@boyanstanchev/nestjs-simple-redis-lock/-/nestjs-simple-redis-lock-1.0.1.tgz","fileCount":27,"unpackedSize":129025,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBAd1cSbxCIT+nRKk8VLk1Th37RjETrKopc5BuxqdApBAiBi9IrwOHHw7jxOrF5iY31jdGfWmCKWkkrCs9G8vGj7uw=="}]},"_npmUser":{"name":"boyanstanchev","email":"boyan_stan4ev@hotmail.com"},"directories":{},"maintainers":[{"name":"boyanstanchev","email":"boyan_stan4ev@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-simple-redis-lock_1.0.1_1691165392189_0.36567591590175375"},"_hasShrinkwrap":false},"1.0.2":{"name":"@boyanstanchev/nestjs-simple-redis-lock","version":"1.0.2","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","npm:publish":"npm publish --access public"},"keywords":["nestjs","redis","redis lock","distributed lock"],"repository":{"type":"git","url":"git+ssh://git@github.com/boyanstanchev/nestjs-simple-redis-lock.git"},"author":{"name":"Alex Chen"},"license":"MIT","homepage":"https://github.com/boyanstanchev/nestjs-simple-redis-lock#readme","dependencies":{"@nestjs/common":"10.x","@nestjs/core":"10.x","@types/debug":"^4.1.5","debug":"^4.1.1","reflect-metadata":"^0.1.13","rxjs":"^7.1.0","ioredis":"^5.3.2","@songkeys/nestjs-redis":"^10.0.0"},"devDependencies":{"@types/node":"^18.11.9","typescript":"^4.6.3"},"types":"./index.d.ts","gitHead":"fb13372da8344352ba310bebd99e9515657a62ab","bugs":{"url":"https://github.com/boyanstanchev/nestjs-simple-redis-lock/issues"},"_id":"@boyanstanchev/nestjs-simple-redis-lock@1.0.2","_nodeVersion":"18.12.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-yViBtQQgZ6tMkMBHH/kuOV4EYTsdPZ4FxL6fTHxZ90PUMbPy3PndKI4tZYmHq22PdMD9lxQWQ+KEhRqKB2+aLQ==","shasum":"12d810faa03d773ad052e080010869c2e5363abb","tarball":"https://registry.npmjs.org/@boyanstanchev/nestjs-simple-redis-lock/-/nestjs-simple-redis-lock-1.0.2.tgz","fileCount":27,"unpackedSize":129070,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCbeioR79AxUP/ada4auHqMJAlcNWYoqZfqA2kBxr3lwAIgfb1oExZAFPqhDX8YY7f2DVV3rKUoYapQ7BYDvDtzmQI="}]},"_npmUser":{"name":"boyanstanchev","email":"boyan_stan4ev@hotmail.com"},"directories":{},"maintainers":[{"name":"boyanstanchev","email":"boyan_stan4ev@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-simple-redis-lock_1.0.2_1691167018384_0.9554873797383927"},"_hasShrinkwrap":false}},"time":{"created":"2023-08-04T16:09:52.069Z","1.0.1":"2023-08-04T16:09:52.564Z","modified":"2023-08-04T16:36:58.787Z","1.0.2":"2023-08-04T16:36:58.616Z"},"maintainers":[{"name":"boyanstanchev","email":"boyan_stan4ev@hotmail.com"}],"description":"Distributed lock with single redis instance, simple and easy to use for Nestjs","homepage":"https://github.com/boyanstanchev/nestjs-simple-redis-lock#readme","keywords":["nestjs","redis","redis lock","distributed lock"],"repository":{"type":"git","url":"git+ssh://git@github.com/boyanstanchev/nestjs-simple-redis-lock.git"},"author":{"name":"Alex Chen"},"bugs":{"url":"https://github.com/boyanstanchev/nestjs-simple-redis-lock/issues"},"license":"MIT","readme":"# 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 nestjs-simple-redis-lock\r\n```\r\n\r\n## Usage\r\nYou must install [nestjs-redis](https://github.com/kyknow/nestjs-redis), and use in Nest. This package use it to access redis:\r\n```JavaScript\r\n// app.ts\r\nimport { RedisModule } from 'nestjs-redis';\r\nimport { RedisLockModule } from '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.register({}), // 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 '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 600, about 1min\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 `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 '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 600, about 1min\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 '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"}