mongo-locks 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,53 +1,56 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.MongoLocksError = void 0;
13
- const schema_1 = require("./src/schema");
14
- class MongoLocksError extends Error {
15
- constructor(message) {
16
- super(message);
17
- }
18
- }
19
- exports.MongoLocksError = MongoLocksError;
20
- const makeLockId = (array) => Array.prototype.slice.apply(array).join(":");
21
- class LockManager {
22
- constructor() {
23
- this.Locks = null;
24
- this.MongoLocksError = MongoLocksError;
25
- this.noop = () => __awaiter(this, void 0, void 0, function* () { });
26
- }
27
- init(connection, options) {
28
- options = options || {};
29
- this.Locks = connection.model(options.collection || "Locks", schema_1.default);
30
- }
31
- lock(...actions) {
32
- if (!this.Locks) {
33
- throw new MongoLocksError("You must initialize mongo-locks with a mongoose connection");
34
- }
35
- const lockId = makeLockId(actions);
36
- const l = new this.Locks();
37
- l.action = lockId;
38
- return l.save().then(() => () => this.Locks.deleteOne({ action: lockId }).exec());
39
- }
40
- refresh(...actions) {
41
- if (!this.Locks) {
42
- throw new MongoLocksError("You must initialize mongo-locks with a mongoose connection");
43
- }
44
- return this.Locks.updateOne({ action: makeLockId(actions) }, { $set: { refreshedAt: Date.now() } }, { upsert: true }).exec();
45
- }
46
- free(...actions) {
47
- if (!this.Locks) {
48
- throw new MongoLocksError("You must initialize mongo-locks with a mongoose connection");
49
- }
50
- return this.Locks.deleteOne({ action: makeLockId(actions) }).exec();
51
- }
52
- }
53
- exports.default = new LockManager();
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MongoLocksError = void 0;
13
+ const schema_1 = require("./src/schema");
14
+ class MongoLocksError extends Error {
15
+ constructor(message) {
16
+ super(message);
17
+ }
18
+ }
19
+ exports.MongoLocksError = MongoLocksError;
20
+ const makeLockId = (array) => Array.prototype.slice.apply(array).join(":");
21
+ class LockManager {
22
+ constructor() {
23
+ this.Locks = null;
24
+ this.MongoLocksError = MongoLocksError;
25
+ this.noop = () => __awaiter(this, void 0, void 0, function* () { });
26
+ }
27
+ init(connection, options) {
28
+ options = options || {};
29
+ this.Locks = connection.model(options.collection || "Locks", schema_1.default);
30
+ }
31
+ lock(...actions) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ if (!this.Locks) {
34
+ throw new MongoLocksError("You must initialize mongo-locks with a mongoose connection");
35
+ }
36
+ const lockId = makeLockId(actions);
37
+ const l = new this.Locks();
38
+ l.action = lockId;
39
+ yield l.save();
40
+ return () => this.Locks.deleteOne({ action: lockId }).exec();
41
+ });
42
+ }
43
+ refresh(...actions) {
44
+ if (!this.Locks) {
45
+ throw new MongoLocksError("You must initialize mongo-locks with a mongoose connection");
46
+ }
47
+ return this.Locks.updateOne({ action: makeLockId(actions) }, { $set: { refreshedAt: Date.now() } }, { upsert: true }).exec();
48
+ }
49
+ free(...actions) {
50
+ if (!this.Locks) {
51
+ throw new MongoLocksError("You must initialize mongo-locks with a mongoose connection");
52
+ }
53
+ return this.Locks.deleteOne({ action: makeLockId(actions) }).exec();
54
+ }
55
+ }
56
+ exports.default = new LockManager();
@@ -1,23 +1,23 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const mongoose = require("mongoose");
4
- const Schema = mongoose.Schema;
5
- // define the schema for our user model
6
- const lockSchema = new Schema({
7
- createdAt: {
8
- type: Date,
9
- default: Date.now
10
- },
11
- refreshedAt: {
12
- type: Date,
13
- default: Date.now,
14
- // Locks are not supposed to last longer than a minute
15
- expires: 60
16
- },
17
- action: {
18
- type: String,
19
- unique: true,
20
- required: true
21
- }
22
- });
23
- exports.default = lockSchema;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const mongoose = require("mongoose");
4
+ const Schema = mongoose.Schema;
5
+ // define the schema for our user model
6
+ const lockSchema = new Schema({
7
+ createdAt: {
8
+ type: Date,
9
+ default: Date.now
10
+ },
11
+ refreshedAt: {
12
+ type: Date,
13
+ default: Date.now,
14
+ // Locks are not supposed to last longer than a minute
15
+ expires: 60
16
+ },
17
+ action: {
18
+ type: String,
19
+ unique: true,
20
+ required: true
21
+ }
22
+ });
23
+ exports.default = lockSchema;
package/index.ts CHANGED
@@ -7,10 +7,10 @@ export class MongoLocksError extends Error {
7
7
  }
8
8
  }
9
9
 
10
- const makeLockId = (array) => Array.prototype.slice.apply(array).join(":");
10
+ const makeLockId = (array: any[]) => Array.prototype.slice.apply(array).join(":");
11
11
 
12
12
  class LockManager {
13
- Locks: mongoose.Model<any> = null;
13
+ Locks: mongoose.Model<any> | null = null;
14
14
  MongoLocksError = MongoLocksError;
15
15
  noop: () => Promise<any> = async () => {};
16
16
 
@@ -19,7 +19,7 @@ class LockManager {
19
19
  this.Locks = connection.model(options.collection || "Locks", lockSchema);
20
20
  }
21
21
 
22
- lock(...actions: any[]): Promise<() => Promise<any>> {
22
+ async lock(...actions: any[]): Promise<() => Promise<any>> {
23
23
  if (!this.Locks) {
24
24
  throw new MongoLocksError("You must initialize mongo-locks with a mongoose connection");
25
25
  }
@@ -28,7 +28,8 @@ class LockManager {
28
28
 
29
29
  const l = new this.Locks();
30
30
  l.action = lockId;
31
- return l.save().then(() => () => this.Locks.deleteOne({ action: lockId }).exec());
31
+ await l.save();
32
+ return () => this.Locks!.deleteOne({ action: lockId }).exec()
32
33
  }
33
34
 
34
35
  refresh(...actions: any[]) {
package/package.json CHANGED
@@ -1,13 +1,8 @@
1
1
  {
2
2
  "name": "mongo-locks",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Simple and bountiful locks to avoid doing the same operation multiple times",
5
5
  "main": "dist/index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
8
- "prepublishOnly": "npm run clean && tsc",
9
- "clean": "rm -Rf dist"
10
- },
11
6
  "repository": {
12
7
  "type": "git",
13
8
  "url": "git+https://github.com/coyotte508/mongo-locks.git"
@@ -38,5 +33,9 @@
38
33
  "index.d.ts",
39
34
  "index.ts",
40
35
  "tsconfig.json"
41
- ]
42
- }
36
+ ],
37
+ "scripts": {
38
+ "test": "echo \"Error: no test specified\" && exit 1",
39
+ "clean": "rm -Rf dist"
40
+ }
41
+ }
package/tsconfig.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "strict": true,
3
4
  /* Basic Options */
4
5
  "target": "es2015",
5
6
  "module": "commonjs",