plugin-data-source-postgres 2.0.0 → 2.0.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/README.md +1 -1
- package/dist/externalVersion.js +1 -0
- package/dist/server/collections/sql-collection.js +2 -2
- package/dist/server/collections/sql-model.js +4 -2
- package/dist/server/hooks/field-hooks.js +20 -0
- package/dist/server/plugin.js +603 -20
- package/dist/server/postgres-collection-manager.js +192 -25
- package/dist/server/postgres-datasource.js +47 -13
- package/dist/server/utils/sql-validator.js +119 -69
- package/package.json +2 -2
- package/src/server/__tests__/postgres-datasource.unit.test.ts +885 -0
- package/src/server/__tests__/sql-model.test.ts +47 -3
- package/src/server/__tests__/sql-validator.test.ts +8 -1
- package/src/server/collections/sql-collection.ts +2 -2
- package/src/server/collections/sql-model.ts +2 -2
- package/src/server/hooks/field-hooks.ts +36 -2
- package/src/server/plugin.ts +752 -17
- package/src/server/postgres-collection-manager.ts +228 -28
- package/src/server/postgres-datasource.ts +57 -12
- package/src/server/utils/sql-validator.ts +123 -68
package/dist/server/plugin.js
CHANGED
|
@@ -31,11 +31,13 @@ __export(plugin_exports, {
|
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(plugin_exports);
|
|
33
33
|
var import_server = require("@nocobase/server");
|
|
34
|
+
var import_utils = require("@nocobase/utils");
|
|
34
35
|
var import_lodash = require("lodash");
|
|
35
36
|
var import_constants = require("../constants");
|
|
36
37
|
var import_sql_collection = require("./collections/sql-collection");
|
|
37
38
|
var import_tree_collection = require("./collections/tree-collection");
|
|
38
39
|
var import_field_hooks = require("./hooks/field-hooks");
|
|
40
|
+
var import_postgres_collection_manager = require("./postgres-collection-manager");
|
|
39
41
|
var import_postgres_datasource = require("./postgres-datasource");
|
|
40
42
|
var import_sql_validator = require("./utils/sql-validator");
|
|
41
43
|
var import_validation_error = require("./utils/validation-error");
|
|
@@ -63,6 +65,475 @@ const PRESET_FIELDS = {
|
|
|
63
65
|
function isRecord(value) {
|
|
64
66
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
65
67
|
}
|
|
68
|
+
const UNRESOLVED_TEMPLATE_RE = /\{\{[\s\S]*\}\}/;
|
|
69
|
+
const NEGATED_ARRAY_OPERATORS = /* @__PURE__ */ new Set([
|
|
70
|
+
"$ne",
|
|
71
|
+
"$notIn",
|
|
72
|
+
"$notIncludes",
|
|
73
|
+
"$notStartsWith",
|
|
74
|
+
"$notEndWith",
|
|
75
|
+
"$empty",
|
|
76
|
+
"$noneOf"
|
|
77
|
+
]);
|
|
78
|
+
const KNOWN_FILTER_OPERATORS = /* @__PURE__ */ new Set([
|
|
79
|
+
"$eq",
|
|
80
|
+
"$ne",
|
|
81
|
+
"$in",
|
|
82
|
+
"$notIn",
|
|
83
|
+
"$includes",
|
|
84
|
+
"$notIncludes",
|
|
85
|
+
"$startsWith",
|
|
86
|
+
"$notStartsWith",
|
|
87
|
+
"$endWith",
|
|
88
|
+
"$notEndWith",
|
|
89
|
+
"$empty",
|
|
90
|
+
"$notEmpty",
|
|
91
|
+
"$isFalsy",
|
|
92
|
+
"$isTruly",
|
|
93
|
+
"$gt",
|
|
94
|
+
"$gte",
|
|
95
|
+
"$lt",
|
|
96
|
+
"$lte",
|
|
97
|
+
"$match",
|
|
98
|
+
"$notMatch",
|
|
99
|
+
"$anyOf",
|
|
100
|
+
"$noneOf",
|
|
101
|
+
"$exists",
|
|
102
|
+
"$notExists"
|
|
103
|
+
]);
|
|
104
|
+
const LOGICAL_OPERATORS = /* @__PURE__ */ new Set(["$and", "$or", "$not"]);
|
|
105
|
+
function containsUnresolvedTemplate(value) {
|
|
106
|
+
if (typeof value === "string") return UNRESOLVED_TEMPLATE_RE.test(value);
|
|
107
|
+
if (Array.isArray(value)) return value.some((item) => containsUnresolvedTemplate(item));
|
|
108
|
+
if (isRecord(value)) return Object.values(value).some((item) => containsUnresolvedTemplate(item));
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
function compareOrdered(actual, operand) {
|
|
112
|
+
if (typeof actual === "number" && typeof operand === "number" && Number.isFinite(actual) && Number.isFinite(operand)) {
|
|
113
|
+
return actual === operand ? 0 : actual < operand ? -1 : 1;
|
|
114
|
+
}
|
|
115
|
+
if (typeof actual === "string" && typeof operand === "string") {
|
|
116
|
+
return actual === operand ? 0 : actual < operand ? -1 : 1;
|
|
117
|
+
}
|
|
118
|
+
if (actual instanceof Date && operand instanceof Date) {
|
|
119
|
+
const left = actual.getTime();
|
|
120
|
+
const right = operand.getTime();
|
|
121
|
+
if (!Number.isFinite(left) || !Number.isFinite(right)) return null;
|
|
122
|
+
return left === right ? 0 : left < right ? -1 : 1;
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
function isCaseInsensitiveEqualityDialect(dialect) {
|
|
127
|
+
return dialect === "mysql" || dialect === "mariadb";
|
|
128
|
+
}
|
|
129
|
+
function valuesEqual(left, right, metadataDialect) {
|
|
130
|
+
if (isCaseInsensitiveEqualityDialect(metadataDialect) && typeof left === "string" && typeof right === "string") {
|
|
131
|
+
return left.toLowerCase() === right.toLowerCase();
|
|
132
|
+
}
|
|
133
|
+
return (0, import_lodash.isEqual)(left, right);
|
|
134
|
+
}
|
|
135
|
+
function coerceLikeActual(actual, metadataDialect) {
|
|
136
|
+
if (typeof actual === "string") return actual;
|
|
137
|
+
if ((metadataDialect === "postgres" || metadataDialect === "mysql" || metadataDialect === "mariadb" || metadataDialect === "sqlite") && (typeof actual === "number" || typeof actual === "boolean")) {
|
|
138
|
+
return String(actual);
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
function foldLikeCase(text, metadataDialect) {
|
|
143
|
+
return metadataDialect === "sqlite" ? text.replace(/[A-Z]/g, (ch) => ch.toLowerCase()) : text.toLowerCase();
|
|
144
|
+
}
|
|
145
|
+
function escapeRegExpChar(ch) {
|
|
146
|
+
return /[\\^$.*+?()[\]{}|]/.test(ch) ? `\\${ch}` : ch;
|
|
147
|
+
}
|
|
148
|
+
function buildCoreLikePattern(operand, metadataDialect, mode) {
|
|
149
|
+
const escaped = operand.replace(/[_%]/g, "\\$&");
|
|
150
|
+
if (mode === "startsWith") return `${escaped}%`;
|
|
151
|
+
if (mode === "endsWith") return `%${escaped}`;
|
|
152
|
+
return `%${escaped}%`;
|
|
153
|
+
}
|
|
154
|
+
function compileSqlLikePattern(patternFolded, metadataDialect) {
|
|
155
|
+
const escapeChar = metadataDialect === "sqlite" ? null : "\\";
|
|
156
|
+
const chars = Array.from(patternFolded);
|
|
157
|
+
let source = "^";
|
|
158
|
+
let prevWildcardMany = false;
|
|
159
|
+
for (let idx = 0; idx < chars.length; idx += 1) {
|
|
160
|
+
const ch = chars[idx];
|
|
161
|
+
if (escapeChar !== null && ch === escapeChar) {
|
|
162
|
+
if (idx + 1 >= chars.length) {
|
|
163
|
+
if (metadataDialect === "postgres") return null;
|
|
164
|
+
source += "\\\\";
|
|
165
|
+
prevWildcardMany = false;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
idx += 1;
|
|
169
|
+
source += escapeRegExpChar(chars[idx]);
|
|
170
|
+
prevWildcardMany = false;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (ch === "%") {
|
|
174
|
+
if (!prevWildcardMany) {
|
|
175
|
+
source += ".*";
|
|
176
|
+
prevWildcardMany = true;
|
|
177
|
+
}
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
prevWildcardMany = false;
|
|
181
|
+
source += ch === "_" ? "." : escapeRegExpChar(ch);
|
|
182
|
+
}
|
|
183
|
+
source += "$";
|
|
184
|
+
return new RegExp(source, "su");
|
|
185
|
+
}
|
|
186
|
+
function matchLikeValue(actualFolded, rawOperand, metadataDialect, mode) {
|
|
187
|
+
if (metadataDialect === "mssql") {
|
|
188
|
+
const foldedOperand = foldLikeCase(rawOperand, metadataDialect);
|
|
189
|
+
if (mode === "startsWith") return actualFolded.startsWith(foldedOperand);
|
|
190
|
+
if (mode === "endsWith") return actualFolded.endsWith(foldedOperand);
|
|
191
|
+
return actualFolded.includes(foldedOperand);
|
|
192
|
+
}
|
|
193
|
+
const patternFolded = foldLikeCase(buildCoreLikePattern(rawOperand, metadataDialect, mode), metadataDialect);
|
|
194
|
+
const compiled = compileSqlLikePattern(patternFolded, metadataDialect);
|
|
195
|
+
return compiled !== null && compiled.test(actualFolded);
|
|
196
|
+
}
|
|
197
|
+
function evaluateLikeOperator(actual, operand, metadataDialect, mode, negated) {
|
|
198
|
+
if (mode === "includes" && operand === null) {
|
|
199
|
+
return negated ? actual !== null && actual !== void 0 : actual === null || actual === void 0;
|
|
200
|
+
}
|
|
201
|
+
const actualText = coerceLikeActual(actual, metadataDialect);
|
|
202
|
+
if (actualText === null) return false;
|
|
203
|
+
const actualFolded = foldLikeCase(actualText, metadataDialect);
|
|
204
|
+
if (typeof operand === "string") {
|
|
205
|
+
const matched = matchLikeValue(actualFolded, operand, metadataDialect, mode);
|
|
206
|
+
return negated ? !matched : matched;
|
|
207
|
+
}
|
|
208
|
+
if (Array.isArray(operand) && operand.every((item) => typeof item === "string")) {
|
|
209
|
+
if (negated) {
|
|
210
|
+
return operand.every((item) => !matchLikeValue(actualFolded, item, metadataDialect, mode));
|
|
211
|
+
}
|
|
212
|
+
return operand.some((item) => matchLikeValue(actualFolded, item, metadataDialect, mode));
|
|
213
|
+
}
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
function evaluateSingleOperator(actual, operator, operand, metadataDialect = "postgres") {
|
|
217
|
+
if (!KNOWN_FILTER_OPERATORS.has(operator) || containsUnresolvedTemplate(operand)) return false;
|
|
218
|
+
switch (operator) {
|
|
219
|
+
case "$eq":
|
|
220
|
+
if (operand === void 0) return true;
|
|
221
|
+
if (operand === null) return actual === null || actual === void 0;
|
|
222
|
+
if (Array.isArray(operand) && !Array.isArray(actual)) {
|
|
223
|
+
return actual !== null && actual !== void 0 && operand.some((item) => item !== null && item !== void 0 && valuesEqual(actual, item, metadataDialect));
|
|
224
|
+
}
|
|
225
|
+
return valuesEqual(actual, operand, metadataDialect);
|
|
226
|
+
case "$ne":
|
|
227
|
+
if (operand === null) return actual !== null && actual !== void 0;
|
|
228
|
+
if (Array.isArray(operand) && !Array.isArray(actual)) {
|
|
229
|
+
if (operand.length === 0) return true;
|
|
230
|
+
if (actual === null || actual === void 0) return false;
|
|
231
|
+
if (operand.some((item) => item === null || item === void 0)) return false;
|
|
232
|
+
return !operand.some((item) => valuesEqual(actual, item, metadataDialect));
|
|
233
|
+
}
|
|
234
|
+
return !Array.isArray(actual) && (actual === null || actual === void 0) ? true : !valuesEqual(actual, operand, metadataDialect);
|
|
235
|
+
case "$in": {
|
|
236
|
+
if (operand === null || operand === void 0) return false;
|
|
237
|
+
const list = Array.isArray(operand) ? operand : [operand];
|
|
238
|
+
return actual !== null && actual !== void 0 && list.some((item) => item !== null && item !== void 0 && valuesEqual(actual, item, metadataDialect));
|
|
239
|
+
}
|
|
240
|
+
case "$notIn":
|
|
241
|
+
if (!Array.isArray(operand)) return false;
|
|
242
|
+
if (actual === null || actual === void 0 || operand.length === 0) return true;
|
|
243
|
+
if (operand.some((item) => item === null || item === void 0)) return false;
|
|
244
|
+
return !operand.some((item) => valuesEqual(actual, item, metadataDialect));
|
|
245
|
+
case "$exists": {
|
|
246
|
+
if (operand !== void 0 && typeof operand !== "boolean") return false;
|
|
247
|
+
const exists = Array.isArray(actual) ? actual.length > 0 : actual !== null && actual !== void 0;
|
|
248
|
+
return operand === false ? !exists : exists;
|
|
249
|
+
}
|
|
250
|
+
case "$notExists": {
|
|
251
|
+
if (operand !== void 0 && typeof operand !== "boolean") return false;
|
|
252
|
+
const exists = Array.isArray(actual) ? actual.length > 0 : actual !== null && actual !== void 0;
|
|
253
|
+
return operand === false ? exists : !exists;
|
|
254
|
+
}
|
|
255
|
+
case "$includes":
|
|
256
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, "includes", false);
|
|
257
|
+
case "$notIncludes":
|
|
258
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, "includes", true);
|
|
259
|
+
case "$startsWith":
|
|
260
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, "startsWith", false);
|
|
261
|
+
case "$notStartsWith":
|
|
262
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, "startsWith", true);
|
|
263
|
+
case "$endWith":
|
|
264
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, "endsWith", false);
|
|
265
|
+
case "$notEndWith":
|
|
266
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, "endsWith", true);
|
|
267
|
+
case "$empty": {
|
|
268
|
+
if (operand !== void 0 && typeof operand !== "boolean") return false;
|
|
269
|
+
const empty = actual === null || actual === void 0 || actual === "" || Array.isArray(actual) && !actual.length;
|
|
270
|
+
return operand === false ? !empty : empty;
|
|
271
|
+
}
|
|
272
|
+
case "$notEmpty": {
|
|
273
|
+
if (operand !== void 0 && typeof operand !== "boolean") return false;
|
|
274
|
+
const empty = actual === null || actual === void 0 || actual === "" || Array.isArray(actual) && !actual.length;
|
|
275
|
+
return operand === false ? empty : !empty;
|
|
276
|
+
}
|
|
277
|
+
case "$isFalsy":
|
|
278
|
+
if (operand !== void 0 && typeof operand !== "boolean") return false;
|
|
279
|
+
return operand === false ? Boolean(actual) : !actual;
|
|
280
|
+
case "$isTruly":
|
|
281
|
+
if (operand !== void 0 && typeof operand !== "boolean") return false;
|
|
282
|
+
return operand === false ? !actual : Boolean(actual);
|
|
283
|
+
case "$gt": {
|
|
284
|
+
const cmp = compareOrdered(actual, operand);
|
|
285
|
+
return cmp !== null && cmp > 0;
|
|
286
|
+
}
|
|
287
|
+
case "$gte": {
|
|
288
|
+
const cmp = compareOrdered(actual, operand);
|
|
289
|
+
return cmp !== null && cmp >= 0;
|
|
290
|
+
}
|
|
291
|
+
case "$lt": {
|
|
292
|
+
const cmp = compareOrdered(actual, operand);
|
|
293
|
+
return cmp !== null && cmp < 0;
|
|
294
|
+
}
|
|
295
|
+
case "$lte": {
|
|
296
|
+
const cmp = compareOrdered(actual, operand);
|
|
297
|
+
return cmp !== null && cmp <= 0;
|
|
298
|
+
}
|
|
299
|
+
case "$anyOf":
|
|
300
|
+
return Array.isArray(actual) && Array.isArray(operand) && actual.some((item) => operand.some((target) => valuesEqual(item, target, metadataDialect)));
|
|
301
|
+
case "$noneOf":
|
|
302
|
+
return Array.isArray(actual) && Array.isArray(operand) && !actual.some((item) => operand.some((target) => valuesEqual(item, target, metadataDialect)));
|
|
303
|
+
case "$match":
|
|
304
|
+
return Array.isArray(actual) && isRecord(operand) && actual.some((item) => isRecord(item) && matchesCollectionFilter(item, operand, metadataDialect));
|
|
305
|
+
case "$notMatch":
|
|
306
|
+
return Array.isArray(actual) && isRecord(operand) && !actual.some((item) => isRecord(item) && matchesCollectionFilter(item, operand, metadataDialect));
|
|
307
|
+
default:
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
const ARRAY_LEVEL_OPERATORS = /* @__PURE__ */ new Set([
|
|
312
|
+
"$match",
|
|
313
|
+
"$notMatch",
|
|
314
|
+
"$anyOf",
|
|
315
|
+
"$noneOf",
|
|
316
|
+
"$empty",
|
|
317
|
+
"$notEmpty",
|
|
318
|
+
"$exists",
|
|
319
|
+
"$notExists"
|
|
320
|
+
]);
|
|
321
|
+
const CONTAINER_ARRAY_KEYS = /* @__PURE__ */ new Set(["fields", "indexes"]);
|
|
322
|
+
const MAX_PINNED_FILTER_CONTEXTS = 1e4;
|
|
323
|
+
function evaluateOperatorGroup(target, entries, metadataDialect) {
|
|
324
|
+
if (!entries.length) return true;
|
|
325
|
+
if (entries.some(([operator, operand]) => !KNOWN_FILTER_OPERATORS.has(operator) || containsUnresolvedTemplate(operand))) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
if (target.traversedArray && entries.every(([operator]) => NEGATED_ARRAY_OPERATORS.has(operator))) {
|
|
329
|
+
return target.values.every(
|
|
330
|
+
(value) => entries.every(([operator, operand]) => evaluateSingleOperator(value, operator, operand, metadataDialect))
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
return target.values.some(
|
|
334
|
+
(value) => entries.every(([operator, operand]) => evaluateSingleOperator(value, operator, operand, metadataDialect))
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
function collectPositiveArrayPrefixes(filter, out, prefix = []) {
|
|
338
|
+
if (!isRecord(filter)) return;
|
|
339
|
+
for (const [rawKey, condition] of Object.entries(filter)) {
|
|
340
|
+
if (rawKey === "$and" || rawKey === "$or") {
|
|
341
|
+
const items = Array.isArray(condition) ? condition : isRecord(condition) ? Object.values(condition) : null;
|
|
342
|
+
if (items) {
|
|
343
|
+
for (const item of items) collectPositiveArrayPrefixes(item, out, prefix);
|
|
344
|
+
}
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (rawKey === "$not" || rawKey.startsWith("$")) continue;
|
|
348
|
+
const parts = rawKey.split(".");
|
|
349
|
+
const lastPart = parts[parts.length - 1];
|
|
350
|
+
const hasTrailingOp = parts.length > 1 && lastPart.startsWith("$");
|
|
351
|
+
const fieldParts = hasTrailingOp ? parts.slice(0, -1) : parts;
|
|
352
|
+
if (fieldParts.some((part) => !part || part.startsWith("$"))) continue;
|
|
353
|
+
const fullParts = [...prefix, ...fieldParts];
|
|
354
|
+
let arrayLevelOnly = false;
|
|
355
|
+
if (hasTrailingOp) {
|
|
356
|
+
arrayLevelOnly = ARRAY_LEVEL_OPERATORS.has(lastPart);
|
|
357
|
+
} else if (isRecord(condition)) {
|
|
358
|
+
const keys = Object.keys(condition);
|
|
359
|
+
if (keys.length && keys.every((key) => KNOWN_FILTER_OPERATORS.has(key))) {
|
|
360
|
+
arrayLevelOnly = keys.every((key) => ARRAY_LEVEL_OPERATORS.has(key));
|
|
361
|
+
} else if (keys.length && keys.every((key) => !key.startsWith("$") || LOGICAL_OPERATORS.has(key))) {
|
|
362
|
+
for (let len = 1; len <= fullParts.length; len += 1) {
|
|
363
|
+
out.push(fullParts.slice(0, len));
|
|
364
|
+
}
|
|
365
|
+
collectPositiveArrayPrefixes(condition, out, fullParts);
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const maxLen = arrayLevelOnly ? fullParts.length - 1 : fullParts.length;
|
|
370
|
+
for (let len = 1; len <= maxLen; len += 1) {
|
|
371
|
+
out.push(fullParts.slice(0, len));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function readPathWithPins(record, parts, pins, ignoreLeafPin = false) {
|
|
376
|
+
let current = [record];
|
|
377
|
+
let traversedArray = false;
|
|
378
|
+
for (let idx = 0; idx < parts.length; idx += 1) {
|
|
379
|
+
const part = parts[idx];
|
|
380
|
+
const pathSoFar = parts.slice(0, idx + 1).join(".");
|
|
381
|
+
if (!(ignoreLeafPin && idx === parts.length - 1) && pins.has(pathSoFar)) {
|
|
382
|
+
current = [pins.get(pathSoFar)];
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const next = [];
|
|
386
|
+
for (const item of current) {
|
|
387
|
+
if (Array.isArray(item)) {
|
|
388
|
+
traversedArray = true;
|
|
389
|
+
for (const entry of item) {
|
|
390
|
+
if (isRecord(entry)) {
|
|
391
|
+
if (part in entry) next.push(entry[part]);
|
|
392
|
+
else if (isRecord(entry.options) && part in entry.options) next.push(entry.options[part]);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
} else if (isRecord(item)) {
|
|
396
|
+
if (part in item) {
|
|
397
|
+
next.push(item[part]);
|
|
398
|
+
} else if (isRecord(item.options) && part in item.options) {
|
|
399
|
+
next.push(item.options[part]);
|
|
400
|
+
} else {
|
|
401
|
+
next.push(void 0);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
current = next;
|
|
406
|
+
}
|
|
407
|
+
return { values: current.length ? current : [void 0], traversedArray };
|
|
408
|
+
}
|
|
409
|
+
function expandPinsForPrefixes(record, inheritedPins, candidatePrefixes) {
|
|
410
|
+
const uniquePrefixes = /* @__PURE__ */ new Map();
|
|
411
|
+
for (const parts of candidatePrefixes) {
|
|
412
|
+
const key = parts.join(".");
|
|
413
|
+
if (!inheritedPins.has(key) && !uniquePrefixes.has(key)) {
|
|
414
|
+
uniquePrefixes.set(key, parts);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const sortedPrefixes = [...uniquePrefixes.entries()].sort((a, b) => a[1].length - b[1].length);
|
|
418
|
+
let contexts = [new Map(inheritedPins)];
|
|
419
|
+
for (const [key, parts] of sortedPrefixes) {
|
|
420
|
+
const leafKey = parts[parts.length - 1];
|
|
421
|
+
const nextContexts = [];
|
|
422
|
+
for (const ctxPins of contexts) {
|
|
423
|
+
const rawTarget = readPathWithPins(record, parts, ctxPins, true);
|
|
424
|
+
if (rawTarget.values.length === 1 && Array.isArray(rawTarget.values[0])) {
|
|
425
|
+
const arr = rawTarget.values[0];
|
|
426
|
+
if (CONTAINER_ARRAY_KEYS.has(leafKey) || arr.some((entry) => isRecord(entry))) {
|
|
427
|
+
if (arr.length === 0) {
|
|
428
|
+
const forked = new Map(ctxPins);
|
|
429
|
+
forked.set(key, void 0);
|
|
430
|
+
nextContexts.push(forked);
|
|
431
|
+
} else {
|
|
432
|
+
for (const entry of arr) {
|
|
433
|
+
const forked = new Map(ctxPins);
|
|
434
|
+
forked.set(key, entry);
|
|
435
|
+
nextContexts.push(forked);
|
|
436
|
+
if (nextContexts.length > MAX_PINNED_FILTER_CONTEXTS) return null;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
nextContexts.push(ctxPins);
|
|
443
|
+
if (nextContexts.length > MAX_PINNED_FILTER_CONTEXTS) return null;
|
|
444
|
+
}
|
|
445
|
+
contexts = nextContexts;
|
|
446
|
+
}
|
|
447
|
+
return contexts;
|
|
448
|
+
}
|
|
449
|
+
function extractSubPins(pins, prefix) {
|
|
450
|
+
const prefixDot = `${prefix}.`;
|
|
451
|
+
const subPins = /* @__PURE__ */ new Map();
|
|
452
|
+
for (const [key, value] of pins.entries()) {
|
|
453
|
+
if (key.startsWith(prefixDot)) {
|
|
454
|
+
subPins.set(key.slice(prefixDot.length), value);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return subPins;
|
|
458
|
+
}
|
|
459
|
+
function evaluateEntriesWithPins(record, pins, filter, metadataDialect) {
|
|
460
|
+
if (!isRecord(filter)) return false;
|
|
461
|
+
for (const [rawKey, condition] of Object.entries(filter)) {
|
|
462
|
+
if (rawKey === "$and") {
|
|
463
|
+
const items = Array.isArray(condition) ? condition : isRecord(condition) ? Object.values(condition) : null;
|
|
464
|
+
if (!items || !items.every((item) => evaluateEntriesWithPins(record, pins, item, metadataDialect))) return false;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (rawKey === "$or") {
|
|
468
|
+
const items = Array.isArray(condition) ? condition : isRecord(condition) ? Object.values(condition) : null;
|
|
469
|
+
if (!items || !items.some((item) => matchesScope(record, pins, item, metadataDialect))) return false;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (rawKey === "$not") {
|
|
473
|
+
if (!isRecord(condition) || matchesScope(record, pins, condition, metadataDialect)) return false;
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
if (rawKey.startsWith("$")) return false;
|
|
477
|
+
const parts = rawKey.split(".");
|
|
478
|
+
const lastPart = parts[parts.length - 1];
|
|
479
|
+
const hasTrailingOp = parts.length > 1 && lastPart.startsWith("$");
|
|
480
|
+
const fieldParts = hasTrailingOp ? parts.slice(0, -1) : parts;
|
|
481
|
+
if (fieldParts.some((part) => !part || part.startsWith("$"))) return false;
|
|
482
|
+
if (hasTrailingOp) {
|
|
483
|
+
const target2 = readPathWithPins(record, fieldParts, pins, ARRAY_LEVEL_OPERATORS.has(lastPart));
|
|
484
|
+
if (!evaluateOperatorGroup(target2, [[lastPart, condition]], metadataDialect)) return false;
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (isRecord(condition)) {
|
|
488
|
+
const entries = Object.entries(condition);
|
|
489
|
+
if (!entries.length) continue;
|
|
490
|
+
if (entries.every(([key]) => KNOWN_FILTER_OPERATORS.has(key))) {
|
|
491
|
+
const arrayLevelEntries = entries.filter(([key]) => ARRAY_LEVEL_OPERATORS.has(key));
|
|
492
|
+
const elementEntries = entries.filter(([key]) => !ARRAY_LEVEL_OPERATORS.has(key));
|
|
493
|
+
if (arrayLevelEntries.length) {
|
|
494
|
+
const arrayTarget = readPathWithPins(record, fieldParts, pins, true);
|
|
495
|
+
if (!evaluateOperatorGroup(arrayTarget, arrayLevelEntries, metadataDialect)) return false;
|
|
496
|
+
}
|
|
497
|
+
if (elementEntries.length) {
|
|
498
|
+
const elemTarget = readPathWithPins(record, fieldParts, pins, false);
|
|
499
|
+
if (!evaluateOperatorGroup(elemTarget, elementEntries, metadataDialect)) return false;
|
|
500
|
+
}
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
if (!entries.every(([key]) => !key.startsWith("$") || LOGICAL_OPERATORS.has(key))) return false;
|
|
504
|
+
const fieldPathKey = fieldParts.join(".");
|
|
505
|
+
const subPins = extractSubPins(pins, fieldPathKey);
|
|
506
|
+
if (pins.has(fieldPathKey)) {
|
|
507
|
+
const pinnedVal = pins.get(fieldPathKey);
|
|
508
|
+
if (!isRecord(pinnedVal) || !matchesScope(pinnedVal, subPins, condition, metadataDialect)) return false;
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
const target2 = readPathWithPins(record, fieldParts, pins, false);
|
|
512
|
+
const nestedMatched = target2.values.some((value) => {
|
|
513
|
+
if (Array.isArray(value)) {
|
|
514
|
+
return value.some((entry) => isRecord(entry) && matchesScope(entry, subPins, condition, metadataDialect));
|
|
515
|
+
}
|
|
516
|
+
return isRecord(value) && matchesScope(value, subPins, condition, metadataDialect);
|
|
517
|
+
});
|
|
518
|
+
if (!nestedMatched) return false;
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
const target = readPathWithPins(record, fieldParts, pins, false);
|
|
522
|
+
if (!evaluateOperatorGroup(target, [["$eq", condition]], metadataDialect)) return false;
|
|
523
|
+
}
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
function matchesScope(record, inheritedPins, filter, metadataDialect) {
|
|
527
|
+
if (!isRecord(filter)) return false;
|
|
528
|
+
const prefixes = [];
|
|
529
|
+
collectPositiveArrayPrefixes(filter, prefixes);
|
|
530
|
+
const expanded = expandPinsForPrefixes(record, inheritedPins, prefixes);
|
|
531
|
+
if (!expanded) return false;
|
|
532
|
+
return expanded.some((pins) => evaluateEntriesWithPins(record, pins, filter, metadataDialect));
|
|
533
|
+
}
|
|
534
|
+
function matchesCollectionFilter(record, filter, metadataDialect = "postgres") {
|
|
535
|
+
return matchesScope(record, /* @__PURE__ */ new Map(), filter, metadataDialect);
|
|
536
|
+
}
|
|
66
537
|
function extractSql(values) {
|
|
67
538
|
if (!values) return void 0;
|
|
68
539
|
if (values.sql !== void 0) return values.sql;
|
|
@@ -88,7 +559,17 @@ function prepareFields(values) {
|
|
|
88
559
|
values.timestamps = false;
|
|
89
560
|
}
|
|
90
561
|
const mapped = values.fields.map((field) => {
|
|
91
|
-
if (typeof field !== "string")
|
|
562
|
+
if (typeof field !== "string") {
|
|
563
|
+
if (isRecord(field.settings)) {
|
|
564
|
+
if (field.elementType === void 0 && typeof field.settings.elementType === "string") {
|
|
565
|
+
field.elementType = field.settings.elementType;
|
|
566
|
+
}
|
|
567
|
+
if (field.dataType === void 0 && typeof field.settings.dataType === "string") {
|
|
568
|
+
field.dataType = field.settings.dataType;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
return (0, import_postgres_collection_manager.applyPostgresArrayDataType)(field);
|
|
572
|
+
}
|
|
92
573
|
const preset = PRESET_FIELDS[field];
|
|
93
574
|
if (!preset) throw new import_validation_error.PostgresValidationError("Unknown preset field {{name}}", { name: field });
|
|
94
575
|
return (0, import_lodash.cloneDeep)(preset);
|
|
@@ -415,10 +896,11 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
415
896
|
});
|
|
416
897
|
this.app.resourceManager.use(
|
|
417
898
|
async (ctx, next) => {
|
|
418
|
-
var _a2, _b2;
|
|
899
|
+
var _a2, _b2, _c;
|
|
419
900
|
const { resourceName, actionName, params } = ctx.action || {};
|
|
420
901
|
if (resourceName === "dataSources.collections") {
|
|
421
|
-
const
|
|
902
|
+
const dataSourceKey = ((_a2 = ctx.action) == null ? void 0 : _a2.sourceId) || (params == null ? void 0 : params.associatedIndex);
|
|
903
|
+
const dataSource = dataSourceKey ? this.app.dataSourceManager.get(dataSourceKey) : void 0;
|
|
422
904
|
if (dataSource instanceof import_postgres_datasource.PostgresDataSource) {
|
|
423
905
|
if (actionName === "create" || actionName === "destroy") {
|
|
424
906
|
await this.mutateCollection(ctx, dataSource, next);
|
|
@@ -428,7 +910,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
428
910
|
const sql = extractSql(params.values);
|
|
429
911
|
if (sql !== void 0) {
|
|
430
912
|
const existing = dataSource.collectionManager.getCollection(params.filterByTk);
|
|
431
|
-
if (!((
|
|
913
|
+
if (!((_b2 = existing == null ? void 0 : existing.options) == null ? void 0 : _b2.sql)) {
|
|
432
914
|
ctx.throw(400, (0, import_validation_error.translate)(ctx, "Only SQL collections can update SQL queries"));
|
|
433
915
|
}
|
|
434
916
|
(0, import_sql_validator.checkSQL)(typeof sql === "string" ? sql : "");
|
|
@@ -451,7 +933,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
451
933
|
const separator = index.indexOf(".");
|
|
452
934
|
const indexDataSourceKey = separator !== -1 ? index.slice(0, separator) : "";
|
|
453
935
|
const indexCollectionName = separator !== -1 ? index.slice(separator + 1) : "";
|
|
454
|
-
const rawBody = (
|
|
936
|
+
const rawBody = (_c = ctx.request) == null ? void 0 : _c.body;
|
|
455
937
|
const input = params.values || (isRecord(rawBody) ? rawBody : {});
|
|
456
938
|
const dataSourceKey = actionName === "apply" && typeof input.dataSourceKey === "string" && input.dataSourceKey ? input.dataSourceKey : indexDataSourceKey;
|
|
457
939
|
const collectionName = actionName === "apply" && typeof input.collectionName === "string" && input.collectionName ? input.collectionName : indexCollectionName;
|
|
@@ -466,6 +948,83 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
466
948
|
{ tag: "postgres-schema-actions", after: ["acl"] }
|
|
467
949
|
);
|
|
468
950
|
}
|
|
951
|
+
async resolveCollectionsToDestroy(ctx, source) {
|
|
952
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
953
|
+
const params = ((_a = ctx.action) == null ? void 0 : _a.params) || {};
|
|
954
|
+
const permission = ctx.permission;
|
|
955
|
+
const rawFilter = params.filter;
|
|
956
|
+
const scopeFilter = ((_b = permission == null ? void 0 : permission.parsedParams) == null ? void 0 : _b.filter) ?? ((_d = (_c = permission == null ? void 0 : permission.can) == null ? void 0 : _c.params) == null ? void 0 : _d.filter);
|
|
957
|
+
const hasRawFilter = (0, import_utils.isValidFilter)(rawFilter);
|
|
958
|
+
const hasScopeFilter = (0, import_utils.isValidFilter)(scopeFilter);
|
|
959
|
+
const effectiveFilter = hasRawFilter && hasScopeFilter && !(0, import_lodash.isEqual)(rawFilter, scopeFilter) ? { $and: [rawFilter, scopeFilter] } : hasRawFilter ? rawFilter : hasScopeFilter ? scopeFilter : void 0;
|
|
960
|
+
const hasFilterByTk = params.filterByTk !== void 0 && params.filterByTk !== null;
|
|
961
|
+
let requestedNames;
|
|
962
|
+
if (hasFilterByTk) {
|
|
963
|
+
if (Array.isArray(params.filterByTk) && params.filterByTk.length === 0) {
|
|
964
|
+
return [];
|
|
965
|
+
}
|
|
966
|
+
requestedNames = names(params.filterByTk);
|
|
967
|
+
} else if (!effectiveFilter) {
|
|
968
|
+
throw new import_validation_error.PostgresValidationError("A collection name is required");
|
|
969
|
+
}
|
|
970
|
+
const manager = source.collectionManager;
|
|
971
|
+
if (!effectiveFilter) {
|
|
972
|
+
for (const name of requestedNames ?? []) {
|
|
973
|
+
if (!manager.getCollection(name)) {
|
|
974
|
+
ctx.throw(404, (0, import_validation_error.translate)(ctx, "Collection {{name}} does not exist", { name }));
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
return requestedNames ?? [];
|
|
978
|
+
}
|
|
979
|
+
const db = this.app.db || ctx.db;
|
|
980
|
+
const metadataDialect = ((_f = (_e = db == null ? void 0 : db.sequelize) == null ? void 0 : _e.getDialect) == null ? void 0 : _f.call(_e)) ?? "postgres";
|
|
981
|
+
const collectionsRepo = (_g = db == null ? void 0 : db.getRepository) == null ? void 0 : _g.call(db, "dataSourcesCollections");
|
|
982
|
+
if (collectionsRepo && typeof collectionsRepo.find === "function") {
|
|
983
|
+
const baseFilter = {
|
|
984
|
+
dataSourceKey: source.name,
|
|
985
|
+
...requestedNames ? { name: { $in: requestedNames } } : {}
|
|
986
|
+
};
|
|
987
|
+
const persistedAll = await collectionsRepo.find({ filter: baseFilter, context: ctx });
|
|
988
|
+
const persistedNames = new Set(persistedAll.map((row) => String(row.get("name"))));
|
|
989
|
+
const persistedMatched = await collectionsRepo.find({
|
|
990
|
+
filter: { $and: [baseFilter, effectiveFilter] },
|
|
991
|
+
context: ctx
|
|
992
|
+
});
|
|
993
|
+
const matchedSet = new Set(persistedMatched.map((row) => String(row.get("name"))));
|
|
994
|
+
for (const collection of manager.getCollections()) {
|
|
995
|
+
if (persistedNames.has(collection.name)) continue;
|
|
996
|
+
if (requestedNames && !requestedNames.includes(collection.name)) continue;
|
|
997
|
+
const record = {
|
|
998
|
+
...collection.options,
|
|
999
|
+
name: collection.name,
|
|
1000
|
+
dataSourceKey: source.name,
|
|
1001
|
+
dataSources: { key: source.name, type: import_constants.DATA_SOURCE_TYPE },
|
|
1002
|
+
options: collection.options,
|
|
1003
|
+
fields: collection.getFields().map((field) => field.options)
|
|
1004
|
+
};
|
|
1005
|
+
if (matchesCollectionFilter(record, effectiveFilter, metadataDialect)) {
|
|
1006
|
+
matchedSet.add(collection.name);
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
return requestedNames ? requestedNames.filter((name) => matchedSet.has(name)) : [...matchedSet];
|
|
1010
|
+
}
|
|
1011
|
+
const matchedRuntime = /* @__PURE__ */ new Set();
|
|
1012
|
+
for (const collection of manager.getCollections()) {
|
|
1013
|
+
if (requestedNames && !requestedNames.includes(collection.name)) continue;
|
|
1014
|
+
const record = {
|
|
1015
|
+
...collection.options,
|
|
1016
|
+
name: collection.name,
|
|
1017
|
+
dataSourceKey: source.name,
|
|
1018
|
+
dataSources: { key: source.name, type: import_constants.DATA_SOURCE_TYPE },
|
|
1019
|
+
options: collection.options,
|
|
1020
|
+
fields: collection.getFields().map((field) => field.options)
|
|
1021
|
+
};
|
|
1022
|
+
if (matchesCollectionFilter(record, effectiveFilter, metadataDialect)) {
|
|
1023
|
+
matchedRuntime.add(collection.name);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
return requestedNames ? requestedNames.filter((name) => matchedRuntime.has(name)) : [...matchedRuntime];
|
|
1027
|
+
}
|
|
469
1028
|
async mutateCollection(ctx, source, next) {
|
|
470
1029
|
var _a, _b, _c, _d, _e, _f;
|
|
471
1030
|
const manager = source.collectionManager;
|
|
@@ -513,22 +1072,32 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
513
1072
|
await this.sendSyncMessage({ type: "collectionCreated", dataSourceKey: source.name, collectionName: name });
|
|
514
1073
|
return;
|
|
515
1074
|
}
|
|
516
|
-
const collectionNames =
|
|
517
|
-
|
|
1075
|
+
const collectionNames = await this.resolveCollectionsToDestroy(ctx, source);
|
|
1076
|
+
if (collectionNames.length === 0) {
|
|
1077
|
+
ctx.body = true;
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
const snapshots = collectionNames.flatMap((name) => {
|
|
518
1081
|
const collection = manager.getCollection(name);
|
|
519
|
-
if (!collection)
|
|
520
|
-
return
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
1082
|
+
if (!collection) return [];
|
|
1083
|
+
return [
|
|
1084
|
+
{
|
|
1085
|
+
name,
|
|
1086
|
+
physicalName: String(collection.options.tableName || name),
|
|
1087
|
+
isSql: Boolean(collection.options.sql),
|
|
1088
|
+
options: (0, import_lodash.cloneDeep)({
|
|
1089
|
+
...collection.options,
|
|
1090
|
+
tableName: String(collection.options.tableName || name),
|
|
1091
|
+
fields: collection.getFields().map((field) => field.options)
|
|
1092
|
+
})
|
|
1093
|
+
}
|
|
1094
|
+
];
|
|
530
1095
|
});
|
|
531
1096
|
const cascade = params.cascade === true || params.cascade === "true";
|
|
1097
|
+
const hadFilterByTk = Object.prototype.hasOwnProperty.call(params, "filterByTk");
|
|
1098
|
+
const hadFilter = Object.prototype.hasOwnProperty.call(params, "filter");
|
|
1099
|
+
const originalFilterByTk = params.filterByTk;
|
|
1100
|
+
const originalFilter = params.filter;
|
|
532
1101
|
try {
|
|
533
1102
|
await manager.db.sequelize.transaction(async (transaction) => {
|
|
534
1103
|
for (const snapshot of [...snapshots].sort(
|
|
@@ -540,15 +1109,21 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
540
1109
|
});
|
|
541
1110
|
if (!snapshot.isSql) await manager.dropTable(snapshot.name, cascade, transaction);
|
|
542
1111
|
}
|
|
1112
|
+
params.filterByTk = collectionNames;
|
|
1113
|
+
delete params.filter;
|
|
543
1114
|
await next();
|
|
544
1115
|
});
|
|
545
1116
|
} catch (error) {
|
|
1117
|
+
if (hadFilterByTk) params.filterByTk = originalFilterByTk;
|
|
1118
|
+
else delete params.filterByTk;
|
|
1119
|
+
if (hadFilter) params.filter = originalFilter;
|
|
1120
|
+
else delete params.filter;
|
|
546
1121
|
for (const snapshot of snapshots) {
|
|
547
1122
|
if (!manager.hasCollection(snapshot.name)) manager.defineCollection(snapshot.options);
|
|
548
1123
|
}
|
|
549
1124
|
throw error;
|
|
550
1125
|
}
|
|
551
|
-
const fieldsRepo = (_d = (_c = this.app.db) == null ? void 0 : _c.getRepository) == null ? void 0 : _d.call(_c, "dataSourcesFields");
|
|
1126
|
+
const fieldsRepo = (_d = (_c = this.app.db || ctx.db) == null ? void 0 : _c.getRepository) == null ? void 0 : _d.call(_c, "dataSourcesFields");
|
|
552
1127
|
if (fieldsRepo && typeof fieldsRepo.destroy === "function") {
|
|
553
1128
|
try {
|
|
554
1129
|
await fieldsRepo.destroy({
|
|
@@ -572,7 +1147,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
572
1147
|
await this.sendSyncMessage({
|
|
573
1148
|
type: "collectionRemoved",
|
|
574
1149
|
dataSourceKey: source.name,
|
|
575
|
-
collectionNames
|
|
1150
|
+
collectionNames
|
|
576
1151
|
});
|
|
577
1152
|
}
|
|
578
1153
|
async mutateField(ctx, source, collectionName, input, next) {
|
|
@@ -633,6 +1208,14 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
633
1208
|
});
|
|
634
1209
|
}
|
|
635
1210
|
await (0, import_field_hooks.syncFieldAction)(manager, action, collectionName, fieldName, input, transaction);
|
|
1211
|
+
if (input.dataType === "array" && isRecord(params.values)) {
|
|
1212
|
+
if (params.values.dataType === void 0 || params.values.dataType === null || params.values.dataType === "") {
|
|
1213
|
+
params.values.dataType = "array";
|
|
1214
|
+
}
|
|
1215
|
+
if (params.values.elementType === void 0 && typeof input.elementType === "string") {
|
|
1216
|
+
params.values.elementType = input.elementType;
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
636
1219
|
metadataActionStarted = true;
|
|
637
1220
|
await next();
|
|
638
1221
|
});
|