@event-driven-io/pongo 0.17.0-alpha.6 → 0.17.0-beta.10

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.
Files changed (54) hide show
  1. package/dist/{chunk-OO7GMTMP.js → chunk-RBJRJFQY.js} +573 -876
  2. package/dist/chunk-RBJRJFQY.js.map +1 -0
  3. package/dist/chunk-TFE4LVVL.cjs +330 -0
  4. package/dist/chunk-TFE4LVVL.cjs.map +1 -0
  5. package/dist/chunk-TP73JUIX.cjs +872 -0
  6. package/dist/chunk-TP73JUIX.cjs.map +1 -0
  7. package/dist/chunk-XOVARYG4.js +330 -0
  8. package/dist/chunk-XOVARYG4.js.map +1 -0
  9. package/dist/cli.cjs +110 -35
  10. package/dist/cli.cjs.map +1 -1
  11. package/dist/cli.js +109 -34
  12. package/dist/cli.js.map +1 -1
  13. package/dist/cloudflare.cjs +53 -0
  14. package/dist/cloudflare.cjs.map +1 -0
  15. package/dist/cloudflare.d.cts +11 -0
  16. package/dist/cloudflare.d.ts +11 -0
  17. package/dist/cloudflare.js +53 -0
  18. package/dist/cloudflare.js.map +1 -0
  19. package/dist/index-BJopB-em.d.cts +7 -0
  20. package/dist/index-G5DECNb_.d.ts +7 -0
  21. package/dist/index.cjs +2 -6
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.cts +58 -18
  24. package/dist/index.d.ts +58 -18
  25. package/dist/index.js +13 -17
  26. package/dist/pg.cjs +347 -3
  27. package/dist/pg.cjs.map +1 -1
  28. package/dist/pg.d.cts +36 -6
  29. package/dist/pg.d.ts +36 -6
  30. package/dist/pg.js +354 -10
  31. package/dist/pg.js.map +1 -1
  32. package/dist/pongoCollectionSchemaComponent-t_e9n2Wc.d.cts +426 -0
  33. package/dist/pongoCollectionSchemaComponent-t_e9n2Wc.d.ts +426 -0
  34. package/dist/shim.cjs +41 -7
  35. package/dist/shim.cjs.map +1 -1
  36. package/dist/shim.d.cts +12 -9
  37. package/dist/shim.d.ts +12 -9
  38. package/dist/shim.js +39 -5
  39. package/dist/shim.js.map +1 -1
  40. package/dist/sqlite3.cjs +65 -1
  41. package/dist/sqlite3.cjs.map +1 -1
  42. package/dist/sqlite3.d.cts +13 -1
  43. package/dist/sqlite3.d.ts +13 -1
  44. package/dist/sqlite3.js +64 -0
  45. package/dist/sqlite3.js.map +1 -1
  46. package/package.json +53 -13
  47. package/README.md +0 -230
  48. package/dist/chunk-AV4SHJQB.cjs +0 -1175
  49. package/dist/chunk-AV4SHJQB.cjs.map +0 -1
  50. package/dist/chunk-OO7GMTMP.js.map +0 -1
  51. package/dist/pg-BfTNWLV9.d.ts +0 -39
  52. package/dist/pg-C9NmCQe7.d.cts +0 -39
  53. package/dist/pongoClient-D8jPedlZ.d.cts +0 -364
  54. package/dist/pongoClient-D8jPedlZ.d.ts +0 -364
@@ -1,157 +1,597 @@
1
- // src/storage/postgresql/sqlBuilder/index.ts
2
- import {
3
- isSQL,
4
- JSONSerializer as JSONSerializer4,
5
- rawSql,
6
- sql as sql3,
7
- sqlMigration
8
- } from "@event-driven-io/dumbo";
9
-
10
1
  // src/core/collection/pongoCollection.ts
11
2
  import {
12
- schemaComponent as schemaComponent2,
3
+ mapColumnToJSON,
4
+ runSQLMigrations as runSQLMigrations2,
13
5
  single
14
6
  } from "@event-driven-io/dumbo";
15
- import { runPostgreSQLMigrations as runPostgreSQLMigrations2 } from "@event-driven-io/dumbo/pg";
16
- import { v7 as uuid } from "uuid";
7
+ import { v7 as uuid2 } from "uuid";
8
+
9
+ // src/core/collection/pongoCollectionSchema.ts
10
+ import "@event-driven-io/dumbo";
17
11
 
18
- // src/storage/postgresql/dbClient.ts
12
+ // src/core/collection/pongoCollectionSchemaComponent.ts
19
13
  import {
20
14
  schemaComponent
21
15
  } from "@event-driven-io/dumbo";
16
+ var PongoCollectionSchemaComponent = ({
17
+ definition,
18
+ migrationsOrSchemaComponents,
19
+ sqlBuilder
20
+ }) => ({
21
+ ...schemaComponent(
22
+ `sc:pongo:collection:${definition.name}`,
23
+ migrationsOrSchemaComponents
24
+ ),
25
+ sqlBuilder,
26
+ definition,
27
+ collectionName: definition.name
28
+ });
29
+
30
+ // src/core/collection/query.ts
31
+ var QueryOperators = {
32
+ $eq: "$eq",
33
+ $gt: "$gt",
34
+ $gte: "$gte",
35
+ $lt: "$lt",
36
+ $lte: "$lte",
37
+ $ne: "$ne",
38
+ $in: "$in",
39
+ $nin: "$nin",
40
+ $elemMatch: "$elemMatch",
41
+ $all: "$all",
42
+ $size: "$size"
43
+ };
44
+ var OperatorMap = {
45
+ $gt: ">",
46
+ $gte: ">=",
47
+ $lt: "<",
48
+ $lte: "<=",
49
+ $ne: "!="
50
+ };
51
+ var isOperator = (key) => key.startsWith("$");
52
+ var hasOperators = (value) => Object.keys(value).some(isOperator);
53
+
54
+ // src/core/database/pongoDatabaseCache.ts
55
+ var PongoDatabaseCache = ({
56
+ driver,
57
+ typedSchema
58
+ }) => {
59
+ const dbClients = /* @__PURE__ */ new Map();
60
+ const getDatabaseDefinition = (dbName) => Object.values(typedSchema?.dbs ?? {}).find((d) => d.name === dbName);
61
+ return {
62
+ getOrCreate: (createOptions) => {
63
+ const dbName = createOptions.databaseName ?? (driver.getDatabaseNameOrDefault ? driver.getDatabaseNameOrDefault(createOptions) : "db:default");
64
+ const existing = dbClients.get(dbName);
65
+ if (existing) return existing;
66
+ const definition = getDatabaseDefinition(createOptions.databaseName);
67
+ const newDb = driver.databaseFactory({
68
+ ...createOptions,
69
+ databaseName: dbName,
70
+ schema: {
71
+ ...createOptions.schema,
72
+ ...definition ? { definition } : {}
73
+ }
74
+ });
75
+ dbClients.set(dbName, newDb);
76
+ return newDb;
77
+ },
78
+ all: () => Array.from(dbClients.values()),
79
+ forAll: (func) => {
80
+ return Promise.all(
81
+ Array.from(dbClients.values()).map((v) => v).map(func)
82
+ );
83
+ }
84
+ };
85
+ };
86
+
87
+ // src/core/database/pongoDatabaseSchemaComponent.ts
88
+ import {
89
+ schemaComponent as schemaComponent2
90
+ } from "@event-driven-io/dumbo";
91
+
92
+ // src/core/typing/entries.ts
93
+ var objectEntries = (obj) => Object.entries(obj).map(([key, value]) => [key, value]);
94
+
95
+ // src/core/typing/operations.ts
22
96
  import {
23
- dumbo,
24
- getDatabaseNameOrDefault,
25
- NodePostgresConnectorType,
26
- runPostgreSQLMigrations
27
- } from "@event-driven-io/dumbo/pg";
28
- var isPostgresClientOptions = (options) => options.connector === NodePostgresConnectorType;
29
- var postgresDb = (options) => {
30
- const { connectionString, dbName } = options;
31
- const databaseName = dbName ?? getDatabaseNameOrDefault(connectionString);
32
- const pool = dumbo({
33
- connectionString,
34
- ...options.connectionOptions
35
- });
97
+ JSONSerializer
98
+ } from "@event-driven-io/dumbo";
99
+ import { v7 as uuid } from "uuid";
100
+
101
+ // src/core/errors/index.ts
102
+ var isNumber = (val) => typeof val === "number" && val === val;
103
+ var isString = (val) => typeof val === "string";
104
+ var PongoError = class _PongoError extends Error {
105
+ errorCode;
106
+ constructor(options) {
107
+ const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : 500;
108
+ const message = options && typeof options === "object" && "message" in options ? options.message : isString(options) ? options : `Error with status code '${errorCode}' ocurred during Pongo processing`;
109
+ super(message);
110
+ this.errorCode = errorCode;
111
+ Object.setPrototypeOf(this, _PongoError.prototype);
112
+ }
113
+ };
114
+ var ConcurrencyError = class _ConcurrencyError extends PongoError {
115
+ constructor(message) {
116
+ super({
117
+ errorCode: 412,
118
+ message: message ?? `Expected document state does not match current one!`
119
+ });
120
+ Object.setPrototypeOf(this, _ConcurrencyError.prototype);
121
+ }
122
+ };
123
+
124
+ // src/core/typing/operations.ts
125
+ var ObjectId = (value) => value ?? uuid();
126
+ var DOCUMENT_EXISTS = "DOCUMENT_EXISTS";
127
+ var DOCUMENT_DOES_NOT_EXIST = "DOCUMENT_DOES_NOT_EXIST";
128
+ var NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
129
+ var isGeneralExpectedDocumentVersion = (version) => version === "DOCUMENT_DOES_NOT_EXIST" || version === "DOCUMENT_EXISTS" || version === "NO_CONCURRENCY_CHECK";
130
+ var expectedVersionValue = (version) => version === void 0 || isGeneralExpectedDocumentVersion(version) ? null : version;
131
+ var expectedVersion = (version) => {
132
+ return version ? BigInt(version) : NO_CONCURRENCY_CHECK;
133
+ };
134
+ var operationResult = (result, options) => {
135
+ const operationResult2 = {
136
+ ...result,
137
+ acknowledged: true,
138
+ successful: result.successful,
139
+ assertSuccessful: (errorMessage) => {
140
+ const { successful } = result;
141
+ const { operationName, collectionName } = options;
142
+ if (!successful)
143
+ throw new ConcurrencyError(
144
+ errorMessage ?? `${operationName} on ${collectionName} failed. Expected document state does not match current one! Result: ${JSONSerializer.serialize(result)}!`
145
+ );
146
+ }
147
+ };
148
+ if (options.errors?.throwOnOperationFailures)
149
+ operationResult2.assertSuccessful();
150
+ return operationResult2;
151
+ };
152
+
153
+ // src/core/schema/index.ts
154
+ var pongoCollectionSchema = (name) => ({
155
+ name
156
+ });
157
+ pongoCollectionSchema.from = (collectionNames) => collectionNames.reduce(
158
+ (acc, collectionName) => (acc[collectionName] = pongoSchema.collection(collectionName), acc),
159
+ {}
160
+ );
161
+ function pongoDbSchema(nameOrCollections, collections) {
162
+ if (collections === void 0) {
163
+ if (typeof nameOrCollections === "string") {
164
+ throw new Error("You need to provide colleciton definition");
165
+ }
166
+ return {
167
+ collections: nameOrCollections
168
+ };
169
+ }
170
+ return nameOrCollections && typeof nameOrCollections === "string" ? {
171
+ name: nameOrCollections,
172
+ collections
173
+ } : { collections };
174
+ }
175
+ pongoDbSchema.from = (databaseName, collectionNames) => databaseName ? pongoDbSchema(databaseName, pongoCollectionSchema.from(collectionNames)) : pongoDbSchema(pongoCollectionSchema.from(collectionNames));
176
+ var pongoClientSchema = (dbs) => ({
177
+ dbs
178
+ });
179
+ var pongoSchema = {
180
+ client: pongoClientSchema,
181
+ db: pongoDbSchema,
182
+ collection: pongoCollectionSchema
183
+ };
184
+ var proxyPongoDbWithSchema = (pongoDb, dbSchema, collections) => {
185
+ const collectionNames = Object.keys(dbSchema.collections);
186
+ for (const collectionName of collectionNames) {
187
+ collections.set(collectionName, pongoDb.collection(collectionName));
188
+ }
189
+ return new Proxy(
190
+ pongoDb,
191
+ {
192
+ get(target, prop) {
193
+ return collections.get(prop) ?? target[prop];
194
+ }
195
+ }
196
+ );
197
+ };
198
+ var proxyClientWithSchema = (client, schema) => {
199
+ if (!schema)
200
+ return client;
201
+ const dbNames = Object.keys(schema.dbs);
202
+ return new Proxy(
203
+ client,
204
+ {
205
+ get(target, prop) {
206
+ if (dbNames.includes(prop)) return client.db(schema.dbs[prop]?.name);
207
+ return target[prop];
208
+ }
209
+ }
210
+ );
211
+ };
212
+ var toDbSchemaMetadata = (schema) => ({
213
+ name: schema.name,
214
+ collections: objectEntries(schema.collections).map((c) => ({
215
+ name: c[1].name
216
+ }))
217
+ });
218
+ var toClientSchemaMetadata = (schema) => {
219
+ const databases = objectEntries(schema.dbs).map(
220
+ (e) => toDbSchemaMetadata(e[1])
221
+ );
222
+ return {
223
+ databases,
224
+ database: (name) => databases.find((db) => db.name === name)
225
+ };
226
+ };
227
+
228
+ // src/core/database/pongoDatabaseSchemaComponent.ts
229
+ var PongoDatabaseSchemaComponent = ({
230
+ definition,
231
+ collectionFactory
232
+ }) => {
233
+ const collections = Object.values(definition.collections).map(collectionFactory) ?? [];
234
+ return {
235
+ ...schemaComponent2(`sc:dumbo:database:${definition.name}`, {
236
+ components: collections
237
+ }),
238
+ definition,
239
+ collections,
240
+ collection: (schema) => {
241
+ const existing = collections.find(
242
+ (c) => c.collectionName === schema.name
243
+ );
244
+ if (existing) return existing;
245
+ const newCollection = collectionFactory(
246
+ pongoSchema.collection(schema.name)
247
+ );
248
+ collections.push(newCollection);
249
+ definition.collections[schema.name] = schema;
250
+ return newCollection;
251
+ }
252
+ };
253
+ };
254
+
255
+ // src/core/database/pongoDb.ts
256
+ import {
257
+ runSQLMigrations
258
+ } from "@event-driven-io/dumbo";
259
+ var PongoDatabase = (options) => {
260
+ const { databaseName, schemaComponent: schemaComponent3, pool } = options;
36
261
  const collections = /* @__PURE__ */ new Map();
37
- const command = async (sql4, options2) => (await transactionExecutorOrDefault(db, options2, pool.execute)).command(sql4);
38
- const query = async (sql4, options2) => (await transactionExecutorOrDefault(db, options2, pool.execute)).query(
39
- sql4
262
+ const command = async (sql, options2) => (await transactionExecutorOrDefault(db, options2, pool.execute)).command(sql);
263
+ const query = async (sql, options2) => (await transactionExecutorOrDefault(db, options2, pool.execute)).query(
264
+ sql
40
265
  );
266
+ const driverType = pool.driverType;
41
267
  const db = {
42
- connector: options.connector,
268
+ driverType,
43
269
  databaseName,
44
270
  connect: () => Promise.resolve(),
45
271
  close: () => pool.close(),
46
272
  collections: () => [...collections.values()],
47
- collection: (collectionName) => pongoCollection({
273
+ collection: (collectionName) => collections.get(collectionName) ?? pongoCollection({
48
274
  collectionName,
49
275
  db,
50
276
  pool,
51
- sqlBuilder: postgresSQLBuilder(collectionName),
277
+ schemaComponent: schemaComponent3.collection(
278
+ pongoSchema.collection(collectionName)
279
+ ),
52
280
  schema: options.schema ? options.schema : {},
53
281
  errors: options.errors ? options.errors : {}
54
282
  }),
55
283
  transaction: () => pool.transaction(),
56
284
  withTransaction: (handle) => pool.withTransaction(handle),
57
285
  schema: {
58
- get component() {
59
- return schemaComponent("pongoDb", {
60
- components: [...collections.values()].map((c) => c.schema.component)
61
- });
62
- },
63
- migrate: () => runPostgreSQLMigrations(
64
- pool,
65
- [...collections.values()].flatMap(
66
- (c) => (
67
- // TODO: This needs to change to support more connectors
68
- c.schema.component.migrations({ connector: "PostgreSQL:pg" })
69
- )
70
- )
71
- )
286
+ component: schemaComponent3,
287
+ migrate: () => runSQLMigrations(pool, schemaComponent3.migrations)
72
288
  },
73
289
  sql: {
74
- async query(sql4, options2) {
75
- const result = await query(sql4, options2);
290
+ async query(sql, options2) {
291
+ const result = await query(sql, options2);
76
292
  return result.rows;
77
293
  },
78
- async command(sql4, options2) {
79
- return command(sql4, options2);
294
+ async command(sql, options2) {
295
+ return command(sql, options2);
80
296
  }
81
297
  }
82
298
  };
83
- const dbsSchema = options?.schema?.definition?.dbs;
84
- if (dbsSchema) {
85
- const dbSchema = objectEntries(dbsSchema).map((e) => e[1]).find((db2) => db2.name === dbName || db2.name === databaseName);
86
- if (dbSchema) return proxyPongoDbWithSchema(db, dbSchema, collections);
299
+ const dbSchema = options?.schema?.definition;
300
+ if (dbSchema) {
301
+ return proxyPongoDbWithSchema(db, dbSchema, collections);
87
302
  }
88
303
  return db;
89
304
  };
90
- var pongoDbSchemaComponent = (collections) => {
91
- const components = collections.length > 0 && typeof collections[0] === "string" ? collections.map(
92
- (collectionName) => pongoCollectionSchemaComponent(collectionName)
93
- ) : collections;
94
- return schemaComponent("pongo:schema_component:db", {
95
- components
96
- });
97
- };
98
305
 
99
- // src/core/collection/pongoCollection.ts
100
- var enlistIntoTransactionIfActive = async (db, options) => {
101
- const transaction = options?.session?.transaction;
102
- if (!transaction || !transaction.isActive) return null;
103
- return await transaction.enlistDatabase(db);
104
- };
105
- var transactionExecutorOrDefault = async (db, options, defaultSqlExecutor) => {
106
- const existingTransaction = await enlistIntoTransactionIfActive(db, options);
107
- return existingTransaction?.execute ?? defaultSqlExecutor;
108
- };
109
- var pongoCollection = ({
110
- db,
111
- collectionName,
112
- pool,
113
- sqlBuilder: SqlFor,
114
- schema,
115
- errors
116
- }) => {
117
- const sqlExecutor = pool.execute;
118
- const command = async (sql4, options) => (await transactionExecutorOrDefault(db, options, sqlExecutor)).command(sql4);
119
- const query = async (sql4, options) => (await transactionExecutorOrDefault(db, options, sqlExecutor)).query(
120
- sql4
121
- );
122
- let shouldMigrate = schema?.autoMigration !== "None";
123
- const createCollection2 = (options) => {
124
- shouldMigrate = false;
125
- if (options?.session) return command(SqlFor.createCollection(), options);
126
- else return command(SqlFor.createCollection());
306
+ // src/core/drivers/databaseDriver.ts
307
+ var PongoDatabaseDriverRegistry = () => {
308
+ const drivers = /* @__PURE__ */ new Map();
309
+ const register = (driverType, driver) => {
310
+ const entry = drivers.get(driverType);
311
+ if (entry && (typeof entry !== "function" || typeof driver === "function")) {
312
+ return;
313
+ }
314
+ drivers.set(driverType, driver);
127
315
  };
128
- const ensureCollectionCreated = (options) => {
129
- if (!shouldMigrate) {
130
- return Promise.resolve();
316
+ const tryResolve = async (driverType) => {
317
+ const entry = drivers.get(driverType);
318
+ if (!entry) return null;
319
+ if (typeof entry !== "function") return entry;
320
+ const driver = await entry();
321
+ register(driverType, driver);
322
+ return driver;
323
+ };
324
+ const tryGet = (driverType) => {
325
+ const entry = drivers.get(driverType);
326
+ return entry && typeof entry !== "function" ? entry : null;
327
+ };
328
+ const has = (driverType) => drivers.has(driverType);
329
+ return {
330
+ register,
331
+ tryResolve,
332
+ tryGet,
333
+ has,
334
+ get databaseDriverTypes() {
335
+ return Array.from(drivers.keys());
131
336
  }
132
- return createCollection2(options);
133
337
  };
134
- const collection = {
135
- dbName: db.databaseName,
136
- collectionName,
137
- createCollection: async (options) => {
138
- await createCollection2(options);
338
+ };
339
+ var pongoDatabaseDriverRegistry = globalThis.pongoDatabaseDriverRegistry = globalThis.pongoDatabaseDriverRegistry ?? PongoDatabaseDriverRegistry();
340
+
341
+ // src/core/pongoTransaction.ts
342
+ var pongoTransaction = (options) => {
343
+ let isCommitted = false;
344
+ let isRolledBack = false;
345
+ let databaseName = null;
346
+ let transaction = null;
347
+ return {
348
+ enlistDatabase: async (db) => {
349
+ if (transaction && databaseName !== db.databaseName)
350
+ throw new Error(
351
+ "There's already other database assigned to transaction"
352
+ );
353
+ if (transaction && databaseName === db.databaseName) return transaction;
354
+ databaseName = db.databaseName;
355
+ transaction = db.transaction();
356
+ await transaction.begin();
357
+ return transaction;
139
358
  },
140
- insertOne: async (document, options) => {
141
- await ensureCollectionCreated(options);
142
- const _id = document._id ?? uuid();
143
- const _version = document._version ?? 1n;
144
- const result = await command(
145
- SqlFor.insertOne({
146
- ...document,
147
- _id,
148
- _version
149
- }),
150
- options
151
- );
152
- const successful = (result.rowCount ?? 0) > 0;
153
- return operationResult(
154
- {
359
+ commit: async () => {
360
+ if (!transaction) throw new Error("No database transaction started!");
361
+ if (isCommitted) return;
362
+ if (isRolledBack) throw new Error("Transaction is not active!");
363
+ isCommitted = true;
364
+ await transaction.commit();
365
+ transaction = null;
366
+ },
367
+ rollback: async (error) => {
368
+ if (!transaction) throw new Error("No database transaction started!");
369
+ if (isCommitted) throw new Error("Cannot rollback commited transaction!");
370
+ if (isRolledBack) return;
371
+ isRolledBack = true;
372
+ await transaction.rollback(error);
373
+ transaction = null;
374
+ },
375
+ databaseName,
376
+ isStarting: false,
377
+ isCommitted,
378
+ get isActive() {
379
+ return !isCommitted && !isRolledBack;
380
+ },
381
+ get sqlExecutor() {
382
+ if (transaction === null)
383
+ throw new Error("No database transaction was started");
384
+ return transaction.execute;
385
+ },
386
+ options
387
+ };
388
+ };
389
+
390
+ // src/core/pongoSession.ts
391
+ var isActive = (transaction) => transaction?.isActive === true;
392
+ function assertInActiveTransaction(transaction) {
393
+ if (!isActive(transaction)) throw new Error("No active transaction exists!");
394
+ }
395
+ function assertNotInActiveTransaction(transaction) {
396
+ if (isActive(transaction))
397
+ throw new Error("Active transaction already exists!");
398
+ }
399
+ var pongoSession = (options) => {
400
+ const explicit = options?.explicit === true;
401
+ const defaultTransactionOptions = options?.defaultTransactionOptions ?? {
402
+ get snapshotEnabled() {
403
+ return false;
404
+ }
405
+ };
406
+ let transaction = null;
407
+ let hasEnded = false;
408
+ const startTransaction = (options2) => {
409
+ assertNotInActiveTransaction(transaction);
410
+ transaction = pongoTransaction(options2 ?? defaultTransactionOptions);
411
+ };
412
+ const commitTransaction = async () => {
413
+ assertInActiveTransaction(transaction);
414
+ await transaction.commit();
415
+ };
416
+ const abortTransaction = async () => {
417
+ assertInActiveTransaction(transaction);
418
+ await transaction.rollback();
419
+ };
420
+ const endSession = async () => {
421
+ if (hasEnded) return;
422
+ hasEnded = true;
423
+ if (isActive(transaction)) await transaction.rollback();
424
+ };
425
+ const session = {
426
+ get hasEnded() {
427
+ return hasEnded;
428
+ },
429
+ explicit,
430
+ defaultTransactionOptions: defaultTransactionOptions ?? {
431
+ get snapshotEnabled() {
432
+ return false;
433
+ }
434
+ },
435
+ get transaction() {
436
+ return transaction;
437
+ },
438
+ get snapshotEnabled() {
439
+ return defaultTransactionOptions.snapshotEnabled;
440
+ },
441
+ endSession,
442
+ incrementTransactionNumber: () => {
443
+ },
444
+ inTransaction: () => isActive(transaction),
445
+ startTransaction,
446
+ commitTransaction,
447
+ abortTransaction,
448
+ withTransaction: async (fn, options2) => {
449
+ startTransaction(options2);
450
+ try {
451
+ const result = await fn(session);
452
+ await commitTransaction();
453
+ return result;
454
+ } catch (error) {
455
+ await abortTransaction();
456
+ throw error;
457
+ }
458
+ }
459
+ };
460
+ return session;
461
+ };
462
+
463
+ // src/core/pongoClient.ts
464
+ var pongoClient = (options) => {
465
+ const { driver, schema, errors, ...connectionOptions } = options;
466
+ const dbClients = PongoDatabaseCache({
467
+ driver,
468
+ typedSchema: schema?.definition
469
+ });
470
+ const pongoClient2 = {
471
+ driverType: driver.driverType,
472
+ connect: async () => {
473
+ await dbClients.forAll((db) => db.connect());
474
+ return pongoClient2;
475
+ },
476
+ close: async () => {
477
+ await dbClients.forAll((db) => db.close());
478
+ },
479
+ db: (dbName) => {
480
+ const db = dbClients.getOrCreate({
481
+ ...connectionOptions,
482
+ databaseName: dbName,
483
+ errors
484
+ });
485
+ return db;
486
+ },
487
+ startSession: pongoSession,
488
+ withSession: async (callback) => {
489
+ const session = pongoSession();
490
+ try {
491
+ return await callback(session);
492
+ } finally {
493
+ await session.endSession();
494
+ }
495
+ }
496
+ };
497
+ return proxyClientWithSchema(pongoClient2, schema?.definition);
498
+ };
499
+
500
+ // src/core/utils/deepEquals.ts
501
+ var deepEquals = (left, right) => {
502
+ if (isEquatable(left)) {
503
+ return left.equals(right);
504
+ }
505
+ if (Array.isArray(left)) {
506
+ return Array.isArray(right) && left.length === right.length && left.every((val, index) => deepEquals(val, right[index]));
507
+ }
508
+ if (typeof left !== "object" || typeof right !== "object" || left === null || right === null) {
509
+ return left === right;
510
+ }
511
+ if (Array.isArray(right)) return false;
512
+ const keys1 = Object.keys(left);
513
+ const keys2 = Object.keys(right);
514
+ if (keys1.length !== keys2.length || !keys1.every((key) => keys2.includes(key)))
515
+ return false;
516
+ for (const key in left) {
517
+ if (left[key] instanceof Function && right[key] instanceof Function)
518
+ continue;
519
+ const isEqual = deepEquals(left[key], right[key]);
520
+ if (!isEqual) {
521
+ return false;
522
+ }
523
+ }
524
+ return true;
525
+ };
526
+ var isEquatable = (left) => {
527
+ return left && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
528
+ };
529
+
530
+ // src/core/collection/pongoCollection.ts
531
+ var enlistIntoTransactionIfActive = async (db, options) => {
532
+ const transaction = options?.session?.transaction;
533
+ if (!transaction || !transaction.isActive) return null;
534
+ return await transaction.enlistDatabase(db);
535
+ };
536
+ var transactionExecutorOrDefault = async (db, options, defaultSqlExecutor) => {
537
+ const existingTransaction = await enlistIntoTransactionIfActive(db, options);
538
+ return existingTransaction?.execute ?? defaultSqlExecutor;
539
+ };
540
+ var columnMapping = {
541
+ mapping: {
542
+ ...mapColumnToJSON("data")
543
+ //...mapColumnToJSON('metadata'),
544
+ //...mapColumnToBigint('_version'),
545
+ }
546
+ };
547
+ var pongoCollection = ({
548
+ db,
549
+ collectionName,
550
+ pool,
551
+ schemaComponent: schemaComponent3,
552
+ schema,
553
+ errors
554
+ }) => {
555
+ const SqlFor = schemaComponent3.sqlBuilder;
556
+ const sqlExecutor = pool.execute;
557
+ const command = async (sql, options) => (await transactionExecutorOrDefault(db, options, sqlExecutor)).command(sql);
558
+ const query = async (sql, options) => (await transactionExecutorOrDefault(db, options, sqlExecutor)).query(
559
+ sql,
560
+ columnMapping
561
+ );
562
+ let shouldMigrate = schema?.autoMigration !== "None";
563
+ const createCollection = (options) => {
564
+ shouldMigrate = false;
565
+ if (options?.session) return command(SqlFor.createCollection(), options);
566
+ else return command(SqlFor.createCollection());
567
+ };
568
+ const ensureCollectionCreated = (options) => {
569
+ if (!shouldMigrate) {
570
+ return Promise.resolve();
571
+ }
572
+ return createCollection(options);
573
+ };
574
+ const collection = {
575
+ dbName: db.databaseName,
576
+ collectionName,
577
+ createCollection: async (options) => {
578
+ await createCollection(options);
579
+ },
580
+ insertOne: async (document, options) => {
581
+ await ensureCollectionCreated(options);
582
+ const _id = document._id ?? uuid2();
583
+ const _version = document._version ?? 1n;
584
+ const result = await command(
585
+ SqlFor.insertOne({
586
+ ...document,
587
+ _id,
588
+ _version
589
+ }),
590
+ options
591
+ );
592
+ const successful = (result.rowCount ?? 0) > 0;
593
+ return operationResult(
594
+ {
155
595
  successful,
156
596
  insertedId: successful ? _id : null,
157
597
  nextExpectedVersion: _version
@@ -163,7 +603,7 @@ var pongoCollection = ({
163
603
  await ensureCollectionCreated(options);
164
604
  const rows = documents.map((doc) => ({
165
605
  ...doc,
166
- _id: doc._id ?? uuid(),
606
+ _id: doc._id ?? uuid2(),
167
607
  _version: doc._version ?? 1n
168
608
  }));
169
609
  const result = await command(
@@ -190,7 +630,7 @@ var pongoCollection = ({
190
630
  successful: result.rows.length > 0 && result.rows[0].modified === result.rows[0].matched,
191
631
  modifiedCount: Number(result.rows[0]?.modified ?? 0),
192
632
  matchedCount: Number(result.rows[0]?.matched ?? 0),
193
- nextExpectedVersion: result.rows[0]?.version ?? 0n
633
+ nextExpectedVersion: BigInt(result.rows[0]?.version ?? 0n)
194
634
  },
195
635
  { operationName: "updateOne", collectionName, errors }
196
636
  );
@@ -206,7 +646,7 @@ var pongoCollection = ({
206
646
  successful: result.rows.length > 0 && result.rows[0].modified > 0,
207
647
  modifiedCount: Number(result.rows[0]?.modified ?? 0),
208
648
  matchedCount: Number(result.rows[0]?.matched ?? 0),
209
- nextExpectedVersion: result.rows[0]?.version ?? 0n
649
+ nextExpectedVersion: BigInt(result.rows[0]?.version ?? 0n)
210
650
  },
211
651
  { operationName: "replaceOne", collectionName, errors }
212
652
  );
@@ -374,786 +814,38 @@ var pongoCollection = ({
374
814
  return collection;
375
815
  },
376
816
  sql: {
377
- async query(sql4, options) {
817
+ async query(sql, options) {
378
818
  await ensureCollectionCreated(options);
379
- const result = await query(sql4, options);
819
+ const result = await query(sql, options);
380
820
  return result.rows;
381
821
  },
382
- async command(sql4, options) {
822
+ async command(sql, options) {
383
823
  await ensureCollectionCreated(options);
384
- return command(sql4, options);
824
+ return command(sql, options);
385
825
  }
386
826
  },
387
827
  schema: {
388
- get component() {
389
- return schemaComponent2("pongo:schema_component:collection", {
390
- migrations: SqlFor.migrations
391
- });
392
- },
393
- migrate: () => runPostgreSQLMigrations2(pool, SqlFor.migrations())
394
- // TODO: This needs to change to support more connectors
828
+ component: schemaComponent3,
829
+ migrate: () => runSQLMigrations2(pool, schemaComponent3.migrations)
395
830
  }
396
831
  };
397
832
  return collection;
398
833
  };
399
- var pongoCollectionSchemaComponent = (collectionName) => schemaComponent2("pongo:schema_component:collection", {
400
- migrations: () => pongoCollectionPostgreSQLMigrations(collectionName)
401
- // TODO: This needs to change to support more connectors
402
- });
403
-
404
- // src/core/collection/query.ts
405
- var QueryOperators = {
406
- $eq: "$eq",
407
- $gt: "$gt",
408
- $gte: "$gte",
409
- $lt: "$lt",
410
- $lte: "$lte",
411
- $ne: "$ne",
412
- $in: "$in",
413
- $nin: "$nin",
414
- $elemMatch: "$elemMatch",
415
- $all: "$all",
416
- $size: "$size"
417
- };
418
- var OperatorMap = {
419
- $gt: ">",
420
- $gte: ">=",
421
- $lt: "<",
422
- $lte: "<=",
423
- $ne: "!="
424
- };
425
- var isOperator = (key) => key.startsWith("$");
426
- var hasOperators = (value) => Object.keys(value).some(isOperator);
427
-
428
- // src/core/errors/index.ts
429
- var isNumber = (val) => typeof val === "number" && val === val;
430
- var isString = (val) => typeof val === "string";
431
- var PongoError = class _PongoError extends Error {
432
- errorCode;
433
- constructor(options) {
434
- const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : 500;
435
- const message = options && typeof options === "object" && "message" in options ? options.message : isString(options) ? options : `Error with status code '${errorCode}' ocurred during Pongo processing`;
436
- super(message);
437
- this.errorCode = errorCode;
438
- Object.setPrototypeOf(this, _PongoError.prototype);
439
- }
440
- };
441
- var ConcurrencyError = class _ConcurrencyError extends PongoError {
442
- constructor(message) {
443
- super({
444
- errorCode: 412,
445
- message: message ?? `Expected document state does not match current one!`
446
- });
447
- Object.setPrototypeOf(this, _ConcurrencyError.prototype);
448
- }
449
- };
450
-
451
- // src/core/pongoClient.ts
452
- import "@event-driven-io/dumbo";
453
- import {
454
- NodePostgresConnectorType as NodePostgresConnectorType2
455
- } from "@event-driven-io/dumbo/pg";
456
- import "pg";
457
-
458
- // src/core/pongoDb.ts
459
- var getPongoDb = (options) => {
460
- const { connector } = options;
461
- if (!isPostgresClientOptions(options))
462
- throw new Error(`Unsupported db type: ${connector}`);
463
- return postgresDb(options);
464
- };
465
-
466
- // src/core/pongoTransaction.ts
467
- var pongoTransaction = (options) => {
468
- let isCommitted = false;
469
- let isRolledBack = false;
470
- let databaseName = null;
471
- let transaction = null;
472
- return {
473
- enlistDatabase: async (db) => {
474
- if (transaction && databaseName !== db.databaseName)
475
- throw new Error(
476
- "There's already other database assigned to transaction"
477
- );
478
- if (transaction && databaseName === db.databaseName) return transaction;
479
- databaseName = db.databaseName;
480
- transaction = db.transaction();
481
- await transaction.begin();
482
- return transaction;
483
- },
484
- commit: async () => {
485
- if (!transaction) throw new Error("No database transaction started!");
486
- if (isCommitted) return;
487
- if (isRolledBack) throw new Error("Transaction is not active!");
488
- isCommitted = true;
489
- await transaction.commit();
490
- transaction = null;
491
- },
492
- rollback: async (error) => {
493
- if (!transaction) throw new Error("No database transaction started!");
494
- if (isCommitted) throw new Error("Cannot rollback commited transaction!");
495
- if (isRolledBack) return;
496
- isRolledBack = true;
497
- await transaction.rollback(error);
498
- transaction = null;
499
- },
500
- databaseName,
501
- isStarting: false,
502
- isCommitted,
503
- get isActive() {
504
- return !isCommitted && !isRolledBack;
505
- },
506
- get sqlExecutor() {
507
- if (transaction === null)
508
- throw new Error("No database transaction was started");
509
- return transaction.execute;
510
- },
511
- options
512
- };
513
- };
514
-
515
- // src/core/pongoSession.ts
516
- var isActive = (transaction) => transaction?.isActive === true;
517
- function assertInActiveTransaction(transaction) {
518
- if (!isActive(transaction)) throw new Error("No active transaction exists!");
519
- }
520
- function assertNotInActiveTransaction(transaction) {
521
- if (isActive(transaction))
522
- throw new Error("Active transaction already exists!");
523
- }
524
- var pongoSession = (options) => {
525
- const explicit = options?.explicit === true;
526
- const defaultTransactionOptions = options?.defaultTransactionOptions ?? {
527
- get snapshotEnabled() {
528
- return false;
529
- }
530
- };
531
- let transaction = null;
532
- let hasEnded = false;
533
- const startTransaction = (options2) => {
534
- assertNotInActiveTransaction(transaction);
535
- transaction = pongoTransaction(options2 ?? defaultTransactionOptions);
536
- };
537
- const commitTransaction = async () => {
538
- assertInActiveTransaction(transaction);
539
- await transaction.commit();
540
- };
541
- const abortTransaction = async () => {
542
- assertInActiveTransaction(transaction);
543
- await transaction.rollback();
544
- };
545
- const endSession = async () => {
546
- if (hasEnded) return;
547
- hasEnded = true;
548
- if (isActive(transaction)) await transaction.rollback();
549
- };
550
- const session = {
551
- get hasEnded() {
552
- return hasEnded;
553
- },
554
- explicit,
555
- defaultTransactionOptions: defaultTransactionOptions ?? {
556
- get snapshotEnabled() {
557
- return false;
558
- }
559
- },
560
- get transaction() {
561
- return transaction;
562
- },
563
- get snapshotEnabled() {
564
- return defaultTransactionOptions.snapshotEnabled;
565
- },
566
- endSession,
567
- incrementTransactionNumber: () => {
568
- },
569
- inTransaction: () => isActive(transaction),
570
- startTransaction,
571
- commitTransaction,
572
- abortTransaction,
573
- withTransaction: async (fn, options2) => {
574
- startTransaction(options2);
575
- try {
576
- const result = await fn(session);
577
- await commitTransaction();
578
- return result;
579
- } catch (error) {
580
- await abortTransaction();
581
- throw error;
582
- }
583
- }
584
- };
585
- return session;
586
- };
587
-
588
- // src/core/typing/entries.ts
589
- var objectEntries = (obj) => Object.entries(obj).map(([key, value]) => [key, value]);
590
-
591
- // src/core/typing/operations.ts
592
- import {
593
- JSONSerializer
594
- } from "@event-driven-io/dumbo";
595
- import { v7 as uuid2 } from "uuid";
596
- var ObjectId = (value) => value ?? uuid2();
597
- var DOCUMENT_EXISTS = "DOCUMENT_EXISTS";
598
- var DOCUMENT_DOES_NOT_EXIST = "DOCUMENT_DOES_NOT_EXIST";
599
- var NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
600
- var isGeneralExpectedDocumentVersion = (version) => version === "DOCUMENT_DOES_NOT_EXIST" || version === "DOCUMENT_EXISTS" || version === "NO_CONCURRENCY_CHECK";
601
- var expectedVersionValue = (version) => version === void 0 || isGeneralExpectedDocumentVersion(version) ? null : version;
602
- var expectedVersion = (version) => {
603
- return version ? BigInt(version) : NO_CONCURRENCY_CHECK;
604
- };
605
- var operationResult = (result, options) => {
606
- const operationResult2 = {
607
- ...result,
608
- acknowledged: true,
609
- successful: result.successful,
610
- assertSuccessful: (errorMessage) => {
611
- const { successful } = result;
612
- const { operationName, collectionName } = options;
613
- if (!successful)
614
- throw new ConcurrencyError(
615
- errorMessage ?? `${operationName} on ${collectionName} failed. Expected document state does not match current one! Result: ${JSONSerializer.serialize(result)}!`
616
- );
617
- }
618
- };
619
- if (options.errors?.throwOnOperationFailures)
620
- operationResult2.assertSuccessful();
621
- return operationResult2;
622
- };
623
-
624
- // src/core/schema/index.ts
625
- var pongoCollectionSchema = (name) => ({
626
- name
627
- });
628
- function pongoDbSchema(nameOrCollections, collections) {
629
- if (collections === void 0) {
630
- if (typeof nameOrCollections === "string") {
631
- throw new Error("You need to provide colleciton definition");
632
- }
633
- return {
634
- collections: nameOrCollections
635
- };
636
- }
637
- return nameOrCollections && typeof nameOrCollections === "string" ? {
638
- name: nameOrCollections,
639
- collections
640
- } : { collections };
641
- }
642
- var pongoClientSchema = (dbs) => ({
643
- dbs
644
- });
645
- var pongoSchema = {
646
- client: pongoClientSchema,
647
- db: pongoDbSchema,
648
- collection: pongoCollectionSchema
649
- };
650
- var proxyPongoDbWithSchema = (pongoDb, dbSchema, collections) => {
651
- const collectionNames = Object.keys(dbSchema.collections);
652
- for (const collectionName of collectionNames) {
653
- collections.set(collectionName, pongoDb.collection(collectionName));
654
- }
655
- return new Proxy(
656
- pongoDb,
657
- {
658
- get(target, prop) {
659
- return collections.get(prop) ?? target[prop];
660
- }
661
- }
662
- );
663
- };
664
- var proxyClientWithSchema = (client, schema) => {
665
- if (!schema) return client;
666
- const dbNames = Object.keys(schema.dbs);
667
- return new Proxy(
668
- client,
669
- {
670
- get(target, prop) {
671
- if (dbNames.includes(prop)) return client.db(schema.dbs[prop]?.name);
672
- return target[prop];
673
- }
674
- }
675
- );
676
- };
677
- var toDbSchemaMetadata = (schema) => ({
678
- name: schema.name,
679
- collections: objectEntries(schema.collections).map((c) => ({
680
- name: c[1].name
681
- }))
682
- });
683
- var toClientSchemaMetadata = (schema) => {
684
- const databases = objectEntries(schema.dbs).map(
685
- (e) => toDbSchemaMetadata(e[1])
686
- );
687
- return {
688
- databases,
689
- database: (name) => databases.find((db) => db.name === name)
690
- };
691
- };
692
-
693
- // src/core/pongoClient.ts
694
- var pongoClient = (connectionString, options = {}) => {
695
- const dbClients = /* @__PURE__ */ new Map();
696
- const dbClient = getPongoDb(
697
- clientToDbOptions({
698
- connectionString,
699
- clientOptions: options
700
- })
701
- );
702
- dbClients.set(dbClient.databaseName, dbClient);
703
- const pongoClient2 = {
704
- connect: async () => {
705
- await dbClient.connect();
706
- return pongoClient2;
707
- },
708
- close: async () => {
709
- for (const db of dbClients.values()) {
710
- await db.close();
711
- }
712
- },
713
- db: (dbName) => {
714
- if (!dbName) return dbClient;
715
- return dbClients.get(dbName) ?? dbClients.set(
716
- dbName,
717
- getPongoDb(
718
- clientToDbOptions({
719
- connectionString,
720
- dbName,
721
- clientOptions: options
722
- })
723
- )
724
- ).get(dbName);
725
- },
726
- startSession: pongoSession,
727
- withSession: async (callback) => {
728
- const session = pongoSession();
729
- try {
730
- return await callback(session);
731
- } finally {
732
- await session.endSession();
733
- }
734
- }
735
- };
736
- return proxyClientWithSchema(pongoClient2, options?.schema?.definition);
737
- };
738
- var clientToDbOptions = (options) => {
739
- const postgreSQLOptions = {
740
- connector: NodePostgresConnectorType2,
741
- connectionString: options.connectionString,
742
- dbName: options.dbName,
743
- ...options.clientOptions
744
- };
745
- return postgreSQLOptions;
746
- };
747
-
748
- // src/core/utils/deepEquals.ts
749
- var deepEquals = (left, right) => {
750
- if (isEquatable(left)) {
751
- return left.equals(right);
752
- }
753
- if (Array.isArray(left)) {
754
- return Array.isArray(right) && left.length === right.length && left.every((val, index) => deepEquals(val, right[index]));
755
- }
756
- if (typeof left !== "object" || typeof right !== "object" || left === null || right === null) {
757
- return left === right;
758
- }
759
- if (Array.isArray(right)) return false;
760
- const keys1 = Object.keys(left);
761
- const keys2 = Object.keys(right);
762
- if (keys1.length !== keys2.length || !keys1.every((key) => keys2.includes(key)))
763
- return false;
764
- for (const key in left) {
765
- if (left[key] instanceof Function && right[key] instanceof Function)
766
- continue;
767
- const isEqual = deepEquals(left[key], right[key]);
768
- if (!isEqual) {
769
- return false;
770
- }
771
- }
772
- return true;
773
- };
774
- var isEquatable = (left) => {
775
- return left && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
776
- };
777
-
778
- // src/storage/postgresql/sqlBuilder/filter/queryOperators.ts
779
- import { JSONSerializer as JSONSerializer2, sql } from "@event-driven-io/dumbo";
780
- var handleOperator = (path, operator, value) => {
781
- if (path === "_id" || path === "_version") {
782
- return handleMetadataOperator(path, operator, value);
783
- }
784
- switch (operator) {
785
- case "$eq":
786
- return sql(
787
- `(data @> %L::jsonb OR jsonb_path_exists(data, '$.%s[*] ? (@ == %s)'))`,
788
- JSONSerializer2.serialize(buildNestedObject(path, value)),
789
- path,
790
- JSONSerializer2.serialize(value)
791
- );
792
- case "$gt":
793
- case "$gte":
794
- case "$lt":
795
- case "$lte":
796
- case "$ne":
797
- return sql(
798
- `data #>> %L ${OperatorMap[operator]} %L`,
799
- `{${path.split(".").join(",")}}`,
800
- value
801
- );
802
- case "$in":
803
- return sql(
804
- "data #>> %L IN (%s)",
805
- `{${path.split(".").join(",")}}`,
806
- value.map((v) => sql("%L", v)).join(", ")
807
- );
808
- case "$nin":
809
- return sql(
810
- "data #>> %L NOT IN (%s)",
811
- `{${path.split(".").join(",")}}`,
812
- value.map((v) => sql("%L", v)).join(", ")
813
- );
814
- case "$elemMatch": {
815
- const subQuery = objectEntries(value).map(
816
- ([subKey, subValue]) => sql(`@."%s" == %s`, subKey, JSONSerializer2.serialize(subValue))
817
- ).join(" && ");
818
- return sql(`jsonb_path_exists(data, '$.%s[*] ? (%s)')`, path, subQuery);
819
- }
820
- case "$all":
821
- return sql(
822
- "data @> %L::jsonb",
823
- JSONSerializer2.serialize(buildNestedObject(path, value))
824
- );
825
- case "$size":
826
- return sql(
827
- "jsonb_array_length(data #> %L) = %L",
828
- `{${path.split(".").join(",")}}`,
829
- value
830
- );
831
- default:
832
- throw new Error(`Unsupported operator: ${operator}`);
833
- }
834
- };
835
- var handleMetadataOperator = (fieldName, operator, value) => {
836
- switch (operator) {
837
- case "$eq":
838
- return sql(`${fieldName} = %L`, value);
839
- case "$gt":
840
- case "$gte":
841
- case "$lt":
842
- case "$lte":
843
- case "$ne":
844
- return sql(`${fieldName} ${OperatorMap[operator]} %L`, value);
845
- case "$in":
846
- return sql(
847
- `${fieldName} IN (%s)`,
848
- value.map((v) => sql("%L", v)).join(", ")
849
- );
850
- case "$nin":
851
- return sql(
852
- `${fieldName} NOT IN (%s)`,
853
- value.map((v) => sql("%L", v)).join(", ")
854
- );
855
- default:
856
- throw new Error(`Unsupported operator: ${operator}`);
857
- }
858
- };
859
- var buildNestedObject = (path, value) => path.split(".").reverse().reduce((acc, key) => ({ [key]: acc }), value);
860
-
861
- // src/storage/postgresql/sqlBuilder/filter/index.ts
862
- var AND = "AND";
863
- var constructFilterQuery = (filter) => Object.entries(filter).map(
864
- ([key, value]) => isRecord(value) ? constructComplexFilterQuery(key, value) : handleOperator(key, "$eq", value)
865
- ).join(` ${AND} `);
866
- var constructComplexFilterQuery = (key, value) => {
867
- const isEquality = !hasOperators(value);
868
- return objectEntries(value).map(
869
- ([nestedKey, val]) => isEquality ? handleOperator(`${key}.${nestedKey}`, QueryOperators.$eq, val) : handleOperator(key, nestedKey, val)
870
- // operator
871
- ).join(` ${AND} `);
872
- };
873
- var isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
874
-
875
- // src/storage/postgresql/sqlBuilder/update/index.ts
876
- import { JSONSerializer as JSONSerializer3, sql as sql2 } from "@event-driven-io/dumbo";
877
- var buildUpdateQuery = (update) => objectEntries(update).reduce((currentUpdateQuery, [op, value]) => {
878
- switch (op) {
879
- case "$set":
880
- return buildSetQuery(value, currentUpdateQuery);
881
- case "$unset":
882
- return buildUnsetQuery(value, currentUpdateQuery);
883
- case "$inc":
884
- return buildIncQuery(value, currentUpdateQuery);
885
- case "$push":
886
- return buildPushQuery(value, currentUpdateQuery);
887
- default:
888
- return currentUpdateQuery;
889
- }
890
- }, sql2("data"));
891
- var buildSetQuery = (set, currentUpdateQuery) => sql2("%s || %L::jsonb", currentUpdateQuery, JSONSerializer3.serialize(set));
892
- var buildUnsetQuery = (unset, currentUpdateQuery) => sql2(
893
- "%s - %L",
894
- currentUpdateQuery,
895
- Object.keys(unset).map((k) => `{${k}}`).join(", ")
896
- );
897
- var buildIncQuery = (inc, currentUpdateQuery) => {
898
- for (const [key, value] of Object.entries(inc)) {
899
- currentUpdateQuery = sql2(
900
- typeof value === "bigint" ? "jsonb_set(%s, '{%s}', to_jsonb((COALESCE((data->>'%s')::BIGINT, 0) + %L)::TEXT), true)" : "jsonb_set(%s, '{%s}', to_jsonb(COALESCE((data->>'%s')::NUMERIC, 0) + %L), true)",
901
- currentUpdateQuery,
902
- key,
903
- key,
904
- value
905
- );
906
- }
907
- return currentUpdateQuery;
908
- };
909
- var buildPushQuery = (push, currentUpdateQuery) => {
910
- for (const [key, value] of Object.entries(push)) {
911
- currentUpdateQuery = sql2(
912
- "jsonb_set(%s, '{%s}', (coalesce(data->'%s', '[]'::jsonb) || %L::jsonb), true)",
913
- currentUpdateQuery,
914
- key,
915
- key,
916
- JSONSerializer3.serialize([value])
917
- );
918
- }
919
- return currentUpdateQuery;
920
- };
921
-
922
- // src/storage/postgresql/sqlBuilder/index.ts
923
- var createCollection = (collectionName) => sql3(
924
- `CREATE TABLE IF NOT EXISTS %I (
925
- _id TEXT PRIMARY KEY,
926
- data JSONB NOT NULL,
927
- metadata JSONB NOT NULL DEFAULT '{}',
928
- _version BIGINT NOT NULL DEFAULT 1,
929
- _partition TEXT NOT NULL DEFAULT 'png_global',
930
- _archived BOOLEAN NOT NULL DEFAULT FALSE,
931
- _created TIMESTAMPTZ NOT NULL DEFAULT now(),
932
- _updated TIMESTAMPTZ NOT NULL DEFAULT now()
933
- )`,
934
- collectionName
935
- );
936
- var pongoCollectionPostgreSQLMigrations = (collectionName) => [
937
- sqlMigration(`pongoCollection:${collectionName}:001:createtable`, [
938
- createCollection(collectionName)
939
- ])
940
- ];
941
- var postgresSQLBuilder = (collectionName) => ({
942
- migrations: () => pongoCollectionPostgreSQLMigrations(collectionName),
943
- createCollection: () => createCollection(collectionName),
944
- insertOne: (document) => {
945
- return sql3(
946
- "INSERT INTO %I (_id, data, _version) VALUES (%L, %L, %L) ON CONFLICT(_id) DO NOTHING;",
947
- collectionName,
948
- document._id,
949
- JSONSerializer4.serialize(document),
950
- document._version ?? 1n
951
- );
952
- },
953
- insertMany: (documents) => {
954
- const values = documents.map(
955
- (doc) => sql3(
956
- "(%L, %L, %L)",
957
- doc._id,
958
- JSONSerializer4.serialize(doc),
959
- doc._version ?? 1n
960
- )
961
- ).join(", ");
962
- return sql3(
963
- `INSERT INTO %I (_id, data, _version) VALUES %s
964
- ON CONFLICT(_id) DO NOTHING
965
- RETURNING _id;`,
966
- collectionName,
967
- values
968
- );
969
- },
970
- updateOne: (filter, update, options) => {
971
- const expectedVersion2 = expectedVersionValue(options?.expectedVersion);
972
- const expectedVersionUpdate = expectedVersion2 != null ? "AND %I._version = %L" : "";
973
- const expectedVersionParams = expectedVersion2 != null ? [collectionName, expectedVersion2] : [];
974
- const filterQuery = isSQL(filter) ? filter : constructFilterQuery(filter);
975
- const updateQuery = isSQL(update) ? update : buildUpdateQuery(update);
976
- return sql3(
977
- `WITH existing AS (
978
- SELECT _id, _version as current_version
979
- FROM %I %s
980
- LIMIT 1
981
- ),
982
- updated AS (
983
- UPDATE %I
984
- SET
985
- data = %s || jsonb_build_object('_id', %I._id) || jsonb_build_object('_version', (_version + 1)::text),
986
- _version = _version + 1
987
- FROM existing
988
- WHERE %I._id = existing._id ${expectedVersionUpdate}
989
- RETURNING %I._id, %I._version
990
- )
991
- SELECT
992
- existing._id,
993
- COALESCE(updated._version, existing.current_version) AS version,
994
- COUNT(existing._id) over() AS matched,
995
- COUNT(updated._id) over() AS modified
996
- FROM existing
997
- LEFT JOIN updated
998
- ON existing._id = updated._id;`,
999
- collectionName,
1000
- where(filterQuery),
1001
- collectionName,
1002
- updateQuery,
1003
- collectionName,
1004
- collectionName,
1005
- ...expectedVersionParams,
1006
- collectionName,
1007
- collectionName
1008
- );
1009
- },
1010
- replaceOne: (filter, document, options) => {
1011
- const expectedVersion2 = expectedVersionValue(options?.expectedVersion);
1012
- const expectedVersionUpdate = expectedVersion2 != null ? "AND %I._version = %L" : "";
1013
- const expectedVersionParams = expectedVersion2 != null ? [collectionName, expectedVersion2] : [];
1014
- const filterQuery = isSQL(filter) ? filter : constructFilterQuery(filter);
1015
- return sql3(
1016
- `WITH existing AS (
1017
- SELECT _id, _version as current_version
1018
- FROM %I %s
1019
- LIMIT 1
1020
- ),
1021
- updated AS (
1022
- UPDATE %I
1023
- SET
1024
- data = %L || jsonb_build_object('_id', %I._id) || jsonb_build_object('_version', (_version + 1)::text),
1025
- _version = _version + 1
1026
- FROM existing
1027
- WHERE %I._id = existing._id ${expectedVersionUpdate}
1028
- RETURNING %I._id, %I._version
1029
- )
1030
- SELECT
1031
- existing._id,
1032
- COALESCE(updated._version, existing.current_version) AS version,
1033
- COUNT(existing._id) over() AS matched,
1034
- COUNT(updated._id) over() AS modified
1035
- FROM existing
1036
- LEFT JOIN updated
1037
- ON existing._id = updated._id;`,
1038
- collectionName,
1039
- where(filterQuery),
1040
- collectionName,
1041
- JSONSerializer4.serialize(document),
1042
- collectionName,
1043
- collectionName,
1044
- ...expectedVersionParams,
1045
- collectionName,
1046
- collectionName
1047
- );
1048
- },
1049
- updateMany: (filter, update) => {
1050
- const filterQuery = isSQL(filter) ? filter : constructFilterQuery(filter);
1051
- const updateQuery = isSQL(update) ? update : buildUpdateQuery(update);
1052
- return sql3(
1053
- `UPDATE %I
1054
- SET
1055
- data = %s || jsonb_build_object('_version', (_version + 1)::text),
1056
- _version = _version + 1
1057
- %s;`,
1058
- collectionName,
1059
- updateQuery,
1060
- where(filterQuery)
1061
- );
1062
- },
1063
- deleteOne: (filter, options) => {
1064
- const expectedVersion2 = expectedVersionValue(options?.expectedVersion);
1065
- const expectedVersionUpdate = expectedVersion2 != null ? "AND %I._version = %L" : "";
1066
- const expectedVersionParams = expectedVersion2 != null ? [collectionName, expectedVersion2] : [];
1067
- const filterQuery = isSQL(filter) ? filter : constructFilterQuery(filter);
1068
- return sql3(
1069
- `WITH existing AS (
1070
- SELECT _id
1071
- FROM %I %s
1072
- LIMIT 1
1073
- ),
1074
- deleted AS (
1075
- DELETE FROM %I
1076
- USING existing
1077
- WHERE %I._id = existing._id ${expectedVersionUpdate}
1078
- RETURNING %I._id
1079
- )
1080
- SELECT
1081
- existing._id,
1082
- COUNT(existing._id) over() AS matched,
1083
- COUNT(deleted._id) over() AS deleted
1084
- FROM existing
1085
- LEFT JOIN deleted
1086
- ON existing._id = deleted._id;`,
1087
- collectionName,
1088
- where(filterQuery),
1089
- collectionName,
1090
- collectionName,
1091
- ...expectedVersionParams,
1092
- collectionName
1093
- );
1094
- },
1095
- deleteMany: (filter) => {
1096
- const filterQuery = isSQL(filter) ? filter : constructFilterQuery(filter);
1097
- return sql3("DELETE FROM %I %s", collectionName, where(filterQuery));
1098
- },
1099
- findOne: (filter) => {
1100
- const filterQuery = isSQL(filter) ? filter : constructFilterQuery(filter);
1101
- return sql3(
1102
- "SELECT data FROM %I %s LIMIT 1;",
1103
- collectionName,
1104
- where(filterQuery)
1105
- );
1106
- },
1107
- find: (filter, options) => {
1108
- const filterQuery = isSQL(filter) ? filter : constructFilterQuery(filter);
1109
- const query = [];
1110
- query.push(sql3("SELECT data FROM %I", collectionName));
1111
- const whereStmt = where(filterQuery);
1112
- if (whereStmt.length > 0) {
1113
- query.push(sql3("%s", whereStmt));
1114
- }
1115
- if (options?.limit) {
1116
- query.push(sql3("LIMIT %s", options.limit));
1117
- }
1118
- if (options?.skip) {
1119
- query.push(sql3("OFFSET %s", options.skip));
1120
- }
1121
- return sql3(query.join(" ") + ";");
1122
- },
1123
- countDocuments: (filter) => {
1124
- const filterQuery = isSQL(filter) ? filter : constructFilterQuery(filter);
1125
- return sql3(
1126
- "SELECT COUNT(1) as count FROM %I %s;",
1127
- collectionName,
1128
- where(filterQuery)
1129
- );
1130
- },
1131
- rename: (newName) => sql3("ALTER TABLE %I RENAME TO %I;", collectionName, newName),
1132
- drop: (targetName = collectionName) => sql3("DROP TABLE IF EXISTS %I", targetName)
1133
- });
1134
- var where = (filter) => filter.length > 0 ? sql3("WHERE %s", filter) : rawSql("");
1135
834
 
1136
835
  export {
1137
- pongoCollectionPostgreSQLMigrations,
1138
- postgresSQLBuilder,
1139
- isPostgresClientOptions,
1140
- postgresDb,
1141
- pongoDbSchemaComponent,
1142
836
  transactionExecutorOrDefault,
1143
837
  pongoCollection,
1144
- pongoCollectionSchemaComponent,
838
+ PongoCollectionSchemaComponent,
1145
839
  QueryOperators,
1146
840
  OperatorMap,
1147
841
  isOperator,
1148
842
  hasOperators,
843
+ PongoDatabaseCache,
844
+ objectEntries,
1149
845
  isNumber,
1150
846
  isString,
1151
847
  PongoError,
1152
848
  ConcurrencyError,
1153
- getPongoDb,
1154
- pongoTransaction,
1155
- pongoSession,
1156
- objectEntries,
1157
849
  ObjectId,
1158
850
  DOCUMENT_EXISTS,
1159
851
  DOCUMENT_DOES_NOT_EXIST,
@@ -1167,9 +859,14 @@ export {
1167
859
  proxyClientWithSchema,
1168
860
  toDbSchemaMetadata,
1169
861
  toClientSchemaMetadata,
862
+ PongoDatabaseSchemaComponent,
863
+ PongoDatabase,
864
+ PongoDatabaseDriverRegistry,
865
+ pongoDatabaseDriverRegistry,
866
+ pongoTransaction,
867
+ pongoSession,
1170
868
  pongoClient,
1171
- clientToDbOptions,
1172
869
  deepEquals,
1173
870
  isEquatable
1174
871
  };
1175
- //# sourceMappingURL=chunk-OO7GMTMP.js.map
872
+ //# sourceMappingURL=chunk-RBJRJFQY.js.map