@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
|
@@ -1,154 +1,598 @@
|
|
|
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
|
-
|
|
3
|
+
mapColumnToJSON,
|
|
4
|
+
runSQLMigrations as runSQLMigrations2,
|
|
13
5
|
single
|
|
14
6
|
} from "@event-driven-io/dumbo";
|
|
15
|
-
import {
|
|
16
|
-
|
|
7
|
+
import { v7 as uuid2 } from "uuid";
|
|
8
|
+
|
|
9
|
+
// src/core/collection/pongoCollectionSchema.ts
|
|
10
|
+
import "@event-driven-io/dumbo";
|
|
17
11
|
|
|
18
|
-
// src/
|
|
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(createOptions.connectionString);
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var
|
|
29
|
-
var
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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 (
|
|
38
|
-
const query = async (
|
|
39
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
59
|
-
|
|
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(
|
|
75
|
-
const result = await query(
|
|
290
|
+
async query(sql, options2) {
|
|
291
|
+
const result = await query(sql, options2);
|
|
76
292
|
return result.rows;
|
|
77
293
|
},
|
|
78
|
-
async command(
|
|
79
|
-
return command(
|
|
294
|
+
async command(sql, options2) {
|
|
295
|
+
return command(sql, options2);
|
|
80
296
|
}
|
|
81
297
|
}
|
|
82
298
|
};
|
|
83
|
-
const
|
|
84
|
-
if (
|
|
85
|
-
|
|
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/
|
|
100
|
-
var
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
338
|
+
};
|
|
339
|
+
var pongoDatabaseDriverRegistry = globalThis.pongoDatabaseDriverRegistry = globalThis.pongoDatabaseDriverRegistry ?? PongoDatabaseDriverRegistry();
|
|
340
|
+
|
|
341
|
+
// src/core/pongoClient.ts
|
|
342
|
+
import "@event-driven-io/dumbo";
|
|
343
|
+
|
|
344
|
+
// src/core/pongoTransaction.ts
|
|
345
|
+
var pongoTransaction = (options) => {
|
|
346
|
+
let isCommitted = false;
|
|
347
|
+
let isRolledBack = false;
|
|
348
|
+
let databaseName = null;
|
|
349
|
+
let transaction = null;
|
|
350
|
+
return {
|
|
351
|
+
enlistDatabase: async (db) => {
|
|
352
|
+
if (transaction && databaseName !== db.databaseName)
|
|
353
|
+
throw new Error(
|
|
354
|
+
"There's already other database assigned to transaction"
|
|
355
|
+
);
|
|
356
|
+
if (transaction && databaseName === db.databaseName) return transaction;
|
|
357
|
+
databaseName = db.databaseName;
|
|
358
|
+
transaction = db.transaction();
|
|
359
|
+
await transaction.begin();
|
|
360
|
+
return transaction;
|
|
139
361
|
},
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
);
|
|
362
|
+
commit: async () => {
|
|
363
|
+
if (!transaction) throw new Error("No database transaction started!");
|
|
364
|
+
if (isCommitted) return;
|
|
365
|
+
if (isRolledBack) throw new Error("Transaction is not active!");
|
|
366
|
+
isCommitted = true;
|
|
367
|
+
await transaction.commit();
|
|
368
|
+
transaction = null;
|
|
369
|
+
},
|
|
370
|
+
rollback: async (error) => {
|
|
371
|
+
if (!transaction) throw new Error("No database transaction started!");
|
|
372
|
+
if (isCommitted) throw new Error("Cannot rollback commited transaction!");
|
|
373
|
+
if (isRolledBack) return;
|
|
374
|
+
isRolledBack = true;
|
|
375
|
+
await transaction.rollback(error);
|
|
376
|
+
transaction = null;
|
|
377
|
+
},
|
|
378
|
+
databaseName,
|
|
379
|
+
isStarting: false,
|
|
380
|
+
isCommitted,
|
|
381
|
+
get isActive() {
|
|
382
|
+
return !isCommitted && !isRolledBack;
|
|
383
|
+
},
|
|
384
|
+
get sqlExecutor() {
|
|
385
|
+
if (transaction === null)
|
|
386
|
+
throw new Error("No database transaction was started");
|
|
387
|
+
return transaction.execute;
|
|
388
|
+
},
|
|
389
|
+
options
|
|
390
|
+
};
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// src/core/pongoSession.ts
|
|
394
|
+
var isActive = (transaction) => transaction?.isActive === true;
|
|
395
|
+
function assertInActiveTransaction(transaction) {
|
|
396
|
+
if (!isActive(transaction)) throw new Error("No active transaction exists!");
|
|
397
|
+
}
|
|
398
|
+
function assertNotInActiveTransaction(transaction) {
|
|
399
|
+
if (isActive(transaction))
|
|
400
|
+
throw new Error("Active transaction already exists!");
|
|
401
|
+
}
|
|
402
|
+
var pongoSession = (options) => {
|
|
403
|
+
const explicit = options?.explicit === true;
|
|
404
|
+
const defaultTransactionOptions = options?.defaultTransactionOptions ?? {
|
|
405
|
+
get snapshotEnabled() {
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
let transaction = null;
|
|
410
|
+
let hasEnded = false;
|
|
411
|
+
const startTransaction = (options2) => {
|
|
412
|
+
assertNotInActiveTransaction(transaction);
|
|
413
|
+
transaction = pongoTransaction(options2 ?? defaultTransactionOptions);
|
|
414
|
+
};
|
|
415
|
+
const commitTransaction = async () => {
|
|
416
|
+
assertInActiveTransaction(transaction);
|
|
417
|
+
await transaction.commit();
|
|
418
|
+
};
|
|
419
|
+
const abortTransaction = async () => {
|
|
420
|
+
assertInActiveTransaction(transaction);
|
|
421
|
+
await transaction.rollback();
|
|
422
|
+
};
|
|
423
|
+
const endSession = async () => {
|
|
424
|
+
if (hasEnded) return;
|
|
425
|
+
hasEnded = true;
|
|
426
|
+
if (isActive(transaction)) await transaction.rollback();
|
|
427
|
+
};
|
|
428
|
+
const session = {
|
|
429
|
+
get hasEnded() {
|
|
430
|
+
return hasEnded;
|
|
431
|
+
},
|
|
432
|
+
explicit,
|
|
433
|
+
defaultTransactionOptions: defaultTransactionOptions ?? {
|
|
434
|
+
get snapshotEnabled() {
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
get transaction() {
|
|
439
|
+
return transaction;
|
|
440
|
+
},
|
|
441
|
+
get snapshotEnabled() {
|
|
442
|
+
return defaultTransactionOptions.snapshotEnabled;
|
|
443
|
+
},
|
|
444
|
+
endSession,
|
|
445
|
+
incrementTransactionNumber: () => {
|
|
446
|
+
},
|
|
447
|
+
inTransaction: () => isActive(transaction),
|
|
448
|
+
startTransaction,
|
|
449
|
+
commitTransaction,
|
|
450
|
+
abortTransaction,
|
|
451
|
+
withTransaction: async (fn, options2) => {
|
|
452
|
+
startTransaction(options2);
|
|
453
|
+
try {
|
|
454
|
+
const result = await fn(session);
|
|
455
|
+
await commitTransaction();
|
|
456
|
+
return result;
|
|
457
|
+
} catch (error) {
|
|
458
|
+
await abortTransaction();
|
|
459
|
+
throw error;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
return session;
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
// src/core/pongoClient.ts
|
|
467
|
+
var pongoClient = (options) => {
|
|
468
|
+
const { driver, connectionString, schema, errors, ...connectionOptions } = options;
|
|
469
|
+
const dbClients = PongoDatabaseCache({
|
|
470
|
+
driver,
|
|
471
|
+
typedSchema: schema?.definition
|
|
472
|
+
});
|
|
473
|
+
const pongoClient2 = {
|
|
474
|
+
driverType: driver.driverType,
|
|
475
|
+
connect: async () => {
|
|
476
|
+
await dbClients.forAll((db) => db.connect());
|
|
477
|
+
return pongoClient2;
|
|
478
|
+
},
|
|
479
|
+
close: async () => {
|
|
480
|
+
await dbClients.forAll((db) => db.close());
|
|
481
|
+
},
|
|
482
|
+
db: (dbName) => {
|
|
483
|
+
const db = dbClients.getOrCreate({
|
|
484
|
+
...connectionOptions,
|
|
485
|
+
connectionString,
|
|
486
|
+
databaseName: dbName,
|
|
487
|
+
errors
|
|
488
|
+
});
|
|
489
|
+
return db;
|
|
490
|
+
},
|
|
491
|
+
startSession: pongoSession,
|
|
492
|
+
withSession: async (callback) => {
|
|
493
|
+
const session = pongoSession();
|
|
494
|
+
try {
|
|
495
|
+
return await callback(session);
|
|
496
|
+
} finally {
|
|
497
|
+
await session.endSession();
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
return proxyClientWithSchema(pongoClient2, schema?.definition);
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
// src/core/utils/deepEquals.ts
|
|
505
|
+
var deepEquals = (left, right) => {
|
|
506
|
+
if (isEquatable(left)) {
|
|
507
|
+
return left.equals(right);
|
|
508
|
+
}
|
|
509
|
+
if (Array.isArray(left)) {
|
|
510
|
+
return Array.isArray(right) && left.length === right.length && left.every((val, index) => deepEquals(val, right[index]));
|
|
511
|
+
}
|
|
512
|
+
if (typeof left !== "object" || typeof right !== "object" || left === null || right === null) {
|
|
513
|
+
return left === right;
|
|
514
|
+
}
|
|
515
|
+
if (Array.isArray(right)) return false;
|
|
516
|
+
const keys1 = Object.keys(left);
|
|
517
|
+
const keys2 = Object.keys(right);
|
|
518
|
+
if (keys1.length !== keys2.length || !keys1.every((key) => keys2.includes(key)))
|
|
519
|
+
return false;
|
|
520
|
+
for (const key in left) {
|
|
521
|
+
if (left[key] instanceof Function && right[key] instanceof Function)
|
|
522
|
+
continue;
|
|
523
|
+
const isEqual = deepEquals(left[key], right[key]);
|
|
524
|
+
if (!isEqual) {
|
|
525
|
+
return false;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return true;
|
|
529
|
+
};
|
|
530
|
+
var isEquatable = (left) => {
|
|
531
|
+
return left && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
// src/core/collection/pongoCollection.ts
|
|
535
|
+
var enlistIntoTransactionIfActive = async (db, options) => {
|
|
536
|
+
const transaction = options?.session?.transaction;
|
|
537
|
+
if (!transaction || !transaction.isActive) return null;
|
|
538
|
+
return await transaction.enlistDatabase(db);
|
|
539
|
+
};
|
|
540
|
+
var transactionExecutorOrDefault = async (db, options, defaultSqlExecutor) => {
|
|
541
|
+
const existingTransaction = await enlistIntoTransactionIfActive(db, options);
|
|
542
|
+
return existingTransaction?.execute ?? defaultSqlExecutor;
|
|
543
|
+
};
|
|
544
|
+
var columnMapping = {
|
|
545
|
+
mapping: {
|
|
546
|
+
...mapColumnToJSON("data")
|
|
547
|
+
//...mapColumnToJSON('metadata'),
|
|
548
|
+
//...mapColumnToBigint('_version'),
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
var pongoCollection = ({
|
|
552
|
+
db,
|
|
553
|
+
collectionName,
|
|
554
|
+
pool,
|
|
555
|
+
schemaComponent: schemaComponent3,
|
|
556
|
+
schema,
|
|
557
|
+
errors
|
|
558
|
+
}) => {
|
|
559
|
+
const SqlFor = schemaComponent3.sqlBuilder;
|
|
560
|
+
const sqlExecutor = pool.execute;
|
|
561
|
+
const command = async (sql, options) => (await transactionExecutorOrDefault(db, options, sqlExecutor)).command(sql);
|
|
562
|
+
const query = async (sql, options) => (await transactionExecutorOrDefault(db, options, sqlExecutor)).query(
|
|
563
|
+
sql,
|
|
564
|
+
columnMapping
|
|
565
|
+
);
|
|
566
|
+
let shouldMigrate = schema?.autoMigration !== "None";
|
|
567
|
+
const createCollection = (options) => {
|
|
568
|
+
shouldMigrate = false;
|
|
569
|
+
if (options?.session) return command(SqlFor.createCollection(), options);
|
|
570
|
+
else return command(SqlFor.createCollection());
|
|
571
|
+
};
|
|
572
|
+
const ensureCollectionCreated = (options) => {
|
|
573
|
+
if (!shouldMigrate) {
|
|
574
|
+
return Promise.resolve();
|
|
575
|
+
}
|
|
576
|
+
return createCollection(options);
|
|
577
|
+
};
|
|
578
|
+
const collection = {
|
|
579
|
+
dbName: db.databaseName,
|
|
580
|
+
collectionName,
|
|
581
|
+
createCollection: async (options) => {
|
|
582
|
+
await createCollection(options);
|
|
583
|
+
},
|
|
584
|
+
insertOne: async (document, options) => {
|
|
585
|
+
await ensureCollectionCreated(options);
|
|
586
|
+
const _id = document._id ?? uuid2();
|
|
587
|
+
const _version = document._version ?? 1n;
|
|
588
|
+
const result = await command(
|
|
589
|
+
SqlFor.insertOne({
|
|
590
|
+
...document,
|
|
591
|
+
_id,
|
|
592
|
+
_version
|
|
593
|
+
}),
|
|
594
|
+
options
|
|
595
|
+
);
|
|
152
596
|
const successful = (result.rowCount ?? 0) > 0;
|
|
153
597
|
return operationResult(
|
|
154
598
|
{
|
|
@@ -163,7 +607,7 @@ var pongoCollection = ({
|
|
|
163
607
|
await ensureCollectionCreated(options);
|
|
164
608
|
const rows = documents.map((doc) => ({
|
|
165
609
|
...doc,
|
|
166
|
-
_id: doc._id ??
|
|
610
|
+
_id: doc._id ?? uuid2(),
|
|
167
611
|
_version: doc._version ?? 1n
|
|
168
612
|
}));
|
|
169
613
|
const result = await command(
|
|
@@ -190,7 +634,7 @@ var pongoCollection = ({
|
|
|
190
634
|
successful: result.rows.length > 0 && result.rows[0].modified === result.rows[0].matched,
|
|
191
635
|
modifiedCount: Number(result.rows[0]?.modified ?? 0),
|
|
192
636
|
matchedCount: Number(result.rows[0]?.matched ?? 0),
|
|
193
|
-
nextExpectedVersion: result.rows[0]?.version ?? 0n
|
|
637
|
+
nextExpectedVersion: BigInt(result.rows[0]?.version ?? 0n)
|
|
194
638
|
},
|
|
195
639
|
{ operationName: "updateOne", collectionName, errors }
|
|
196
640
|
);
|
|
@@ -206,7 +650,7 @@ var pongoCollection = ({
|
|
|
206
650
|
successful: result.rows.length > 0 && result.rows[0].modified > 0,
|
|
207
651
|
modifiedCount: Number(result.rows[0]?.modified ?? 0),
|
|
208
652
|
matchedCount: Number(result.rows[0]?.matched ?? 0),
|
|
209
|
-
nextExpectedVersion: result.rows[0]?.version ?? 0n
|
|
653
|
+
nextExpectedVersion: BigInt(result.rows[0]?.version ?? 0n)
|
|
210
654
|
},
|
|
211
655
|
{ operationName: "replaceOne", collectionName, errors }
|
|
212
656
|
);
|
|
@@ -374,786 +818,38 @@ var pongoCollection = ({
|
|
|
374
818
|
return collection;
|
|
375
819
|
},
|
|
376
820
|
sql: {
|
|
377
|
-
async query(
|
|
821
|
+
async query(sql, options) {
|
|
378
822
|
await ensureCollectionCreated(options);
|
|
379
|
-
const result = await query(
|
|
823
|
+
const result = await query(sql, options);
|
|
380
824
|
return result.rows;
|
|
381
825
|
},
|
|
382
|
-
async command(
|
|
826
|
+
async command(sql, options) {
|
|
383
827
|
await ensureCollectionCreated(options);
|
|
384
|
-
return command(
|
|
828
|
+
return command(sql, options);
|
|
385
829
|
}
|
|
386
830
|
},
|
|
387
831
|
schema: {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
migrations: SqlFor.migrations
|
|
391
|
-
});
|
|
392
|
-
},
|
|
393
|
-
migrate: () => runPostgreSQLMigrations2(pool, SqlFor.migrations())
|
|
394
|
-
// TODO: This needs to change to support more connectors
|
|
832
|
+
component: schemaComponent3,
|
|
833
|
+
migrate: () => runSQLMigrations2(pool, schemaComponent3.migrations)
|
|
395
834
|
}
|
|
396
835
|
};
|
|
397
836
|
return collection;
|
|
398
837
|
};
|
|
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
838
|
|
|
1136
839
|
export {
|
|
1137
|
-
pongoCollectionPostgreSQLMigrations,
|
|
1138
|
-
postgresSQLBuilder,
|
|
1139
|
-
isPostgresClientOptions,
|
|
1140
|
-
postgresDb,
|
|
1141
|
-
pongoDbSchemaComponent,
|
|
1142
840
|
transactionExecutorOrDefault,
|
|
1143
841
|
pongoCollection,
|
|
1144
|
-
|
|
842
|
+
PongoCollectionSchemaComponent,
|
|
1145
843
|
QueryOperators,
|
|
1146
844
|
OperatorMap,
|
|
1147
845
|
isOperator,
|
|
1148
846
|
hasOperators,
|
|
847
|
+
PongoDatabaseCache,
|
|
848
|
+
objectEntries,
|
|
1149
849
|
isNumber,
|
|
1150
850
|
isString,
|
|
1151
851
|
PongoError,
|
|
1152
852
|
ConcurrencyError,
|
|
1153
|
-
getPongoDb,
|
|
1154
|
-
pongoTransaction,
|
|
1155
|
-
pongoSession,
|
|
1156
|
-
objectEntries,
|
|
1157
853
|
ObjectId,
|
|
1158
854
|
DOCUMENT_EXISTS,
|
|
1159
855
|
DOCUMENT_DOES_NOT_EXIST,
|
|
@@ -1167,9 +863,14 @@ export {
|
|
|
1167
863
|
proxyClientWithSchema,
|
|
1168
864
|
toDbSchemaMetadata,
|
|
1169
865
|
toClientSchemaMetadata,
|
|
866
|
+
PongoDatabaseSchemaComponent,
|
|
867
|
+
PongoDatabase,
|
|
868
|
+
PongoDatabaseDriverRegistry,
|
|
869
|
+
pongoDatabaseDriverRegistry,
|
|
870
|
+
pongoTransaction,
|
|
871
|
+
pongoSession,
|
|
1170
872
|
pongoClient,
|
|
1171
|
-
clientToDbOptions,
|
|
1172
873
|
deepEquals,
|
|
1173
874
|
isEquatable
|
|
1174
875
|
};
|
|
1175
|
-
//# sourceMappingURL=chunk-
|
|
876
|
+
//# sourceMappingURL=chunk-DL4E3N6J.js.map
|