@event-driven-io/pongo 0.17.0-beta.35 → 0.17.0-beta.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +311 -494
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.d.cts +4 -4
- package/dist/cli.d.ts +4 -3
- package/dist/cli.js +279 -494
- package/dist/cli.js.map +1 -1
- package/dist/cloudflare.cjs +36 -48
- package/dist/cloudflare.cjs.map +1 -1
- package/dist/cloudflare.d.cts +7 -7
- package/dist/cloudflare.d.ts +7 -7
- package/dist/cloudflare.js +29 -47
- package/dist/cloudflare.js.map +1 -1
- package/dist/core-CwxxuUAn.cjs +1590 -0
- package/dist/core-CwxxuUAn.cjs.map +1 -0
- package/dist/core-D_iZiiYe.js +1308 -0
- package/dist/core-D_iZiiYe.js.map +1 -0
- package/dist/core-DwoTLYbR.cjs +319 -0
- package/dist/core-DwoTLYbR.cjs.map +1 -0
- package/dist/core-fsJmsQDa.js +307 -0
- package/dist/core-fsJmsQDa.js.map +1 -0
- package/dist/index-DMq6F3x9.d.ts +705 -0
- package/dist/index-Du_IHXAj.d.ts +10 -0
- package/dist/index-ZKOB86ub.d.cts +10 -0
- package/dist/index-qeC-p0Tq.d.cts +705 -0
- package/dist/index.cjs +61 -118
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -130
- package/dist/index.d.ts +11 -130
- package/dist/index.js +14 -119
- package/dist/index.js.map +1 -1
- package/dist/pg.cjs +214 -320
- package/dist/pg.cjs.map +1 -1
- package/dist/pg.d.cts +24 -22
- package/dist/pg.d.ts +24 -22
- package/dist/pg.js +209 -321
- package/dist/pg.js.map +1 -1
- package/dist/shim.cjs +281 -367
- package/dist/shim.cjs.map +1 -1
- package/dist/shim.d.cts +140 -137
- package/dist/shim.d.ts +140 -137
- package/dist/shim.js +275 -366
- package/dist/shim.js.map +1 -1
- package/dist/sqlite3.cjs +41 -56
- package/dist/sqlite3.cjs.map +1 -1
- package/dist/sqlite3.d.cts +9 -9
- package/dist/sqlite3.d.ts +9 -9
- package/dist/sqlite3.js +35 -56
- package/dist/sqlite3.js.map +1 -1
- package/package.json +15 -15
- package/dist/chunk-EQ3T4XHT.cjs +0 -1583
- package/dist/chunk-EQ3T4XHT.cjs.map +0 -1
- package/dist/chunk-H6YIW5C5.js +0 -399
- package/dist/chunk-H6YIW5C5.js.map +0 -1
- package/dist/chunk-NTHMSHNG.cjs +0 -399
- package/dist/chunk-NTHMSHNG.cjs.map +0 -1
- package/dist/chunk-NWYI26VT.js +0 -1583
- package/dist/chunk-NWYI26VT.js.map +0 -1
- package/dist/index-BfBszcG4.d.ts +0 -8
- package/dist/index-a-fxODW1.d.cts +0 -8
- package/dist/pongoTransactionCache-CRuPGag3.d.cts +0 -556
- package/dist/pongoTransactionCache-CRuPGag3.d.ts +0 -556
package/dist/pg.cjs
CHANGED
|
@@ -1,150 +1,88 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_core = require('./core-CwxxuUAn.cjs');
|
|
3
|
+
const require_cli = require('./cli.cjs');
|
|
4
|
+
let _event_driven_io_dumbo = require("@event-driven-io/dumbo");
|
|
5
|
+
let _event_driven_io_dumbo_pg = require("@event-driven-io/dumbo/pg");
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
case "$eq": {
|
|
28
|
-
const nestedPath = serializer.serialize(buildNestedObject(path, value));
|
|
29
|
-
const serializedValue = serializer.serialize(value);
|
|
30
|
-
return _dumbo.SQL`(data @> ${nestedPath}::jsonb OR jsonb_path_exists(data, '$.${_dumbo.SQL.plain(path)}[*] ? (@ == ${_dumbo.SQL.plain(serializedValue)})'))`;
|
|
31
|
-
}
|
|
32
|
-
case "$gt":
|
|
33
|
-
case "$gte":
|
|
34
|
-
case "$lt":
|
|
35
|
-
case "$lte":
|
|
36
|
-
case "$ne": {
|
|
37
|
-
const jsonPath = _dumbo.SQL.plain(path.split(".").join(","));
|
|
38
|
-
return _dumbo.SQL`data ->> '${jsonPath}' ${_dumbo.SQL.plain(_chunkEQ3T4XHTcjs.OperatorMap[operator])} ${value}`;
|
|
39
|
-
}
|
|
40
|
-
case "$in": {
|
|
41
|
-
const jsonPath = `{${path.split(".").join(",")}}`;
|
|
42
|
-
return _dumbo.SQL`data #>> ${jsonPath} = ANY (${value})`;
|
|
43
|
-
}
|
|
44
|
-
case "$nin": {
|
|
45
|
-
const jsonPath = `{${path.split(".").join(",")}}`;
|
|
46
|
-
return _dumbo.SQL`data #>> ${jsonPath} != ALL (${value})`;
|
|
47
|
-
}
|
|
48
|
-
case "$elemMatch": {
|
|
49
|
-
const subQuery = _chunkEQ3T4XHTcjs.objectEntries.call(void 0, value).map(
|
|
50
|
-
([subKey, subValue]) => `@."${subKey}" == ${serializer.serialize(subValue)}`
|
|
51
|
-
).join(" && ");
|
|
52
|
-
return _dumbo.SQL`jsonb_path_exists(data, '$.${_dumbo.SQL.plain(path)}[*] ? (${_dumbo.SQL.plain(subQuery)})')`;
|
|
53
|
-
}
|
|
54
|
-
case "$all": {
|
|
55
|
-
const nestedPath = serializer.serialize(buildNestedObject(path, value));
|
|
56
|
-
return _dumbo.SQL`data @> ${nestedPath}::jsonb`;
|
|
57
|
-
}
|
|
58
|
-
case "$size": {
|
|
59
|
-
const jsonPath = `{${path.split(".").join(",")}}`;
|
|
60
|
-
return _dumbo.SQL`jsonb_array_length(data #> ${jsonPath}) = ${value}`;
|
|
61
|
-
}
|
|
62
|
-
default:
|
|
63
|
-
throw new Error(`Unsupported operator: ${operator}`);
|
|
64
|
-
}
|
|
7
|
+
//#region src/storage/postgresql/core/sqlBuilder/filter/queryOperators.ts
|
|
8
|
+
const handleOperator = (path, operator, value, serializer) => {
|
|
9
|
+
if (path === "_id" || path === "_version") return handleMetadataOperator(path, operator, value);
|
|
10
|
+
switch (operator) {
|
|
11
|
+
case "$eq": {
|
|
12
|
+
const nestedPath = serializer.serialize(buildNestedObject(path, value));
|
|
13
|
+
const serializedValue = serializer.serialize(value);
|
|
14
|
+
return _event_driven_io_dumbo.SQL`(data @> ${nestedPath}::jsonb OR jsonb_path_exists(data, '$.${_event_driven_io_dumbo.SQL.plain(path)}[*] ? (@ == ${_event_driven_io_dumbo.SQL.plain(serializedValue)})'))`;
|
|
15
|
+
}
|
|
16
|
+
case "$gt":
|
|
17
|
+
case "$gte":
|
|
18
|
+
case "$lt":
|
|
19
|
+
case "$lte":
|
|
20
|
+
case "$ne": return _event_driven_io_dumbo.SQL`data ->> '${_event_driven_io_dumbo.SQL.plain(path.split(".").join(","))}' ${_event_driven_io_dumbo.SQL.plain(require_core.OperatorMap[operator])} ${value}`;
|
|
21
|
+
case "$in": return _event_driven_io_dumbo.SQL`data #>> ${`{${path.split(".").join(",")}}`} = ANY (${value})`;
|
|
22
|
+
case "$nin": return _event_driven_io_dumbo.SQL`data #>> ${`{${path.split(".").join(",")}}`} != ALL (${value})`;
|
|
23
|
+
case "$elemMatch": {
|
|
24
|
+
const subQuery = require_core.objectEntries(value).map(([subKey, subValue]) => `@."${subKey}" == ${serializer.serialize(subValue)}`).join(" && ");
|
|
25
|
+
return _event_driven_io_dumbo.SQL`jsonb_path_exists(data, '$.${_event_driven_io_dumbo.SQL.plain(path)}[*] ? (${_event_driven_io_dumbo.SQL.plain(subQuery)})')`;
|
|
26
|
+
}
|
|
27
|
+
case "$all": return _event_driven_io_dumbo.SQL`data @> ${serializer.serialize(buildNestedObject(path, value))}::jsonb`;
|
|
28
|
+
case "$size": return _event_driven_io_dumbo.SQL`jsonb_array_length(data #> ${`{${path.split(".").join(",")}}`}) = ${value}`;
|
|
29
|
+
default: throw new Error(`Unsupported operator: ${operator}`);
|
|
30
|
+
}
|
|
65
31
|
};
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
case "$nin":
|
|
79
|
-
return _dumbo.SQL`${_dumbo.SQL.plain(fieldName)} != ALL (${value})`;
|
|
80
|
-
default:
|
|
81
|
-
throw new Error(`Unsupported operator: ${operator}`);
|
|
82
|
-
}
|
|
32
|
+
const handleMetadataOperator = (fieldName, operator, value) => {
|
|
33
|
+
switch (operator) {
|
|
34
|
+
case "$eq": return _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.plain(fieldName)} = ${value}`;
|
|
35
|
+
case "$gt":
|
|
36
|
+
case "$gte":
|
|
37
|
+
case "$lt":
|
|
38
|
+
case "$lte":
|
|
39
|
+
case "$ne": return _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.plain(fieldName)} ${_event_driven_io_dumbo.SQL.plain(require_core.OperatorMap[operator])} ${value}`;
|
|
40
|
+
case "$in": return _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.plain(fieldName)} = ANY (${value})`;
|
|
41
|
+
case "$nin": return _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.plain(fieldName)} != ALL (${value})`;
|
|
42
|
+
default: throw new Error(`Unsupported operator: ${operator}`);
|
|
43
|
+
}
|
|
83
44
|
};
|
|
84
|
-
|
|
45
|
+
const buildNestedObject = (path, value) => path.split(".").reverse().reduce((acc, key) => ({ [key]: acc }), value);
|
|
85
46
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
);
|
|
94
|
-
var constructComplexFilterQuery = (key, value, serializer) => {
|
|
95
|
-
const isEquality = !_chunkEQ3T4XHTcjs.hasOperators.call(void 0, value);
|
|
96
|
-
return _dumbo.SQL.merge(
|
|
97
|
-
_chunkEQ3T4XHTcjs.objectEntries.call(void 0, value).map(
|
|
98
|
-
([nestedKey, val]) => isEquality ? handleOperator(
|
|
99
|
-
`${key}.${nestedKey}`,
|
|
100
|
-
_chunkEQ3T4XHTcjs.QueryOperators.$eq,
|
|
101
|
-
val,
|
|
102
|
-
serializer
|
|
103
|
-
) : handleOperator(key, nestedKey, val, serializer)
|
|
104
|
-
),
|
|
105
|
-
` ${AND} `
|
|
106
|
-
);
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/storage/postgresql/core/sqlBuilder/filter/index.ts
|
|
49
|
+
const AND = "AND";
|
|
50
|
+
const constructFilterQuery = (filter, serializer) => _event_driven_io_dumbo.SQL.merge(Object.entries(filter).map(([key, value]) => isRecord(value) ? constructComplexFilterQuery(key, value, serializer) : handleOperator(key, "$eq", value, serializer)), ` ${AND} `);
|
|
51
|
+
const constructComplexFilterQuery = (key, value, serializer) => {
|
|
52
|
+
const isEquality = !require_core.hasOperators(value);
|
|
53
|
+
return _event_driven_io_dumbo.SQL.merge(require_core.objectEntries(value).map(([nestedKey, val]) => isEquality ? handleOperator(`${key}.${nestedKey}`, require_core.QueryOperators.$eq, val, serializer) : handleOperator(key, nestedKey, val, serializer)), ` ${AND} `);
|
|
107
54
|
};
|
|
108
|
-
|
|
55
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
109
56
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
},
|
|
127
|
-
_dumbo.SQL`data`
|
|
128
|
-
);
|
|
129
|
-
var buildSetQuery = (set, currentUpdateQuery, serializer) => _dumbo.SQL`${currentUpdateQuery} || ${serializer.serialize(set)}::jsonb`;
|
|
130
|
-
var buildUnsetQuery = (unset, currentUpdateQuery) => _dumbo.SQL`${currentUpdateQuery} - ${Object.keys(unset).map((k) => `{${k}}`).join(", ")}`;
|
|
131
|
-
var buildIncQuery = (inc, currentUpdateQuery) => {
|
|
132
|
-
for (const [key, value] of Object.entries(inc)) {
|
|
133
|
-
currentUpdateQuery = typeof value === "bigint" ? _dumbo.SQL`jsonb_set(${currentUpdateQuery}, '{${_dumbo.SQL.plain(key)}}', to_jsonb((COALESCE((data->>'${_dumbo.SQL.plain(key)}')::BIGINT, 0) + ${value})::TEXT), true)` : _dumbo.SQL`jsonb_set(${currentUpdateQuery}, '{${_dumbo.SQL.plain(key)}}', to_jsonb(COALESCE((data->>'${_dumbo.SQL.plain(key)}')::NUMERIC, 0) + ${value}), true)`;
|
|
134
|
-
}
|
|
135
|
-
return currentUpdateQuery;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/storage/postgresql/core/sqlBuilder/update/index.ts
|
|
59
|
+
const buildUpdateQuery = (update, serializer) => require_core.objectEntries(update).reduce((currentUpdateQuery, [op, value]) => {
|
|
60
|
+
switch (op) {
|
|
61
|
+
case "$set": return buildSetQuery(value, currentUpdateQuery, serializer);
|
|
62
|
+
case "$unset": return buildUnsetQuery(value, currentUpdateQuery);
|
|
63
|
+
case "$inc": return buildIncQuery(value, currentUpdateQuery);
|
|
64
|
+
case "$push": return buildPushQuery(value, currentUpdateQuery, serializer);
|
|
65
|
+
default: return currentUpdateQuery;
|
|
66
|
+
}
|
|
67
|
+
}, _event_driven_io_dumbo.SQL`data`);
|
|
68
|
+
const buildSetQuery = (set, currentUpdateQuery, serializer) => _event_driven_io_dumbo.SQL`${currentUpdateQuery} || ${serializer.serialize(set)}::jsonb`;
|
|
69
|
+
const buildUnsetQuery = (unset, currentUpdateQuery) => _event_driven_io_dumbo.SQL`${currentUpdateQuery} - ${Object.keys(unset).map((k) => `{${k}}`).join(", ")}`;
|
|
70
|
+
const buildIncQuery = (inc, currentUpdateQuery) => {
|
|
71
|
+
for (const [key, value] of Object.entries(inc)) currentUpdateQuery = typeof value === "bigint" ? _event_driven_io_dumbo.SQL`jsonb_set(${currentUpdateQuery}, '{${_event_driven_io_dumbo.SQL.plain(key)}}', to_jsonb((COALESCE((data->>'${_event_driven_io_dumbo.SQL.plain(key)}')::BIGINT, 0) + ${value})::TEXT), true)` : _event_driven_io_dumbo.SQL`jsonb_set(${currentUpdateQuery}, '{${_event_driven_io_dumbo.SQL.plain(key)}}', to_jsonb(COALESCE((data->>'${_event_driven_io_dumbo.SQL.plain(key)}')::NUMERIC, 0) + ${value}), true)`;
|
|
72
|
+
return currentUpdateQuery;
|
|
136
73
|
};
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
74
|
+
const buildPushQuery = (push, currentUpdateQuery, serializer) => {
|
|
75
|
+
for (const [key, value] of Object.entries(push)) {
|
|
76
|
+
const serializedValue = serializer.serialize([value]);
|
|
77
|
+
currentUpdateQuery = _event_driven_io_dumbo.SQL`jsonb_set(${currentUpdateQuery}, '{${_event_driven_io_dumbo.SQL.plain(key)}}', (coalesce(data->'${_event_driven_io_dumbo.SQL.plain(key)}', '[]'::jsonb) || ${serializedValue}::jsonb), true)`;
|
|
78
|
+
}
|
|
79
|
+
return currentUpdateQuery;
|
|
143
80
|
};
|
|
144
81
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/storage/postgresql/core/sqlBuilder/index.ts
|
|
84
|
+
const createCollection = (collectionName) => _event_driven_io_dumbo.SQL`
|
|
85
|
+
CREATE TABLE IF NOT EXISTS ${_event_driven_io_dumbo.SQL.identifier(collectionName)} (
|
|
148
86
|
_id TEXT PRIMARY KEY,
|
|
149
87
|
data JSONB NOT NULL,
|
|
150
88
|
metadata JSONB NOT NULL DEFAULT '{}',
|
|
@@ -154,52 +92,43 @@ var createCollection = (collectionName) => _dumbo.SQL`
|
|
|
154
92
|
_created TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
155
93
|
_updated TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
156
94
|
)`;
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const id = document._id;
|
|
167
|
-
const version = _nullishCoalesce(document._version, () => ( 1n));
|
|
168
|
-
return _dumbo.SQL`
|
|
169
|
-
INSERT INTO ${_dumbo.SQL.identifier(collectionName)} (_id, data, _version)
|
|
95
|
+
const pongoCollectionPostgreSQLMigrations = (collectionName) => [(0, _event_driven_io_dumbo.sqlMigration)(`pongoCollection:${collectionName}:001:createtable`, [createCollection(collectionName)])];
|
|
96
|
+
const postgresSQLBuilder = (collectionName, serializer) => ({
|
|
97
|
+
createCollection: () => createCollection(collectionName),
|
|
98
|
+
insertOne: (document) => {
|
|
99
|
+
const serialized = serializer.serialize(document);
|
|
100
|
+
const id = document._id;
|
|
101
|
+
const version = document._version ?? 1n;
|
|
102
|
+
return _event_driven_io_dumbo.SQL`
|
|
103
|
+
INSERT INTO ${_event_driven_io_dumbo.SQL.identifier(collectionName)} (_id, data, _version)
|
|
170
104
|
VALUES (${id}, ${serialized}, ${version}) ON CONFLICT(_id) DO NOTHING;`;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
),
|
|
177
|
-
","
|
|
178
|
-
);
|
|
179
|
-
return _dumbo.SQL`
|
|
180
|
-
INSERT INTO ${_dumbo.SQL.identifier(collectionName)} (_id, data, _version) VALUES ${values}
|
|
105
|
+
},
|
|
106
|
+
insertMany: (documents) => {
|
|
107
|
+
const values = _event_driven_io_dumbo.SQL.merge(documents.map((doc) => _event_driven_io_dumbo.SQL`(${doc._id}, ${serializer.serialize(doc)}, ${doc._version ?? 1n})`), ",");
|
|
108
|
+
return _event_driven_io_dumbo.SQL`
|
|
109
|
+
INSERT INTO ${_event_driven_io_dumbo.SQL.identifier(collectionName)} (_id, data, _version) VALUES ${values}
|
|
181
110
|
ON CONFLICT(_id) DO NOTHING
|
|
182
111
|
RETURNING _id;`;
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
112
|
+
},
|
|
113
|
+
updateOne: (filter, update, options) => {
|
|
114
|
+
const expectedVersion = require_core.expectedVersionValue(options?.expectedVersion);
|
|
115
|
+
const expectedVersionUpdate = expectedVersion != null ? _event_driven_io_dumbo.SQL`AND ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._version = ${expectedVersion}` : _event_driven_io_dumbo.SQL``;
|
|
116
|
+
const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
|
|
117
|
+
const updateQuery = (0, _event_driven_io_dumbo.isSQL)(update) ? update : buildUpdateQuery(update, serializer);
|
|
118
|
+
return _event_driven_io_dumbo.SQL`
|
|
190
119
|
WITH existing AS (
|
|
191
120
|
SELECT _id, _version as current_version
|
|
192
|
-
FROM ${
|
|
121
|
+
FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)}
|
|
193
122
|
LIMIT 1
|
|
194
123
|
),
|
|
195
124
|
updated AS (
|
|
196
|
-
UPDATE ${
|
|
125
|
+
UPDATE ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
|
|
197
126
|
SET
|
|
198
|
-
data = ${updateQuery} || jsonb_build_object('_id', ${
|
|
127
|
+
data = ${updateQuery} || jsonb_build_object('_id', ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._id) || jsonb_build_object('_version', (_version + 1)::text),
|
|
199
128
|
_version = _version + 1
|
|
200
129
|
FROM existing
|
|
201
|
-
WHERE ${
|
|
202
|
-
RETURNING ${
|
|
130
|
+
WHERE ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._id = existing._id ${expectedVersionUpdate}
|
|
131
|
+
RETURNING ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._id, ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._version
|
|
203
132
|
)
|
|
204
133
|
SELECT
|
|
205
134
|
existing._id,
|
|
@@ -209,25 +138,25 @@ var postgresSQLBuilder = (collectionName, serializer) => ({
|
|
|
209
138
|
FROM existing
|
|
210
139
|
LEFT JOIN updated
|
|
211
140
|
ON existing._id = updated._id;`;
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
141
|
+
},
|
|
142
|
+
replaceOne: (filter, document, options) => {
|
|
143
|
+
const expectedVersion = require_core.expectedVersionValue(options?.expectedVersion);
|
|
144
|
+
const expectedVersionUpdate = expectedVersion != null ? _event_driven_io_dumbo.SQL`AND ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._version = ${expectedVersion}` : _event_driven_io_dumbo.SQL``;
|
|
145
|
+
const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
|
|
146
|
+
return _event_driven_io_dumbo.SQL`
|
|
218
147
|
WITH existing AS (
|
|
219
148
|
SELECT _id, _version as current_version
|
|
220
|
-
FROM ${
|
|
149
|
+
FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)}
|
|
221
150
|
LIMIT 1
|
|
222
151
|
),
|
|
223
152
|
updated AS (
|
|
224
|
-
UPDATE ${
|
|
153
|
+
UPDATE ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
|
|
225
154
|
SET
|
|
226
|
-
data = ${serializer.serialize(document)} || jsonb_build_object('_id', ${
|
|
155
|
+
data = ${serializer.serialize(document)} || jsonb_build_object('_id', ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._id) || jsonb_build_object('_version', (_version + 1)::text),
|
|
227
156
|
_version = _version + 1
|
|
228
157
|
FROM existing
|
|
229
|
-
WHERE ${
|
|
230
|
-
RETURNING ${
|
|
158
|
+
WHERE ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._id = existing._id ${expectedVersionUpdate}
|
|
159
|
+
RETURNING ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._id, ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._version
|
|
231
160
|
)
|
|
232
161
|
SELECT
|
|
233
162
|
existing._id,
|
|
@@ -237,32 +166,32 @@ var postgresSQLBuilder = (collectionName, serializer) => ({
|
|
|
237
166
|
FROM existing
|
|
238
167
|
LEFT JOIN updated
|
|
239
168
|
ON existing._id = updated._id;`;
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
UPDATE ${
|
|
169
|
+
},
|
|
170
|
+
updateMany: (filter, update) => {
|
|
171
|
+
const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
|
|
172
|
+
const updateQuery = (0, _event_driven_io_dumbo.isSQL)(update) ? update : buildUpdateQuery(update, serializer);
|
|
173
|
+
return _event_driven_io_dumbo.SQL`
|
|
174
|
+
UPDATE ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
|
|
246
175
|
SET
|
|
247
176
|
data = ${updateQuery} || jsonb_build_object('_version', (_version + 1)::text),
|
|
248
177
|
_version = _version + 1
|
|
249
178
|
${where(filterQuery)};`;
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
179
|
+
},
|
|
180
|
+
deleteOne: (filter, options) => {
|
|
181
|
+
const expectedVersion = require_core.expectedVersionValue(options?.expectedVersion);
|
|
182
|
+
const expectedVersionUpdate = expectedVersion != null ? _event_driven_io_dumbo.SQL`AND ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._version = ${expectedVersion}` : _event_driven_io_dumbo.SQL``;
|
|
183
|
+
const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
|
|
184
|
+
return _event_driven_io_dumbo.SQL`
|
|
256
185
|
WITH existing AS (
|
|
257
186
|
SELECT _id
|
|
258
|
-
FROM ${
|
|
187
|
+
FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)}
|
|
259
188
|
LIMIT 1
|
|
260
189
|
),
|
|
261
190
|
deleted AS (
|
|
262
|
-
DELETE FROM ${
|
|
191
|
+
DELETE FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)}
|
|
263
192
|
USING existing
|
|
264
|
-
WHERE ${
|
|
265
|
-
RETURNING ${
|
|
193
|
+
WHERE ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._id = existing._id ${expectedVersionUpdate}
|
|
194
|
+
RETURNING ${_event_driven_io_dumbo.SQL.identifier(collectionName)}._id
|
|
266
195
|
)
|
|
267
196
|
SELECT
|
|
268
197
|
existing._id,
|
|
@@ -271,27 +200,17 @@ var postgresSQLBuilder = (collectionName, serializer) => ({
|
|
|
271
200
|
FROM existing
|
|
272
201
|
LEFT JOIN deleted
|
|
273
202
|
ON existing._id = deleted._id;`;
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
(d) => "_version" in d && d._version !== void 0
|
|
282
|
-
);
|
|
283
|
-
if (hasVersions) {
|
|
284
|
-
const values2 = _dumbo.SQL.merge(
|
|
285
|
-
documents.map(
|
|
286
|
-
(d) => _dumbo.SQL`(${d._id}::text, ${serializer.serialize(d)}::jsonb, ${_nullishCoalesce(d._version, () => ( 0n))}::bigint)`
|
|
287
|
-
),
|
|
288
|
-
","
|
|
289
|
-
);
|
|
290
|
-
return _dumbo.SQL`
|
|
203
|
+
},
|
|
204
|
+
deleteMany: (filter) => {
|
|
205
|
+
const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
|
|
206
|
+
return _event_driven_io_dumbo.SQL`DELETE FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)}`;
|
|
207
|
+
},
|
|
208
|
+
replaceMany: (documents) => {
|
|
209
|
+
if (documents.some((d) => "_version" in d && d._version !== void 0)) return _event_driven_io_dumbo.SQL`
|
|
291
210
|
WITH replacements(_id, data, expected_version) AS (
|
|
292
|
-
VALUES ${
|
|
211
|
+
VALUES ${_event_driven_io_dumbo.SQL.merge(documents.map((d) => _event_driven_io_dumbo.SQL`(${d._id}::text, ${serializer.serialize(d)}::jsonb, ${d._version ?? 0n}::bigint)`), ",")}
|
|
293
212
|
)
|
|
294
|
-
UPDATE ${
|
|
213
|
+
UPDATE ${_event_driven_io_dumbo.SQL.identifier(collectionName)} t
|
|
295
214
|
SET
|
|
296
215
|
data = r.data
|
|
297
216
|
|| jsonb_build_object('_id', t._id)
|
|
@@ -300,18 +219,11 @@ var postgresSQLBuilder = (collectionName, serializer) => ({
|
|
|
300
219
|
FROM replacements r
|
|
301
220
|
WHERE t._id = r._id AND t._version = r.expected_version
|
|
302
221
|
RETURNING t._id, t._version AS version;`;
|
|
303
|
-
|
|
304
|
-
const values = _dumbo.SQL.merge(
|
|
305
|
-
documents.map(
|
|
306
|
-
(d) => _dumbo.SQL`(${d._id}::text, ${serializer.serialize(d)}::jsonb)`
|
|
307
|
-
),
|
|
308
|
-
","
|
|
309
|
-
);
|
|
310
|
-
return _dumbo.SQL`
|
|
222
|
+
return _event_driven_io_dumbo.SQL`
|
|
311
223
|
WITH replacements(_id, data) AS (
|
|
312
|
-
VALUES ${
|
|
224
|
+
VALUES ${_event_driven_io_dumbo.SQL.merge(documents.map((d) => _event_driven_io_dumbo.SQL`(${d._id}::text, ${serializer.serialize(d)}::jsonb)`), ",")}
|
|
313
225
|
)
|
|
314
|
-
UPDATE ${
|
|
226
|
+
UPDATE ${_event_driven_io_dumbo.SQL.identifier(collectionName)} t
|
|
315
227
|
SET
|
|
316
228
|
data = r.data
|
|
317
229
|
|| jsonb_build_object('_id', t._id)
|
|
@@ -320,20 +232,14 @@ var postgresSQLBuilder = (collectionName, serializer) => ({
|
|
|
320
232
|
FROM replacements r
|
|
321
233
|
WHERE t._id = r._id
|
|
322
234
|
RETURNING t._id, t._version AS version;`;
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if (hasVersions) {
|
|
327
|
-
const values2 = _dumbo.SQL.merge(
|
|
328
|
-
ids.map((d) => _dumbo.SQL`(${d._id}::text, ${_nullishCoalesce(d._version, () => ( 0n))}::bigint)`),
|
|
329
|
-
","
|
|
330
|
-
);
|
|
331
|
-
return _dumbo.SQL`
|
|
235
|
+
},
|
|
236
|
+
deleteManyByIds: (ids) => {
|
|
237
|
+
if (ids.some((d) => d._version !== void 0)) return _event_driven_io_dumbo.SQL`
|
|
332
238
|
WITH targets(_id, expected_version) AS (
|
|
333
|
-
VALUES ${
|
|
239
|
+
VALUES ${_event_driven_io_dumbo.SQL.merge(ids.map((d) => _event_driven_io_dumbo.SQL`(${d._id}::text, ${d._version ?? 0n}::bigint)`), ",")}
|
|
334
240
|
),
|
|
335
241
|
deleted AS (
|
|
336
|
-
DELETE FROM ${
|
|
242
|
+
DELETE FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} t
|
|
337
243
|
USING targets r
|
|
338
244
|
WHERE t._id = r._id AND t._version = r.expected_version
|
|
339
245
|
RETURNING t._id
|
|
@@ -342,17 +248,12 @@ var postgresSQLBuilder = (collectionName, serializer) => ({
|
|
|
342
248
|
CASE WHEN d._id IS NOT NULL THEN 1 ELSE 0 END as deleted
|
|
343
249
|
FROM targets r
|
|
344
250
|
LEFT JOIN deleted d ON r._id = d._id;`;
|
|
345
|
-
|
|
346
|
-
const values = _dumbo.SQL.merge(
|
|
347
|
-
ids.map((d) => _dumbo.SQL`(${d._id}::text)`),
|
|
348
|
-
","
|
|
349
|
-
);
|
|
350
|
-
return _dumbo.SQL`
|
|
251
|
+
return _event_driven_io_dumbo.SQL`
|
|
351
252
|
WITH targets(_id) AS (
|
|
352
|
-
VALUES ${
|
|
253
|
+
VALUES ${_event_driven_io_dumbo.SQL.merge(ids.map((d) => _event_driven_io_dumbo.SQL`(${d._id}::text)`), ",")}
|
|
353
254
|
),
|
|
354
255
|
deleted AS (
|
|
355
|
-
DELETE FROM ${
|
|
256
|
+
DELETE FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} t
|
|
356
257
|
USING targets r
|
|
357
258
|
WHERE t._id = r._id
|
|
358
259
|
RETURNING t._id
|
|
@@ -361,82 +262,75 @@ var postgresSQLBuilder = (collectionName, serializer) => ({
|
|
|
361
262
|
CASE WHEN d._id IS NOT NULL THEN 1 ELSE 0 END as deleted
|
|
362
263
|
FROM targets r
|
|
363
264
|
LEFT JOIN deleted d ON r._id = d._id;`;
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
265
|
+
},
|
|
266
|
+
findOne: (filter) => {
|
|
267
|
+
const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
|
|
268
|
+
return _event_driven_io_dumbo.SQL`SELECT data, _version FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)} LIMIT 1;`;
|
|
269
|
+
},
|
|
270
|
+
find: (filter, options) => {
|
|
271
|
+
const filterQuery = (0, _event_driven_io_dumbo.isSQL)(filter) ? filter : constructFilterQuery(filter, serializer);
|
|
272
|
+
const query = [];
|
|
273
|
+
query.push(_event_driven_io_dumbo.SQL`SELECT data, _version FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)}`);
|
|
274
|
+
query.push(where(filterQuery));
|
|
275
|
+
if (options?.sort && Object.keys(options.sort).length > 0) {
|
|
276
|
+
const clauses = Object.entries(options.sort).map(([field, dir]) => {
|
|
277
|
+
const isMetadata = field === "_id" || field === "_version";
|
|
278
|
+
const isNested = !isMetadata && field.includes(".");
|
|
279
|
+
const accessor = isMetadata ? _event_driven_io_dumbo.SQL`${_event_driven_io_dumbo.SQL.plain(field)}` : isNested ? _event_driven_io_dumbo.SQL`data #> '${_event_driven_io_dumbo.SQL.plain(`{${field.split(".").join(",")}}`)}'` : _event_driven_io_dumbo.SQL`data -> '${_event_driven_io_dumbo.SQL.plain(field)}'`;
|
|
280
|
+
return dir === 1 ? _event_driven_io_dumbo.SQL`${accessor} ASC NULLS FIRST` : _event_driven_io_dumbo.SQL`${accessor} DESC NULLS LAST`;
|
|
281
|
+
});
|
|
282
|
+
query.push(_event_driven_io_dumbo.SQL`ORDER BY ${_event_driven_io_dumbo.SQL.merge(clauses, ",")}`);
|
|
283
|
+
}
|
|
284
|
+
if (options?.limit) query.push(_event_driven_io_dumbo.SQL`LIMIT ${options.limit}`);
|
|
285
|
+
if (options?.skip) query.push(_event_driven_io_dumbo.SQL`OFFSET ${options.skip}`);
|
|
286
|
+
return _event_driven_io_dumbo.SQL.merge([...query, _event_driven_io_dumbo.SQL`;`]);
|
|
287
|
+
},
|
|
288
|
+
countDocuments: (filter) => {
|
|
289
|
+
const filterQuery = _event_driven_io_dumbo.SQL.check.isSQL(filter) ? filter : constructFilterQuery(filter, serializer);
|
|
290
|
+
return _event_driven_io_dumbo.SQL`SELECT COUNT(1) as count FROM ${_event_driven_io_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)};`;
|
|
291
|
+
},
|
|
292
|
+
rename: (newName) => _event_driven_io_dumbo.SQL`ALTER TABLE ${_event_driven_io_dumbo.SQL.identifier(collectionName)} RENAME TO ${_event_driven_io_dumbo.SQL.identifier(newName)};`,
|
|
293
|
+
drop: (targetName = collectionName) => _event_driven_io_dumbo.SQL`DROP TABLE IF EXISTS ${_event_driven_io_dumbo.SQL.identifier(targetName)}`
|
|
390
294
|
});
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
// src/storage/postgresql/pg/index.ts
|
|
394
|
-
|
|
395
|
-
|
|
295
|
+
const where = (filterQuery) => _event_driven_io_dumbo.SQL.check.isEmpty(filterQuery) ? _event_driven_io_dumbo.SQL.EMPTY : _event_driven_io_dumbo.SQL.merge([_event_driven_io_dumbo.SQL`WHERE `, filterQuery]);
|
|
396
296
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}),
|
|
425
|
-
definition: _nullishCoalesce(_optionalChain([options, 'access', _8 => _8.schema, 'optionalAccess', _9 => _9.definition]), () => ( _chunkEQ3T4XHTcjs.pongoSchema.db(databaseName, {})))
|
|
426
|
-
}),
|
|
427
|
-
databaseName
|
|
428
|
-
});
|
|
429
|
-
}
|
|
297
|
+
//#endregion
|
|
298
|
+
//#region src/storage/postgresql/pg/index.ts
|
|
299
|
+
const pgPongoDriver = {
|
|
300
|
+
driverType: _event_driven_io_dumbo_pg.PgDriverType,
|
|
301
|
+
databaseFactory: (options) => {
|
|
302
|
+
const databaseName = options.databaseName ?? _event_driven_io_dumbo_pg.postgreSQLMetadata.parseDatabaseName(options.connectionString) ?? _event_driven_io_dumbo_pg.postgreSQLMetadata.defaultDatabaseName;
|
|
303
|
+
return require_core.PongoDatabase({
|
|
304
|
+
...options,
|
|
305
|
+
pool: (0, _event_driven_io_dumbo.dumbo)({
|
|
306
|
+
connectionString: options.connectionString,
|
|
307
|
+
driver: _event_driven_io_dumbo_pg.pgDumboDriver,
|
|
308
|
+
...options.connectionOptions,
|
|
309
|
+
serialization: { serializer: options.serializer }
|
|
310
|
+
}),
|
|
311
|
+
schemaComponent: require_core.PongoDatabaseSchemaComponent({
|
|
312
|
+
driverType: _event_driven_io_dumbo_pg.PgDriverType,
|
|
313
|
+
collectionFactory: (schema) => require_core.PongoCollectionSchemaComponent({
|
|
314
|
+
driverType: _event_driven_io_dumbo_pg.PgDriverType,
|
|
315
|
+
definition: schema,
|
|
316
|
+
migrationsOrSchemaComponents: { migrations: pongoCollectionPostgreSQLMigrations(schema.name) },
|
|
317
|
+
sqlBuilder: postgresSQLBuilder(schema.name, options.serialization?.serializer ?? _event_driven_io_dumbo.JSONSerializer)
|
|
318
|
+
}),
|
|
319
|
+
definition: options.schema?.definition ?? require_core.pongoSchema.db(databaseName, {})
|
|
320
|
+
}),
|
|
321
|
+
databaseName
|
|
322
|
+
});
|
|
323
|
+
}
|
|
430
324
|
};
|
|
431
|
-
|
|
432
|
-
|
|
325
|
+
const usePgPongoDriver = () => {
|
|
326
|
+
require_core.pongoDriverRegistry.register(_event_driven_io_dumbo_pg.PgDriverType, pgPongoDriver);
|
|
433
327
|
};
|
|
434
328
|
usePgPongoDriver();
|
|
435
329
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
exports.
|
|
330
|
+
//#endregion
|
|
331
|
+
exports.pgDriver = pgPongoDriver;
|
|
332
|
+
exports.pongoDriver = pgPongoDriver;
|
|
333
|
+
exports.pongoCollectionPostgreSQLMigrations = pongoCollectionPostgreSQLMigrations;
|
|
334
|
+
exports.postgresSQLBuilder = postgresSQLBuilder;
|
|
335
|
+
exports.usePgPongoDriver = usePgPongoDriver;
|
|
442
336
|
//# sourceMappingURL=pg.cjs.map
|