@powersync/service-module-mongodb 0.0.0-dev-20241219145106 → 0.0.0-dev-20250102111825

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.
@@ -1,36 +0,0 @@
1
- import * as framework from '@powersync/lib-services-framework';
2
- import * as bson from 'bson';
3
- import * as mongo from 'mongodb';
4
- /**
5
- * Lock Document Schema
6
- */
7
- export type Lock = {
8
- name: string;
9
- active_lock?: {
10
- lock_id: bson.ObjectId;
11
- ts: Date;
12
- };
13
- };
14
- export type Collection = mongo.Collection<Lock>;
15
- export type AcquireLockParams = {
16
- /**
17
- * Name of the process/user trying to acquire the lock.
18
- */
19
- name: string;
20
- /**
21
- * The TTL of the lock (ms). Default: 60000 ms (1 min)
22
- */
23
- timeout?: number;
24
- };
25
- export declare const releaseLock: (collection: Collection, lock_id: string) => Promise<void>;
26
- export type CreateLockManagerParams = {
27
- /**
28
- * Name of the process/user trying to acquire the lock.
29
- */
30
- name: string;
31
- /**
32
- * The TTL for each lock (ms). Default: 60000 ms (1 min)
33
- */
34
- timeout?: number;
35
- };
36
- export declare const createMongoLockManager: (collection: Collection, params: CreateLockManagerParams) => framework.locks.LockManager;
@@ -1,83 +0,0 @@
1
- import * as framework from '@powersync/lib-services-framework';
2
- import * as bson from 'bson';
3
- const DEFAULT_LOCK_TIMEOUT = 60 * 1000; // 1 minute
4
- const acquireLock = async (collection, params) => {
5
- const now = new Date();
6
- const lock_timeout = params.timeout ?? DEFAULT_LOCK_TIMEOUT;
7
- const lock_id = new bson.ObjectId();
8
- await collection.updateOne({
9
- name: params.name
10
- }, {
11
- $setOnInsert: {
12
- name: params.name
13
- }
14
- }, {
15
- upsert: true
16
- });
17
- const expired_ts = now.getTime() - lock_timeout;
18
- const res = await collection.updateOne({
19
- $and: [
20
- { name: params.name },
21
- {
22
- $or: [{ active_lock: { $exists: false } }, { 'active_lock.ts': { $lte: new Date(expired_ts) } }]
23
- }
24
- ]
25
- }, {
26
- $set: {
27
- active_lock: {
28
- lock_id: lock_id,
29
- ts: now
30
- }
31
- }
32
- });
33
- if (res.modifiedCount === 0) {
34
- return null;
35
- }
36
- return lock_id.toString();
37
- };
38
- const refreshLock = async (collection, lock_id) => {
39
- const lockId = new bson.ObjectId(lock_id);
40
- const res = await collection.updateOne({
41
- 'active_lock.lock_id': lockId
42
- }, {
43
- $set: {
44
- 'active_lock.ts': new Date()
45
- }
46
- });
47
- if (res.modifiedCount === 0) {
48
- throw new Error('Lock not found, could not refresh');
49
- }
50
- };
51
- export const releaseLock = async (collection, lock_id) => {
52
- const lockId = new bson.ObjectId(lock_id);
53
- const res = await collection.updateOne({
54
- 'active_lock.lock_id': lockId
55
- }, {
56
- $unset: {
57
- active_lock: true
58
- }
59
- });
60
- if (res.modifiedCount === 0) {
61
- throw new Error('Lock not found, could not release');
62
- }
63
- };
64
- export const createMongoLockManager = (collection, params) => {
65
- return {
66
- acquire: () => acquireLock(collection, params),
67
- refresh: (lock_id) => refreshLock(collection, lock_id),
68
- release: (lock_id) => releaseLock(collection, lock_id),
69
- lock: async (handler) => {
70
- const lock_id = await acquireLock(collection, params);
71
- if (!lock_id) {
72
- throw new framework.locks.LockActiveError();
73
- }
74
- try {
75
- await handler(() => refreshLock(collection, lock_id));
76
- }
77
- finally {
78
- await releaseLock(collection, lock_id);
79
- }
80
- }
81
- };
82
- };
83
- //# sourceMappingURL=MonogLocks.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"MonogLocks.js","sourceRoot":"","sources":["../../src/locks/MonogLocks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,mCAAmC,CAAC;AAC/D,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AA2B7B,MAAM,oBAAoB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,WAAW;AAEnD,MAAM,WAAW,GAAG,KAAK,EAAE,UAAsB,EAAE,MAAyB,EAAE,EAAE;IAC9E,MAAM,GAAG,GAAG,IAAI,IAAI,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GAAG,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC;YACxD,CAAC;oBAAS,CAAC;gBACT,MAAM,WAAW,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;YACzC,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
@@ -1,147 +0,0 @@
1
- import * as framework from '@powersync/lib-services-framework';
2
- import * as bson from 'bson';
3
- import * as mongo from 'mongodb';
4
-
5
- /**
6
- * Lock Document Schema
7
- */
8
- export type Lock = {
9
- name: string;
10
- active_lock?: {
11
- lock_id: bson.ObjectId;
12
- ts: Date;
13
- };
14
- };
15
-
16
- export type Collection = mongo.Collection<Lock>;
17
-
18
- export type AcquireLockParams = {
19
- /**
20
- * Name of the process/user trying to acquire the lock.
21
- */
22
- name: string;
23
- /**
24
- * The TTL of the lock (ms). Default: 60000 ms (1 min)
25
- */
26
- timeout?: number;
27
- };
28
-
29
- const DEFAULT_LOCK_TIMEOUT = 60 * 1000; // 1 minute
30
-
31
- const acquireLock = async (collection: Collection, params: AcquireLockParams) => {
32
- const now = new Date();
33
- const lock_timeout = params.timeout ?? DEFAULT_LOCK_TIMEOUT;
34
- const lock_id = new bson.ObjectId();
35
-
36
- await collection.updateOne(
37
- {
38
- name: params.name
39
- },
40
- {
41
- $setOnInsert: {
42
- name: params.name
43
- }
44
- },
45
- {
46
- upsert: true
47
- }
48
- );
49
-
50
- const expired_ts = now.getTime() - lock_timeout;
51
-
52
- const res = await collection.updateOne(
53
- {
54
- $and: [
55
- { name: params.name },
56
- {
57
- $or: [{ active_lock: { $exists: false } }, { 'active_lock.ts': { $lte: new Date(expired_ts) } }]
58
- }
59
- ]
60
- },
61
- {
62
- $set: {
63
- active_lock: {
64
- lock_id: lock_id,
65
- ts: now
66
- }
67
- }
68
- }
69
- );
70
-
71
- if (res.modifiedCount === 0) {
72
- return null;
73
- }
74
-
75
- return lock_id.toString();
76
- };
77
-
78
- const refreshLock = async (collection: Collection, lock_id: string) => {
79
- const lockId = new bson.ObjectId(lock_id);
80
- const res = await collection.updateOne(
81
- {
82
- 'active_lock.lock_id': lockId
83
- },
84
- {
85
- $set: {
86
- 'active_lock.ts': new Date()
87
- }
88
- }
89
- );
90
-
91
- if (res.modifiedCount === 0) {
92
- throw new Error('Lock not found, could not refresh');
93
- }
94
- };
95
-
96
- export const releaseLock = async (collection: Collection, lock_id: string) => {
97
- const lockId = new bson.ObjectId(lock_id);
98
- const res = await collection.updateOne(
99
- {
100
- 'active_lock.lock_id': lockId
101
- },
102
- {
103
- $unset: {
104
- active_lock: true
105
- }
106
- }
107
- );
108
-
109
- if (res.modifiedCount === 0) {
110
- throw new Error('Lock not found, could not release');
111
- }
112
- };
113
-
114
- export type CreateLockManagerParams = {
115
- /**
116
- * Name of the process/user trying to acquire the lock.
117
- */
118
- name: string;
119
- /**
120
- * The TTL for each lock (ms). Default: 60000 ms (1 min)
121
- */
122
- timeout?: number;
123
- };
124
-
125
- export const createMongoLockManager = (
126
- collection: Collection,
127
- params: CreateLockManagerParams
128
- ): framework.locks.LockManager => {
129
- return {
130
- acquire: () => acquireLock(collection, params),
131
- refresh: (lock_id: string) => refreshLock(collection, lock_id),
132
- release: (lock_id: string) => releaseLock(collection, lock_id),
133
-
134
- lock: async (handler) => {
135
- const lock_id = await acquireLock(collection, params);
136
- if (!lock_id) {
137
- throw new framework.locks.LockActiveError();
138
- }
139
-
140
- try {
141
- await handler(() => refreshLock(collection, lock_id));
142
- } finally {
143
- await releaseLock(collection, lock_id);
144
- }
145
- }
146
- };
147
- };