@tslock/mongo 1.0.0
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/LICENSE +21 -0
- package/dist/index.cjs +123 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +95 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dan Maas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
MongoLockProvider: () => MongoLockProvider,
|
|
24
|
+
createMongoLockProvider: () => createMongoLockProvider
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/mongo-accessor.ts
|
|
29
|
+
var import_core2 = require("@tslock/core");
|
|
30
|
+
var import_mongodb = require("mongodb");
|
|
31
|
+
|
|
32
|
+
// src/mongo-lock.ts
|
|
33
|
+
var import_core = require("@tslock/core");
|
|
34
|
+
var MongoLock = class extends import_core.AbstractSimpleLock {
|
|
35
|
+
constructor(config, accessor) {
|
|
36
|
+
super(config);
|
|
37
|
+
this.accessor = accessor;
|
|
38
|
+
}
|
|
39
|
+
accessor;
|
|
40
|
+
async doUnlock() {
|
|
41
|
+
await this.accessor.unlock(this.config);
|
|
42
|
+
}
|
|
43
|
+
async doExtend(newConfig) {
|
|
44
|
+
return await this.accessor.extend(newConfig);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/mongo-accessor.ts
|
|
49
|
+
var MongoAccessor = class {
|
|
50
|
+
constructor(collection) {
|
|
51
|
+
this.collection = collection;
|
|
52
|
+
}
|
|
53
|
+
collection;
|
|
54
|
+
async lock(config) {
|
|
55
|
+
const now = import_core2.ClockProvider.now();
|
|
56
|
+
const hostname = import_core2.Utils.getHostname();
|
|
57
|
+
try {
|
|
58
|
+
const result = await this.collection.findOneAndUpdate(
|
|
59
|
+
{ _id: config.name, lockUntil: { $lte: new Date(now) } },
|
|
60
|
+
{
|
|
61
|
+
$set: {
|
|
62
|
+
lockUntil: new Date((0, import_core2.lockAtMostUntil)(config)),
|
|
63
|
+
lockedAt: new Date(now),
|
|
64
|
+
lockedBy: hostname
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
{ upsert: true, returnDocument: "after" }
|
|
68
|
+
);
|
|
69
|
+
if (!result) return void 0;
|
|
70
|
+
return new MongoLock(config, this);
|
|
71
|
+
} catch (e) {
|
|
72
|
+
if (e instanceof import_mongodb.MongoServerError && e.code === 11e3) {
|
|
73
|
+
return void 0;
|
|
74
|
+
}
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async extend(config) {
|
|
79
|
+
const now = import_core2.ClockProvider.now();
|
|
80
|
+
const hostname = import_core2.Utils.getHostname();
|
|
81
|
+
const result = await this.collection.findOneAndUpdate(
|
|
82
|
+
{
|
|
83
|
+
_id: config.name,
|
|
84
|
+
lockUntil: { $gt: new Date(now) },
|
|
85
|
+
lockedBy: hostname
|
|
86
|
+
},
|
|
87
|
+
{ $set: { lockUntil: new Date((0, import_core2.lockAtMostUntil)(config)) } },
|
|
88
|
+
{ returnDocument: "after" }
|
|
89
|
+
);
|
|
90
|
+
if (!result) return void 0;
|
|
91
|
+
return new MongoLock(config, this);
|
|
92
|
+
}
|
|
93
|
+
async unlock(config) {
|
|
94
|
+
await this.collection.findOneAndUpdate(
|
|
95
|
+
{ _id: config.name },
|
|
96
|
+
{ $set: { lockUntil: new Date((0, import_core2.unlockTime)(config)) } }
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// src/mongo-lock-provider.ts
|
|
102
|
+
var MongoLockProvider = class {
|
|
103
|
+
accessor;
|
|
104
|
+
constructor(collection) {
|
|
105
|
+
this.accessor = new MongoAccessor(collection);
|
|
106
|
+
}
|
|
107
|
+
async lock(config) {
|
|
108
|
+
return this.accessor.lock(config);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
function createMongoLockProvider(db, options) {
|
|
112
|
+
const collectionName = options?.collection ?? "shedLock";
|
|
113
|
+
const writeConcern = { w: "majority", ...options?.collectionOptions?.writeConcern };
|
|
114
|
+
const readConcern = { level: "majority", ...options?.collectionOptions?.readConcern };
|
|
115
|
+
const collection = db.collection(collectionName, { writeConcern, readConcern });
|
|
116
|
+
return new MongoLockProvider(collection);
|
|
117
|
+
}
|
|
118
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
+
0 && (module.exports = {
|
|
120
|
+
MongoLockProvider,
|
|
121
|
+
createMongoLockProvider
|
|
122
|
+
});
|
|
123
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/mongo-accessor.ts","../src/mongo-lock.ts","../src/mongo-lock-provider.ts"],"sourcesContent":["export type { MongoLockDocument } from './mongo-lock-document.js';\nexport type { MongoLockProviderOptions } from './mongo-lock-provider.js';\nexport { createMongoLockProvider, MongoLockProvider } from './mongo-lock-provider.js';\n","import { ClockProvider, type LockConfiguration, Utils, lockAtMostUntil, unlockTime } from '@tslock/core';\nimport type { Collection, Filter, FindOneAndUpdateOptions } from 'mongodb';\nimport { MongoServerError } from 'mongodb';\nimport type { MongoLockDocument } from './mongo-lock-document.js';\nimport { MongoLock } from './mongo-lock.js';\n\nexport class MongoAccessor {\n constructor(private readonly collection: Collection<MongoLockDocument>) {}\n\n async lock(config: LockConfiguration): Promise<MongoLock | undefined> {\n const now = ClockProvider.now();\n const hostname = Utils.getHostname();\n try {\n const result = await this.collection.findOneAndUpdate(\n { _id: config.name, lockUntil: { $lte: new Date(now) } } as Filter<MongoLockDocument>,\n {\n $set: {\n lockUntil: new Date(lockAtMostUntil(config)),\n lockedAt: new Date(now),\n lockedBy: hostname,\n } as any,\n },\n { upsert: true, returnDocument: 'after' } as FindOneAndUpdateOptions,\n );\n if (!result) return undefined;\n return new MongoLock(config, this);\n } catch (e) {\n if (e instanceof MongoServerError && e.code === 11000) {\n return undefined;\n }\n throw e;\n }\n }\n\n async extend(config: LockConfiguration): Promise<MongoLock | undefined> {\n const now = ClockProvider.now();\n const hostname = Utils.getHostname();\n const result = await this.collection.findOneAndUpdate(\n {\n _id: config.name,\n lockUntil: { $gt: new Date(now) },\n lockedBy: hostname,\n } as Filter<MongoLockDocument>,\n { $set: { lockUntil: new Date(lockAtMostUntil(config)) } } as any,\n { returnDocument: 'after' } as FindOneAndUpdateOptions,\n );\n if (!result) return undefined;\n return new MongoLock(config, this);\n }\n\n async unlock(config: LockConfiguration): Promise<void> {\n await this.collection.findOneAndUpdate(\n { _id: config.name } as Filter<MongoLockDocument>,\n { $set: { lockUntil: new Date(unlockTime(config)) } } as any,\n );\n }\n}\n","import { AbstractSimpleLock, type LockConfiguration, type SimpleLock } from '@tslock/core';\nimport type { MongoAccessor } from './mongo-accessor.js';\n\nexport class MongoLock extends AbstractSimpleLock {\n constructor(\n config: LockConfiguration,\n private readonly accessor: MongoAccessor,\n ) {\n super(config);\n }\n\n protected override async doUnlock(): Promise<void> {\n await this.accessor.unlock(this.config);\n }\n\n protected override async doExtend(newConfig: LockConfiguration): Promise<SimpleLock | undefined> {\n return await this.accessor.extend(newConfig);\n }\n}\n","import type { ExtensibleLockProvider, LockConfiguration, SimpleLock } from '@tslock/core';\nimport type { Collection, Db } from 'mongodb';\nimport { MongoAccessor } from './mongo-accessor.js';\nimport type { MongoLockDocument } from './mongo-lock-document.js';\n\nexport interface MongoLockProviderOptions {\n collection?: string;\n collectionOptions?: {\n writeConcern?: { w?: 'majority' | number; j?: boolean; wtimeoutMS?: number };\n readConcern?: { level?: 'local' | 'majority' | 'linearizable' | 'available' | 'snapshot' };\n };\n}\n\nexport class MongoLockProvider implements ExtensibleLockProvider {\n private readonly accessor: MongoAccessor;\n\n constructor(collection: Collection<MongoLockDocument>) {\n this.accessor = new MongoAccessor(collection);\n }\n\n async lock(config: LockConfiguration): Promise<SimpleLock | undefined> {\n return this.accessor.lock(config);\n }\n}\n\nexport function createMongoLockProvider(db: Db, options?: MongoLockProviderOptions): MongoLockProvider {\n const collectionName = options?.collection ?? 'shedLock';\n const writeConcern = { w: 'majority' as const, ...options?.collectionOptions?.writeConcern };\n const readConcern = { level: 'majority' as const, ...options?.collectionOptions?.readConcern };\n const collection = db.collection<MongoLockDocument>(collectionName, { writeConcern, readConcern });\n return new MongoLockProvider(collection);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,eAA0F;AAE1F,qBAAiC;;;ACFjC,kBAA4E;AAGrE,IAAM,YAAN,cAAwB,+BAAmB;AAAA,EAChD,YACE,QACiB,UACjB;AACA,UAAM,MAAM;AAFK;AAAA,EAGnB;AAAA,EAHmB;AAAA,EAKnB,MAAyB,WAA0B;AACjD,UAAM,KAAK,SAAS,OAAO,KAAK,MAAM;AAAA,EACxC;AAAA,EAEA,MAAyB,SAAS,WAA+D;AAC/F,WAAO,MAAM,KAAK,SAAS,OAAO,SAAS;AAAA,EAC7C;AACF;;;ADZO,IAAM,gBAAN,MAAoB;AAAA,EACzB,YAA6B,YAA2C;AAA3C;AAAA,EAA4C;AAAA,EAA5C;AAAA,EAE7B,MAAM,KAAK,QAA2D;AACpE,UAAM,MAAM,2BAAc,IAAI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CO,IAAM,oBAAN,MAA0D;AAAA,EAC9C;AAAA,EAEjB,YAAY,YAA2C;AACrD,SAAK,WAAW,IAAI,cAAc,UAAU;AAAA,EAC9C;AAAA,EAEA,MAAM,KAAK,QAA4D;AACrE,WAAO,KAAK,SAAS,KAAK,MAAM;AAAA,EAClC;AACF;AAEO,SAAS,wBAAwB,IAAQ,SAAuD;AACrG,QAAM,iBAAiB,SAAS,cAAc;AAC9C,QAAM,eAAe,EAAE,GAAG,YAAqB,GAAG,SAAS,mBAAmB,aAAa;AAC3F,QAAM,cAAc,EAAE,OAAO,YAAqB,GAAG,SAAS,mBAAmB,YAAY;AAC7F,QAAM,aAAa,GAAG,WAA8B,gBAAgB,EAAE,cAAc,YAAY,CAAC;AACjG,SAAO,IAAI,kBAAkB,UAAU;AACzC;","names":["import_core"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ExtensibleLockProvider, LockConfiguration, SimpleLock } from '@tslock/core';
|
|
2
|
+
import { Collection, Db } from 'mongodb';
|
|
3
|
+
|
|
4
|
+
interface MongoLockDocument {
|
|
5
|
+
_id: string;
|
|
6
|
+
lockUntil: Date;
|
|
7
|
+
lockedAt: Date;
|
|
8
|
+
lockedBy: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface MongoLockProviderOptions {
|
|
12
|
+
collection?: string;
|
|
13
|
+
collectionOptions?: {
|
|
14
|
+
writeConcern?: {
|
|
15
|
+
w?: 'majority' | number;
|
|
16
|
+
j?: boolean;
|
|
17
|
+
wtimeoutMS?: number;
|
|
18
|
+
};
|
|
19
|
+
readConcern?: {
|
|
20
|
+
level?: 'local' | 'majority' | 'linearizable' | 'available' | 'snapshot';
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
declare class MongoLockProvider implements ExtensibleLockProvider {
|
|
25
|
+
private readonly accessor;
|
|
26
|
+
constructor(collection: Collection<MongoLockDocument>);
|
|
27
|
+
lock(config: LockConfiguration): Promise<SimpleLock | undefined>;
|
|
28
|
+
}
|
|
29
|
+
declare function createMongoLockProvider(db: Db, options?: MongoLockProviderOptions): MongoLockProvider;
|
|
30
|
+
|
|
31
|
+
export { type MongoLockDocument, MongoLockProvider, type MongoLockProviderOptions, createMongoLockProvider };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ExtensibleLockProvider, LockConfiguration, SimpleLock } from '@tslock/core';
|
|
2
|
+
import { Collection, Db } from 'mongodb';
|
|
3
|
+
|
|
4
|
+
interface MongoLockDocument {
|
|
5
|
+
_id: string;
|
|
6
|
+
lockUntil: Date;
|
|
7
|
+
lockedAt: Date;
|
|
8
|
+
lockedBy: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface MongoLockProviderOptions {
|
|
12
|
+
collection?: string;
|
|
13
|
+
collectionOptions?: {
|
|
14
|
+
writeConcern?: {
|
|
15
|
+
w?: 'majority' | number;
|
|
16
|
+
j?: boolean;
|
|
17
|
+
wtimeoutMS?: number;
|
|
18
|
+
};
|
|
19
|
+
readConcern?: {
|
|
20
|
+
level?: 'local' | 'majority' | 'linearizable' | 'available' | 'snapshot';
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
declare class MongoLockProvider implements ExtensibleLockProvider {
|
|
25
|
+
private readonly accessor;
|
|
26
|
+
constructor(collection: Collection<MongoLockDocument>);
|
|
27
|
+
lock(config: LockConfiguration): Promise<SimpleLock | undefined>;
|
|
28
|
+
}
|
|
29
|
+
declare function createMongoLockProvider(db: Db, options?: MongoLockProviderOptions): MongoLockProvider;
|
|
30
|
+
|
|
31
|
+
export { type MongoLockDocument, MongoLockProvider, type MongoLockProviderOptions, createMongoLockProvider };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/mongo-accessor.ts
|
|
2
|
+
import { ClockProvider, Utils, lockAtMostUntil, unlockTime } from "@tslock/core";
|
|
3
|
+
import { MongoServerError } from "mongodb";
|
|
4
|
+
|
|
5
|
+
// src/mongo-lock.ts
|
|
6
|
+
import { AbstractSimpleLock } from "@tslock/core";
|
|
7
|
+
var MongoLock = class extends AbstractSimpleLock {
|
|
8
|
+
constructor(config, accessor) {
|
|
9
|
+
super(config);
|
|
10
|
+
this.accessor = accessor;
|
|
11
|
+
}
|
|
12
|
+
accessor;
|
|
13
|
+
async doUnlock() {
|
|
14
|
+
await this.accessor.unlock(this.config);
|
|
15
|
+
}
|
|
16
|
+
async doExtend(newConfig) {
|
|
17
|
+
return await this.accessor.extend(newConfig);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/mongo-accessor.ts
|
|
22
|
+
var MongoAccessor = class {
|
|
23
|
+
constructor(collection) {
|
|
24
|
+
this.collection = collection;
|
|
25
|
+
}
|
|
26
|
+
collection;
|
|
27
|
+
async lock(config) {
|
|
28
|
+
const now = ClockProvider.now();
|
|
29
|
+
const hostname = Utils.getHostname();
|
|
30
|
+
try {
|
|
31
|
+
const result = await this.collection.findOneAndUpdate(
|
|
32
|
+
{ _id: config.name, lockUntil: { $lte: new Date(now) } },
|
|
33
|
+
{
|
|
34
|
+
$set: {
|
|
35
|
+
lockUntil: new Date(lockAtMostUntil(config)),
|
|
36
|
+
lockedAt: new Date(now),
|
|
37
|
+
lockedBy: hostname
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
{ upsert: true, returnDocument: "after" }
|
|
41
|
+
);
|
|
42
|
+
if (!result) return void 0;
|
|
43
|
+
return new MongoLock(config, this);
|
|
44
|
+
} catch (e) {
|
|
45
|
+
if (e instanceof MongoServerError && e.code === 11e3) {
|
|
46
|
+
return void 0;
|
|
47
|
+
}
|
|
48
|
+
throw e;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async extend(config) {
|
|
52
|
+
const now = ClockProvider.now();
|
|
53
|
+
const hostname = Utils.getHostname();
|
|
54
|
+
const result = await this.collection.findOneAndUpdate(
|
|
55
|
+
{
|
|
56
|
+
_id: config.name,
|
|
57
|
+
lockUntil: { $gt: new Date(now) },
|
|
58
|
+
lockedBy: hostname
|
|
59
|
+
},
|
|
60
|
+
{ $set: { lockUntil: new Date(lockAtMostUntil(config)) } },
|
|
61
|
+
{ returnDocument: "after" }
|
|
62
|
+
);
|
|
63
|
+
if (!result) return void 0;
|
|
64
|
+
return new MongoLock(config, this);
|
|
65
|
+
}
|
|
66
|
+
async unlock(config) {
|
|
67
|
+
await this.collection.findOneAndUpdate(
|
|
68
|
+
{ _id: config.name },
|
|
69
|
+
{ $set: { lockUntil: new Date(unlockTime(config)) } }
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/mongo-lock-provider.ts
|
|
75
|
+
var MongoLockProvider = class {
|
|
76
|
+
accessor;
|
|
77
|
+
constructor(collection) {
|
|
78
|
+
this.accessor = new MongoAccessor(collection);
|
|
79
|
+
}
|
|
80
|
+
async lock(config) {
|
|
81
|
+
return this.accessor.lock(config);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
function createMongoLockProvider(db, options) {
|
|
85
|
+
const collectionName = options?.collection ?? "shedLock";
|
|
86
|
+
const writeConcern = { w: "majority", ...options?.collectionOptions?.writeConcern };
|
|
87
|
+
const readConcern = { level: "majority", ...options?.collectionOptions?.readConcern };
|
|
88
|
+
const collection = db.collection(collectionName, { writeConcern, readConcern });
|
|
89
|
+
return new MongoLockProvider(collection);
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
MongoLockProvider,
|
|
93
|
+
createMongoLockProvider
|
|
94
|
+
};
|
|
95
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/mongo-accessor.ts","../src/mongo-lock.ts","../src/mongo-lock-provider.ts"],"sourcesContent":["import { ClockProvider, type LockConfiguration, Utils, lockAtMostUntil, unlockTime } from '@tslock/core';\nimport type { Collection, Filter, FindOneAndUpdateOptions } from 'mongodb';\nimport { MongoServerError } from 'mongodb';\nimport type { MongoLockDocument } from './mongo-lock-document.js';\nimport { MongoLock } from './mongo-lock.js';\n\nexport class MongoAccessor {\n constructor(private readonly collection: Collection<MongoLockDocument>) {}\n\n async lock(config: LockConfiguration): Promise<MongoLock | undefined> {\n const now = ClockProvider.now();\n const hostname = Utils.getHostname();\n try {\n const result = await this.collection.findOneAndUpdate(\n { _id: config.name, lockUntil: { $lte: new Date(now) } } as Filter<MongoLockDocument>,\n {\n $set: {\n lockUntil: new Date(lockAtMostUntil(config)),\n lockedAt: new Date(now),\n lockedBy: hostname,\n } as any,\n },\n { upsert: true, returnDocument: 'after' } as FindOneAndUpdateOptions,\n );\n if (!result) return undefined;\n return new MongoLock(config, this);\n } catch (e) {\n if (e instanceof MongoServerError && e.code === 11000) {\n return undefined;\n }\n throw e;\n }\n }\n\n async extend(config: LockConfiguration): Promise<MongoLock | undefined> {\n const now = ClockProvider.now();\n const hostname = Utils.getHostname();\n const result = await this.collection.findOneAndUpdate(\n {\n _id: config.name,\n lockUntil: { $gt: new Date(now) },\n lockedBy: hostname,\n } as Filter<MongoLockDocument>,\n { $set: { lockUntil: new Date(lockAtMostUntil(config)) } } as any,\n { returnDocument: 'after' } as FindOneAndUpdateOptions,\n );\n if (!result) return undefined;\n return new MongoLock(config, this);\n }\n\n async unlock(config: LockConfiguration): Promise<void> {\n await this.collection.findOneAndUpdate(\n { _id: config.name } as Filter<MongoLockDocument>,\n { $set: { lockUntil: new Date(unlockTime(config)) } } as any,\n );\n }\n}\n","import { AbstractSimpleLock, type LockConfiguration, type SimpleLock } from '@tslock/core';\nimport type { MongoAccessor } from './mongo-accessor.js';\n\nexport class MongoLock extends AbstractSimpleLock {\n constructor(\n config: LockConfiguration,\n private readonly accessor: MongoAccessor,\n ) {\n super(config);\n }\n\n protected override async doUnlock(): Promise<void> {\n await this.accessor.unlock(this.config);\n }\n\n protected override async doExtend(newConfig: LockConfiguration): Promise<SimpleLock | undefined> {\n return await this.accessor.extend(newConfig);\n }\n}\n","import type { ExtensibleLockProvider, LockConfiguration, SimpleLock } from '@tslock/core';\nimport type { Collection, Db } from 'mongodb';\nimport { MongoAccessor } from './mongo-accessor.js';\nimport type { MongoLockDocument } from './mongo-lock-document.js';\n\nexport interface MongoLockProviderOptions {\n collection?: string;\n collectionOptions?: {\n writeConcern?: { w?: 'majority' | number; j?: boolean; wtimeoutMS?: number };\n readConcern?: { level?: 'local' | 'majority' | 'linearizable' | 'available' | 'snapshot' };\n };\n}\n\nexport class MongoLockProvider implements ExtensibleLockProvider {\n private readonly accessor: MongoAccessor;\n\n constructor(collection: Collection<MongoLockDocument>) {\n this.accessor = new MongoAccessor(collection);\n }\n\n async lock(config: LockConfiguration): Promise<SimpleLock | undefined> {\n return this.accessor.lock(config);\n }\n}\n\nexport function createMongoLockProvider(db: Db, options?: MongoLockProviderOptions): MongoLockProvider {\n const collectionName = options?.collection ?? 'shedLock';\n const writeConcern = { w: 'majority' as const, ...options?.collectionOptions?.writeConcern };\n const readConcern = { level: 'majority' as const, ...options?.collectionOptions?.readConcern };\n const collection = db.collection<MongoLockDocument>(collectionName, { writeConcern, readConcern });\n return new MongoLockProvider(collection);\n}\n"],"mappings":";AAAA,SAAS,eAAuC,OAAO,iBAAiB,kBAAkB;AAE1F,SAAS,wBAAwB;;;ACFjC,SAAS,0BAAmE;AAGrE,IAAM,YAAN,cAAwB,mBAAmB;AAAA,EAChD,YACE,QACiB,UACjB;AACA,UAAM,MAAM;AAFK;AAAA,EAGnB;AAAA,EAHmB;AAAA,EAKnB,MAAyB,WAA0B;AACjD,UAAM,KAAK,SAAS,OAAO,KAAK,MAAM;AAAA,EACxC;AAAA,EAEA,MAAyB,SAAS,WAA+D;AAC/F,WAAO,MAAM,KAAK,SAAS,OAAO,SAAS;AAAA,EAC7C;AACF;;;ADZO,IAAM,gBAAN,MAAoB;AAAA,EACzB,YAA6B,YAA2C;AAA3C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E,WAAW,IAAI,KAAK,WAAW,MAAM,CAAC,EAAE,EAAE;AAAA,IACtD;AAAA,EACF;AACF;;;AE3CO,IAAM,oBAAN,MAA0D;AAAA,EAC9C;AAAA,EAEjB,YAAY,YAA2C;AACrD,SAAK,WAAW,IAAI,cAAc,UAAU;AAAA,EAC9C;AAAA,EAEA,MAAM,KAAK,QAA4D;AACrE,WAAO,KAAK,SAAS,KAAK,MAAM;AAAA,EAClC;AACF;AAEO,SAAS,wBAAwB,IAAQ,SAAuD;AACrG,QAAM,iBAAiB,SAAS,cAAc;AAC9C,QAAM,eAAe,EAAE,GAAG,YAAqB,GAAG,SAAS,mBAAmB,aAAa;AAC3F,QAAM,cAAc,EAAE,OAAO,YAAqB,GAAG,SAAS,mBAAmB,YAAY;AAC7F,QAAM,aAAa,GAAG,WAA8B,gBAAgB,EAAE,cAAc,YAAY,CAAC;AACjG,SAAO,IAAI,kBAAkB,UAAU;AACzC;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tslock/mongo",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TSLock provider for MongoDB",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"mongodb": "^6.0.0",
|
|
25
|
+
"@tslock/core": "1.0.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"@tslock/core": {
|
|
29
|
+
"optional": false
|
|
30
|
+
},
|
|
31
|
+
"mongodb": {
|
|
32
|
+
"optional": false
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"typescript": "^5.5.0",
|
|
37
|
+
"tsup": "^8.0.0",
|
|
38
|
+
"vitest": "^2.0.0",
|
|
39
|
+
"mongodb": "^6.0.0",
|
|
40
|
+
"@tslock/core": "1.0.0"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/drmaas/tslock.git",
|
|
48
|
+
"directory": "packages/mongo"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsup",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"test:watch": "vitest",
|
|
54
|
+
"typecheck": "tsc --noEmit"
|
|
55
|
+
}
|
|
56
|
+
}
|