{"_id":"@andrewda/nestjs-simple-redis-lock","_rev":"1-7444ac47840a88d28be3b2851d8a31a9","name":"@andrewda/nestjs-simple-redis-lock","dist-tags":{"latest":"0.2.3"},"versions":{"0.2.2":{"name":"@andrewda/nestjs-simple-redis-lock","version":"0.2.2","description":"Distributed lock with single Redis instance, simple and easy to use for Nest.js","private":false,"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/andrewda/nestjs-simple-redis-lock.git"},"author":{"name":"Alex Chen"},"license":"MIT","homepage":"https://github.com/andrewda/nestjs-simple-redis-lock#readme","dependencies":{"@nestjs/common":"^9.0.0","@nestjs/core":"^9.0.0","@types/debug":"^4.1.5","debug":"^4.1.1","reflect-metadata":"^0.1.13","rxjs":"^7.1.0","ioredis":"^5.3.2","@liaoliaots/nestjs-redis":"^9.0.5"},"devDependencies":{"@types/node":"^18.11.9","typescript":"^4.6.3"},"types":"./index.d.ts","gitHead":"beea59a4cb24f275787f669d4c76f8c9305d525e","bugs":{"url":"https://github.com/andrewda/nestjs-simple-redis-lock/issues"},"_id":"@andrewda/nestjs-simple-redis-lock@0.2.2","_nodeVersion":"19.9.0","_npmVersion":"9.6.3","dist":{"integrity":"sha512-5DhGDfGd9Psl/NPNcvCfvylb73iAEKDoAAgnOQs/050ICiAXWerDe/w2UW6H2beBEKT/vTIolO1dERoUxdYzsA==","shasum":"d41a2417a1c179b1b421ee539f777109043fdc75","tarball":"https://registry.npmjs.org/@andrewda/nestjs-simple-redis-lock/-/nestjs-simple-redis-lock-0.2.2.tgz","fileCount":27,"unpackedSize":452315,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFmtLMtjTTW1ZDPfGZnDQ91nArLAsbWI4580QvSFVazRAiEA3cTI2HWsCTAxlFxReVu3p9l+xXKmE1M69AdFyE6oXWQ="}]},"_npmUser":{"name":"andrewda","email":"dassonville.andrew@gmail.com"},"directories":{},"maintainers":[{"name":"andrewda","email":"dassonville.andrew@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-simple-redis-lock_0.2.2_1684034187370_0.05442034010189856"},"_hasShrinkwrap":false},"0.2.3":{"name":"@andrewda/nestjs-simple-redis-lock","version":"0.2.3","description":"Distributed lock with single Redis instance, simple and easy to use for Nest.js","private":false,"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/andrewda/nestjs-simple-redis-lock.git"},"author":{"name":"Alex Chen"},"license":"MIT","homepage":"https://github.com/andrewda/nestjs-simple-redis-lock#readme","dependencies":{"@nestjs/common":"^9.0.0","@nestjs/core":"^9.0.0","@types/debug":"^4.1.5","debug":"^4.1.1","reflect-metadata":"^0.1.13","rxjs":"^7.1.0","ioredis":"^5.3.2","@liaoliaots/nestjs-redis":"^9.0.5"},"devDependencies":{"@types/node":"^18.11.9","typescript":"^4.6.3"},"types":"./index.d.ts","gitHead":"cb923d8f94bc2d90804eec928af27efa5f753d7f","bugs":{"url":"https://github.com/andrewda/nestjs-simple-redis-lock/issues"},"_id":"@andrewda/nestjs-simple-redis-lock@0.2.3","_nodeVersion":"19.9.0","_npmVersion":"9.6.3","dist":{"integrity":"sha512-SH/WxgZy9tjxLtYAb1pMHcPANMJX2DRAjB//4XaEtriaJ6VytRqhGYZVpHXWI4FE9giUFwVTpY/AYfitTvzlPA==","shasum":"42d091f8440c451d0bc56642f1fc17d6ff580128","tarball":"https://registry.npmjs.org/@andrewda/nestjs-simple-redis-lock/-/nestjs-simple-redis-lock-0.2.3.tgz","fileCount":27,"unpackedSize":129430,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCJAzpRn/QFpxJAi7Vj21k3vIzGA7umFQrCAsbmhpwdCQIgX7Rf94T/e9ZdGiEMTa6UdqfrbI0nTlH6AqV5cLFB3Pc="}]},"_npmUser":{"name":"andrewda","email":"dassonville.andrew@gmail.com"},"directories":{},"maintainers":[{"name":"andrewda","email":"dassonville.andrew@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-simple-redis-lock_0.2.3_1684034532436_0.9694075275097391"},"_hasShrinkwrap":false}},"time":{"created":"2023-05-14T03:16:27.295Z","0.2.2":"2023-05-14T03:16:27.562Z","modified":"2023-05-14T03:22:12.740Z","0.2.3":"2023-05-14T03:22:12.635Z"},"maintainers":[{"name":"andrewda","email":"dassonville.andrew@gmail.com"}],"description":"Distributed lock with single Redis instance, simple and easy to use for Nest.js","homepage":"https://github.com/andrewda/nestjs-simple-redis-lock#readme","keywords":["nestjs","redis","redis lock","distributed lock"],"repository":{"type":"git","url":"git+ssh://git@github.com/andrewda/nestjs-simple-redis-lock.git"},"author":{"name":"Alex Chen"},"bugs":{"url":"https://github.com/andrewda/nestjs-simple-redis-lock/issues"},"license":"MIT","readme":"# nestjs-simple-redis-lock\nDistributed lock with single redis instance, simple and easy to use for [Nestjs](https://github.com/nestjs/nest)\n\n## Installation\n```\nnpm install nestjs-simple-redis-lock\n```\n\n## Usage\nYou must install [nestjs-redis](https://github.com/kyknow/nestjs-redis), and use in Nest. This package use it to access redis:\n```JavaScript\n// app.ts\nimport { RedisModule } from 'nestjs-redis';\nimport { RedisLockModule } from 'nestjs-simple-redis-lock';\n\n@Module({\n  imports: [\n    ...\n    RedisModule.forRootAsync({ // import RedisModule before RedisLockModule\n      imports: [ConfigModule],\n      useFactory: (config: ConfigService) => ({\n        host: config.get('REDIS_HOST'),\n        port: config.get('REDIS_PORT'),\n        db: parseInt(config.get('REDIS_DB'), 10),\n        password: config.get('REDIS_PASSWORD'),\n        keyPrefix: config.get('REDIS_KEY_PREFIX'),\n      }),\n      inject: [ConfigService],\n    }),\n    RedisLockModule.register({}), // import RedisLockModule, use default configuration\n  ]\n})\nexport class AppModule {}\n```\n### 1. Simple example\n```TypeScript\nimport { RedisLockService } from 'nestjs-simple-redis-lock';\n\nexport class FooService {\n  constructor(\n    protected readonly lockService: RedisLockService, // inject RedisLockService \n  ) {}\n\n  async test1() {\n    try {\n      /**\n       * Get a lock by name\n       * Automatically unlock after 1min\n       * Try again after 100ms\n       * The max times to retry is 36000, about 1h\n       */\n      await this.lockService.lock('test1');\n      // Do somethings\n    } finally { // use 'finally' to ensure unlocking\n      this.lockService.unlock('test1'); // unlock\n      // Or: await this.lockService.unlock('test1'); wait for the unlocking\n    }\n  }\n  \n  async test2() {\n    /**\n     * Automatically unlock after 2min\n     * Try again after 50ms if failed\n     * The max times to retry is 100\n     */\n    await this.lockService.lock('test1', 2 * 60 * 1000, 50, 100);\n    // Do somethings\n    await this.lockService.setTTL('test1', 60000); // Renewal the lock when the program is very time consuming, avoiding automatically unlock\n    this.lockService.unlock('test1');\n  }\n}\n```\n\n### 2. Example by using decorator\nUsing `nestjs-simple-redis-lock` by decorator, the locking and unlocking will be very easy.\nSimple example with constant lock name:\n```TypeScript\nimport { RedisLockService, RedisLock } from 'nestjs-simple-redis-lock';\n\nexport class FooService {\n  constructor(\n    protected readonly lockService: RedisLockService, // inject RedisLockService \n  ) {}\n\n  /**\n   * Wrap the method, starting with getting a lock, ending with unlocking\n   * The first parameter is lock name\n   * By default, automatically unlock after 1min.\n   * By default, try again after 100ms if failed\n   * By default, the max times to retry is 36000, about 1h\n   */\n  @RedisLock('test2')\n  async test1() {\n    // Do somethings\n    return 'some values';\n  }\n\n  /**\n   * Automatically unlock after 2min\n   * Try again after 50ms if failed\n   * The max times to retry is 100\n   */ \n  @RedisLock('test2', 2 * 60 * 1000, 50, 100)\n  async test2() {\n    // Do somethings\n    return 'some values';\n  }\n}\n```\n\nThe first parameter of this decorator is a powerful function. It can use to determinate lock name by many ways. \nSimple example with dynamic lock name:\n```TypeScript\nimport { RedisLockService, RedisLock } from 'nestjs-simple-redis-lock';\n\nexport class FooService {\n  lockName = 'test3';\n\n  constructor(\n    protected readonly lockService: RedisLockService, // inject RedisLockService \n  ) {}\n\n  /**\n   * Determinate lock name from 'this'\n   * The first parameter is 'this', so you can access any member in 'this' for create a dynamic lock name.\n   */\n  @RedisLock((target) => target.lockName)\n  async test1() {\n    // Do somethings\n    return 'some values';\n  }\n\n  /**\n   * Determinate lock name from the parameters of the method\n   * The original parameters also pass to the function, so you can determinate the lock name by the parameters.\n   */\n  @RedisLock((target, param1, param2) => param1 + param2)\n  async test2(param1, param2) {\n    // Do somethings\n    return 'some values';\n  }\n}\n```\n\n## Configuration\n* Register:*\n```TypeScript\n@Module({\n  imports: [\n    RedisLockModule.register({\n      clientName: 'client_name', // the Redis client name in nestjs-redis, to use specific Redis client. Default to use default client\n      prefix: 'my_lock:', // By default, the prefix is 'lock:'\n    })\n  ]\n})\n```\n*Async register:*\n```TypeScript\n@Module({\n  imports: [\n    RedisLockModule.registerAsync({\n          imports: [ConfigModule],\n          useFactory: async (config: ConfigService) => ({\n            clientName: config.get('REDIS_LOCK_CLIENT_NAME')\n          }),\n          inject: [ConfigService],\n        }),\n  ]\n})\n```\n\n## Debug\nAdd a environment variable `DEBUG=nestjs-simple-redis-lock` when start application to check log:\n```json\n// package.json\n{\n  \"scripts\": {\n    \"start:dev\": \"DEBUG=nestjs-simple-redis-lock tsc-watch -p tsconfig.build.json --onSuccess \\\"node dist/main.js\\\"\",\n  }\n}\n```\n","readmeFilename":"README.md"}