plugin-data-source-postgres 2.0.0 → 2.1.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/client/index.js +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/constants.js +3 -0
- package/dist/externalVersion.js +10 -8
- package/dist/locale/en-US.json +8 -1
- package/dist/locale/zh-CN.json +8 -1
- 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 +727 -23
- 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/client/ConfigurationForm.tsx +312 -133
- package/src/client/__tests__/client-v1.test.tsx +80 -0
- package/src/client-v2/PostgresConfigForm.tsx +133 -6
- package/src/client-v2/__tests__/plugin.test.tsx +69 -0
- package/src/client-v2/plugin.tsx +2 -2
- package/src/constants.ts +15 -0
- package/src/locale/en-US.json +8 -1
- package/src/locale/zh-CN.json +8 -1
- package/src/server/__tests__/postgres-datasource.unit.test.ts +1038 -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 +912 -18
- 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/client-v2/985.34da95acdf7743ab.js +0 -10
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);
|
|
@@ -104,6 +585,80 @@ function prepareFields(values) {
|
|
|
104
585
|
}
|
|
105
586
|
values.fields = mapped;
|
|
106
587
|
}
|
|
588
|
+
function toSavedPostgresConnection(record) {
|
|
589
|
+
const key = String(record.get("key") || "").trim();
|
|
590
|
+
const options = record.get("options");
|
|
591
|
+
if (!key || !isRecord(options)) return null;
|
|
592
|
+
const host = typeof options.host === "string" ? options.host.trim() : "";
|
|
593
|
+
const username = typeof options.username === "string" ? options.username.trim() : "";
|
|
594
|
+
if (!host || !username) return null;
|
|
595
|
+
const rawPort = Number(options.port);
|
|
596
|
+
const port = Number.isInteger(rawPort) && rawPort >= 1 && rawPort <= 65535 ? rawPort : import_constants.DEFAULT_PORT;
|
|
597
|
+
const database = typeof options.database === "string" ? options.database.trim() : "";
|
|
598
|
+
const schema = typeof options.schema === "string" && options.schema.trim() ? options.schema.trim() : import_constants.DEFAULT_SCHEMA;
|
|
599
|
+
const displayName = String(record.get("displayName") || key);
|
|
600
|
+
return {
|
|
601
|
+
key,
|
|
602
|
+
displayName,
|
|
603
|
+
host,
|
|
604
|
+
port,
|
|
605
|
+
database,
|
|
606
|
+
schema,
|
|
607
|
+
username,
|
|
608
|
+
ssl: Boolean(options.ssl),
|
|
609
|
+
hasPassword: typeof options.password === "string" && options.password.length > 0,
|
|
610
|
+
hasSslCa: typeof options.sslCa === "string" && options.sslCa.trim().length > 0
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
async function resolveSavedConnectionOptions(ctx, targetOptions) {
|
|
614
|
+
var _a, _b, _c;
|
|
615
|
+
const rawSourceKey = targetOptions.sourceConnectionKey;
|
|
616
|
+
if ("sourceConnectionKey" in targetOptions) {
|
|
617
|
+
delete targetOptions.sourceConnectionKey;
|
|
618
|
+
}
|
|
619
|
+
const sourceKey = typeof rawSourceKey === "string" ? rawSourceKey.trim() : "";
|
|
620
|
+
if (!sourceKey) return;
|
|
621
|
+
const repo = (_b = (_a = ctx.db) == null ? void 0 : _a.getRepository) == null ? void 0 : _b.call(_a, "dataSources");
|
|
622
|
+
const sourceRecord = await ((_c = repo == null ? void 0 : repo.findOne) == null ? void 0 : _c.call(repo, {
|
|
623
|
+
filter: { key: sourceKey, type: import_constants.DATA_SOURCE_TYPE }
|
|
624
|
+
}));
|
|
625
|
+
if (!sourceRecord) {
|
|
626
|
+
throw new import_validation_error.PostgresValidationError("Saved PostgreSQL connection {{name}} does not exist", { name: sourceKey });
|
|
627
|
+
}
|
|
628
|
+
const sourceOptions = sourceRecord.get("options");
|
|
629
|
+
if (!isRecord(sourceOptions)) return;
|
|
630
|
+
if (!targetOptions.host && typeof sourceOptions.host === "string") {
|
|
631
|
+
targetOptions.host = sourceOptions.host;
|
|
632
|
+
}
|
|
633
|
+
if ((targetOptions.port === void 0 || targetOptions.port === null || targetOptions.port === "") && sourceOptions.port !== void 0) {
|
|
634
|
+
targetOptions.port = sourceOptions.port;
|
|
635
|
+
}
|
|
636
|
+
if (!targetOptions.database && typeof sourceOptions.database === "string") {
|
|
637
|
+
targetOptions.database = sourceOptions.database;
|
|
638
|
+
}
|
|
639
|
+
if (!targetOptions.schema && typeof sourceOptions.schema === "string") {
|
|
640
|
+
targetOptions.schema = sourceOptions.schema;
|
|
641
|
+
}
|
|
642
|
+
if (!targetOptions.username && typeof sourceOptions.username === "string") {
|
|
643
|
+
targetOptions.username = sourceOptions.username;
|
|
644
|
+
}
|
|
645
|
+
if (targetOptions.ssl === void 0 && sourceOptions.ssl !== void 0) {
|
|
646
|
+
targetOptions.ssl = Boolean(sourceOptions.ssl);
|
|
647
|
+
}
|
|
648
|
+
const sameHost = String(targetOptions.host || "").trim() === String(sourceOptions.host || "").trim();
|
|
649
|
+
const samePort = Number(targetOptions.port ?? import_constants.DEFAULT_PORT) === Number(sourceOptions.port ?? import_constants.DEFAULT_PORT);
|
|
650
|
+
const sameUsername = String(targetOptions.username || "").trim() === String(sourceOptions.username || "").trim();
|
|
651
|
+
if (sameHost && samePort && sameUsername) {
|
|
652
|
+
const hasCustomPassword = typeof targetOptions.password === "string" && targetOptions.password.length > 0;
|
|
653
|
+
if (!hasCustomPassword && typeof sourceOptions.password === "string" && sourceOptions.password.length > 0) {
|
|
654
|
+
targetOptions.password = sourceOptions.password;
|
|
655
|
+
}
|
|
656
|
+
const hasCustomSslCa = typeof targetOptions.sslCa === "string" && targetOptions.sslCa.trim().length > 0;
|
|
657
|
+
if (!hasCustomSslCa && targetOptions.ssl !== false && typeof sourceOptions.sslCa === "string" && sourceOptions.sslCa.trim().length > 0) {
|
|
658
|
+
targetOptions.sslCa = sourceOptions.sslCa;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
107
662
|
async function localizeValidationErrors(ctx, next) {
|
|
108
663
|
var _a, _b, _c, _d, _e;
|
|
109
664
|
try {
|
|
@@ -345,7 +900,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
345
900
|
});
|
|
346
901
|
}
|
|
347
902
|
async load() {
|
|
348
|
-
var _a, _b;
|
|
903
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
349
904
|
(_b = (_a = this.app.db) == null ? void 0 : _a.on) == null ? void 0 : _b.call(
|
|
350
905
|
_a,
|
|
351
906
|
"dataSources.afterDestroy",
|
|
@@ -370,13 +925,35 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
370
925
|
);
|
|
371
926
|
}
|
|
372
927
|
);
|
|
928
|
+
const listSavedConnectionsHandler = async (ctx, next) => {
|
|
929
|
+
const records = await ctx.db.getRepository("dataSources").find({
|
|
930
|
+
filter: { type: import_constants.DATA_SOURCE_TYPE },
|
|
931
|
+
sort: ["createdAt"]
|
|
932
|
+
});
|
|
933
|
+
ctx.body = records.map((record) => toSavedPostgresConnection(record)).filter((item) => item !== null);
|
|
934
|
+
await next();
|
|
935
|
+
};
|
|
936
|
+
const hasDefinedDataSources = Boolean((_d = (_c = this.app.resourceManager).isDefined) == null ? void 0 : _d.call(_c, "dataSources"));
|
|
937
|
+
if (hasDefinedDataSources) {
|
|
938
|
+
const dataSourcesResource = (_f = (_e = this.app.resourceManager).getResource) == null ? void 0 : _f.call(_e, "dataSources");
|
|
939
|
+
if (dataSourcesResource && !((_g = dataSourcesResource.actions) == null ? void 0 : _g.has("listPostgresConnections"))) {
|
|
940
|
+
(_h = dataSourcesResource.addAction) == null ? void 0 : _h.call(dataSourcesResource, "listPostgresConnections", listSavedConnectionsHandler);
|
|
941
|
+
}
|
|
942
|
+
} else {
|
|
943
|
+
(_j = (_i = this.app.resourceManager).registerActionHandlers) == null ? void 0 : _j.call(_i, {
|
|
944
|
+
[import_constants.LIST_SAVED_CONNECTIONS_ACTION]: listSavedConnectionsHandler
|
|
945
|
+
});
|
|
946
|
+
}
|
|
373
947
|
this.app.resourceManager.registerPreActionHandler("dataSources:readTables", async (ctx, next) => {
|
|
374
|
-
var _a2, _b2,
|
|
948
|
+
var _a2, _b2, _c2;
|
|
375
949
|
const { dataSourceKey, dbOptions } = ctx.action.params.values || {};
|
|
376
950
|
if ((dbOptions == null ? void 0 : dbOptions.type) !== import_constants.DATA_SOURCE_TYPE) {
|
|
377
951
|
await next();
|
|
378
952
|
return;
|
|
379
953
|
}
|
|
954
|
+
if (isRecord(dbOptions)) {
|
|
955
|
+
await resolveSavedConnectionOptions(ctx, dbOptions);
|
|
956
|
+
}
|
|
380
957
|
const manager = this.app.dataSourceManager;
|
|
381
958
|
const renderedOptions = ((_a2 = this.app.environment) == null ? void 0 : _a2.renderJsonTemplate) ? this.app.environment.renderJsonTemplate(dbOptions) : dbOptions;
|
|
382
959
|
const probe = manager.factory.create(import_constants.DATA_SOURCE_TYPE, {
|
|
@@ -398,7 +975,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
398
975
|
await probe.close();
|
|
399
976
|
} catch (error) {
|
|
400
977
|
if (requestFailed) {
|
|
401
|
-
(
|
|
978
|
+
(_c2 = (_b2 = this.app.log) == null ? void 0 : _b2.warn) == null ? void 0 : _c2.call(_b2, "[PostgreSQL] Failed to close temporary table-list connection");
|
|
402
979
|
} else {
|
|
403
980
|
requestFailed = true;
|
|
404
981
|
requestError = error;
|
|
@@ -415,10 +992,11 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
415
992
|
});
|
|
416
993
|
this.app.resourceManager.use(
|
|
417
994
|
async (ctx, next) => {
|
|
418
|
-
var _a2, _b2;
|
|
995
|
+
var _a2, _b2, _c2;
|
|
419
996
|
const { resourceName, actionName, params } = ctx.action || {};
|
|
420
997
|
if (resourceName === "dataSources.collections") {
|
|
421
|
-
const
|
|
998
|
+
const dataSourceKey = ((_a2 = ctx.action) == null ? void 0 : _a2.sourceId) || (params == null ? void 0 : params.associatedIndex);
|
|
999
|
+
const dataSource = dataSourceKey ? this.app.dataSourceManager.get(dataSourceKey) : void 0;
|
|
422
1000
|
if (dataSource instanceof import_postgres_datasource.PostgresDataSource) {
|
|
423
1001
|
if (actionName === "create" || actionName === "destroy") {
|
|
424
1002
|
await this.mutateCollection(ctx, dataSource, next);
|
|
@@ -428,7 +1006,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
428
1006
|
const sql = extractSql(params.values);
|
|
429
1007
|
if (sql !== void 0) {
|
|
430
1008
|
const existing = dataSource.collectionManager.getCollection(params.filterByTk);
|
|
431
|
-
if (!((
|
|
1009
|
+
if (!((_b2 = existing == null ? void 0 : existing.options) == null ? void 0 : _b2.sql)) {
|
|
432
1010
|
ctx.throw(400, (0, import_validation_error.translate)(ctx, "Only SQL collections can update SQL queries"));
|
|
433
1011
|
}
|
|
434
1012
|
(0, import_sql_validator.checkSQL)(typeof sql === "string" ? sql : "");
|
|
@@ -451,7 +1029,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
451
1029
|
const separator = index.indexOf(".");
|
|
452
1030
|
const indexDataSourceKey = separator !== -1 ? index.slice(0, separator) : "";
|
|
453
1031
|
const indexCollectionName = separator !== -1 ? index.slice(separator + 1) : "";
|
|
454
|
-
const rawBody = (
|
|
1032
|
+
const rawBody = (_c2 = ctx.request) == null ? void 0 : _c2.body;
|
|
455
1033
|
const input = params.values || (isRecord(rawBody) ? rawBody : {});
|
|
456
1034
|
const dataSourceKey = actionName === "apply" && typeof input.dataSourceKey === "string" && input.dataSourceKey ? input.dataSourceKey : indexDataSourceKey;
|
|
457
1035
|
const collectionName = actionName === "apply" && typeof input.collectionName === "string" && input.collectionName ? input.collectionName : indexCollectionName;
|
|
@@ -465,6 +1043,108 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
465
1043
|
},
|
|
466
1044
|
{ tag: "postgres-schema-actions", after: ["acl"] }
|
|
467
1045
|
);
|
|
1046
|
+
this.app.resourceManager.use(
|
|
1047
|
+
async (ctx, next) => {
|
|
1048
|
+
var _a2, _b2, _c2, _d2, _e2;
|
|
1049
|
+
const { resourceName, actionName, params } = ctx.action || {};
|
|
1050
|
+
if (resourceName === "dataSources") {
|
|
1051
|
+
const values = isRecord(params == null ? void 0 : params.values) ? params.values : void 0;
|
|
1052
|
+
if (actionName === "readTables" && isRecord(values == null ? void 0 : values.dbOptions) && values.dbOptions.type === import_constants.DATA_SOURCE_TYPE) {
|
|
1053
|
+
await resolveSavedConnectionOptions(ctx, values.dbOptions);
|
|
1054
|
+
} else if ((actionName === "testConnection" || actionName === "create") && (values == null ? void 0 : values.type) === import_constants.DATA_SOURCE_TYPE && isRecord(values.options)) {
|
|
1055
|
+
await resolveSavedConnectionOptions(ctx, values.options);
|
|
1056
|
+
} else if (actionName === "update" && isRecord(values == null ? void 0 : values.options) && "sourceConnectionKey" in values.options) {
|
|
1057
|
+
let isPostgresTarget = values.type === import_constants.DATA_SOURCE_TYPE || this.app.dataSourceManager.get(params == null ? void 0 : params.filterByTk) instanceof import_postgres_datasource.PostgresDataSource;
|
|
1058
|
+
if (!isPostgresTarget && (params == null ? void 0 : params.filterByTk)) {
|
|
1059
|
+
const target = await ((_d2 = (_c2 = (_b2 = (_a2 = ctx.db) == null ? void 0 : _a2.getRepository) == null ? void 0 : _b2.call(_a2, "dataSources")) == null ? void 0 : _c2.findOne) == null ? void 0 : _d2.call(_c2, { filterByTk: params.filterByTk }));
|
|
1060
|
+
isPostgresTarget = ((_e2 = target == null ? void 0 : target.get) == null ? void 0 : _e2.call(target, "type")) === import_constants.DATA_SOURCE_TYPE;
|
|
1061
|
+
}
|
|
1062
|
+
if (isPostgresTarget) {
|
|
1063
|
+
await resolveSavedConnectionOptions(ctx, values.options);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
await next();
|
|
1068
|
+
},
|
|
1069
|
+
{ tag: "postgres-saved-connection", after: ["acl"], before: ["default"] }
|
|
1070
|
+
);
|
|
1071
|
+
}
|
|
1072
|
+
async resolveCollectionsToDestroy(ctx, source) {
|
|
1073
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1074
|
+
const params = ((_a = ctx.action) == null ? void 0 : _a.params) || {};
|
|
1075
|
+
const permission = ctx.permission;
|
|
1076
|
+
const rawFilter = params.filter;
|
|
1077
|
+
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);
|
|
1078
|
+
const hasRawFilter = (0, import_utils.isValidFilter)(rawFilter);
|
|
1079
|
+
const hasScopeFilter = (0, import_utils.isValidFilter)(scopeFilter);
|
|
1080
|
+
const effectiveFilter = hasRawFilter && hasScopeFilter && !(0, import_lodash.isEqual)(rawFilter, scopeFilter) ? { $and: [rawFilter, scopeFilter] } : hasRawFilter ? rawFilter : hasScopeFilter ? scopeFilter : void 0;
|
|
1081
|
+
const hasFilterByTk = params.filterByTk !== void 0 && params.filterByTk !== null;
|
|
1082
|
+
let requestedNames;
|
|
1083
|
+
if (hasFilterByTk) {
|
|
1084
|
+
if (Array.isArray(params.filterByTk) && params.filterByTk.length === 0) {
|
|
1085
|
+
return [];
|
|
1086
|
+
}
|
|
1087
|
+
requestedNames = names(params.filterByTk);
|
|
1088
|
+
} else if (!effectiveFilter) {
|
|
1089
|
+
throw new import_validation_error.PostgresValidationError("A collection name is required");
|
|
1090
|
+
}
|
|
1091
|
+
const manager = source.collectionManager;
|
|
1092
|
+
if (!effectiveFilter) {
|
|
1093
|
+
for (const name of requestedNames ?? []) {
|
|
1094
|
+
if (!manager.getCollection(name)) {
|
|
1095
|
+
ctx.throw(404, (0, import_validation_error.translate)(ctx, "Collection {{name}} does not exist", { name }));
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
return requestedNames ?? [];
|
|
1099
|
+
}
|
|
1100
|
+
const db = this.app.db || ctx.db;
|
|
1101
|
+
const metadataDialect = ((_f = (_e = db == null ? void 0 : db.sequelize) == null ? void 0 : _e.getDialect) == null ? void 0 : _f.call(_e)) ?? "postgres";
|
|
1102
|
+
const collectionsRepo = (_g = db == null ? void 0 : db.getRepository) == null ? void 0 : _g.call(db, "dataSourcesCollections");
|
|
1103
|
+
if (collectionsRepo && typeof collectionsRepo.find === "function") {
|
|
1104
|
+
const baseFilter = {
|
|
1105
|
+
dataSourceKey: source.name,
|
|
1106
|
+
...requestedNames ? { name: { $in: requestedNames } } : {}
|
|
1107
|
+
};
|
|
1108
|
+
const persistedAll = await collectionsRepo.find({ filter: baseFilter, context: ctx });
|
|
1109
|
+
const persistedNames = new Set(persistedAll.map((row) => String(row.get("name"))));
|
|
1110
|
+
const persistedMatched = await collectionsRepo.find({
|
|
1111
|
+
filter: { $and: [baseFilter, effectiveFilter] },
|
|
1112
|
+
context: ctx
|
|
1113
|
+
});
|
|
1114
|
+
const matchedSet = new Set(persistedMatched.map((row) => String(row.get("name"))));
|
|
1115
|
+
for (const collection of manager.getCollections()) {
|
|
1116
|
+
if (persistedNames.has(collection.name)) continue;
|
|
1117
|
+
if (requestedNames && !requestedNames.includes(collection.name)) continue;
|
|
1118
|
+
const record = {
|
|
1119
|
+
...collection.options,
|
|
1120
|
+
name: collection.name,
|
|
1121
|
+
dataSourceKey: source.name,
|
|
1122
|
+
dataSources: { key: source.name, type: import_constants.DATA_SOURCE_TYPE },
|
|
1123
|
+
options: collection.options,
|
|
1124
|
+
fields: collection.getFields().map((field) => field.options)
|
|
1125
|
+
};
|
|
1126
|
+
if (matchesCollectionFilter(record, effectiveFilter, metadataDialect)) {
|
|
1127
|
+
matchedSet.add(collection.name);
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
return requestedNames ? requestedNames.filter((name) => matchedSet.has(name)) : [...matchedSet];
|
|
1131
|
+
}
|
|
1132
|
+
const matchedRuntime = /* @__PURE__ */ new Set();
|
|
1133
|
+
for (const collection of manager.getCollections()) {
|
|
1134
|
+
if (requestedNames && !requestedNames.includes(collection.name)) continue;
|
|
1135
|
+
const record = {
|
|
1136
|
+
...collection.options,
|
|
1137
|
+
name: collection.name,
|
|
1138
|
+
dataSourceKey: source.name,
|
|
1139
|
+
dataSources: { key: source.name, type: import_constants.DATA_SOURCE_TYPE },
|
|
1140
|
+
options: collection.options,
|
|
1141
|
+
fields: collection.getFields().map((field) => field.options)
|
|
1142
|
+
};
|
|
1143
|
+
if (matchesCollectionFilter(record, effectiveFilter, metadataDialect)) {
|
|
1144
|
+
matchedRuntime.add(collection.name);
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
return requestedNames ? requestedNames.filter((name) => matchedRuntime.has(name)) : [...matchedRuntime];
|
|
468
1148
|
}
|
|
469
1149
|
async mutateCollection(ctx, source, next) {
|
|
470
1150
|
var _a, _b, _c, _d, _e, _f;
|
|
@@ -513,22 +1193,32 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
513
1193
|
await this.sendSyncMessage({ type: "collectionCreated", dataSourceKey: source.name, collectionName: name });
|
|
514
1194
|
return;
|
|
515
1195
|
}
|
|
516
|
-
const collectionNames =
|
|
517
|
-
|
|
1196
|
+
const collectionNames = await this.resolveCollectionsToDestroy(ctx, source);
|
|
1197
|
+
if (collectionNames.length === 0) {
|
|
1198
|
+
ctx.body = true;
|
|
1199
|
+
return;
|
|
1200
|
+
}
|
|
1201
|
+
const snapshots = collectionNames.flatMap((name) => {
|
|
518
1202
|
const collection = manager.getCollection(name);
|
|
519
|
-
if (!collection)
|
|
520
|
-
return
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
1203
|
+
if (!collection) return [];
|
|
1204
|
+
return [
|
|
1205
|
+
{
|
|
1206
|
+
name,
|
|
1207
|
+
physicalName: String(collection.options.tableName || name),
|
|
1208
|
+
isSql: Boolean(collection.options.sql),
|
|
1209
|
+
options: (0, import_lodash.cloneDeep)({
|
|
1210
|
+
...collection.options,
|
|
1211
|
+
tableName: String(collection.options.tableName || name),
|
|
1212
|
+
fields: collection.getFields().map((field) => field.options)
|
|
1213
|
+
})
|
|
1214
|
+
}
|
|
1215
|
+
];
|
|
530
1216
|
});
|
|
531
1217
|
const cascade = params.cascade === true || params.cascade === "true";
|
|
1218
|
+
const hadFilterByTk = Object.prototype.hasOwnProperty.call(params, "filterByTk");
|
|
1219
|
+
const hadFilter = Object.prototype.hasOwnProperty.call(params, "filter");
|
|
1220
|
+
const originalFilterByTk = params.filterByTk;
|
|
1221
|
+
const originalFilter = params.filter;
|
|
532
1222
|
try {
|
|
533
1223
|
await manager.db.sequelize.transaction(async (transaction) => {
|
|
534
1224
|
for (const snapshot of [...snapshots].sort(
|
|
@@ -540,15 +1230,21 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
540
1230
|
});
|
|
541
1231
|
if (!snapshot.isSql) await manager.dropTable(snapshot.name, cascade, transaction);
|
|
542
1232
|
}
|
|
1233
|
+
params.filterByTk = collectionNames;
|
|
1234
|
+
delete params.filter;
|
|
543
1235
|
await next();
|
|
544
1236
|
});
|
|
545
1237
|
} catch (error) {
|
|
1238
|
+
if (hadFilterByTk) params.filterByTk = originalFilterByTk;
|
|
1239
|
+
else delete params.filterByTk;
|
|
1240
|
+
if (hadFilter) params.filter = originalFilter;
|
|
1241
|
+
else delete params.filter;
|
|
546
1242
|
for (const snapshot of snapshots) {
|
|
547
1243
|
if (!manager.hasCollection(snapshot.name)) manager.defineCollection(snapshot.options);
|
|
548
1244
|
}
|
|
549
1245
|
throw error;
|
|
550
1246
|
}
|
|
551
|
-
const fieldsRepo = (_d = (_c = this.app.db) == null ? void 0 : _c.getRepository) == null ? void 0 : _d.call(_c, "dataSourcesFields");
|
|
1247
|
+
const fieldsRepo = (_d = (_c = this.app.db || ctx.db) == null ? void 0 : _c.getRepository) == null ? void 0 : _d.call(_c, "dataSourcesFields");
|
|
552
1248
|
if (fieldsRepo && typeof fieldsRepo.destroy === "function") {
|
|
553
1249
|
try {
|
|
554
1250
|
await fieldsRepo.destroy({
|
|
@@ -572,7 +1268,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
572
1268
|
await this.sendSyncMessage({
|
|
573
1269
|
type: "collectionRemoved",
|
|
574
1270
|
dataSourceKey: source.name,
|
|
575
|
-
collectionNames
|
|
1271
|
+
collectionNames
|
|
576
1272
|
});
|
|
577
1273
|
}
|
|
578
1274
|
async mutateField(ctx, source, collectionName, input, next) {
|
|
@@ -633,6 +1329,14 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
|
|
|
633
1329
|
});
|
|
634
1330
|
}
|
|
635
1331
|
await (0, import_field_hooks.syncFieldAction)(manager, action, collectionName, fieldName, input, transaction);
|
|
1332
|
+
if (input.dataType === "array" && isRecord(params.values)) {
|
|
1333
|
+
if (params.values.dataType === void 0 || params.values.dataType === null || params.values.dataType === "") {
|
|
1334
|
+
params.values.dataType = "array";
|
|
1335
|
+
}
|
|
1336
|
+
if (params.values.elementType === void 0 && typeof input.elementType === "string") {
|
|
1337
|
+
params.values.elementType = input.elementType;
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
636
1340
|
metadataActionStarted = true;
|
|
637
1341
|
await next();
|
|
638
1342
|
});
|