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