@event-driven-io/pongo 0.17.0-beta.42 → 0.17.0-beta.44
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/cli.cjs +571 -16
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +560 -3
- package/dist/cli.js.map +1 -1
- package/dist/cloudflare-CzlFuMmZ.js +38 -0
- package/dist/cloudflare-CzlFuMmZ.js.map +1 -0
- package/dist/cloudflare-WGIFZXop.cjs +42 -0
- package/dist/cloudflare-WGIFZXop.cjs.map +1 -0
- package/dist/cloudflare.cjs +1463 -12
- package/dist/cloudflare.cjs.map +1 -1
- package/dist/cloudflare.d.cts +435 -2
- package/dist/cloudflare.d.ts +435 -2
- package/dist/cloudflare.js +1455 -3
- package/dist/cloudflare.js.map +1 -1
- package/dist/{core-5npofT4H.js → core-BW9XZXW8.js} +2 -2
- package/dist/{core-5npofT4H.js.map → core-BW9XZXW8.js.map} +1 -1
- package/dist/{core-q2eF-VHQ.cjs → core-BicrzIKX.cjs} +10 -11
- package/dist/{core-q2eF-VHQ.cjs.map → core-BicrzIKX.cjs.map} +1 -1
- package/dist/index.cjs +1477 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +717 -1
- package/dist/index.d.ts +717 -1
- package/dist/index.js +1429 -4
- package/dist/index.js.map +1 -1
- package/dist/pg-BdtFllz7.cjs +399 -0
- package/dist/pg-BdtFllz7.cjs.map +1 -0
- package/dist/pg-BtUtOO8Y.js +395 -0
- package/dist/pg-BtUtOO8Y.js.map +1 -0
- package/dist/pg.cjs +1124 -15
- package/dist/pg.cjs.map +1 -1
- package/dist/pg.d.cts +430 -2
- package/dist/pg.d.ts +430 -2
- package/dist/pg.js +1113 -3
- package/dist/pg.js.map +1 -1
- package/dist/shim.cjs +554 -6
- package/dist/shim.cjs.map +1 -1
- package/dist/shim.d.cts +445 -1
- package/dist/shim.d.ts +445 -1
- package/dist/shim.js +551 -2
- package/dist/shim.js.map +1 -1
- package/dist/sqlite3-CFwH2ot3.cjs +44 -0
- package/dist/sqlite3-CFwH2ot3.cjs.map +1 -0
- package/dist/sqlite3-Dv0y-lg6.js +40 -0
- package/dist/sqlite3-Dv0y-lg6.js.map +1 -0
- package/dist/sqlite3.cjs +1463 -12
- package/dist/sqlite3.cjs.map +1 -1
- package/dist/sqlite3.d.cts +435 -2
- package/dist/sqlite3.d.ts +435 -2
- package/dist/sqlite3.js +1455 -3
- package/dist/sqlite3.js.map +1 -1
- package/package.json +2 -2
- package/dist/core-BHdOCUrr.js +0 -1429
- package/dist/core-BHdOCUrr.js.map +0 -1
- package/dist/core-C9SB3XMx.cjs +0 -1717
- package/dist/core-C9SB3XMx.cjs.map +0 -1
- package/dist/index-C3pnS1S_.d.cts +0 -720
- package/dist/index-CZOmOsQt.d.ts +0 -10
- package/dist/index-FXnldVnn.d.cts +0 -10
- package/dist/index-r7V4paf_.d.ts +0 -720
package/dist/shim.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_core = require('./core-C9SB3XMx.cjs');
|
|
3
|
-
const require_cli = require('./cli.cjs');
|
|
4
2
|
let _event_driven_io_dumbo = require("@event-driven-io/dumbo");
|
|
3
|
+
let lru_cache = require("lru-cache");
|
|
4
|
+
require("uuid");
|
|
5
5
|
|
|
6
6
|
//#region src/mongo/findCursor.ts
|
|
7
7
|
var FindCursor = class {
|
|
@@ -33,6 +33,554 @@ var FindCursor = class {
|
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/core/cache/pongoCacheWrapper.ts
|
|
38
|
+
const pongoCacheWrapper = (options) => {
|
|
39
|
+
const { provider, hooks } = options;
|
|
40
|
+
const onError = (error, operation) => {
|
|
41
|
+
hooks?.onError?.(error, operation);
|
|
42
|
+
};
|
|
43
|
+
let isClosed = false;
|
|
44
|
+
return {
|
|
45
|
+
cacheType: provider.cacheType,
|
|
46
|
+
async get(key) {
|
|
47
|
+
try {
|
|
48
|
+
const result = await provider.get(key);
|
|
49
|
+
if (result !== void 0) hooks?.onHit?.(key);
|
|
50
|
+
else hooks?.onMiss?.(key);
|
|
51
|
+
return result;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
onError(error, "get");
|
|
54
|
+
hooks?.onMiss?.(key);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
async getMany(keys) {
|
|
59
|
+
try {
|
|
60
|
+
return await provider.getMany(keys);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
onError(error, "getMany");
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
async set(key, value) {
|
|
67
|
+
try {
|
|
68
|
+
await provider.set(key, value);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
onError(error, "set");
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
async setMany(entries) {
|
|
74
|
+
try {
|
|
75
|
+
await provider.setMany(entries);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
onError(error, "setMany");
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
async update(key, updater) {
|
|
81
|
+
try {
|
|
82
|
+
await provider.update(key, updater);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
onError(error, "update");
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
async updateMany(keys, updater) {
|
|
88
|
+
try {
|
|
89
|
+
await provider.updateMany(keys, updater);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
onError(error, "updateMany");
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
async delete(key) {
|
|
95
|
+
try {
|
|
96
|
+
await provider.delete(key);
|
|
97
|
+
hooks?.onEvict?.(key);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
onError(error, "delete");
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
async deleteMany(keys) {
|
|
103
|
+
try {
|
|
104
|
+
await provider.deleteMany(keys);
|
|
105
|
+
for (const key of keys) hooks?.onEvict?.(key);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
onError(error, "deleteMany");
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
clear() {
|
|
111
|
+
return provider.clear();
|
|
112
|
+
},
|
|
113
|
+
close() {
|
|
114
|
+
if (isClosed) return;
|
|
115
|
+
isClosed = true;
|
|
116
|
+
return provider.close();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region src/core/cache/providers/identityMapCache.ts
|
|
123
|
+
const identityMapCache = () => {
|
|
124
|
+
const store = /* @__PURE__ */ new Map();
|
|
125
|
+
return {
|
|
126
|
+
cacheType: "pongo:cache:identity-map",
|
|
127
|
+
get: (key) => Promise.resolve(store.has(key) ? store.get(key) : void 0),
|
|
128
|
+
getMany: (keys) => keys.map((k) => store.has(k) ? store.get(k) : void 0),
|
|
129
|
+
set: (key, value) => {
|
|
130
|
+
store.set(key, value);
|
|
131
|
+
},
|
|
132
|
+
setMany: (entries) => {
|
|
133
|
+
for (const { key, value } of entries) store.set(key, value);
|
|
134
|
+
},
|
|
135
|
+
update: (key, _updater) => {
|
|
136
|
+
store.delete(key);
|
|
137
|
+
},
|
|
138
|
+
updateMany: (keys, _updater) => {
|
|
139
|
+
for (const key of keys) store.delete(key);
|
|
140
|
+
},
|
|
141
|
+
delete: (key) => {
|
|
142
|
+
store.delete(key);
|
|
143
|
+
},
|
|
144
|
+
deleteMany: (keys) => {
|
|
145
|
+
for (const key of keys) store.delete(key);
|
|
146
|
+
},
|
|
147
|
+
clear: () => {
|
|
148
|
+
store.clear();
|
|
149
|
+
},
|
|
150
|
+
close: () => {
|
|
151
|
+
store.clear();
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/core/cache/providers/lruCache.ts
|
|
158
|
+
const defaultLRUCacheOptions = { max: 1e3 };
|
|
159
|
+
const lruCache = (options) => {
|
|
160
|
+
const cache = new lru_cache.LRUCache({
|
|
161
|
+
...defaultLRUCacheOptions,
|
|
162
|
+
...options
|
|
163
|
+
});
|
|
164
|
+
return {
|
|
165
|
+
cacheType: "pongo:cache:lru",
|
|
166
|
+
get: (key) => {
|
|
167
|
+
const entry = cache.get(key);
|
|
168
|
+
if (entry === void 0) return void 0;
|
|
169
|
+
return entry.doc;
|
|
170
|
+
},
|
|
171
|
+
getMany: (keys) => keys.map((k) => {
|
|
172
|
+
const entry = cache.get(k);
|
|
173
|
+
if (entry === void 0) return void 0;
|
|
174
|
+
return entry.doc;
|
|
175
|
+
}),
|
|
176
|
+
set: (key, value) => {
|
|
177
|
+
cache.set(key, { doc: value });
|
|
178
|
+
},
|
|
179
|
+
setMany: (entries) => {
|
|
180
|
+
for (const { key, value } of entries) cache.set(key, { doc: value });
|
|
181
|
+
},
|
|
182
|
+
update: (key, _updater) => {
|
|
183
|
+
cache.delete(key);
|
|
184
|
+
},
|
|
185
|
+
updateMany(keys, _updater) {
|
|
186
|
+
for (const key of keys) cache.delete(key);
|
|
187
|
+
},
|
|
188
|
+
delete: (key) => {
|
|
189
|
+
cache.delete(key);
|
|
190
|
+
},
|
|
191
|
+
deleteMany: (keys) => {
|
|
192
|
+
for (const key of keys) cache.delete(key);
|
|
193
|
+
},
|
|
194
|
+
clear: () => {
|
|
195
|
+
cache.clear();
|
|
196
|
+
},
|
|
197
|
+
close: () => {
|
|
198
|
+
cache.clear();
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
//#endregion
|
|
204
|
+
//#region src/core/cache/providers/noopCache.ts
|
|
205
|
+
const noopCacheProvider = {
|
|
206
|
+
cacheType: "pongo:cache:no-op",
|
|
207
|
+
get: () => void 0,
|
|
208
|
+
set: () => {},
|
|
209
|
+
update: () => {},
|
|
210
|
+
delete: () => {},
|
|
211
|
+
getMany: (keys) => keys.map(() => void 0),
|
|
212
|
+
setMany: () => {},
|
|
213
|
+
updateMany: () => {},
|
|
214
|
+
deleteMany: () => {},
|
|
215
|
+
clear: () => {},
|
|
216
|
+
close: () => {}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src/core/cache/pongoCache.ts
|
|
221
|
+
const DEFAULT_CONFIG = { type: "in-memory" };
|
|
222
|
+
const pongoCache = (options) => {
|
|
223
|
+
if (options === void 0 || options === "disabled") return noopCacheProvider;
|
|
224
|
+
if ("cacheType" in options) return options;
|
|
225
|
+
const config = options ?? DEFAULT_CONFIG;
|
|
226
|
+
if (config.type === "identity-map") return identityMapCache();
|
|
227
|
+
return pongoCacheWrapper({ provider: lruCache(config) });
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
//#endregion
|
|
231
|
+
//#region src/core/cache/transactions/pongoTransactionCache.ts
|
|
232
|
+
const pongoTransactionCache = (options) => {
|
|
233
|
+
const innerCache = options?.cache ?? identityMapCache();
|
|
234
|
+
const operations = [];
|
|
235
|
+
return {
|
|
236
|
+
type: "pongo:cache:transaction-buffer",
|
|
237
|
+
get(key) {
|
|
238
|
+
return innerCache.get(key);
|
|
239
|
+
},
|
|
240
|
+
set(key, value, options) {
|
|
241
|
+
const { mainCache } = options;
|
|
242
|
+
innerCache.set(key, value);
|
|
243
|
+
operations.push({
|
|
244
|
+
type: "set",
|
|
245
|
+
key,
|
|
246
|
+
value,
|
|
247
|
+
mainCache
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
update(key, updater, options) {
|
|
251
|
+
const { mainCache } = options;
|
|
252
|
+
innerCache.update(key, updater);
|
|
253
|
+
operations.push({
|
|
254
|
+
type: "update",
|
|
255
|
+
key,
|
|
256
|
+
updater,
|
|
257
|
+
mainCache
|
|
258
|
+
});
|
|
259
|
+
},
|
|
260
|
+
delete(key, options) {
|
|
261
|
+
innerCache.delete(key);
|
|
262
|
+
operations.push({
|
|
263
|
+
type: "delete",
|
|
264
|
+
key,
|
|
265
|
+
mainCache: options.mainCache
|
|
266
|
+
});
|
|
267
|
+
},
|
|
268
|
+
getMany(keys) {
|
|
269
|
+
return innerCache.getMany(keys);
|
|
270
|
+
},
|
|
271
|
+
setMany(entries, options) {
|
|
272
|
+
innerCache.setMany(entries);
|
|
273
|
+
operations.push({
|
|
274
|
+
type: "setMany",
|
|
275
|
+
entries,
|
|
276
|
+
mainCache: options.mainCache
|
|
277
|
+
});
|
|
278
|
+
},
|
|
279
|
+
updateMany(keys, updater, options) {
|
|
280
|
+
const { mainCache } = options;
|
|
281
|
+
innerCache.updateMany(keys, updater);
|
|
282
|
+
operations.push({
|
|
283
|
+
type: "updateMany",
|
|
284
|
+
keys,
|
|
285
|
+
updater,
|
|
286
|
+
mainCache
|
|
287
|
+
});
|
|
288
|
+
},
|
|
289
|
+
deleteMany(keys, options) {
|
|
290
|
+
innerCache.deleteMany(keys);
|
|
291
|
+
operations.push({
|
|
292
|
+
type: "deleteMany",
|
|
293
|
+
keys,
|
|
294
|
+
mainCache: options.mainCache
|
|
295
|
+
});
|
|
296
|
+
},
|
|
297
|
+
clear() {
|
|
298
|
+
innerCache.clear();
|
|
299
|
+
operations.length = 0;
|
|
300
|
+
},
|
|
301
|
+
async commit() {
|
|
302
|
+
for (const op of operations) switch (op.type) {
|
|
303
|
+
case "set":
|
|
304
|
+
await op.mainCache.set(op.key, op.value);
|
|
305
|
+
break;
|
|
306
|
+
case "setMany":
|
|
307
|
+
await op.mainCache.setMany(op.entries);
|
|
308
|
+
break;
|
|
309
|
+
case "update":
|
|
310
|
+
await op.mainCache.update(op.key, op.updater);
|
|
311
|
+
break;
|
|
312
|
+
case "updateMany":
|
|
313
|
+
await op.mainCache.updateMany(op.keys, op.updater);
|
|
314
|
+
break;
|
|
315
|
+
case "delete":
|
|
316
|
+
await op.mainCache.delete(op.key);
|
|
317
|
+
break;
|
|
318
|
+
case "deleteMany":
|
|
319
|
+
await op.mainCache.deleteMany(op.keys);
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
innerCache.clear();
|
|
323
|
+
operations.length = 0;
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
//#endregion
|
|
329
|
+
//#region src/core/database/pongoDatabaseCache.ts
|
|
330
|
+
const PongoDatabaseCache = ({ driver, typedSchema }) => {
|
|
331
|
+
const dbClients = /* @__PURE__ */ new Map();
|
|
332
|
+
const getDatabaseDefinition = (dbName) => Object.values(typedSchema?.dbs ?? {}).find((d) => d.name === dbName);
|
|
333
|
+
return {
|
|
334
|
+
getOrCreate: (createOptions) => {
|
|
335
|
+
const metadata = (0, _event_driven_io_dumbo.getDatabaseMetadata)(driver.driverType);
|
|
336
|
+
const dbName = createOptions.databaseName ?? metadata?.parseDatabaseName?.("connectionString" in createOptions ? createOptions.connectionString : void 0) ?? "db:default";
|
|
337
|
+
const existing = dbClients.get(dbName);
|
|
338
|
+
if (existing) return existing;
|
|
339
|
+
const definition = getDatabaseDefinition(createOptions.databaseName);
|
|
340
|
+
const newDb = driver.databaseFactory({
|
|
341
|
+
...createOptions,
|
|
342
|
+
databaseName: dbName,
|
|
343
|
+
schema: {
|
|
344
|
+
...createOptions.schema,
|
|
345
|
+
...definition ? { definition } : {}
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
dbClients.set(dbName, newDb);
|
|
349
|
+
return newDb;
|
|
350
|
+
},
|
|
351
|
+
all: () => Array.from(dbClients.values()),
|
|
352
|
+
forAll: (func) => {
|
|
353
|
+
return Promise.all(Array.from(dbClients.values()).map((v) => v).map(func));
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/core/schema/index.ts
|
|
360
|
+
const pongoCollectionSchema = (name) => ({ name });
|
|
361
|
+
pongoCollectionSchema.from = (collectionNames) => collectionNames.reduce((acc, collectionName) => (acc[collectionName] = pongoSchema.collection(collectionName), acc), {});
|
|
362
|
+
function pongoDbSchema(nameOrCollections, collections) {
|
|
363
|
+
if (collections === void 0) {
|
|
364
|
+
if (typeof nameOrCollections === "string") throw new Error("You need to provide colleciton definition");
|
|
365
|
+
return { collections: nameOrCollections };
|
|
366
|
+
}
|
|
367
|
+
return nameOrCollections && typeof nameOrCollections === "string" ? {
|
|
368
|
+
name: nameOrCollections,
|
|
369
|
+
collections
|
|
370
|
+
} : { collections };
|
|
371
|
+
}
|
|
372
|
+
pongoDbSchema.from = (databaseName, collectionNames) => databaseName ? pongoDbSchema(databaseName, pongoCollectionSchema.from(collectionNames)) : pongoDbSchema(pongoCollectionSchema.from(collectionNames));
|
|
373
|
+
const pongoClientSchema = (dbs) => ({ dbs });
|
|
374
|
+
const pongoSchema = {
|
|
375
|
+
client: pongoClientSchema,
|
|
376
|
+
db: pongoDbSchema,
|
|
377
|
+
collection: pongoCollectionSchema
|
|
378
|
+
};
|
|
379
|
+
const proxyClientWithSchema = (client, schema) => {
|
|
380
|
+
if (!schema) return client;
|
|
381
|
+
const dbNames = Object.keys(schema.dbs);
|
|
382
|
+
return new Proxy(client, { get(target, prop) {
|
|
383
|
+
if (dbNames.includes(prop)) return client.db(schema.dbs[prop]?.name);
|
|
384
|
+
return target[prop];
|
|
385
|
+
} });
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
//#endregion
|
|
389
|
+
//#region src/core/drivers/databaseDriver.ts
|
|
390
|
+
const PongoDriverRegistry = () => {
|
|
391
|
+
const drivers = /* @__PURE__ */ new Map();
|
|
392
|
+
const register = (driverType, driver) => {
|
|
393
|
+
const entry = drivers.get(driverType);
|
|
394
|
+
if (entry && (typeof entry !== "function" || typeof driver === "function")) return;
|
|
395
|
+
drivers.set(driverType, driver);
|
|
396
|
+
};
|
|
397
|
+
const tryResolve = async (driverType) => {
|
|
398
|
+
const entry = drivers.get(driverType);
|
|
399
|
+
if (!entry) return null;
|
|
400
|
+
if (typeof entry !== "function") return entry;
|
|
401
|
+
const driver = await entry();
|
|
402
|
+
register(driverType, driver);
|
|
403
|
+
return driver;
|
|
404
|
+
};
|
|
405
|
+
const tryGet = (driverType) => {
|
|
406
|
+
const entry = drivers.get(driverType);
|
|
407
|
+
return entry && typeof entry !== "function" ? entry : null;
|
|
408
|
+
};
|
|
409
|
+
const has = (driverType) => drivers.has(driverType);
|
|
410
|
+
return {
|
|
411
|
+
register,
|
|
412
|
+
tryResolve,
|
|
413
|
+
tryGet,
|
|
414
|
+
has,
|
|
415
|
+
get databaseDriverTypes() {
|
|
416
|
+
return Array.from(drivers.keys());
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
};
|
|
420
|
+
const pongoDriverRegistry$1 = globalThis.pongoDriverRegistry = globalThis.pongoDriverRegistry ?? PongoDriverRegistry();
|
|
421
|
+
|
|
422
|
+
//#endregion
|
|
423
|
+
//#region src/core/pongoTransaction.ts
|
|
424
|
+
const pongoTransaction = (options) => {
|
|
425
|
+
let isCommitted = false;
|
|
426
|
+
let isRolledBack = false;
|
|
427
|
+
let databaseName = null;
|
|
428
|
+
let transaction = null;
|
|
429
|
+
const cache = pongoTransactionCache();
|
|
430
|
+
return {
|
|
431
|
+
cache,
|
|
432
|
+
enlistDatabase: async (db) => {
|
|
433
|
+
if (transaction && databaseName !== db.databaseName) throw new Error("There's already other database assigned to transaction");
|
|
434
|
+
if (transaction && databaseName === db.databaseName) return transaction;
|
|
435
|
+
databaseName = db.databaseName;
|
|
436
|
+
transaction = db.transaction();
|
|
437
|
+
await transaction.begin();
|
|
438
|
+
return transaction;
|
|
439
|
+
},
|
|
440
|
+
commit: async () => {
|
|
441
|
+
if (isCommitted) return;
|
|
442
|
+
if (isRolledBack) throw new Error("Transaction is not active!");
|
|
443
|
+
isCommitted = true;
|
|
444
|
+
if (transaction) {
|
|
445
|
+
await transaction.commit();
|
|
446
|
+
transaction = null;
|
|
447
|
+
}
|
|
448
|
+
await cache.commit();
|
|
449
|
+
},
|
|
450
|
+
rollback: async (error) => {
|
|
451
|
+
if (isCommitted) throw new Error("Cannot rollback commited transaction!");
|
|
452
|
+
if (isRolledBack) return;
|
|
453
|
+
isRolledBack = true;
|
|
454
|
+
if (transaction) {
|
|
455
|
+
await transaction.rollback(error);
|
|
456
|
+
transaction = null;
|
|
457
|
+
}
|
|
458
|
+
cache.clear();
|
|
459
|
+
},
|
|
460
|
+
databaseName,
|
|
461
|
+
isStarting: false,
|
|
462
|
+
isCommitted,
|
|
463
|
+
get isActive() {
|
|
464
|
+
return !isCommitted && !isRolledBack;
|
|
465
|
+
},
|
|
466
|
+
get sqlExecutor() {
|
|
467
|
+
if (transaction === null) throw new Error("No database transaction was started");
|
|
468
|
+
return transaction.execute;
|
|
469
|
+
},
|
|
470
|
+
options
|
|
471
|
+
};
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
//#endregion
|
|
475
|
+
//#region src/core/pongoSession.ts
|
|
476
|
+
const isActive = (transaction) => transaction?.isActive === true;
|
|
477
|
+
function assertInActiveTransaction(transaction) {
|
|
478
|
+
if (!isActive(transaction)) throw new Error("No active transaction exists!");
|
|
479
|
+
}
|
|
480
|
+
function assertNotInActiveTransaction(transaction) {
|
|
481
|
+
if (isActive(transaction)) throw new Error("Active transaction already exists!");
|
|
482
|
+
}
|
|
483
|
+
const pongoSession = (options) => {
|
|
484
|
+
const explicit = options?.explicit === true;
|
|
485
|
+
const defaultTransactionOptions = options?.defaultTransactionOptions ?? { get snapshotEnabled() {
|
|
486
|
+
return false;
|
|
487
|
+
} };
|
|
488
|
+
let transaction = null;
|
|
489
|
+
let hasEnded = false;
|
|
490
|
+
const startTransaction = (options) => {
|
|
491
|
+
assertNotInActiveTransaction(transaction);
|
|
492
|
+
transaction = pongoTransaction(options ?? defaultTransactionOptions);
|
|
493
|
+
};
|
|
494
|
+
const commitTransaction = async () => {
|
|
495
|
+
assertInActiveTransaction(transaction);
|
|
496
|
+
await transaction.commit();
|
|
497
|
+
};
|
|
498
|
+
const abortTransaction = async () => {
|
|
499
|
+
assertInActiveTransaction(transaction);
|
|
500
|
+
await transaction.rollback();
|
|
501
|
+
};
|
|
502
|
+
const endSession = async () => {
|
|
503
|
+
if (hasEnded) return;
|
|
504
|
+
hasEnded = true;
|
|
505
|
+
if (isActive(transaction)) await transaction.rollback();
|
|
506
|
+
};
|
|
507
|
+
const session = {
|
|
508
|
+
get hasEnded() {
|
|
509
|
+
return hasEnded;
|
|
510
|
+
},
|
|
511
|
+
explicit,
|
|
512
|
+
defaultTransactionOptions: defaultTransactionOptions ?? { get snapshotEnabled() {
|
|
513
|
+
return false;
|
|
514
|
+
} },
|
|
515
|
+
get transaction() {
|
|
516
|
+
return transaction;
|
|
517
|
+
},
|
|
518
|
+
get snapshotEnabled() {
|
|
519
|
+
return defaultTransactionOptions.snapshotEnabled;
|
|
520
|
+
},
|
|
521
|
+
endSession,
|
|
522
|
+
incrementTransactionNumber: () => {},
|
|
523
|
+
inTransaction: () => isActive(transaction),
|
|
524
|
+
startTransaction,
|
|
525
|
+
commitTransaction,
|
|
526
|
+
abortTransaction,
|
|
527
|
+
withTransaction: async (fn, options) => {
|
|
528
|
+
startTransaction(options);
|
|
529
|
+
try {
|
|
530
|
+
const result = await fn(session);
|
|
531
|
+
await commitTransaction();
|
|
532
|
+
return result;
|
|
533
|
+
} catch (error) {
|
|
534
|
+
await abortTransaction();
|
|
535
|
+
throw error;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
return session;
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
//#endregion
|
|
543
|
+
//#region src/core/pongoClient.ts
|
|
544
|
+
const pongoClient = (options) => {
|
|
545
|
+
const { driver, schema, errors, cache: cacheOptions, serialization, ...connectionOptions } = options;
|
|
546
|
+
const dbClients = PongoDatabaseCache({
|
|
547
|
+
driver,
|
|
548
|
+
typedSchema: schema?.definition
|
|
549
|
+
});
|
|
550
|
+
const serializer = _event_driven_io_dumbo.JSONSerializer.from(options);
|
|
551
|
+
const cache = cacheOptions === "disabled" || cacheOptions === void 0 ? "disabled" : pongoCache(cacheOptions);
|
|
552
|
+
const pongoClient = {
|
|
553
|
+
driverType: driver.driverType,
|
|
554
|
+
connect: async () => {
|
|
555
|
+
await dbClients.forAll((db) => db.connect());
|
|
556
|
+
return pongoClient;
|
|
557
|
+
},
|
|
558
|
+
close: async () => {
|
|
559
|
+
await dbClients.forAll((db) => db.close());
|
|
560
|
+
},
|
|
561
|
+
db: (dbName, options) => {
|
|
562
|
+
return dbClients.getOrCreate({
|
|
563
|
+
...connectionOptions,
|
|
564
|
+
databaseName: dbName,
|
|
565
|
+
serializer,
|
|
566
|
+
errors,
|
|
567
|
+
cache: options?.cache ?? cache,
|
|
568
|
+
serialization
|
|
569
|
+
});
|
|
570
|
+
},
|
|
571
|
+
startSession: pongoSession,
|
|
572
|
+
withSession: async (callback) => {
|
|
573
|
+
const session = pongoSession();
|
|
574
|
+
try {
|
|
575
|
+
return await callback(session);
|
|
576
|
+
} finally {
|
|
577
|
+
await session.endSession();
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
return proxyClientWithSchema(pongoClient, schema?.definition);
|
|
582
|
+
};
|
|
583
|
+
|
|
36
584
|
//#endregion
|
|
37
585
|
//#region src/mongo/mongoCollection.ts
|
|
38
586
|
const toCollectionOperationOptions = (options) => options?.session ? { session: options.session } : void 0;
|
|
@@ -260,13 +808,13 @@ var MongoClient = class {
|
|
|
260
808
|
pongoClient;
|
|
261
809
|
constructor(connectionStringOrOptions, options) {
|
|
262
810
|
if (typeof connectionStringOrOptions !== "string") {
|
|
263
|
-
this.pongoClient =
|
|
811
|
+
this.pongoClient = pongoClient(connectionStringOrOptions);
|
|
264
812
|
return;
|
|
265
813
|
}
|
|
266
814
|
const { databaseType, driverName } = (0, _event_driven_io_dumbo.parseConnectionString)(connectionStringOrOptions);
|
|
267
815
|
const driver = options?.driver ?? pongoDriverRegistry.tryGet((0, _event_driven_io_dumbo.toDatabaseDriverType)(databaseType, driverName));
|
|
268
816
|
if (driver === null) throw new Error(`No database driver registered for ${databaseType} with name ${driverName}`);
|
|
269
|
-
this.pongoClient =
|
|
817
|
+
this.pongoClient = pongoClient({
|
|
270
818
|
...options ?? {},
|
|
271
819
|
connectionString: connectionStringOrOptions,
|
|
272
820
|
driver
|
|
@@ -283,11 +831,11 @@ var MongoClient = class {
|
|
|
283
831
|
return new Db(this.pongoClient.db(dbName));
|
|
284
832
|
}
|
|
285
833
|
startSession(_options) {
|
|
286
|
-
return
|
|
834
|
+
return pongoSession();
|
|
287
835
|
}
|
|
288
836
|
async withSession(optionsOrExecutor, executor) {
|
|
289
837
|
const callback = typeof optionsOrExecutor === "function" ? optionsOrExecutor : executor;
|
|
290
|
-
const session =
|
|
838
|
+
const session = pongoSession();
|
|
291
839
|
try {
|
|
292
840
|
return await callback(session);
|
|
293
841
|
} finally {
|