@event-driven-io/pongo 0.17.0-alpha.6 → 0.17.0-beta.1
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/chunk-3KNMMQUV.cjs +362 -0
- package/dist/chunk-3KNMMQUV.cjs.map +1 -0
- package/dist/chunk-5LN762VW.js +362 -0
- package/dist/chunk-5LN762VW.js.map +1 -0
- package/dist/chunk-7W6X4QGY.cjs +10 -0
- package/dist/chunk-7W6X4QGY.cjs.map +1 -0
- package/dist/{chunk-OO7GMTMP.js → chunk-DL4E3N6J.js} +574 -873
- package/dist/chunk-DL4E3N6J.js.map +1 -0
- package/dist/chunk-IBJKZ6TS.js +10 -0
- package/dist/chunk-IBJKZ6TS.js.map +1 -0
- package/dist/chunk-YLV7YIPZ.cjs +876 -0
- package/dist/chunk-YLV7YIPZ.cjs.map +1 -0
- package/dist/cli.cjs +94 -35
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +92 -33
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +3 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +58 -18
- package/dist/index.d.ts +58 -18
- package/dist/index.js +14 -17
- package/dist/pg-WUYRNGST.js +11 -0
- package/dist/pg-WUYRNGST.js.map +1 -0
- package/dist/pg-XCWP4FAM.cjs +11 -0
- package/dist/pg-XCWP4FAM.cjs.map +1 -0
- package/dist/pg.cjs +4 -3
- package/dist/pg.cjs.map +1 -1
- package/dist/pg.d.cts +33 -6
- package/dist/pg.d.ts +33 -6
- package/dist/pg.js +10 -9
- package/dist/pongoCollectionSchemaComponent-BsHlVyN-.d.cts +422 -0
- package/dist/pongoCollectionSchemaComponent-BsHlVyN-.d.ts +422 -0
- package/dist/shim.cjs +38 -8
- package/dist/shim.cjs.map +1 -1
- package/dist/shim.d.cts +10 -8
- package/dist/shim.d.ts +10 -8
- package/dist/shim.js +35 -5
- package/dist/shim.js.map +1 -1
- package/dist/sqlite3.cjs +382 -1
- package/dist/sqlite3.cjs.map +1 -1
- package/dist/sqlite3.d.cts +12 -1
- package/dist/sqlite3.d.ts +12 -1
- package/dist/sqlite3.js +381 -0
- package/dist/sqlite3.js.map +1 -1
- package/package.json +20 -12
- package/README.md +0 -230
- package/dist/chunk-AV4SHJQB.cjs +0 -1175
- package/dist/chunk-AV4SHJQB.cjs.map +0 -1
- package/dist/chunk-OO7GMTMP.js.map +0 -1
- package/dist/pg-BfTNWLV9.d.ts +0 -39
- package/dist/pg-C9NmCQe7.d.cts +0 -39
- package/dist/pongoClient-D8jPedlZ.d.cts +0 -364
- package/dist/pongoClient-D8jPedlZ.d.ts +0 -364
package/dist/chunk-AV4SHJQB.cjs
DELETED
|
@@ -1,1175 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/storage/postgresql/sqlBuilder/index.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var _dumbo = require('@event-driven-io/dumbo');
|
|
9
|
-
|
|
10
|
-
// src/core/collection/pongoCollection.ts
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
var _pg = require('@event-driven-io/dumbo/pg');
|
|
16
|
-
var _uuid = require('uuid');
|
|
17
|
-
|
|
18
|
-
// src/storage/postgresql/dbClient.ts
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var isPostgresClientOptions = (options) => options.connector === _pg.NodePostgresConnectorType;
|
|
29
|
-
var postgresDb = (options) => {
|
|
30
|
-
const { connectionString, dbName } = options;
|
|
31
|
-
const databaseName = _nullishCoalesce(dbName, () => ( _pg.getDatabaseNameOrDefault.call(void 0, connectionString)));
|
|
32
|
-
const pool = _pg.dumbo.call(void 0, {
|
|
33
|
-
connectionString,
|
|
34
|
-
...options.connectionOptions
|
|
35
|
-
});
|
|
36
|
-
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
|
|
40
|
-
);
|
|
41
|
-
const db = {
|
|
42
|
-
connector: options.connector,
|
|
43
|
-
databaseName,
|
|
44
|
-
connect: () => Promise.resolve(),
|
|
45
|
-
close: () => pool.close(),
|
|
46
|
-
collections: () => [...collections.values()],
|
|
47
|
-
collection: (collectionName) => pongoCollection({
|
|
48
|
-
collectionName,
|
|
49
|
-
db,
|
|
50
|
-
pool,
|
|
51
|
-
sqlBuilder: postgresSQLBuilder(collectionName),
|
|
52
|
-
schema: options.schema ? options.schema : {},
|
|
53
|
-
errors: options.errors ? options.errors : {}
|
|
54
|
-
}),
|
|
55
|
-
transaction: () => pool.transaction(),
|
|
56
|
-
withTransaction: (handle) => pool.withTransaction(handle),
|
|
57
|
-
schema: {
|
|
58
|
-
get component() {
|
|
59
|
-
return _dumbo.schemaComponent.call(void 0, "pongoDb", {
|
|
60
|
-
components: [...collections.values()].map((c) => c.schema.component)
|
|
61
|
-
});
|
|
62
|
-
},
|
|
63
|
-
migrate: () => _pg.runPostgreSQLMigrations.call(void 0,
|
|
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
|
-
)
|
|
72
|
-
},
|
|
73
|
-
sql: {
|
|
74
|
-
async query(sql4, options2) {
|
|
75
|
-
const result = await query(sql4, options2);
|
|
76
|
-
return result.rows;
|
|
77
|
-
},
|
|
78
|
-
async command(sql4, options2) {
|
|
79
|
-
return command(sql4, options2);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
const dbsSchema = _optionalChain([options, 'optionalAccess', _ => _.schema, 'optionalAccess', _2 => _2.definition, 'optionalAccess', _3 => _3.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);
|
|
87
|
-
}
|
|
88
|
-
return db;
|
|
89
|
-
};
|
|
90
|
-
var pongoDbSchemaComponent = (collections) => {
|
|
91
|
-
const components = collections.length > 0 && typeof collections[0] === "string" ? collections.map(
|
|
92
|
-
(collectionName) => pongoCollectionSchemaComponent(collectionName)
|
|
93
|
-
) : collections;
|
|
94
|
-
return _dumbo.schemaComponent.call(void 0, "pongo:schema_component:db", {
|
|
95
|
-
components
|
|
96
|
-
});
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
// src/core/collection/pongoCollection.ts
|
|
100
|
-
var enlistIntoTransactionIfActive = async (db, options) => {
|
|
101
|
-
const transaction = _optionalChain([options, 'optionalAccess', _4 => _4.session, 'optionalAccess', _5 => _5.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 _nullishCoalesce(_optionalChain([existingTransaction, 'optionalAccess', _6 => _6.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 = _optionalChain([schema, 'optionalAccess', _7 => _7.autoMigration]) !== "None";
|
|
123
|
-
const createCollection2 = (options) => {
|
|
124
|
-
shouldMigrate = false;
|
|
125
|
-
if (_optionalChain([options, 'optionalAccess', _8 => _8.session])) return command(SqlFor.createCollection(), options);
|
|
126
|
-
else return command(SqlFor.createCollection());
|
|
127
|
-
};
|
|
128
|
-
const ensureCollectionCreated = (options) => {
|
|
129
|
-
if (!shouldMigrate) {
|
|
130
|
-
return Promise.resolve();
|
|
131
|
-
}
|
|
132
|
-
return createCollection2(options);
|
|
133
|
-
};
|
|
134
|
-
const collection = {
|
|
135
|
-
dbName: db.databaseName,
|
|
136
|
-
collectionName,
|
|
137
|
-
createCollection: async (options) => {
|
|
138
|
-
await createCollection2(options);
|
|
139
|
-
},
|
|
140
|
-
insertOne: async (document, options) => {
|
|
141
|
-
await ensureCollectionCreated(options);
|
|
142
|
-
const _id = _nullishCoalesce(document._id, () => ( _uuid.v7.call(void 0, )));
|
|
143
|
-
const _version = _nullishCoalesce(document._version, () => ( 1n));
|
|
144
|
-
const result = await command(
|
|
145
|
-
SqlFor.insertOne({
|
|
146
|
-
...document,
|
|
147
|
-
_id,
|
|
148
|
-
_version
|
|
149
|
-
}),
|
|
150
|
-
options
|
|
151
|
-
);
|
|
152
|
-
const successful = (_nullishCoalesce(result.rowCount, () => ( 0))) > 0;
|
|
153
|
-
return operationResult(
|
|
154
|
-
{
|
|
155
|
-
successful,
|
|
156
|
-
insertedId: successful ? _id : null,
|
|
157
|
-
nextExpectedVersion: _version
|
|
158
|
-
},
|
|
159
|
-
{ operationName: "insertOne", collectionName, errors }
|
|
160
|
-
);
|
|
161
|
-
},
|
|
162
|
-
insertMany: async (documents, options) => {
|
|
163
|
-
await ensureCollectionCreated(options);
|
|
164
|
-
const rows = documents.map((doc) => ({
|
|
165
|
-
...doc,
|
|
166
|
-
_id: _nullishCoalesce(doc._id, () => ( _uuid.v7.call(void 0, ))),
|
|
167
|
-
_version: _nullishCoalesce(doc._version, () => ( 1n))
|
|
168
|
-
}));
|
|
169
|
-
const result = await command(
|
|
170
|
-
SqlFor.insertMany(rows),
|
|
171
|
-
options
|
|
172
|
-
);
|
|
173
|
-
return operationResult(
|
|
174
|
-
{
|
|
175
|
-
successful: result.rowCount === rows.length,
|
|
176
|
-
insertedCount: _nullishCoalesce(result.rowCount, () => ( 0)),
|
|
177
|
-
insertedIds: result.rows.map((d) => d._id)
|
|
178
|
-
},
|
|
179
|
-
{ operationName: "insertMany", collectionName, errors }
|
|
180
|
-
);
|
|
181
|
-
},
|
|
182
|
-
updateOne: async (filter, update, options) => {
|
|
183
|
-
await ensureCollectionCreated(options);
|
|
184
|
-
const result = await command(
|
|
185
|
-
SqlFor.updateOne(filter, update, options),
|
|
186
|
-
options
|
|
187
|
-
);
|
|
188
|
-
return operationResult(
|
|
189
|
-
{
|
|
190
|
-
successful: result.rows.length > 0 && result.rows[0].modified === result.rows[0].matched,
|
|
191
|
-
modifiedCount: Number(_nullishCoalesce(_optionalChain([result, 'access', _9 => _9.rows, 'access', _10 => _10[0], 'optionalAccess', _11 => _11.modified]), () => ( 0))),
|
|
192
|
-
matchedCount: Number(_nullishCoalesce(_optionalChain([result, 'access', _12 => _12.rows, 'access', _13 => _13[0], 'optionalAccess', _14 => _14.matched]), () => ( 0))),
|
|
193
|
-
nextExpectedVersion: _nullishCoalesce(_optionalChain([result, 'access', _15 => _15.rows, 'access', _16 => _16[0], 'optionalAccess', _17 => _17.version]), () => ( 0n))
|
|
194
|
-
},
|
|
195
|
-
{ operationName: "updateOne", collectionName, errors }
|
|
196
|
-
);
|
|
197
|
-
},
|
|
198
|
-
replaceOne: async (filter, document, options) => {
|
|
199
|
-
await ensureCollectionCreated(options);
|
|
200
|
-
const result = await command(
|
|
201
|
-
SqlFor.replaceOne(filter, document, options),
|
|
202
|
-
options
|
|
203
|
-
);
|
|
204
|
-
return operationResult(
|
|
205
|
-
{
|
|
206
|
-
successful: result.rows.length > 0 && result.rows[0].modified > 0,
|
|
207
|
-
modifiedCount: Number(_nullishCoalesce(_optionalChain([result, 'access', _18 => _18.rows, 'access', _19 => _19[0], 'optionalAccess', _20 => _20.modified]), () => ( 0))),
|
|
208
|
-
matchedCount: Number(_nullishCoalesce(_optionalChain([result, 'access', _21 => _21.rows, 'access', _22 => _22[0], 'optionalAccess', _23 => _23.matched]), () => ( 0))),
|
|
209
|
-
nextExpectedVersion: _nullishCoalesce(_optionalChain([result, 'access', _24 => _24.rows, 'access', _25 => _25[0], 'optionalAccess', _26 => _26.version]), () => ( 0n))
|
|
210
|
-
},
|
|
211
|
-
{ operationName: "replaceOne", collectionName, errors }
|
|
212
|
-
);
|
|
213
|
-
},
|
|
214
|
-
updateMany: async (filter, update, options) => {
|
|
215
|
-
await ensureCollectionCreated(options);
|
|
216
|
-
const result = await command(SqlFor.updateMany(filter, update), options);
|
|
217
|
-
return operationResult(
|
|
218
|
-
{
|
|
219
|
-
successful: true,
|
|
220
|
-
modifiedCount: _nullishCoalesce(result.rowCount, () => ( 0)),
|
|
221
|
-
matchedCount: _nullishCoalesce(result.rowCount, () => ( 0))
|
|
222
|
-
},
|
|
223
|
-
{ operationName: "updateMany", collectionName, errors }
|
|
224
|
-
);
|
|
225
|
-
},
|
|
226
|
-
deleteOne: async (filter, options) => {
|
|
227
|
-
await ensureCollectionCreated(options);
|
|
228
|
-
const result = await command(
|
|
229
|
-
SqlFor.deleteOne(_nullishCoalesce(filter, () => ( {})), options),
|
|
230
|
-
options
|
|
231
|
-
);
|
|
232
|
-
return operationResult(
|
|
233
|
-
{
|
|
234
|
-
successful: result.rows.length > 0 && result.rows[0].deleted > 0,
|
|
235
|
-
deletedCount: Number(_nullishCoalesce(_optionalChain([result, 'access', _27 => _27.rows, 'access', _28 => _28[0], 'optionalAccess', _29 => _29.deleted]), () => ( 0))),
|
|
236
|
-
matchedCount: Number(_nullishCoalesce(_optionalChain([result, 'access', _30 => _30.rows, 'access', _31 => _31[0], 'optionalAccess', _32 => _32.matched]), () => ( 0)))
|
|
237
|
-
},
|
|
238
|
-
{ operationName: "deleteOne", collectionName, errors }
|
|
239
|
-
);
|
|
240
|
-
},
|
|
241
|
-
deleteMany: async (filter, options) => {
|
|
242
|
-
await ensureCollectionCreated(options);
|
|
243
|
-
const result = await command(SqlFor.deleteMany(_nullishCoalesce(filter, () => ( {}))), options);
|
|
244
|
-
return operationResult(
|
|
245
|
-
{
|
|
246
|
-
successful: (_nullishCoalesce(result.rowCount, () => ( 0))) > 0,
|
|
247
|
-
deletedCount: _nullishCoalesce(result.rowCount, () => ( 0)),
|
|
248
|
-
matchedCount: _nullishCoalesce(result.rowCount, () => ( 0))
|
|
249
|
-
},
|
|
250
|
-
{ operationName: "deleteMany", collectionName, errors }
|
|
251
|
-
);
|
|
252
|
-
},
|
|
253
|
-
findOne: async (filter, options) => {
|
|
254
|
-
await ensureCollectionCreated(options);
|
|
255
|
-
const result = await query(SqlFor.findOne(_nullishCoalesce(filter, () => ( {}))), options);
|
|
256
|
-
return _nullishCoalesce(_optionalChain([result, 'access', _33 => _33.rows, 'access', _34 => _34[0], 'optionalAccess', _35 => _35.data]), () => ( null));
|
|
257
|
-
},
|
|
258
|
-
findOneAndDelete: async (filter, options) => {
|
|
259
|
-
await ensureCollectionCreated(options);
|
|
260
|
-
const existingDoc = await collection.findOne(filter, options);
|
|
261
|
-
if (existingDoc === null) return null;
|
|
262
|
-
await collection.deleteOne(filter, options);
|
|
263
|
-
return existingDoc;
|
|
264
|
-
},
|
|
265
|
-
findOneAndReplace: async (filter, replacement, options) => {
|
|
266
|
-
await ensureCollectionCreated(options);
|
|
267
|
-
const existingDoc = await collection.findOne(filter, options);
|
|
268
|
-
if (existingDoc === null) return null;
|
|
269
|
-
await collection.replaceOne(filter, replacement, options);
|
|
270
|
-
return existingDoc;
|
|
271
|
-
},
|
|
272
|
-
findOneAndUpdate: async (filter, update, options) => {
|
|
273
|
-
await ensureCollectionCreated(options);
|
|
274
|
-
const existingDoc = await collection.findOne(filter, options);
|
|
275
|
-
if (existingDoc === null) return null;
|
|
276
|
-
await collection.updateOne(filter, update, options);
|
|
277
|
-
return existingDoc;
|
|
278
|
-
},
|
|
279
|
-
handle: async (id, handle, options) => {
|
|
280
|
-
const { expectedVersion: version, ...operationOptions } = _nullishCoalesce(options, () => ( {}));
|
|
281
|
-
await ensureCollectionCreated(options);
|
|
282
|
-
const byId = { _id: id };
|
|
283
|
-
const existing = await collection.findOne(
|
|
284
|
-
byId,
|
|
285
|
-
options
|
|
286
|
-
);
|
|
287
|
-
const expectedVersion2 = expectedVersionValue(version);
|
|
288
|
-
if (existing == null && version === "DOCUMENT_EXISTS" || existing == null && expectedVersion2 != null || existing != null && version === "DOCUMENT_DOES_NOT_EXIST" || existing != null && expectedVersion2 !== null && existing._version !== expectedVersion2) {
|
|
289
|
-
return operationResult(
|
|
290
|
-
{
|
|
291
|
-
successful: false,
|
|
292
|
-
document: existing
|
|
293
|
-
},
|
|
294
|
-
{ operationName: "handle", collectionName, errors }
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
const result = await handle(
|
|
298
|
-
existing !== null ? { ...existing } : null
|
|
299
|
-
);
|
|
300
|
-
if (deepEquals(existing, result))
|
|
301
|
-
return operationResult(
|
|
302
|
-
{
|
|
303
|
-
successful: true,
|
|
304
|
-
document: existing
|
|
305
|
-
},
|
|
306
|
-
{ operationName: "handle", collectionName, errors }
|
|
307
|
-
);
|
|
308
|
-
if (!existing && result) {
|
|
309
|
-
const newDoc = { ...result, _id: id };
|
|
310
|
-
const insertResult = await collection.insertOne(
|
|
311
|
-
{ ...newDoc, _id: id },
|
|
312
|
-
{
|
|
313
|
-
...operationOptions,
|
|
314
|
-
expectedVersion: "DOCUMENT_DOES_NOT_EXIST"
|
|
315
|
-
}
|
|
316
|
-
);
|
|
317
|
-
return {
|
|
318
|
-
...insertResult,
|
|
319
|
-
document: {
|
|
320
|
-
...newDoc,
|
|
321
|
-
_version: insertResult.nextExpectedVersion
|
|
322
|
-
}
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
if (existing && !result) {
|
|
326
|
-
const deleteResult = await collection.deleteOne(byId, {
|
|
327
|
-
...operationOptions,
|
|
328
|
-
expectedVersion: _nullishCoalesce(expectedVersion2, () => ( "DOCUMENT_EXISTS"))
|
|
329
|
-
});
|
|
330
|
-
return { ...deleteResult, document: null };
|
|
331
|
-
}
|
|
332
|
-
if (existing && result) {
|
|
333
|
-
const replaceResult = await collection.replaceOne(byId, result, {
|
|
334
|
-
...operationOptions,
|
|
335
|
-
expectedVersion: _nullishCoalesce(expectedVersion2, () => ( "DOCUMENT_EXISTS"))
|
|
336
|
-
});
|
|
337
|
-
return {
|
|
338
|
-
...replaceResult,
|
|
339
|
-
document: {
|
|
340
|
-
...result,
|
|
341
|
-
_version: replaceResult.nextExpectedVersion
|
|
342
|
-
}
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
return operationResult(
|
|
346
|
-
{
|
|
347
|
-
successful: true,
|
|
348
|
-
document: existing
|
|
349
|
-
},
|
|
350
|
-
{ operationName: "handle", collectionName, errors }
|
|
351
|
-
);
|
|
352
|
-
},
|
|
353
|
-
find: async (filter, options) => {
|
|
354
|
-
await ensureCollectionCreated(options);
|
|
355
|
-
const result = await query(SqlFor.find(_nullishCoalesce(filter, () => ( {})), options));
|
|
356
|
-
return result.rows.map((row) => row.data);
|
|
357
|
-
},
|
|
358
|
-
countDocuments: async (filter, options) => {
|
|
359
|
-
await ensureCollectionCreated(options);
|
|
360
|
-
const { count } = await _dumbo.single.call(void 0,
|
|
361
|
-
query(SqlFor.countDocuments(_nullishCoalesce(filter, () => ( {}))))
|
|
362
|
-
);
|
|
363
|
-
return count;
|
|
364
|
-
},
|
|
365
|
-
drop: async (options) => {
|
|
366
|
-
await ensureCollectionCreated(options);
|
|
367
|
-
const result = await command(SqlFor.drop());
|
|
368
|
-
return (_nullishCoalesce(_optionalChain([result, 'optionalAccess', _36 => _36.rowCount]), () => ( 0))) > 0;
|
|
369
|
-
},
|
|
370
|
-
rename: async (newName, options) => {
|
|
371
|
-
await ensureCollectionCreated(options);
|
|
372
|
-
await command(SqlFor.rename(newName));
|
|
373
|
-
collectionName = newName;
|
|
374
|
-
return collection;
|
|
375
|
-
},
|
|
376
|
-
sql: {
|
|
377
|
-
async query(sql4, options) {
|
|
378
|
-
await ensureCollectionCreated(options);
|
|
379
|
-
const result = await query(sql4, options);
|
|
380
|
-
return result.rows;
|
|
381
|
-
},
|
|
382
|
-
async command(sql4, options) {
|
|
383
|
-
await ensureCollectionCreated(options);
|
|
384
|
-
return command(sql4, options);
|
|
385
|
-
}
|
|
386
|
-
},
|
|
387
|
-
schema: {
|
|
388
|
-
get component() {
|
|
389
|
-
return _dumbo.schemaComponent.call(void 0, "pongo:schema_component:collection", {
|
|
390
|
-
migrations: SqlFor.migrations
|
|
391
|
-
});
|
|
392
|
-
},
|
|
393
|
-
migrate: () => _pg.runPostgreSQLMigrations.call(void 0, pool, SqlFor.migrations())
|
|
394
|
-
// TODO: This needs to change to support more connectors
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
return collection;
|
|
398
|
-
};
|
|
399
|
-
var pongoCollectionSchemaComponent = (collectionName) => _dumbo.schemaComponent.call(void 0, "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
|
-
|
|
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: _nullishCoalesce(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
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
require('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) => _optionalChain([transaction, 'optionalAccess', _37 => _37.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 = _optionalChain([options, 'optionalAccess', _38 => _38.explicit]) === true;
|
|
526
|
-
const defaultTransactionOptions = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _39 => _39.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(_nullishCoalesce(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: _nullishCoalesce(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
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
var ObjectId = (value) => _nullishCoalesce(value, () => ( _uuid.v7.call(void 0, )));
|
|
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
|
-
_nullishCoalesce(errorMessage, () => ( `${operationName} on ${collectionName} failed. Expected document state does not match current one! Result: ${_dumbo.JSONSerializer.serialize(result)}!`))
|
|
616
|
-
);
|
|
617
|
-
}
|
|
618
|
-
};
|
|
619
|
-
if (_optionalChain([options, 'access', _40 => _40.errors, 'optionalAccess', _41 => _41.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 _nullishCoalesce(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(_optionalChain([schema, 'access', _42 => _42.dbs, 'access', _43 => _43[prop], 'optionalAccess', _44 => _44.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 _nullishCoalesce(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, _optionalChain([options, 'optionalAccess', _45 => _45.schema, 'optionalAccess', _46 => _46.definition]));
|
|
737
|
-
};
|
|
738
|
-
var clientToDbOptions = (options) => {
|
|
739
|
-
const postgreSQLOptions = {
|
|
740
|
-
connector: _pg.NodePostgresConnectorType,
|
|
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
|
-
|
|
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 _dumbo.sql.call(void 0,
|
|
787
|
-
`(data @> %L::jsonb OR jsonb_path_exists(data, '$.%s[*] ? (@ == %s)'))`,
|
|
788
|
-
_dumbo.JSONSerializer.serialize(buildNestedObject(path, value)),
|
|
789
|
-
path,
|
|
790
|
-
_dumbo.JSONSerializer.serialize(value)
|
|
791
|
-
);
|
|
792
|
-
case "$gt":
|
|
793
|
-
case "$gte":
|
|
794
|
-
case "$lt":
|
|
795
|
-
case "$lte":
|
|
796
|
-
case "$ne":
|
|
797
|
-
return _dumbo.sql.call(void 0,
|
|
798
|
-
`data #>> %L ${OperatorMap[operator]} %L`,
|
|
799
|
-
`{${path.split(".").join(",")}}`,
|
|
800
|
-
value
|
|
801
|
-
);
|
|
802
|
-
case "$in":
|
|
803
|
-
return _dumbo.sql.call(void 0,
|
|
804
|
-
"data #>> %L IN (%s)",
|
|
805
|
-
`{${path.split(".").join(",")}}`,
|
|
806
|
-
value.map((v) => _dumbo.sql.call(void 0, "%L", v)).join(", ")
|
|
807
|
-
);
|
|
808
|
-
case "$nin":
|
|
809
|
-
return _dumbo.sql.call(void 0,
|
|
810
|
-
"data #>> %L NOT IN (%s)",
|
|
811
|
-
`{${path.split(".").join(",")}}`,
|
|
812
|
-
value.map((v) => _dumbo.sql.call(void 0, "%L", v)).join(", ")
|
|
813
|
-
);
|
|
814
|
-
case "$elemMatch": {
|
|
815
|
-
const subQuery = objectEntries(value).map(
|
|
816
|
-
([subKey, subValue]) => _dumbo.sql.call(void 0, `@."%s" == %s`, subKey, _dumbo.JSONSerializer.serialize(subValue))
|
|
817
|
-
).join(" && ");
|
|
818
|
-
return _dumbo.sql.call(void 0, `jsonb_path_exists(data, '$.%s[*] ? (%s)')`, path, subQuery);
|
|
819
|
-
}
|
|
820
|
-
case "$all":
|
|
821
|
-
return _dumbo.sql.call(void 0,
|
|
822
|
-
"data @> %L::jsonb",
|
|
823
|
-
_dumbo.JSONSerializer.serialize(buildNestedObject(path, value))
|
|
824
|
-
);
|
|
825
|
-
case "$size":
|
|
826
|
-
return _dumbo.sql.call(void 0,
|
|
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 _dumbo.sql.call(void 0, `${fieldName} = %L`, value);
|
|
839
|
-
case "$gt":
|
|
840
|
-
case "$gte":
|
|
841
|
-
case "$lt":
|
|
842
|
-
case "$lte":
|
|
843
|
-
case "$ne":
|
|
844
|
-
return _dumbo.sql.call(void 0, `${fieldName} ${OperatorMap[operator]} %L`, value);
|
|
845
|
-
case "$in":
|
|
846
|
-
return _dumbo.sql.call(void 0,
|
|
847
|
-
`${fieldName} IN (%s)`,
|
|
848
|
-
value.map((v) => _dumbo.sql.call(void 0, "%L", v)).join(", ")
|
|
849
|
-
);
|
|
850
|
-
case "$nin":
|
|
851
|
-
return _dumbo.sql.call(void 0,
|
|
852
|
-
`${fieldName} NOT IN (%s)`,
|
|
853
|
-
value.map((v) => _dumbo.sql.call(void 0, "%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
|
-
|
|
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
|
-
}, _dumbo.sql.call(void 0, "data"));
|
|
891
|
-
var buildSetQuery = (set, currentUpdateQuery) => _dumbo.sql.call(void 0, "%s || %L::jsonb", currentUpdateQuery, _dumbo.JSONSerializer.serialize(set));
|
|
892
|
-
var buildUnsetQuery = (unset, currentUpdateQuery) => _dumbo.sql.call(void 0,
|
|
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 = _dumbo.sql.call(void 0,
|
|
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 = _dumbo.sql.call(void 0,
|
|
912
|
-
"jsonb_set(%s, '{%s}', (coalesce(data->'%s', '[]'::jsonb) || %L::jsonb), true)",
|
|
913
|
-
currentUpdateQuery,
|
|
914
|
-
key,
|
|
915
|
-
key,
|
|
916
|
-
_dumbo.JSONSerializer.serialize([value])
|
|
917
|
-
);
|
|
918
|
-
}
|
|
919
|
-
return currentUpdateQuery;
|
|
920
|
-
};
|
|
921
|
-
|
|
922
|
-
// src/storage/postgresql/sqlBuilder/index.ts
|
|
923
|
-
var createCollection = (collectionName) => _dumbo.sql.call(void 0,
|
|
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
|
-
_dumbo.sqlMigration.call(void 0, `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 _dumbo.sql.call(void 0,
|
|
946
|
-
"INSERT INTO %I (_id, data, _version) VALUES (%L, %L, %L) ON CONFLICT(_id) DO NOTHING;",
|
|
947
|
-
collectionName,
|
|
948
|
-
document._id,
|
|
949
|
-
_dumbo.JSONSerializer.serialize(document),
|
|
950
|
-
_nullishCoalesce(document._version, () => ( 1n))
|
|
951
|
-
);
|
|
952
|
-
},
|
|
953
|
-
insertMany: (documents) => {
|
|
954
|
-
const values = documents.map(
|
|
955
|
-
(doc) => _dumbo.sql.call(void 0,
|
|
956
|
-
"(%L, %L, %L)",
|
|
957
|
-
doc._id,
|
|
958
|
-
_dumbo.JSONSerializer.serialize(doc),
|
|
959
|
-
_nullishCoalesce(doc._version, () => ( 1n))
|
|
960
|
-
)
|
|
961
|
-
).join(", ");
|
|
962
|
-
return _dumbo.sql.call(void 0,
|
|
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(_optionalChain([options, 'optionalAccess', _47 => _47.expectedVersion]));
|
|
972
|
-
const expectedVersionUpdate = expectedVersion2 != null ? "AND %I._version = %L" : "";
|
|
973
|
-
const expectedVersionParams = expectedVersion2 != null ? [collectionName, expectedVersion2] : [];
|
|
974
|
-
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter);
|
|
975
|
-
const updateQuery = _dumbo.isSQL.call(void 0, update) ? update : buildUpdateQuery(update);
|
|
976
|
-
return _dumbo.sql.call(void 0,
|
|
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(_optionalChain([options, 'optionalAccess', _48 => _48.expectedVersion]));
|
|
1012
|
-
const expectedVersionUpdate = expectedVersion2 != null ? "AND %I._version = %L" : "";
|
|
1013
|
-
const expectedVersionParams = expectedVersion2 != null ? [collectionName, expectedVersion2] : [];
|
|
1014
|
-
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter);
|
|
1015
|
-
return _dumbo.sql.call(void 0,
|
|
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
|
-
_dumbo.JSONSerializer.serialize(document),
|
|
1042
|
-
collectionName,
|
|
1043
|
-
collectionName,
|
|
1044
|
-
...expectedVersionParams,
|
|
1045
|
-
collectionName,
|
|
1046
|
-
collectionName
|
|
1047
|
-
);
|
|
1048
|
-
},
|
|
1049
|
-
updateMany: (filter, update) => {
|
|
1050
|
-
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter);
|
|
1051
|
-
const updateQuery = _dumbo.isSQL.call(void 0, update) ? update : buildUpdateQuery(update);
|
|
1052
|
-
return _dumbo.sql.call(void 0,
|
|
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(_optionalChain([options, 'optionalAccess', _49 => _49.expectedVersion]));
|
|
1065
|
-
const expectedVersionUpdate = expectedVersion2 != null ? "AND %I._version = %L" : "";
|
|
1066
|
-
const expectedVersionParams = expectedVersion2 != null ? [collectionName, expectedVersion2] : [];
|
|
1067
|
-
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter);
|
|
1068
|
-
return _dumbo.sql.call(void 0,
|
|
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 = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter);
|
|
1097
|
-
return _dumbo.sql.call(void 0, "DELETE FROM %I %s", collectionName, where(filterQuery));
|
|
1098
|
-
},
|
|
1099
|
-
findOne: (filter) => {
|
|
1100
|
-
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter);
|
|
1101
|
-
return _dumbo.sql.call(void 0,
|
|
1102
|
-
"SELECT data FROM %I %s LIMIT 1;",
|
|
1103
|
-
collectionName,
|
|
1104
|
-
where(filterQuery)
|
|
1105
|
-
);
|
|
1106
|
-
},
|
|
1107
|
-
find: (filter, options) => {
|
|
1108
|
-
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter);
|
|
1109
|
-
const query = [];
|
|
1110
|
-
query.push(_dumbo.sql.call(void 0, "SELECT data FROM %I", collectionName));
|
|
1111
|
-
const whereStmt = where(filterQuery);
|
|
1112
|
-
if (whereStmt.length > 0) {
|
|
1113
|
-
query.push(_dumbo.sql.call(void 0, "%s", whereStmt));
|
|
1114
|
-
}
|
|
1115
|
-
if (_optionalChain([options, 'optionalAccess', _50 => _50.limit])) {
|
|
1116
|
-
query.push(_dumbo.sql.call(void 0, "LIMIT %s", options.limit));
|
|
1117
|
-
}
|
|
1118
|
-
if (_optionalChain([options, 'optionalAccess', _51 => _51.skip])) {
|
|
1119
|
-
query.push(_dumbo.sql.call(void 0, "OFFSET %s", options.skip));
|
|
1120
|
-
}
|
|
1121
|
-
return _dumbo.sql.call(void 0, query.join(" ") + ";");
|
|
1122
|
-
},
|
|
1123
|
-
countDocuments: (filter) => {
|
|
1124
|
-
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter);
|
|
1125
|
-
return _dumbo.sql.call(void 0,
|
|
1126
|
-
"SELECT COUNT(1) as count FROM %I %s;",
|
|
1127
|
-
collectionName,
|
|
1128
|
-
where(filterQuery)
|
|
1129
|
-
);
|
|
1130
|
-
},
|
|
1131
|
-
rename: (newName) => _dumbo.sql.call(void 0, "ALTER TABLE %I RENAME TO %I;", collectionName, newName),
|
|
1132
|
-
drop: (targetName = collectionName) => _dumbo.sql.call(void 0, "DROP TABLE IF EXISTS %I", targetName)
|
|
1133
|
-
});
|
|
1134
|
-
var where = (filter) => filter.length > 0 ? _dumbo.sql.call(void 0, "WHERE %s", filter) : _dumbo.rawSql.call(void 0, "");
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
exports.pongoCollectionPostgreSQLMigrations = pongoCollectionPostgreSQLMigrations; exports.postgresSQLBuilder = postgresSQLBuilder; exports.isPostgresClientOptions = isPostgresClientOptions; exports.postgresDb = postgresDb; exports.pongoDbSchemaComponent = pongoDbSchemaComponent; exports.transactionExecutorOrDefault = transactionExecutorOrDefault; exports.pongoCollection = pongoCollection; exports.pongoCollectionSchemaComponent = pongoCollectionSchemaComponent; exports.QueryOperators = QueryOperators; exports.OperatorMap = OperatorMap; exports.isOperator = isOperator; exports.hasOperators = hasOperators; exports.isNumber = isNumber; exports.isString = isString; exports.PongoError = PongoError; exports.ConcurrencyError = ConcurrencyError; exports.getPongoDb = getPongoDb; exports.pongoTransaction = pongoTransaction; exports.pongoSession = pongoSession; exports.objectEntries = objectEntries; exports.ObjectId = ObjectId; exports.DOCUMENT_EXISTS = DOCUMENT_EXISTS; exports.DOCUMENT_DOES_NOT_EXIST = DOCUMENT_DOES_NOT_EXIST; exports.NO_CONCURRENCY_CHECK = NO_CONCURRENCY_CHECK; exports.isGeneralExpectedDocumentVersion = isGeneralExpectedDocumentVersion; exports.expectedVersionValue = expectedVersionValue; exports.expectedVersion = expectedVersion; exports.operationResult = operationResult; exports.pongoSchema = pongoSchema; exports.proxyPongoDbWithSchema = proxyPongoDbWithSchema; exports.proxyClientWithSchema = proxyClientWithSchema; exports.toDbSchemaMetadata = toDbSchemaMetadata; exports.toClientSchemaMetadata = toClientSchemaMetadata; exports.pongoClient = pongoClient; exports.clientToDbOptions = clientToDbOptions; exports.deepEquals = deepEquals; exports.isEquatable = isEquatable;
|
|
1175
|
-
//# sourceMappingURL=chunk-AV4SHJQB.cjs.map
|