@event-driven-io/pongo 0.17.0-beta.2 → 0.17.0-beta.21
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-ECQ2CKZE.cjs → chunk-A4DCNQJR.cjs} +45 -43
- package/dist/chunk-A4DCNQJR.cjs.map +1 -0
- package/dist/{chunk-4BL6YWLW.cjs → chunk-BZRKCNRY.cjs} +105 -72
- package/dist/chunk-BZRKCNRY.cjs.map +1 -0
- package/dist/{chunk-NCNRRYVE.js → chunk-H637RRXS.js} +85 -52
- package/dist/chunk-H637RRXS.js.map +1 -0
- package/dist/{chunk-Y7LRKJLJ.js → chunk-TTZGGAZV.js} +40 -38
- package/dist/chunk-TTZGGAZV.js.map +1 -0
- package/dist/cli.cjs +43 -37
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +34 -28
- package/dist/cli.js.map +1 -1
- package/dist/cloudflare.cjs +53 -0
- package/dist/cloudflare.cjs.map +1 -0
- package/dist/cloudflare.d.cts +11 -0
- package/dist/cloudflare.d.ts +11 -0
- package/dist/{d1.js → cloudflare.js} +19 -20
- package/dist/cloudflare.js.map +1 -0
- package/dist/{index-BJopB-em.d.cts → index-BAiq0hu3.d.cts} +3 -2
- package/dist/{index-G5DECNb_.d.ts → index-DF5J9nRH.d.ts} +3 -2
- package/dist/index.cjs +27 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -9
- package/dist/index.d.ts +17 -9
- package/dist/index.js +30 -6
- package/dist/index.js.map +1 -1
- package/dist/pg.cjs +344 -4
- package/dist/pg.cjs.map +1 -1
- package/dist/pg.d.cts +10 -9
- package/dist/pg.d.ts +10 -9
- package/dist/pg.js +350 -10
- package/dist/pg.js.map +1 -1
- package/dist/{pongoCollectionSchemaComponent-t_e9n2Wc.d.ts → pongoCollectionSchemaComponent-BhsINgDe.d.cts} +44 -34
- package/dist/{pongoCollectionSchemaComponent-t_e9n2Wc.d.cts → pongoCollectionSchemaComponent-BhsINgDe.d.ts} +44 -34
- package/dist/shim.cjs +12 -14
- package/dist/shim.cjs.map +1 -1
- package/dist/shim.d.cts +4 -4
- package/dist/shim.d.ts +4 -4
- package/dist/shim.js +7 -9
- package/dist/shim.js.map +1 -1
- package/dist/sqlite3.cjs +21 -26
- package/dist/sqlite3.cjs.map +1 -1
- package/dist/sqlite3.d.cts +6 -6
- package/dist/sqlite3.d.ts +6 -6
- package/dist/sqlite3.js +20 -25
- package/dist/sqlite3.js.map +1 -1
- package/package.json +50 -36
- package/dist/chunk-4BL6YWLW.cjs.map +0 -1
- package/dist/chunk-ECQ2CKZE.cjs.map +0 -1
- package/dist/chunk-EYQDS752.js +0 -364
- package/dist/chunk-EYQDS752.js.map +0 -1
- package/dist/chunk-NCNRRYVE.js.map +0 -1
- package/dist/chunk-WH26IXHN.js +0 -10
- package/dist/chunk-WH26IXHN.js.map +0 -1
- package/dist/chunk-WKW4LGF6.cjs +0 -10
- package/dist/chunk-WKW4LGF6.cjs.map +0 -1
- package/dist/chunk-Y7LRKJLJ.js.map +0 -1
- package/dist/chunk-ZPWKWNK2.cjs +0 -364
- package/dist/chunk-ZPWKWNK2.cjs.map +0 -1
- package/dist/d1.cjs +0 -54
- package/dist/d1.cjs.map +0 -1
- package/dist/d1.d.cts +0 -11
- package/dist/d1.d.ts +0 -11
- package/dist/d1.js.map +0 -1
- package/dist/pg-73DOKU64.js +0 -11
- package/dist/pg-73DOKU64.js.map +0 -1
- package/dist/pg-I267A7IL.cjs +0 -11
- package/dist/pg-I267A7IL.cjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "./chunk-WH26IXHN.js";
|
|
2
1
|
import {
|
|
3
2
|
ConcurrencyError,
|
|
4
3
|
DOCUMENT_DOES_NOT_EXIST,
|
|
@@ -9,8 +8,8 @@ import {
|
|
|
9
8
|
PongoCollectionSchemaComponent,
|
|
10
9
|
PongoDatabase,
|
|
11
10
|
PongoDatabaseCache,
|
|
12
|
-
PongoDatabaseDriverRegistry,
|
|
13
11
|
PongoDatabaseSchemaComponent,
|
|
12
|
+
PongoDriverRegistry,
|
|
14
13
|
PongoError,
|
|
15
14
|
QueryOperators,
|
|
16
15
|
deepEquals,
|
|
@@ -26,7 +25,7 @@ import {
|
|
|
26
25
|
operationResult,
|
|
27
26
|
pongoClient,
|
|
28
27
|
pongoCollection,
|
|
29
|
-
|
|
28
|
+
pongoDriverRegistry as pongoDriverRegistry2,
|
|
30
29
|
pongoSchema,
|
|
31
30
|
pongoSession,
|
|
32
31
|
pongoTransaction,
|
|
@@ -35,7 +34,31 @@ import {
|
|
|
35
34
|
toClientSchemaMetadata,
|
|
36
35
|
toDbSchemaMetadata,
|
|
37
36
|
transactionExecutorOrDefault
|
|
38
|
-
} from "./chunk-
|
|
37
|
+
} from "./chunk-H637RRXS.js";
|
|
38
|
+
|
|
39
|
+
// src/index.ts
|
|
40
|
+
pongoDriverRegistry.register(`PostgreSQL:pg`, () => loadPongoClient("pg"));
|
|
41
|
+
pongoDriverRegistry.register(
|
|
42
|
+
`SQLite:sqlite3`,
|
|
43
|
+
() => loadPongoClient("sqlite3")
|
|
44
|
+
);
|
|
45
|
+
pongoDriverRegistry.register(`SQLite:d1`, () => loadPongoClient("d1"));
|
|
46
|
+
var loadPongoClient = async (path) => {
|
|
47
|
+
let module;
|
|
48
|
+
if (path === "pg") {
|
|
49
|
+
module = await import("./pg.js");
|
|
50
|
+
} else if (path === "sqlite3") {
|
|
51
|
+
module = await import("./sqlite3.js");
|
|
52
|
+
} else if (path === "d1") {
|
|
53
|
+
module = await import("./cloudflare.js");
|
|
54
|
+
} else {
|
|
55
|
+
throw new Error(`Unknown path: ${path}`);
|
|
56
|
+
}
|
|
57
|
+
if (!module.pongoDriver) {
|
|
58
|
+
throw new Error(`Failed to load Pongo client for ${path}`);
|
|
59
|
+
}
|
|
60
|
+
return module.pongoDriver;
|
|
61
|
+
};
|
|
39
62
|
export {
|
|
40
63
|
ConcurrencyError,
|
|
41
64
|
DOCUMENT_DOES_NOT_EXIST,
|
|
@@ -46,8 +69,8 @@ export {
|
|
|
46
69
|
PongoCollectionSchemaComponent,
|
|
47
70
|
PongoDatabase,
|
|
48
71
|
PongoDatabaseCache,
|
|
49
|
-
PongoDatabaseDriverRegistry,
|
|
50
72
|
PongoDatabaseSchemaComponent,
|
|
73
|
+
PongoDriverRegistry,
|
|
51
74
|
PongoError,
|
|
52
75
|
QueryOperators,
|
|
53
76
|
deepEquals,
|
|
@@ -59,11 +82,12 @@ export {
|
|
|
59
82
|
isNumber,
|
|
60
83
|
isOperator,
|
|
61
84
|
isString,
|
|
85
|
+
loadPongoClient,
|
|
62
86
|
objectEntries,
|
|
63
87
|
operationResult,
|
|
64
88
|
pongoClient,
|
|
65
89
|
pongoCollection,
|
|
66
|
-
|
|
90
|
+
pongoDriverRegistry2 as pongoDriverRegistry,
|
|
67
91
|
pongoSchema,
|
|
68
92
|
pongoSession,
|
|
69
93
|
pongoTransaction,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { AnyPongoDriver } from './core';\n\nexport * from './core';\n\npongoDriverRegistry.register(`PostgreSQL:pg`, () => loadPongoClient('pg'));\npongoDriverRegistry.register(`SQLite:sqlite3`, () =>\n loadPongoClient('sqlite3'),\n);\npongoDriverRegistry.register(`SQLite:d1`, () => loadPongoClient('d1'));\n\nexport const loadPongoClient = async (\n path: 'pg' | 'sqlite3' | 'd1',\n): Promise<AnyPongoDriver> => {\n let module;\n\n if (path === 'pg') {\n module = await import('./pg');\n } else if (path === 'sqlite3') {\n module = await import('./sqlite3');\n } else if (path === 'd1') {\n module = await import('./cloudflare');\n } else {\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n throw new Error(`Unknown path: ${path}`);\n }\n\n if (!module.pongoDriver) {\n throw new Error(`Failed to load Pongo client for ${path}`);\n }\n\n return module.pongoDriver;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,oBAAoB,SAAS,iBAAiB,MAAM,gBAAgB,IAAI,CAAC;AACzE,oBAAoB;AAAA,EAAS;AAAA,EAAkB,MAC7C,gBAAgB,SAAS;AAC3B;AACA,oBAAoB,SAAS,aAAa,MAAM,gBAAgB,IAAI,CAAC;AAE9D,IAAM,kBAAkB,OAC7B,SAC4B;AAC5B,MAAI;AAEJ,MAAI,SAAS,MAAM;AACjB,aAAS,MAAM,OAAO,SAAM;AAAA,EAC9B,WAAW,SAAS,WAAW;AAC7B,aAAS,MAAM,OAAO,cAAW;AAAA,EACnC,WAAW,SAAS,MAAM;AACxB,aAAS,MAAM,OAAO,iBAAc;AAAA,EACtC,OAAO;AAEL,UAAM,IAAI,MAAM,iBAAiB,IAAI,EAAE;AAAA,EACzC;AAEA,MAAI,CAAC,OAAO,aAAa;AACvB,UAAM,IAAI,MAAM,mCAAmC,IAAI,EAAE;AAAA,EAC3D;AAEA,SAAO,OAAO;AAChB;","names":[]}
|
package/dist/pg.cjs
CHANGED
|
@@ -1,16 +1,356 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var _chunkZPWKWNK2cjs = require('./chunk-ZPWKWNK2.cjs');
|
|
8
|
-
require('./chunk-4BL6YWLW.cjs');
|
|
9
7
|
|
|
10
8
|
|
|
11
9
|
|
|
12
10
|
|
|
13
11
|
|
|
12
|
+
var _chunkBZRKCNRYcjs = require('./chunk-BZRKCNRY.cjs');
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
// src/storage/postgresql/core/sqlBuilder/index.ts
|
|
15
|
+
var _dumbo = require('@event-driven-io/dumbo');
|
|
16
|
+
|
|
17
|
+
// src/storage/postgresql/core/sqlBuilder/filter/index.ts
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
// src/storage/postgresql/core/sqlBuilder/filter/queryOperators.ts
|
|
21
|
+
|
|
22
|
+
var handleOperator = (path, operator, value, serializer) => {
|
|
23
|
+
if (path === "_id" || path === "_version") {
|
|
24
|
+
return handleMetadataOperator(path, operator, value);
|
|
25
|
+
}
|
|
26
|
+
switch (operator) {
|
|
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(_chunkBZRKCNRYcjs.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 = _chunkBZRKCNRYcjs.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
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var handleMetadataOperator = (fieldName, operator, value) => {
|
|
67
|
+
switch (operator) {
|
|
68
|
+
case "$eq":
|
|
69
|
+
return _dumbo.SQL`${_dumbo.SQL.plain(fieldName)} = ${value}`;
|
|
70
|
+
case "$gt":
|
|
71
|
+
case "$gte":
|
|
72
|
+
case "$lt":
|
|
73
|
+
case "$lte":
|
|
74
|
+
case "$ne":
|
|
75
|
+
return _dumbo.SQL`${_dumbo.SQL.plain(fieldName)} ${_dumbo.SQL.plain(_chunkBZRKCNRYcjs.OperatorMap[operator])} ${value}`;
|
|
76
|
+
case "$in":
|
|
77
|
+
return _dumbo.SQL`${_dumbo.SQL.plain(fieldName)} = ANY (${value})`;
|
|
78
|
+
case "$nin":
|
|
79
|
+
return _dumbo.SQL`${_dumbo.SQL.plain(fieldName)} != ALL (${value})`;
|
|
80
|
+
default:
|
|
81
|
+
throw new Error(`Unsupported operator: ${operator}`);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var buildNestedObject = (path, value) => path.split(".").reverse().reduce((acc, key) => ({ [key]: acc }), value);
|
|
85
|
+
|
|
86
|
+
// src/storage/postgresql/core/sqlBuilder/filter/index.ts
|
|
87
|
+
var AND = "AND";
|
|
88
|
+
var constructFilterQuery = (filter, serializer) => _dumbo.SQL.merge(
|
|
89
|
+
Object.entries(filter).map(
|
|
90
|
+
([key, value]) => isRecord(value) ? constructComplexFilterQuery(key, value, serializer) : handleOperator(key, "$eq", value, serializer)
|
|
91
|
+
),
|
|
92
|
+
` ${AND} `
|
|
93
|
+
);
|
|
94
|
+
var constructComplexFilterQuery = (key, value, serializer) => {
|
|
95
|
+
const isEquality = !_chunkBZRKCNRYcjs.hasOperators.call(void 0, value);
|
|
96
|
+
return _dumbo.SQL.merge(
|
|
97
|
+
_chunkBZRKCNRYcjs.objectEntries.call(void 0, value).map(
|
|
98
|
+
([nestedKey, val]) => isEquality ? handleOperator(
|
|
99
|
+
`${key}.${nestedKey}`,
|
|
100
|
+
_chunkBZRKCNRYcjs.QueryOperators.$eq,
|
|
101
|
+
val,
|
|
102
|
+
serializer
|
|
103
|
+
) : handleOperator(key, nestedKey, val, serializer)
|
|
104
|
+
),
|
|
105
|
+
` ${AND} `
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
var isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
109
|
+
|
|
110
|
+
// src/storage/postgresql/core/sqlBuilder/update/index.ts
|
|
111
|
+
|
|
112
|
+
var buildUpdateQuery = (update, serializer) => _chunkBZRKCNRYcjs.objectEntries.call(void 0, update).reduce(
|
|
113
|
+
(currentUpdateQuery, [op, value]) => {
|
|
114
|
+
switch (op) {
|
|
115
|
+
case "$set":
|
|
116
|
+
return buildSetQuery(value, currentUpdateQuery, serializer);
|
|
117
|
+
case "$unset":
|
|
118
|
+
return buildUnsetQuery(value, currentUpdateQuery);
|
|
119
|
+
case "$inc":
|
|
120
|
+
return buildIncQuery(value, currentUpdateQuery);
|
|
121
|
+
case "$push":
|
|
122
|
+
return buildPushQuery(value, currentUpdateQuery, serializer);
|
|
123
|
+
default:
|
|
124
|
+
return currentUpdateQuery;
|
|
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;
|
|
136
|
+
};
|
|
137
|
+
var buildPushQuery = (push, currentUpdateQuery, serializer) => {
|
|
138
|
+
for (const [key, value] of Object.entries(push)) {
|
|
139
|
+
const serializedValue = serializer.serialize([value]);
|
|
140
|
+
currentUpdateQuery = _dumbo.SQL`jsonb_set(${currentUpdateQuery}, '{${_dumbo.SQL.plain(key)}}', (coalesce(data->'${_dumbo.SQL.plain(key)}', '[]'::jsonb) || ${serializedValue}::jsonb), true)`;
|
|
141
|
+
}
|
|
142
|
+
return currentUpdateQuery;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// src/storage/postgresql/core/sqlBuilder/index.ts
|
|
146
|
+
var createCollection = (collectionName) => _dumbo.SQL`
|
|
147
|
+
CREATE TABLE IF NOT EXISTS ${_dumbo.SQL.identifier(collectionName)} (
|
|
148
|
+
_id TEXT PRIMARY KEY,
|
|
149
|
+
data JSONB NOT NULL,
|
|
150
|
+
metadata JSONB NOT NULL DEFAULT '{}',
|
|
151
|
+
_version BIGINT NOT NULL DEFAULT 1,
|
|
152
|
+
_partition TEXT NOT NULL DEFAULT 'png_global',
|
|
153
|
+
_archived BOOLEAN NOT NULL DEFAULT FALSE,
|
|
154
|
+
_created TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
155
|
+
_updated TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
156
|
+
)`;
|
|
157
|
+
var pongoCollectionPostgreSQLMigrations = (collectionName) => [
|
|
158
|
+
_dumbo.sqlMigration.call(void 0, `pongoCollection:${collectionName}:001:createtable`, [
|
|
159
|
+
createCollection(collectionName)
|
|
160
|
+
])
|
|
161
|
+
];
|
|
162
|
+
var postgresSQLBuilder = (collectionName, serializer) => ({
|
|
163
|
+
createCollection: () => createCollection(collectionName),
|
|
164
|
+
insertOne: (document) => {
|
|
165
|
+
const serialized = serializer.serialize(document);
|
|
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)
|
|
170
|
+
VALUES (${id}, ${serialized}, ${version}) ON CONFLICT(_id) DO NOTHING;`;
|
|
171
|
+
},
|
|
172
|
+
insertMany: (documents) => {
|
|
173
|
+
const values = _dumbo.SQL.merge(
|
|
174
|
+
documents.map(
|
|
175
|
+
(doc) => _dumbo.SQL`(${doc._id}, ${serializer.serialize(doc)}, ${_nullishCoalesce(doc._version, () => ( 1n))})`
|
|
176
|
+
),
|
|
177
|
+
","
|
|
178
|
+
);
|
|
179
|
+
return _dumbo.SQL`
|
|
180
|
+
INSERT INTO ${_dumbo.SQL.identifier(collectionName)} (_id, data, _version) VALUES ${values}
|
|
181
|
+
ON CONFLICT(_id) DO NOTHING
|
|
182
|
+
RETURNING _id;`;
|
|
183
|
+
},
|
|
184
|
+
updateOne: (filter, update, options) => {
|
|
185
|
+
const expectedVersion = _chunkBZRKCNRYcjs.expectedVersionValue.call(void 0, _optionalChain([options, 'optionalAccess', _ => _.expectedVersion]));
|
|
186
|
+
const expectedVersionUpdate = expectedVersion != null ? _dumbo.SQL`AND ${_dumbo.SQL.identifier(collectionName)}._version = ${expectedVersion}` : _dumbo.SQL``;
|
|
187
|
+
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter, serializer);
|
|
188
|
+
const updateQuery = _dumbo.isSQL.call(void 0, update) ? update : buildUpdateQuery(update, serializer);
|
|
189
|
+
return _dumbo.SQL`
|
|
190
|
+
WITH existing AS (
|
|
191
|
+
SELECT _id, _version as current_version
|
|
192
|
+
FROM ${_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)}
|
|
193
|
+
LIMIT 1
|
|
194
|
+
),
|
|
195
|
+
updated AS (
|
|
196
|
+
UPDATE ${_dumbo.SQL.identifier(collectionName)}
|
|
197
|
+
SET
|
|
198
|
+
data = ${updateQuery} || jsonb_build_object('_id', ${_dumbo.SQL.identifier(collectionName)}._id) || jsonb_build_object('_version', (_version + 1)::text),
|
|
199
|
+
_version = _version + 1
|
|
200
|
+
FROM existing
|
|
201
|
+
WHERE ${_dumbo.SQL.identifier(collectionName)}._id = existing._id ${expectedVersionUpdate}
|
|
202
|
+
RETURNING ${_dumbo.SQL.identifier(collectionName)}._id, ${_dumbo.SQL.identifier(collectionName)}._version
|
|
203
|
+
)
|
|
204
|
+
SELECT
|
|
205
|
+
existing._id,
|
|
206
|
+
COALESCE(updated._version, existing.current_version) AS version,
|
|
207
|
+
COUNT(existing._id) over() AS matched,
|
|
208
|
+
COUNT(updated._id) over() AS modified
|
|
209
|
+
FROM existing
|
|
210
|
+
LEFT JOIN updated
|
|
211
|
+
ON existing._id = updated._id;`;
|
|
212
|
+
},
|
|
213
|
+
replaceOne: (filter, document, options) => {
|
|
214
|
+
const expectedVersion = _chunkBZRKCNRYcjs.expectedVersionValue.call(void 0, _optionalChain([options, 'optionalAccess', _2 => _2.expectedVersion]));
|
|
215
|
+
const expectedVersionUpdate = expectedVersion != null ? _dumbo.SQL`AND ${_dumbo.SQL.identifier(collectionName)}._version = ${expectedVersion}` : _dumbo.SQL``;
|
|
216
|
+
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter, serializer);
|
|
217
|
+
return _dumbo.SQL`
|
|
218
|
+
WITH existing AS (
|
|
219
|
+
SELECT _id, _version as current_version
|
|
220
|
+
FROM ${_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)}
|
|
221
|
+
LIMIT 1
|
|
222
|
+
),
|
|
223
|
+
updated AS (
|
|
224
|
+
UPDATE ${_dumbo.SQL.identifier(collectionName)}
|
|
225
|
+
SET
|
|
226
|
+
data = ${serializer.serialize(document)} || jsonb_build_object('_id', ${_dumbo.SQL.identifier(collectionName)}._id) || jsonb_build_object('_version', (_version + 1)::text),
|
|
227
|
+
_version = _version + 1
|
|
228
|
+
FROM existing
|
|
229
|
+
WHERE ${_dumbo.SQL.identifier(collectionName)}._id = existing._id ${expectedVersionUpdate}
|
|
230
|
+
RETURNING ${_dumbo.SQL.identifier(collectionName)}._id, ${_dumbo.SQL.identifier(collectionName)}._version
|
|
231
|
+
)
|
|
232
|
+
SELECT
|
|
233
|
+
existing._id,
|
|
234
|
+
COALESCE(updated._version, existing.current_version) AS version,
|
|
235
|
+
COUNT(existing._id) over() AS matched,
|
|
236
|
+
COUNT(updated._id) over() AS modified
|
|
237
|
+
FROM existing
|
|
238
|
+
LEFT JOIN updated
|
|
239
|
+
ON existing._id = updated._id;`;
|
|
240
|
+
},
|
|
241
|
+
updateMany: (filter, update) => {
|
|
242
|
+
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter, serializer);
|
|
243
|
+
const updateQuery = _dumbo.isSQL.call(void 0, update) ? update : buildUpdateQuery(update, serializer);
|
|
244
|
+
return _dumbo.SQL`
|
|
245
|
+
UPDATE ${_dumbo.SQL.identifier(collectionName)}
|
|
246
|
+
SET
|
|
247
|
+
data = ${updateQuery} || jsonb_build_object('_version', (_version + 1)::text),
|
|
248
|
+
_version = _version + 1
|
|
249
|
+
${where(filterQuery)};`;
|
|
250
|
+
},
|
|
251
|
+
deleteOne: (filter, options) => {
|
|
252
|
+
const expectedVersion = _chunkBZRKCNRYcjs.expectedVersionValue.call(void 0, _optionalChain([options, 'optionalAccess', _3 => _3.expectedVersion]));
|
|
253
|
+
const expectedVersionUpdate = expectedVersion != null ? _dumbo.SQL`AND ${_dumbo.SQL.identifier(collectionName)}._version = ${expectedVersion}` : _dumbo.SQL``;
|
|
254
|
+
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter, serializer);
|
|
255
|
+
return _dumbo.SQL`
|
|
256
|
+
WITH existing AS (
|
|
257
|
+
SELECT _id
|
|
258
|
+
FROM ${_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)}
|
|
259
|
+
LIMIT 1
|
|
260
|
+
),
|
|
261
|
+
deleted AS (
|
|
262
|
+
DELETE FROM ${_dumbo.SQL.identifier(collectionName)}
|
|
263
|
+
USING existing
|
|
264
|
+
WHERE ${_dumbo.SQL.identifier(collectionName)}._id = existing._id ${expectedVersionUpdate}
|
|
265
|
+
RETURNING ${_dumbo.SQL.identifier(collectionName)}._id
|
|
266
|
+
)
|
|
267
|
+
SELECT
|
|
268
|
+
existing._id,
|
|
269
|
+
COUNT(existing._id) over() AS matched,
|
|
270
|
+
COUNT(deleted._id) over() AS deleted
|
|
271
|
+
FROM existing
|
|
272
|
+
LEFT JOIN deleted
|
|
273
|
+
ON existing._id = deleted._id;`;
|
|
274
|
+
},
|
|
275
|
+
deleteMany: (filter) => {
|
|
276
|
+
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter, serializer);
|
|
277
|
+
return _dumbo.SQL`DELETE FROM ${_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)}`;
|
|
278
|
+
},
|
|
279
|
+
findOne: (filter) => {
|
|
280
|
+
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter, serializer);
|
|
281
|
+
return _dumbo.SQL`SELECT data, _version FROM ${_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)} LIMIT 1;`;
|
|
282
|
+
},
|
|
283
|
+
find: (filter, options) => {
|
|
284
|
+
const filterQuery = _dumbo.isSQL.call(void 0, filter) ? filter : constructFilterQuery(filter, serializer);
|
|
285
|
+
const query = [];
|
|
286
|
+
query.push(
|
|
287
|
+
_dumbo.SQL`SELECT data, _version FROM ${_dumbo.SQL.identifier(collectionName)}`
|
|
288
|
+
);
|
|
289
|
+
query.push(where(filterQuery));
|
|
290
|
+
if (_optionalChain([options, 'optionalAccess', _4 => _4.limit])) {
|
|
291
|
+
query.push(_dumbo.SQL`LIMIT ${options.limit}`);
|
|
292
|
+
}
|
|
293
|
+
if (_optionalChain([options, 'optionalAccess', _5 => _5.skip])) {
|
|
294
|
+
query.push(_dumbo.SQL`OFFSET ${options.skip}`);
|
|
295
|
+
}
|
|
296
|
+
return _dumbo.SQL.merge([...query, _dumbo.SQL`;`]);
|
|
297
|
+
},
|
|
298
|
+
countDocuments: (filter) => {
|
|
299
|
+
const filterQuery = _dumbo.SQL.check.isSQL(filter) ? filter : constructFilterQuery(filter, serializer);
|
|
300
|
+
return _dumbo.SQL`SELECT COUNT(1) as count FROM ${_dumbo.SQL.identifier(collectionName)} ${where(filterQuery)};`;
|
|
301
|
+
},
|
|
302
|
+
rename: (newName) => _dumbo.SQL`ALTER TABLE ${_dumbo.SQL.identifier(collectionName)} RENAME TO ${_dumbo.SQL.identifier(newName)};`,
|
|
303
|
+
drop: (targetName = collectionName) => _dumbo.SQL`DROP TABLE IF EXISTS ${_dumbo.SQL.identifier(targetName)}`
|
|
304
|
+
});
|
|
305
|
+
var where = (filterQuery) => _dumbo.SQL.check.isEmpty(filterQuery) ? _dumbo.SQL.EMPTY : _dumbo.SQL.merge([_dumbo.SQL`WHERE `, filterQuery]);
|
|
306
|
+
|
|
307
|
+
// src/storage/postgresql/pg/index.ts
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
var _pg = require('@event-driven-io/dumbo/pg');
|
|
314
|
+
var pgPongoDriver = {
|
|
315
|
+
driverType: _pg.PgDriverType,
|
|
316
|
+
databaseFactory: (options) => {
|
|
317
|
+
const databaseName = _nullishCoalesce(_nullishCoalesce(options.databaseName, () => ( _pg.postgreSQLMetadata.parseDatabaseName(options.connectionString))), () => ( _pg.postgreSQLMetadata.defaultDatabaseName));
|
|
318
|
+
return _chunkBZRKCNRYcjs.PongoDatabase.call(void 0, {
|
|
319
|
+
...options,
|
|
320
|
+
pool: _dumbo.dumbo.call(void 0, {
|
|
321
|
+
connectionString: options.connectionString,
|
|
322
|
+
driver: _pg.pgDumboDriver,
|
|
323
|
+
...options.connectionOptions,
|
|
324
|
+
serialization: { serializer: options.serializer }
|
|
325
|
+
}),
|
|
326
|
+
schemaComponent: _chunkBZRKCNRYcjs.PongoDatabaseSchemaComponent.call(void 0, {
|
|
327
|
+
driverType: _pg.PgDriverType,
|
|
328
|
+
collectionFactory: (schema) => _chunkBZRKCNRYcjs.PongoCollectionSchemaComponent.call(void 0, {
|
|
329
|
+
driverType: _pg.PgDriverType,
|
|
330
|
+
definition: schema,
|
|
331
|
+
migrationsOrSchemaComponents: {
|
|
332
|
+
migrations: pongoCollectionPostgreSQLMigrations(schema.name)
|
|
333
|
+
},
|
|
334
|
+
sqlBuilder: postgresSQLBuilder(
|
|
335
|
+
schema.name,
|
|
336
|
+
_nullishCoalesce(_optionalChain([options, 'access', _6 => _6.serialization, 'optionalAccess', _7 => _7.serializer]), () => ( _dumbo.JSONSerializer))
|
|
337
|
+
)
|
|
338
|
+
}),
|
|
339
|
+
definition: _nullishCoalesce(_optionalChain([options, 'access', _8 => _8.schema, 'optionalAccess', _9 => _9.definition]), () => ( _chunkBZRKCNRYcjs.pongoSchema.db(databaseName, {})))
|
|
340
|
+
}),
|
|
341
|
+
databaseName
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
var usePgPongoDriver = () => {
|
|
346
|
+
_chunkBZRKCNRYcjs.pongoDriverRegistry.register(_pg.PgDriverType, pgPongoDriver);
|
|
347
|
+
};
|
|
348
|
+
usePgPongoDriver();
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
exports.pgDriver = pgPongoDriver; exports.pongoCollectionPostgreSQLMigrations = pongoCollectionPostgreSQLMigrations; exports.pongoDriver = pgPongoDriver; exports.postgresSQLBuilder = postgresSQLBuilder; exports.usePgPongoDriver = usePgPongoDriver;
|
|
16
356
|
//# sourceMappingURL=pg.cjs.map
|
package/dist/pg.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/Pongo/Pongo/src/packages/pongo/dist/pg.cjs"],"names":[],"mappings":"AAAA,yGAA6B;AAC7B;AACE;AACA;AACA;AACA;AACF,wDAA6B;AAC7B,gCAA6B;AAC7B;AACE;AACA;AACA;AACA;AACA;AACF,gWAAC","file":"/home/runner/work/Pongo/Pongo/src/packages/pongo/dist/pg.cjs"}
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/Pongo/Pongo/src/packages/pongo/dist/pg.cjs","../src/storage/postgresql/core/sqlBuilder/index.ts","../src/storage/postgresql/core/sqlBuilder/filter/index.ts","../src/storage/postgresql/core/sqlBuilder/filter/queryOperators.ts","../src/storage/postgresql/core/sqlBuilder/update/index.ts","../src/storage/postgresql/pg/index.ts"],"names":["SQL"],"mappings":"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","file":"/home/runner/work/Pongo/Pongo/src/packages/pongo/dist/pg.cjs","sourcesContent":[null,"import type { JSONSerializer } from '@event-driven-io/dumbo';\nimport { isSQL, SQL, sqlMigration } from '@event-driven-io/dumbo';\nimport {\n expectedVersionValue,\n type DeleteOneOptions,\n type FindOptions,\n type OptionalUnlessRequiredIdAndVersion,\n type PongoCollectionSQLBuilder,\n type PongoFilter,\n type PongoUpdate,\n type ReplaceOneOptions,\n type UpdateOneOptions,\n type WithoutId,\n} from '../../../../core';\nimport { constructFilterQuery } from './filter';\nimport { buildUpdateQuery } from './update';\n\nconst createCollection = (collectionName: string): SQL =>\n SQL`\n CREATE TABLE IF NOT EXISTS ${SQL.identifier(collectionName)} (\n _id TEXT PRIMARY KEY, \n data JSONB NOT NULL, \n metadata JSONB NOT NULL DEFAULT '{}',\n _version BIGINT NOT NULL DEFAULT 1,\n _partition TEXT NOT NULL DEFAULT 'png_global',\n _archived BOOLEAN NOT NULL DEFAULT FALSE,\n _created TIMESTAMPTZ NOT NULL DEFAULT now(),\n _updated TIMESTAMPTZ NOT NULL DEFAULT now()\n )`;\n\nexport const pongoCollectionPostgreSQLMigrations = (collectionName: string) => [\n sqlMigration(`pongoCollection:${collectionName}:001:createtable`, [\n createCollection(collectionName),\n ]),\n];\n\nexport const postgresSQLBuilder = (\n collectionName: string,\n serializer: JSONSerializer,\n): PongoCollectionSQLBuilder => ({\n createCollection: (): SQL => createCollection(collectionName),\n insertOne: <T>(document: OptionalUnlessRequiredIdAndVersion<T>): SQL => {\n const serialized = serializer.serialize(document);\n const id = document._id;\n const version = document._version ?? 1n;\n\n return SQL`\n INSERT INTO ${SQL.identifier(collectionName)} (_id, data, _version) \n VALUES (${id}, ${serialized}, ${version}) ON CONFLICT(_id) DO NOTHING;`;\n },\n insertMany: <T>(documents: OptionalUnlessRequiredIdAndVersion<T>[]): SQL => {\n const values = SQL.merge(\n documents.map(\n (doc) =>\n SQL`(${doc._id}, ${serializer.serialize(doc)}, ${doc._version ?? 1n})`,\n ),\n ',',\n );\n\n return SQL`\n INSERT INTO ${SQL.identifier(collectionName)} (_id, data, _version) VALUES ${values}\n ON CONFLICT(_id) DO NOTHING\n RETURNING _id;`;\n },\n updateOne: <T>(\n filter: PongoFilter<T> | SQL,\n update: PongoUpdate<T> | SQL,\n options?: UpdateOneOptions,\n ): SQL => {\n const expectedVersion = expectedVersionValue(options?.expectedVersion);\n const expectedVersionUpdate =\n expectedVersion != null\n ? SQL`AND ${SQL.identifier(collectionName)}._version = ${expectedVersion}`\n : SQL``;\n\n const filterQuery = isSQL(filter)\n ? filter\n : constructFilterQuery(filter, serializer);\n const updateQuery = isSQL(update)\n ? update\n : buildUpdateQuery(update, serializer);\n\n return SQL`\n WITH existing AS (\n SELECT _id, _version as current_version\n FROM ${SQL.identifier(collectionName)} ${where(filterQuery)}\n LIMIT 1\n ),\n updated AS (\n UPDATE ${SQL.identifier(collectionName)} \n SET \n data = ${updateQuery} || jsonb_build_object('_id', ${SQL.identifier(collectionName)}._id) || jsonb_build_object('_version', (_version + 1)::text),\n _version = _version + 1\n FROM existing \n WHERE ${SQL.identifier(collectionName)}._id = existing._id ${expectedVersionUpdate}\n RETURNING ${SQL.identifier(collectionName)}._id, ${SQL.identifier(collectionName)}._version\n )\n SELECT \n existing._id,\n COALESCE(updated._version, existing.current_version) AS version,\n COUNT(existing._id) over() AS matched,\n COUNT(updated._id) over() AS modified\n FROM existing\n LEFT JOIN updated \n ON existing._id = updated._id;`;\n },\n replaceOne: <T>(\n filter: PongoFilter<T> | SQL,\n document: WithoutId<T>,\n options?: ReplaceOneOptions,\n ): SQL => {\n const expectedVersion = expectedVersionValue(options?.expectedVersion);\n const expectedVersionUpdate =\n expectedVersion != null\n ? SQL`AND ${SQL.identifier(collectionName)}._version = ${expectedVersion}`\n : SQL``;\n\n const filterQuery = isSQL(filter)\n ? filter\n : constructFilterQuery(filter, serializer);\n\n return SQL`\n WITH existing AS (\n SELECT _id, _version as current_version\n FROM ${SQL.identifier(collectionName)} ${where(filterQuery)}\n LIMIT 1\n ),\n updated AS (\n UPDATE ${SQL.identifier(collectionName)} \n SET \n data = ${serializer.serialize(document)} || jsonb_build_object('_id', ${SQL.identifier(collectionName)}._id) || jsonb_build_object('_version', (_version + 1)::text),\n _version = _version + 1\n FROM existing \n WHERE ${SQL.identifier(collectionName)}._id = existing._id ${expectedVersionUpdate}\n RETURNING ${SQL.identifier(collectionName)}._id, ${SQL.identifier(collectionName)}._version\n )\n SELECT \n existing._id,\n COALESCE(updated._version, existing.current_version) AS version,\n COUNT(existing._id) over() AS matched,\n COUNT(updated._id) over() AS modified\n FROM existing\n LEFT JOIN updated \n ON existing._id = updated._id;`;\n },\n updateMany: <T>(\n filter: PongoFilter<T> | SQL,\n update: PongoUpdate<T> | SQL,\n ): SQL => {\n const filterQuery = isSQL(filter)\n ? filter\n : constructFilterQuery(filter, serializer);\n const updateQuery = isSQL(update)\n ? update\n : buildUpdateQuery(update, serializer);\n\n return SQL`\n UPDATE ${SQL.identifier(collectionName)} \n SET \n data = ${updateQuery} || jsonb_build_object('_version', (_version + 1)::text),\n _version = _version + 1\n ${where(filterQuery)};`;\n },\n deleteOne: <T>(\n filter: PongoFilter<T> | SQL,\n options?: DeleteOneOptions,\n ): SQL => {\n const expectedVersion = expectedVersionValue(options?.expectedVersion);\n const expectedVersionUpdate =\n expectedVersion != null\n ? SQL`AND ${SQL.identifier(collectionName)}._version = ${expectedVersion}`\n : SQL``;\n\n const filterQuery = isSQL(filter)\n ? filter\n : constructFilterQuery(filter, serializer);\n\n return SQL`\n WITH existing AS (\n SELECT _id\n FROM ${SQL.identifier(collectionName)} ${where(filterQuery)}\n LIMIT 1\n ),\n deleted AS (\n DELETE FROM ${SQL.identifier(collectionName)}\n USING existing\n WHERE ${SQL.identifier(collectionName)}._id = existing._id ${expectedVersionUpdate}\n RETURNING ${SQL.identifier(collectionName)}._id\n )\n SELECT \n existing._id,\n COUNT(existing._id) over() AS matched,\n COUNT(deleted._id) over() AS deleted\n FROM existing\n LEFT JOIN deleted \n ON existing._id = deleted._id;`;\n },\n deleteMany: <T>(filter: PongoFilter<T> | SQL): SQL => {\n const filterQuery = isSQL(filter)\n ? filter\n : constructFilterQuery(filter, serializer);\n\n return SQL`DELETE FROM ${SQL.identifier(collectionName)} ${where(filterQuery)}`;\n },\n findOne: <T>(filter: PongoFilter<T> | SQL): SQL => {\n const filterQuery = isSQL(filter)\n ? filter\n : constructFilterQuery(filter, serializer);\n\n return SQL`SELECT data, _version FROM ${SQL.identifier(collectionName)} ${where(filterQuery)} LIMIT 1;`;\n },\n find: <T>(filter: PongoFilter<T> | SQL, options?: FindOptions): SQL => {\n const filterQuery = isSQL(filter)\n ? filter\n : constructFilterQuery(filter, serializer);\n const query: SQL[] = [];\n\n query.push(\n SQL`SELECT data, _version FROM ${SQL.identifier(collectionName)}`,\n );\n\n query.push(where(filterQuery));\n\n if (options?.limit) {\n query.push(SQL`LIMIT ${options.limit}`);\n }\n\n if (options?.skip) {\n query.push(SQL`OFFSET ${options.skip}`);\n }\n\n return SQL.merge([...query, SQL`;`]);\n },\n countDocuments: <T>(filter: PongoFilter<T> | SQL): SQL => {\n const filterQuery = SQL.check.isSQL(filter)\n ? filter\n : constructFilterQuery(filter, serializer);\n return SQL`SELECT COUNT(1) as count FROM ${SQL.identifier(collectionName)} ${where(filterQuery)};`;\n },\n rename: (newName: string): SQL =>\n SQL`ALTER TABLE ${SQL.identifier(collectionName)} RENAME TO ${SQL.identifier(newName)};`,\n drop: (targetName: string = collectionName): SQL =>\n SQL`DROP TABLE IF EXISTS ${SQL.identifier(targetName)}`,\n});\n\nconst where = (filterQuery: SQL): SQL =>\n SQL.check.isEmpty(filterQuery)\n ? SQL.EMPTY\n : SQL.merge([SQL`WHERE `, filterQuery]);\n","import type { JSONSerializer } from '@event-driven-io/dumbo';\nimport { SQL } from '@event-driven-io/dumbo';\nimport {\n hasOperators,\n objectEntries,\n QueryOperators,\n type PongoFilter,\n} from '../../../../../core';\nimport { handleOperator } from './queryOperators';\n\nexport * from './queryOperators';\n\nconst AND = 'AND';\n\nexport const constructFilterQuery = <T>(\n filter: PongoFilter<T>,\n serializer: JSONSerializer,\n): SQL =>\n SQL.merge(\n Object.entries(filter).map(([key, value]) =>\n isRecord(value)\n ? constructComplexFilterQuery(key, value, serializer)\n : handleOperator(key, '$eq', value, serializer),\n ),\n ` ${AND} `,\n );\n\nconst constructComplexFilterQuery = (\n key: string,\n value: Record<string, unknown>,\n serializer: JSONSerializer,\n): SQL => {\n const isEquality = !hasOperators(value);\n\n return SQL.merge(\n objectEntries(value).map(([nestedKey, val]) =>\n isEquality\n ? handleOperator(\n `${key}.${nestedKey}`,\n QueryOperators.$eq,\n val,\n serializer,\n )\n : handleOperator(key, nestedKey, val, serializer),\n ),\n ` ${AND} `,\n );\n};\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n value !== null && typeof value === 'object' && !Array.isArray(value);\n","import type { JSONSerializer } from '@event-driven-io/dumbo';\nimport { SQL } from '@event-driven-io/dumbo';\nimport { objectEntries, OperatorMap } from '../../../../../core';\n\nexport const handleOperator = (\n path: string,\n operator: string,\n value: unknown,\n serializer: JSONSerializer,\n): SQL => {\n if (path === '_id' || path === '_version') {\n return handleMetadataOperator(path, operator, value);\n }\n\n switch (operator) {\n case '$eq': {\n const nestedPath = serializer.serialize(buildNestedObject(path, value));\n const serializedValue = serializer.serialize(value);\n\n return SQL`(data @> ${nestedPath}::jsonb OR jsonb_path_exists(data, '$.${SQL.plain(path)}[*] ? (@ == ${SQL.plain(serializedValue)})'))`;\n }\n case '$gt':\n case '$gte':\n case '$lt':\n case '$lte':\n case '$ne': {\n const jsonPath = SQL.plain(path.split('.').join(','));\n\n return SQL`data ->> '${jsonPath}' ${SQL.plain(OperatorMap[operator])} ${value}`;\n }\n case '$in': {\n const jsonPath = `{${path.split('.').join(',')}}`;\n\n return SQL`data #>> ${jsonPath} = ANY (${value as unknown[]})`;\n }\n case '$nin': {\n const jsonPath = `{${path.split('.').join(',')}}`;\n\n return SQL`data #>> ${jsonPath} != ALL (${value as unknown[]})`;\n }\n case '$elemMatch': {\n const subQuery = objectEntries(value as Record<string, unknown>)\n .map(\n ([subKey, subValue]) =>\n `@.\"${subKey}\" == ${serializer.serialize(subValue)}`,\n )\n .join(' && ');\n return SQL`jsonb_path_exists(data, '$.${SQL.plain(path)}[*] ? (${SQL.plain(subQuery)})')`;\n }\n case '$all': {\n const nestedPath = serializer.serialize(buildNestedObject(path, value));\n return SQL`data @> ${nestedPath}::jsonb`;\n }\n case '$size': {\n const jsonPath = `{${path.split('.').join(',')}}`;\n\n return SQL`jsonb_array_length(data #> ${jsonPath}) = ${value}`;\n }\n default:\n throw new Error(`Unsupported operator: ${operator}`);\n }\n};\n\nconst handleMetadataOperator = (\n fieldName: string,\n operator: string,\n value: unknown,\n): SQL => {\n switch (operator) {\n case '$eq':\n return SQL`${SQL.plain(fieldName)} = ${value}`;\n case '$gt':\n case '$gte':\n case '$lt':\n case '$lte':\n case '$ne':\n return SQL`${SQL.plain(fieldName)} ${SQL.plain(OperatorMap[operator])} ${value}`;\n case '$in':\n return SQL`${SQL.plain(fieldName)} = ANY (${value as unknown[]})`;\n case '$nin':\n return SQL`${SQL.plain(fieldName)} != ALL (${value as unknown[]})`;\n default:\n throw new Error(`Unsupported operator: ${operator}`);\n }\n};\n\nconst buildNestedObject = (\n path: string,\n value: unknown,\n): Record<string, unknown> =>\n path\n .split('.')\n .reverse()\n .reduce((acc, key) => ({ [key]: acc }), value as Record<string, unknown>);\n","import type { JSONSerializer } from '@event-driven-io/dumbo';\nimport { SQL } from '@event-driven-io/dumbo';\nimport {\n objectEntries,\n type $inc,\n type $push,\n type $set,\n type $unset,\n type PongoUpdate,\n} from '../../../../../core';\n\nexport const buildUpdateQuery = <T>(\n update: PongoUpdate<T>,\n serializer: JSONSerializer,\n): SQL =>\n objectEntries(update).reduce(\n (currentUpdateQuery, [op, value]) => {\n switch (op) {\n case '$set':\n return buildSetQuery(value, currentUpdateQuery, serializer);\n case '$unset':\n return buildUnsetQuery(value, currentUpdateQuery);\n case '$inc':\n return buildIncQuery(value, currentUpdateQuery);\n case '$push':\n return buildPushQuery(value, currentUpdateQuery, serializer);\n default:\n return currentUpdateQuery;\n }\n },\n SQL`data`,\n );\n\nexport const buildSetQuery = <T>(\n set: $set<T>,\n currentUpdateQuery: SQL,\n serializer: JSONSerializer,\n): SQL => SQL`${currentUpdateQuery} || ${serializer.serialize(set)}::jsonb`;\n\nexport const buildUnsetQuery = <T>(\n unset: $unset<T>,\n currentUpdateQuery: SQL,\n): SQL =>\n SQL`${currentUpdateQuery} - ${Object.keys(unset)\n .map((k) => `{${k}}`)\n .join(', ')}`;\n\nexport const buildIncQuery = <T>(\n inc: $inc<T>,\n currentUpdateQuery: SQL,\n): SQL => {\n for (const [key, value] of Object.entries(inc)) {\n currentUpdateQuery =\n typeof value === 'bigint'\n ? SQL`jsonb_set(${currentUpdateQuery}, '{${SQL.plain(key)}}', to_jsonb((COALESCE((data->>'${SQL.plain(key)}')::BIGINT, 0) + ${value})::TEXT), true)`\n : SQL`jsonb_set(${currentUpdateQuery}, '{${SQL.plain(key)}}', to_jsonb(COALESCE((data->>'${SQL.plain(key)}')::NUMERIC, 0) + ${value}), true)`;\n }\n return currentUpdateQuery;\n};\n\nexport const buildPushQuery = <T>(\n push: $push<T>,\n currentUpdateQuery: SQL,\n serializer: JSONSerializer,\n): SQL => {\n for (const [key, value] of Object.entries(push)) {\n const serializedValue = serializer.serialize([value]);\n currentUpdateQuery = SQL`jsonb_set(${currentUpdateQuery}, '{${SQL.plain(key)}}', (coalesce(data->'${SQL.plain(key)}', '[]'::jsonb) || ${serializedValue}::jsonb), true)`;\n }\n return currentUpdateQuery;\n};\n","import { dumbo, JSONSerializer } from '@event-driven-io/dumbo';\nimport {\n pgDumboDriver as dumboDriver,\n PgDriverType,\n postgreSQLMetadata,\n type PgConnection,\n} from '@event-driven-io/dumbo/pg';\nimport type pg from 'pg';\nimport {\n PongoCollectionSchemaComponent,\n PongoDatabase,\n PongoDatabaseSchemaComponent,\n pongoDriverRegistry,\n pongoSchema,\n type PongoDb,\n type PongoDriver,\n type PongoDriverOptions,\n} from '../../../core';\nimport {\n pongoCollectionPostgreSQLMigrations,\n postgresSQLBuilder,\n} from '../core';\n\nexport type PgPongoClientOptions =\n | PooledPongoClientOptions\n | NotPooledPongoOptions;\n\nexport type PooledPongoClientOptions =\n | {\n pool: pg.Pool;\n }\n | {\n pooled: true;\n }\n | {\n pool: pg.Pool;\n pooled: true;\n }\n | object;\n\nexport type NotPooledPongoOptions =\n | {\n client: pg.Client;\n }\n | {\n pooled: false;\n }\n | {\n client: pg.Client;\n pooled: false;\n }\n | {\n connection: PgConnection;\n pooled?: false;\n };\n\ntype PgDatabaseDriverOptions = PongoDriverOptions<PgPongoClientOptions> & {\n databaseName?: string | undefined;\n connectionString: string;\n};\n\nconst pgPongoDriver: PongoDriver<\n PongoDb<PgDriverType>,\n PgDatabaseDriverOptions\n> = {\n driverType: PgDriverType,\n databaseFactory: (options) => {\n const databaseName =\n options.databaseName ??\n postgreSQLMetadata.parseDatabaseName(options.connectionString) ??\n postgreSQLMetadata.defaultDatabaseName;\n\n return PongoDatabase({\n ...options,\n pool: dumbo({\n connectionString: options.connectionString,\n driver: dumboDriver,\n ...options.connectionOptions,\n serialization: { serializer: options.serializer },\n }),\n schemaComponent: PongoDatabaseSchemaComponent({\n driverType: PgDriverType,\n collectionFactory: (schema) =>\n PongoCollectionSchemaComponent({\n driverType: PgDriverType,\n definition: schema,\n migrationsOrSchemaComponents: {\n migrations: pongoCollectionPostgreSQLMigrations(schema.name),\n },\n sqlBuilder: postgresSQLBuilder(\n schema.name,\n options.serialization?.serializer ?? JSONSerializer,\n ),\n }),\n definition:\n options.schema?.definition ?? pongoSchema.db(databaseName, {}),\n }),\n databaseName,\n });\n },\n};\n\nexport const usePgPongoDriver = () => {\n pongoDriverRegistry.register(PgDriverType, pgPongoDriver);\n};\n\nusePgPongoDriver();\n\nexport { pgPongoDriver as pgDriver, pgPongoDriver as pongoDriver };\n"]}
|
package/dist/pg.d.cts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import * as _event_driven_io_dumbo from '@event-driven-io/dumbo';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { JSONSerializer } from '@event-driven-io/dumbo';
|
|
3
|
+
import { r as PongoCollectionSQLBuilder, P as PongoDriver, a as PongoDb, b as PongoDriverOptions } from './pongoCollectionSchemaComponent-BhsINgDe.cjs';
|
|
4
|
+
import { PgDriverType, PgConnection } from '@event-driven-io/dumbo/pg';
|
|
4
5
|
import pg from 'pg';
|
|
5
6
|
|
|
6
7
|
declare const pongoCollectionPostgreSQLMigrations: (collectionName: string) => _event_driven_io_dumbo.SQLMigration[];
|
|
7
|
-
declare const postgresSQLBuilder: (collectionName: string) => PongoCollectionSQLBuilder;
|
|
8
|
+
declare const postgresSQLBuilder: (collectionName: string, serializer: JSONSerializer) => PongoCollectionSQLBuilder;
|
|
8
9
|
|
|
9
|
-
type
|
|
10
|
+
type PgPongoClientOptions = PooledPongoClientOptions | NotPooledPongoOptions;
|
|
10
11
|
type PooledPongoClientOptions = {
|
|
11
12
|
pool: pg.Pool;
|
|
12
13
|
} | {
|
|
@@ -23,14 +24,14 @@ type NotPooledPongoOptions = {
|
|
|
23
24
|
client: pg.Client;
|
|
24
25
|
pooled: false;
|
|
25
26
|
} | {
|
|
26
|
-
connection:
|
|
27
|
+
connection: PgConnection;
|
|
27
28
|
pooled?: false;
|
|
28
29
|
};
|
|
29
|
-
type
|
|
30
|
+
type PgDatabaseDriverOptions = PongoDriverOptions<PgPongoClientOptions> & {
|
|
30
31
|
databaseName?: string | undefined;
|
|
31
32
|
connectionString: string;
|
|
32
33
|
};
|
|
33
|
-
declare const
|
|
34
|
-
declare const
|
|
34
|
+
declare const pgPongoDriver: PongoDriver<PongoDb<PgDriverType>, PgDatabaseDriverOptions>;
|
|
35
|
+
declare const usePgPongoDriver: () => void;
|
|
35
36
|
|
|
36
|
-
export { type
|
|
37
|
+
export { type NotPooledPongoOptions, type PgPongoClientOptions, type PooledPongoClientOptions, pgPongoDriver as pgDriver, pongoCollectionPostgreSQLMigrations, pgPongoDriver as pongoDriver, postgresSQLBuilder, usePgPongoDriver };
|
package/dist/pg.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import * as _event_driven_io_dumbo from '@event-driven-io/dumbo';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { JSONSerializer } from '@event-driven-io/dumbo';
|
|
3
|
+
import { r as PongoCollectionSQLBuilder, P as PongoDriver, a as PongoDb, b as PongoDriverOptions } from './pongoCollectionSchemaComponent-BhsINgDe.js';
|
|
4
|
+
import { PgDriverType, PgConnection } from '@event-driven-io/dumbo/pg';
|
|
4
5
|
import pg from 'pg';
|
|
5
6
|
|
|
6
7
|
declare const pongoCollectionPostgreSQLMigrations: (collectionName: string) => _event_driven_io_dumbo.SQLMigration[];
|
|
7
|
-
declare const postgresSQLBuilder: (collectionName: string) => PongoCollectionSQLBuilder;
|
|
8
|
+
declare const postgresSQLBuilder: (collectionName: string, serializer: JSONSerializer) => PongoCollectionSQLBuilder;
|
|
8
9
|
|
|
9
|
-
type
|
|
10
|
+
type PgPongoClientOptions = PooledPongoClientOptions | NotPooledPongoOptions;
|
|
10
11
|
type PooledPongoClientOptions = {
|
|
11
12
|
pool: pg.Pool;
|
|
12
13
|
} | {
|
|
@@ -23,14 +24,14 @@ type NotPooledPongoOptions = {
|
|
|
23
24
|
client: pg.Client;
|
|
24
25
|
pooled: false;
|
|
25
26
|
} | {
|
|
26
|
-
connection:
|
|
27
|
+
connection: PgConnection;
|
|
27
28
|
pooled?: false;
|
|
28
29
|
};
|
|
29
|
-
type
|
|
30
|
+
type PgDatabaseDriverOptions = PongoDriverOptions<PgPongoClientOptions> & {
|
|
30
31
|
databaseName?: string | undefined;
|
|
31
32
|
connectionString: string;
|
|
32
33
|
};
|
|
33
|
-
declare const
|
|
34
|
-
declare const
|
|
34
|
+
declare const pgPongoDriver: PongoDriver<PongoDb<PgDriverType>, PgDatabaseDriverOptions>;
|
|
35
|
+
declare const usePgPongoDriver: () => void;
|
|
35
36
|
|
|
36
|
-
export { type
|
|
37
|
+
export { type NotPooledPongoOptions, type PgPongoClientOptions, type PooledPongoClientOptions, pgPongoDriver as pgDriver, pongoCollectionPostgreSQLMigrations, pgPongoDriver as pongoDriver, postgresSQLBuilder, usePgPongoDriver };
|