@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/cli.cjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
3
2
|
//#region \0rolldown/runtime.js
|
|
4
3
|
var __create = Object.create;
|
|
5
4
|
var __defProp = Object.defineProperty;
|
|
@@ -27,17 +26,574 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
26
|
}) : target, mod));
|
|
28
27
|
|
|
29
28
|
//#endregion
|
|
30
|
-
const require_core = require('./core-C9SB3XMx.cjs');
|
|
31
|
-
let _event_driven_io_dumbo = require("@event-driven-io/dumbo");
|
|
32
29
|
let commander = require("commander");
|
|
33
30
|
let node_fs = require("node:fs");
|
|
34
31
|
node_fs = __toESM(node_fs, 1);
|
|
32
|
+
let lru_cache = require("lru-cache");
|
|
33
|
+
let _event_driven_io_dumbo = require("@event-driven-io/dumbo");
|
|
34
|
+
require("uuid");
|
|
35
35
|
let _event_driven_io_dumbo_pg = require("@event-driven-io/dumbo/pg");
|
|
36
36
|
let cli_table3 = require("cli-table3");
|
|
37
37
|
cli_table3 = __toESM(cli_table3, 1);
|
|
38
38
|
let node_repl = require("node:repl");
|
|
39
39
|
node_repl = __toESM(node_repl, 1);
|
|
40
40
|
|
|
41
|
+
//#region src/core/cache/pongoCacheWrapper.ts
|
|
42
|
+
const pongoCacheWrapper = (options) => {
|
|
43
|
+
const { provider, hooks } = options;
|
|
44
|
+
const onError = (error, operation) => {
|
|
45
|
+
hooks?.onError?.(error, operation);
|
|
46
|
+
};
|
|
47
|
+
let isClosed = false;
|
|
48
|
+
return {
|
|
49
|
+
cacheType: provider.cacheType,
|
|
50
|
+
async get(key) {
|
|
51
|
+
try {
|
|
52
|
+
const result = await provider.get(key);
|
|
53
|
+
if (result !== void 0) hooks?.onHit?.(key);
|
|
54
|
+
else hooks?.onMiss?.(key);
|
|
55
|
+
return result;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
onError(error, "get");
|
|
58
|
+
hooks?.onMiss?.(key);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
async getMany(keys) {
|
|
63
|
+
try {
|
|
64
|
+
return await provider.getMany(keys);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
onError(error, "getMany");
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
async set(key, value) {
|
|
71
|
+
try {
|
|
72
|
+
await provider.set(key, value);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
onError(error, "set");
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
async setMany(entries) {
|
|
78
|
+
try {
|
|
79
|
+
await provider.setMany(entries);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
onError(error, "setMany");
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
async update(key, updater) {
|
|
85
|
+
try {
|
|
86
|
+
await provider.update(key, updater);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
onError(error, "update");
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
async updateMany(keys, updater) {
|
|
92
|
+
try {
|
|
93
|
+
await provider.updateMany(keys, updater);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
onError(error, "updateMany");
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
async delete(key) {
|
|
99
|
+
try {
|
|
100
|
+
await provider.delete(key);
|
|
101
|
+
hooks?.onEvict?.(key);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
onError(error, "delete");
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
async deleteMany(keys) {
|
|
107
|
+
try {
|
|
108
|
+
await provider.deleteMany(keys);
|
|
109
|
+
for (const key of keys) hooks?.onEvict?.(key);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
onError(error, "deleteMany");
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
clear() {
|
|
115
|
+
return provider.clear();
|
|
116
|
+
},
|
|
117
|
+
close() {
|
|
118
|
+
if (isClosed) return;
|
|
119
|
+
isClosed = true;
|
|
120
|
+
return provider.close();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/core/cache/providers/identityMapCache.ts
|
|
127
|
+
const identityMapCache = () => {
|
|
128
|
+
const store = /* @__PURE__ */ new Map();
|
|
129
|
+
return {
|
|
130
|
+
cacheType: "pongo:cache:identity-map",
|
|
131
|
+
get: (key) => Promise.resolve(store.has(key) ? store.get(key) : void 0),
|
|
132
|
+
getMany: (keys) => keys.map((k) => store.has(k) ? store.get(k) : void 0),
|
|
133
|
+
set: (key, value) => {
|
|
134
|
+
store.set(key, value);
|
|
135
|
+
},
|
|
136
|
+
setMany: (entries) => {
|
|
137
|
+
for (const { key, value } of entries) store.set(key, value);
|
|
138
|
+
},
|
|
139
|
+
update: (key, _updater) => {
|
|
140
|
+
store.delete(key);
|
|
141
|
+
},
|
|
142
|
+
updateMany: (keys, _updater) => {
|
|
143
|
+
for (const key of keys) store.delete(key);
|
|
144
|
+
},
|
|
145
|
+
delete: (key) => {
|
|
146
|
+
store.delete(key);
|
|
147
|
+
},
|
|
148
|
+
deleteMany: (keys) => {
|
|
149
|
+
for (const key of keys) store.delete(key);
|
|
150
|
+
},
|
|
151
|
+
clear: () => {
|
|
152
|
+
store.clear();
|
|
153
|
+
},
|
|
154
|
+
close: () => {
|
|
155
|
+
store.clear();
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/core/cache/providers/lruCache.ts
|
|
162
|
+
const defaultLRUCacheOptions = { max: 1e3 };
|
|
163
|
+
const lruCache = (options) => {
|
|
164
|
+
const cache = new lru_cache.LRUCache({
|
|
165
|
+
...defaultLRUCacheOptions,
|
|
166
|
+
...options
|
|
167
|
+
});
|
|
168
|
+
return {
|
|
169
|
+
cacheType: "pongo:cache:lru",
|
|
170
|
+
get: (key) => {
|
|
171
|
+
const entry = cache.get(key);
|
|
172
|
+
if (entry === void 0) return void 0;
|
|
173
|
+
return entry.doc;
|
|
174
|
+
},
|
|
175
|
+
getMany: (keys) => keys.map((k) => {
|
|
176
|
+
const entry = cache.get(k);
|
|
177
|
+
if (entry === void 0) return void 0;
|
|
178
|
+
return entry.doc;
|
|
179
|
+
}),
|
|
180
|
+
set: (key, value) => {
|
|
181
|
+
cache.set(key, { doc: value });
|
|
182
|
+
},
|
|
183
|
+
setMany: (entries) => {
|
|
184
|
+
for (const { key, value } of entries) cache.set(key, { doc: value });
|
|
185
|
+
},
|
|
186
|
+
update: (key, _updater) => {
|
|
187
|
+
cache.delete(key);
|
|
188
|
+
},
|
|
189
|
+
updateMany(keys, _updater) {
|
|
190
|
+
for (const key of keys) cache.delete(key);
|
|
191
|
+
},
|
|
192
|
+
delete: (key) => {
|
|
193
|
+
cache.delete(key);
|
|
194
|
+
},
|
|
195
|
+
deleteMany: (keys) => {
|
|
196
|
+
for (const key of keys) cache.delete(key);
|
|
197
|
+
},
|
|
198
|
+
clear: () => {
|
|
199
|
+
cache.clear();
|
|
200
|
+
},
|
|
201
|
+
close: () => {
|
|
202
|
+
cache.clear();
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/core/cache/providers/noopCache.ts
|
|
209
|
+
const noopCacheProvider = {
|
|
210
|
+
cacheType: "pongo:cache:no-op",
|
|
211
|
+
get: () => void 0,
|
|
212
|
+
set: () => {},
|
|
213
|
+
update: () => {},
|
|
214
|
+
delete: () => {},
|
|
215
|
+
getMany: (keys) => keys.map(() => void 0),
|
|
216
|
+
setMany: () => {},
|
|
217
|
+
updateMany: () => {},
|
|
218
|
+
deleteMany: () => {},
|
|
219
|
+
clear: () => {},
|
|
220
|
+
close: () => {}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/core/cache/pongoCache.ts
|
|
225
|
+
const DEFAULT_CONFIG = { type: "in-memory" };
|
|
226
|
+
const pongoCache = (options) => {
|
|
227
|
+
if (options === void 0 || options === "disabled") return noopCacheProvider;
|
|
228
|
+
if ("cacheType" in options) return options;
|
|
229
|
+
const config = options ?? DEFAULT_CONFIG;
|
|
230
|
+
if (config.type === "identity-map") return identityMapCache();
|
|
231
|
+
return pongoCacheWrapper({ provider: lruCache(config) });
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/core/cache/transactions/pongoTransactionCache.ts
|
|
236
|
+
const pongoTransactionCache = (options) => {
|
|
237
|
+
const innerCache = options?.cache ?? identityMapCache();
|
|
238
|
+
const operations = [];
|
|
239
|
+
return {
|
|
240
|
+
type: "pongo:cache:transaction-buffer",
|
|
241
|
+
get(key) {
|
|
242
|
+
return innerCache.get(key);
|
|
243
|
+
},
|
|
244
|
+
set(key, value, options) {
|
|
245
|
+
const { mainCache } = options;
|
|
246
|
+
innerCache.set(key, value);
|
|
247
|
+
operations.push({
|
|
248
|
+
type: "set",
|
|
249
|
+
key,
|
|
250
|
+
value,
|
|
251
|
+
mainCache
|
|
252
|
+
});
|
|
253
|
+
},
|
|
254
|
+
update(key, updater, options) {
|
|
255
|
+
const { mainCache } = options;
|
|
256
|
+
innerCache.update(key, updater);
|
|
257
|
+
operations.push({
|
|
258
|
+
type: "update",
|
|
259
|
+
key,
|
|
260
|
+
updater,
|
|
261
|
+
mainCache
|
|
262
|
+
});
|
|
263
|
+
},
|
|
264
|
+
delete(key, options) {
|
|
265
|
+
innerCache.delete(key);
|
|
266
|
+
operations.push({
|
|
267
|
+
type: "delete",
|
|
268
|
+
key,
|
|
269
|
+
mainCache: options.mainCache
|
|
270
|
+
});
|
|
271
|
+
},
|
|
272
|
+
getMany(keys) {
|
|
273
|
+
return innerCache.getMany(keys);
|
|
274
|
+
},
|
|
275
|
+
setMany(entries, options) {
|
|
276
|
+
innerCache.setMany(entries);
|
|
277
|
+
operations.push({
|
|
278
|
+
type: "setMany",
|
|
279
|
+
entries,
|
|
280
|
+
mainCache: options.mainCache
|
|
281
|
+
});
|
|
282
|
+
},
|
|
283
|
+
updateMany(keys, updater, options) {
|
|
284
|
+
const { mainCache } = options;
|
|
285
|
+
innerCache.updateMany(keys, updater);
|
|
286
|
+
operations.push({
|
|
287
|
+
type: "updateMany",
|
|
288
|
+
keys,
|
|
289
|
+
updater,
|
|
290
|
+
mainCache
|
|
291
|
+
});
|
|
292
|
+
},
|
|
293
|
+
deleteMany(keys, options) {
|
|
294
|
+
innerCache.deleteMany(keys);
|
|
295
|
+
operations.push({
|
|
296
|
+
type: "deleteMany",
|
|
297
|
+
keys,
|
|
298
|
+
mainCache: options.mainCache
|
|
299
|
+
});
|
|
300
|
+
},
|
|
301
|
+
clear() {
|
|
302
|
+
innerCache.clear();
|
|
303
|
+
operations.length = 0;
|
|
304
|
+
},
|
|
305
|
+
async commit() {
|
|
306
|
+
for (const op of operations) switch (op.type) {
|
|
307
|
+
case "set":
|
|
308
|
+
await op.mainCache.set(op.key, op.value);
|
|
309
|
+
break;
|
|
310
|
+
case "setMany":
|
|
311
|
+
await op.mainCache.setMany(op.entries);
|
|
312
|
+
break;
|
|
313
|
+
case "update":
|
|
314
|
+
await op.mainCache.update(op.key, op.updater);
|
|
315
|
+
break;
|
|
316
|
+
case "updateMany":
|
|
317
|
+
await op.mainCache.updateMany(op.keys, op.updater);
|
|
318
|
+
break;
|
|
319
|
+
case "delete":
|
|
320
|
+
await op.mainCache.delete(op.key);
|
|
321
|
+
break;
|
|
322
|
+
case "deleteMany":
|
|
323
|
+
await op.mainCache.deleteMany(op.keys);
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
innerCache.clear();
|
|
327
|
+
operations.length = 0;
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
//#endregion
|
|
333
|
+
//#region src/core/database/pongoDatabaseCache.ts
|
|
334
|
+
const PongoDatabaseCache = ({ driver, typedSchema }) => {
|
|
335
|
+
const dbClients = /* @__PURE__ */ new Map();
|
|
336
|
+
const getDatabaseDefinition = (dbName) => Object.values(typedSchema?.dbs ?? {}).find((d) => d.name === dbName);
|
|
337
|
+
return {
|
|
338
|
+
getOrCreate: (createOptions) => {
|
|
339
|
+
const metadata = (0, _event_driven_io_dumbo.getDatabaseMetadata)(driver.driverType);
|
|
340
|
+
const dbName = createOptions.databaseName ?? metadata?.parseDatabaseName?.("connectionString" in createOptions ? createOptions.connectionString : void 0) ?? "db:default";
|
|
341
|
+
const existing = dbClients.get(dbName);
|
|
342
|
+
if (existing) return existing;
|
|
343
|
+
const definition = getDatabaseDefinition(createOptions.databaseName);
|
|
344
|
+
const newDb = driver.databaseFactory({
|
|
345
|
+
...createOptions,
|
|
346
|
+
databaseName: dbName,
|
|
347
|
+
schema: {
|
|
348
|
+
...createOptions.schema,
|
|
349
|
+
...definition ? { definition } : {}
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
dbClients.set(dbName, newDb);
|
|
353
|
+
return newDb;
|
|
354
|
+
},
|
|
355
|
+
all: () => Array.from(dbClients.values()),
|
|
356
|
+
forAll: (func) => {
|
|
357
|
+
return Promise.all(Array.from(dbClients.values()).map((v) => v).map(func));
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/core/typing/entries.ts
|
|
364
|
+
const objectEntries = (obj) => Object.entries(obj).map(([key, value]) => [key, value]);
|
|
365
|
+
|
|
366
|
+
//#endregion
|
|
367
|
+
//#region src/core/schema/index.ts
|
|
368
|
+
const pongoCollectionSchema = (name) => ({ name });
|
|
369
|
+
pongoCollectionSchema.from = (collectionNames) => collectionNames.reduce((acc, collectionName) => (acc[collectionName] = pongoSchema.collection(collectionName), acc), {});
|
|
370
|
+
function pongoDbSchema(nameOrCollections, collections) {
|
|
371
|
+
if (collections === void 0) {
|
|
372
|
+
if (typeof nameOrCollections === "string") throw new Error("You need to provide colleciton definition");
|
|
373
|
+
return { collections: nameOrCollections };
|
|
374
|
+
}
|
|
375
|
+
return nameOrCollections && typeof nameOrCollections === "string" ? {
|
|
376
|
+
name: nameOrCollections,
|
|
377
|
+
collections
|
|
378
|
+
} : { collections };
|
|
379
|
+
}
|
|
380
|
+
pongoDbSchema.from = (databaseName, collectionNames) => databaseName ? pongoDbSchema(databaseName, pongoCollectionSchema.from(collectionNames)) : pongoDbSchema(pongoCollectionSchema.from(collectionNames));
|
|
381
|
+
const pongoClientSchema = (dbs) => ({ dbs });
|
|
382
|
+
const pongoSchema = {
|
|
383
|
+
client: pongoClientSchema,
|
|
384
|
+
db: pongoDbSchema,
|
|
385
|
+
collection: pongoCollectionSchema
|
|
386
|
+
};
|
|
387
|
+
const proxyClientWithSchema = (client, schema) => {
|
|
388
|
+
if (!schema) return client;
|
|
389
|
+
const dbNames = Object.keys(schema.dbs);
|
|
390
|
+
return new Proxy(client, { get(target, prop) {
|
|
391
|
+
if (dbNames.includes(prop)) return client.db(schema.dbs[prop]?.name);
|
|
392
|
+
return target[prop];
|
|
393
|
+
} });
|
|
394
|
+
};
|
|
395
|
+
const toDbSchemaMetadata = (schema) => ({
|
|
396
|
+
name: schema.name,
|
|
397
|
+
collections: objectEntries(schema.collections).map((c) => ({ name: c[1].name }))
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
//#endregion
|
|
401
|
+
//#region src/core/drivers/databaseDriver.ts
|
|
402
|
+
const PongoDriverRegistry = () => {
|
|
403
|
+
const drivers = /* @__PURE__ */ new Map();
|
|
404
|
+
const register = (driverType, driver) => {
|
|
405
|
+
const entry = drivers.get(driverType);
|
|
406
|
+
if (entry && (typeof entry !== "function" || typeof driver === "function")) return;
|
|
407
|
+
drivers.set(driverType, driver);
|
|
408
|
+
};
|
|
409
|
+
const tryResolve = async (driverType) => {
|
|
410
|
+
const entry = drivers.get(driverType);
|
|
411
|
+
if (!entry) return null;
|
|
412
|
+
if (typeof entry !== "function") return entry;
|
|
413
|
+
const driver = await entry();
|
|
414
|
+
register(driverType, driver);
|
|
415
|
+
return driver;
|
|
416
|
+
};
|
|
417
|
+
const tryGet = (driverType) => {
|
|
418
|
+
const entry = drivers.get(driverType);
|
|
419
|
+
return entry && typeof entry !== "function" ? entry : null;
|
|
420
|
+
};
|
|
421
|
+
const has = (driverType) => drivers.has(driverType);
|
|
422
|
+
return {
|
|
423
|
+
register,
|
|
424
|
+
tryResolve,
|
|
425
|
+
tryGet,
|
|
426
|
+
has,
|
|
427
|
+
get databaseDriverTypes() {
|
|
428
|
+
return Array.from(drivers.keys());
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
};
|
|
432
|
+
const pongoDriverRegistry = globalThis.pongoDriverRegistry = globalThis.pongoDriverRegistry ?? PongoDriverRegistry();
|
|
433
|
+
|
|
434
|
+
//#endregion
|
|
435
|
+
//#region src/core/pongoTransaction.ts
|
|
436
|
+
const pongoTransaction = (options) => {
|
|
437
|
+
let isCommitted = false;
|
|
438
|
+
let isRolledBack = false;
|
|
439
|
+
let databaseName = null;
|
|
440
|
+
let transaction = null;
|
|
441
|
+
const cache = pongoTransactionCache();
|
|
442
|
+
return {
|
|
443
|
+
cache,
|
|
444
|
+
enlistDatabase: async (db) => {
|
|
445
|
+
if (transaction && databaseName !== db.databaseName) throw new Error("There's already other database assigned to transaction");
|
|
446
|
+
if (transaction && databaseName === db.databaseName) return transaction;
|
|
447
|
+
databaseName = db.databaseName;
|
|
448
|
+
transaction = db.transaction();
|
|
449
|
+
await transaction.begin();
|
|
450
|
+
return transaction;
|
|
451
|
+
},
|
|
452
|
+
commit: async () => {
|
|
453
|
+
if (isCommitted) return;
|
|
454
|
+
if (isRolledBack) throw new Error("Transaction is not active!");
|
|
455
|
+
isCommitted = true;
|
|
456
|
+
if (transaction) {
|
|
457
|
+
await transaction.commit();
|
|
458
|
+
transaction = null;
|
|
459
|
+
}
|
|
460
|
+
await cache.commit();
|
|
461
|
+
},
|
|
462
|
+
rollback: async (error) => {
|
|
463
|
+
if (isCommitted) throw new Error("Cannot rollback commited transaction!");
|
|
464
|
+
if (isRolledBack) return;
|
|
465
|
+
isRolledBack = true;
|
|
466
|
+
if (transaction) {
|
|
467
|
+
await transaction.rollback(error);
|
|
468
|
+
transaction = null;
|
|
469
|
+
}
|
|
470
|
+
cache.clear();
|
|
471
|
+
},
|
|
472
|
+
databaseName,
|
|
473
|
+
isStarting: false,
|
|
474
|
+
isCommitted,
|
|
475
|
+
get isActive() {
|
|
476
|
+
return !isCommitted && !isRolledBack;
|
|
477
|
+
},
|
|
478
|
+
get sqlExecutor() {
|
|
479
|
+
if (transaction === null) throw new Error("No database transaction was started");
|
|
480
|
+
return transaction.execute;
|
|
481
|
+
},
|
|
482
|
+
options
|
|
483
|
+
};
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
//#endregion
|
|
487
|
+
//#region src/core/pongoSession.ts
|
|
488
|
+
const isActive = (transaction) => transaction?.isActive === true;
|
|
489
|
+
function assertInActiveTransaction(transaction) {
|
|
490
|
+
if (!isActive(transaction)) throw new Error("No active transaction exists!");
|
|
491
|
+
}
|
|
492
|
+
function assertNotInActiveTransaction(transaction) {
|
|
493
|
+
if (isActive(transaction)) throw new Error("Active transaction already exists!");
|
|
494
|
+
}
|
|
495
|
+
const pongoSession = (options) => {
|
|
496
|
+
const explicit = options?.explicit === true;
|
|
497
|
+
const defaultTransactionOptions = options?.defaultTransactionOptions ?? { get snapshotEnabled() {
|
|
498
|
+
return false;
|
|
499
|
+
} };
|
|
500
|
+
let transaction = null;
|
|
501
|
+
let hasEnded = false;
|
|
502
|
+
const startTransaction = (options) => {
|
|
503
|
+
assertNotInActiveTransaction(transaction);
|
|
504
|
+
transaction = pongoTransaction(options ?? defaultTransactionOptions);
|
|
505
|
+
};
|
|
506
|
+
const commitTransaction = async () => {
|
|
507
|
+
assertInActiveTransaction(transaction);
|
|
508
|
+
await transaction.commit();
|
|
509
|
+
};
|
|
510
|
+
const abortTransaction = async () => {
|
|
511
|
+
assertInActiveTransaction(transaction);
|
|
512
|
+
await transaction.rollback();
|
|
513
|
+
};
|
|
514
|
+
const endSession = async () => {
|
|
515
|
+
if (hasEnded) return;
|
|
516
|
+
hasEnded = true;
|
|
517
|
+
if (isActive(transaction)) await transaction.rollback();
|
|
518
|
+
};
|
|
519
|
+
const session = {
|
|
520
|
+
get hasEnded() {
|
|
521
|
+
return hasEnded;
|
|
522
|
+
},
|
|
523
|
+
explicit,
|
|
524
|
+
defaultTransactionOptions: defaultTransactionOptions ?? { get snapshotEnabled() {
|
|
525
|
+
return false;
|
|
526
|
+
} },
|
|
527
|
+
get transaction() {
|
|
528
|
+
return transaction;
|
|
529
|
+
},
|
|
530
|
+
get snapshotEnabled() {
|
|
531
|
+
return defaultTransactionOptions.snapshotEnabled;
|
|
532
|
+
},
|
|
533
|
+
endSession,
|
|
534
|
+
incrementTransactionNumber: () => {},
|
|
535
|
+
inTransaction: () => isActive(transaction),
|
|
536
|
+
startTransaction,
|
|
537
|
+
commitTransaction,
|
|
538
|
+
abortTransaction,
|
|
539
|
+
withTransaction: async (fn, options) => {
|
|
540
|
+
startTransaction(options);
|
|
541
|
+
try {
|
|
542
|
+
const result = await fn(session);
|
|
543
|
+
await commitTransaction();
|
|
544
|
+
return result;
|
|
545
|
+
} catch (error) {
|
|
546
|
+
await abortTransaction();
|
|
547
|
+
throw error;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
return session;
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
//#endregion
|
|
555
|
+
//#region src/core/pongoClient.ts
|
|
556
|
+
const pongoClient = (options) => {
|
|
557
|
+
const { driver, schema, errors, cache: cacheOptions, serialization, ...connectionOptions } = options;
|
|
558
|
+
const dbClients = PongoDatabaseCache({
|
|
559
|
+
driver,
|
|
560
|
+
typedSchema: schema?.definition
|
|
561
|
+
});
|
|
562
|
+
const serializer = _event_driven_io_dumbo.JSONSerializer.from(options);
|
|
563
|
+
const cache = cacheOptions === "disabled" || cacheOptions === void 0 ? "disabled" : pongoCache(cacheOptions);
|
|
564
|
+
const pongoClient = {
|
|
565
|
+
driverType: driver.driverType,
|
|
566
|
+
connect: async () => {
|
|
567
|
+
await dbClients.forAll((db) => db.connect());
|
|
568
|
+
return pongoClient;
|
|
569
|
+
},
|
|
570
|
+
close: async () => {
|
|
571
|
+
await dbClients.forAll((db) => db.close());
|
|
572
|
+
},
|
|
573
|
+
db: (dbName, options) => {
|
|
574
|
+
return dbClients.getOrCreate({
|
|
575
|
+
...connectionOptions,
|
|
576
|
+
databaseName: dbName,
|
|
577
|
+
serializer,
|
|
578
|
+
errors,
|
|
579
|
+
cache: options?.cache ?? cache,
|
|
580
|
+
serialization
|
|
581
|
+
});
|
|
582
|
+
},
|
|
583
|
+
startSession: pongoSession,
|
|
584
|
+
withSession: async (callback) => {
|
|
585
|
+
const session = pongoSession();
|
|
586
|
+
try {
|
|
587
|
+
return await callback(session);
|
|
588
|
+
} finally {
|
|
589
|
+
await session.endSession();
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
return proxyClientWithSchema(pongoClient, schema?.definition);
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
//#endregion
|
|
41
597
|
//#region src/commandLine/configFile.ts
|
|
42
598
|
const formatTypeName = (input) => {
|
|
43
599
|
if (input.length === 0) return input;
|
|
@@ -91,11 +647,11 @@ const parseDefaultDbSchema = (imported) => {
|
|
|
91
647
|
if (!imported.default) return missingDefaultExport;
|
|
92
648
|
if (!imported.default.schema) return missingSchema;
|
|
93
649
|
if (!imported.default.schema.dbs) return missingDbs;
|
|
94
|
-
const defaultDb =
|
|
650
|
+
const defaultDb = objectEntries(imported.default.schema.dbs).map((db) => db[1]).find((db) => db.name === void 0);
|
|
95
651
|
if (!defaultDb) return missingDefaultDb;
|
|
96
652
|
if (!defaultDb.collections) return missingCollections;
|
|
97
|
-
if (
|
|
98
|
-
return
|
|
653
|
+
if (objectEntries(defaultDb.collections).map((col) => col[1]).length === 0) return missingCollections;
|
|
654
|
+
return toDbSchemaMetadata(defaultDb);
|
|
99
655
|
};
|
|
100
656
|
const configCommand = new commander.Command("config").description("Manage Pongo configuration");
|
|
101
657
|
configCommand.command("sample").description("Generate or print sample configuration").option("--col, --collection <name>", "Specify the collection name", (value, previous) => {
|
|
@@ -172,13 +728,13 @@ migrateCommand.command("sql").description("Generate SQL for database migration")
|
|
|
172
728
|
console.log((0, _event_driven_io_dumbo.combineMigrations)(...migrations));
|
|
173
729
|
});
|
|
174
730
|
const getMigrations = ({ driverType, connectionString, databaseName, collectionNames }) => {
|
|
175
|
-
const driver =
|
|
731
|
+
const driver = pongoDriverRegistry.tryGet(driverType);
|
|
176
732
|
if (driver === null) {
|
|
177
733
|
console.error(`Error: No database driver found for driver type "${driverType}". Make sure the driver is registered and the connection string is correct.`);
|
|
178
734
|
process.exit(1);
|
|
179
735
|
}
|
|
180
736
|
const driverOptions = {
|
|
181
|
-
schema: { definition:
|
|
737
|
+
schema: { definition: pongoSchema.db.from(databaseName, collectionNames) },
|
|
182
738
|
serializer: _event_driven_io_dumbo.JSONSerializer
|
|
183
739
|
};
|
|
184
740
|
const customOptions = {
|
|
@@ -228,7 +784,7 @@ const prettifyLogs = (logLevel) => {
|
|
|
228
784
|
const startRepl = async (options) => {
|
|
229
785
|
setLogLevel(process.env.DUMBO_LOG_LEVEL ?? options.logging.logLevel);
|
|
230
786
|
setLogStyle(process.env.DUMBO_LOG_STYLE ?? options.logging.logStyle);
|
|
231
|
-
console.log(_event_driven_io_dumbo.color.green("Starting Pongo Shell (version: 0.17.0-beta.
|
|
787
|
+
console.log(_event_driven_io_dumbo.color.green("Starting Pongo Shell (version: 0.17.0-beta.44)"));
|
|
232
788
|
if (options.logging.printOptions) {
|
|
233
789
|
console.log(_event_driven_io_dumbo.color.green("With Options:"));
|
|
234
790
|
console.log((0, _event_driven_io_dumbo.prettyJson)(options));
|
|
@@ -237,7 +793,7 @@ const startRepl = async (options) => {
|
|
|
237
793
|
if (!(options.connectionString ?? process.env.DB_CONNECTION_STRING)) console.log(_event_driven_io_dumbo.color.yellow(`No connection string provided, using: 'postgresql://postgres:postgres@localhost:5432/postgres'`));
|
|
238
794
|
const { databaseType } = (0, _event_driven_io_dumbo.parseConnectionString)(connectionString);
|
|
239
795
|
const driverType = `${databaseType}:${options.databaseDriver}`;
|
|
240
|
-
const driver =
|
|
796
|
+
const driver = pongoDriverRegistry.tryGet(driverType);
|
|
241
797
|
if (driver === null) {
|
|
242
798
|
console.error(`Error: No database driver found for driver type "${driverType}". Make sure the driver is installed and the connection string is correct.`);
|
|
243
799
|
process.exit(1);
|
|
@@ -261,16 +817,16 @@ const startRepl = async (options) => {
|
|
|
261
817
|
let db;
|
|
262
818
|
if (options.schema.collections.length > 0) {
|
|
263
819
|
const collectionsSchema = {};
|
|
264
|
-
for (const collectionName of options.schema.collections) collectionsSchema[collectionName] =
|
|
820
|
+
for (const collectionName of options.schema.collections) collectionsSchema[collectionName] = pongoSchema.collection(collectionName);
|
|
265
821
|
const driverOptions = {
|
|
266
822
|
driver,
|
|
267
823
|
schema: {
|
|
268
|
-
definition:
|
|
824
|
+
definition: pongoSchema.client({ database: pongoSchema.db(options.schema.database, collectionsSchema) }),
|
|
269
825
|
autoMigration: options.schema.autoMigration
|
|
270
826
|
}
|
|
271
827
|
};
|
|
272
828
|
const customOptions = { connectionString };
|
|
273
|
-
const typedClient =
|
|
829
|
+
const typedClient = pongoClient({
|
|
274
830
|
...driverOptions,
|
|
275
831
|
...customOptions
|
|
276
832
|
});
|
|
@@ -283,7 +839,7 @@ const startRepl = async (options) => {
|
|
|
283
839
|
schema: { autoMigration: options.schema.autoMigration }
|
|
284
840
|
};
|
|
285
841
|
const customOptions = { connectionString };
|
|
286
|
-
pongo =
|
|
842
|
+
pongo = pongoClient({
|
|
287
843
|
...driverOptions,
|
|
288
844
|
...customOptions
|
|
289
845
|
});
|
|
@@ -344,6 +900,5 @@ program.addCommand(shellCommand);
|
|
|
344
900
|
program.parse(process.argv);
|
|
345
901
|
|
|
346
902
|
//#endregion
|
|
347
|
-
exports
|
|
348
|
-
exports.default = program;
|
|
903
|
+
module.exports = program;
|
|
349
904
|
//# sourceMappingURL=cli.cjs.map
|