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/src/server/plugin.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type { Context, Next } from '@nocobase/actions';
|
|
|
11
11
|
import type { DataSource, LoadedCollections } from '@nocobase/data-source-manager';
|
|
12
12
|
import type { Collection, Model } from '@nocobase/database';
|
|
13
13
|
import { Plugin } from '@nocobase/server';
|
|
14
|
+
import { isValidFilter } from '@nocobase/utils';
|
|
14
15
|
import { cloneDeep, isEqual, omit, pick } from 'lodash';
|
|
15
16
|
import type { Transaction } from 'sequelize';
|
|
16
17
|
import { DATA_SOURCE_TYPE } from '../constants';
|
|
@@ -23,6 +24,7 @@ import {
|
|
|
23
24
|
removesPhysicalColumn,
|
|
24
25
|
syncFieldAction,
|
|
25
26
|
} from './hooks/field-hooks';
|
|
27
|
+
import { applyPostgresArrayDataType } from './postgres-collection-manager';
|
|
26
28
|
import { PostgresDataSource } from './postgres-datasource';
|
|
27
29
|
import { checkSQL } from './utils/sql-validator';
|
|
28
30
|
import { PostgresValidationError, translate, translateError } from './utils/validation-error';
|
|
@@ -66,6 +68,599 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
66
68
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
67
69
|
}
|
|
68
70
|
|
|
71
|
+
const UNRESOLVED_TEMPLATE_RE = /\{\{[\s\S]*\}\}/;
|
|
72
|
+
const NEGATED_ARRAY_OPERATORS = new Set([
|
|
73
|
+
'$ne',
|
|
74
|
+
'$notIn',
|
|
75
|
+
'$notIncludes',
|
|
76
|
+
'$notStartsWith',
|
|
77
|
+
'$notEndWith',
|
|
78
|
+
'$empty',
|
|
79
|
+
'$noneOf',
|
|
80
|
+
]);
|
|
81
|
+
const KNOWN_FILTER_OPERATORS = new Set([
|
|
82
|
+
'$eq',
|
|
83
|
+
'$ne',
|
|
84
|
+
'$in',
|
|
85
|
+
'$notIn',
|
|
86
|
+
'$includes',
|
|
87
|
+
'$notIncludes',
|
|
88
|
+
'$startsWith',
|
|
89
|
+
'$notStartsWith',
|
|
90
|
+
'$endWith',
|
|
91
|
+
'$notEndWith',
|
|
92
|
+
'$empty',
|
|
93
|
+
'$notEmpty',
|
|
94
|
+
'$isFalsy',
|
|
95
|
+
'$isTruly',
|
|
96
|
+
'$gt',
|
|
97
|
+
'$gte',
|
|
98
|
+
'$lt',
|
|
99
|
+
'$lte',
|
|
100
|
+
'$match',
|
|
101
|
+
'$notMatch',
|
|
102
|
+
'$anyOf',
|
|
103
|
+
'$noneOf',
|
|
104
|
+
'$exists',
|
|
105
|
+
'$notExists',
|
|
106
|
+
]);
|
|
107
|
+
const LOGICAL_OPERATORS = new Set(['$and', '$or', '$not']);
|
|
108
|
+
|
|
109
|
+
function containsUnresolvedTemplate(value: unknown): boolean {
|
|
110
|
+
if (typeof value === 'string') return UNRESOLVED_TEMPLATE_RE.test(value);
|
|
111
|
+
if (Array.isArray(value)) return value.some((item) => containsUnresolvedTemplate(item));
|
|
112
|
+
if (isRecord(value)) return Object.values(value).some((item) => containsUnresolvedTemplate(item));
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function compareOrdered(actual: unknown, operand: unknown): number | null {
|
|
117
|
+
if (
|
|
118
|
+
typeof actual === 'number' &&
|
|
119
|
+
typeof operand === 'number' &&
|
|
120
|
+
Number.isFinite(actual) &&
|
|
121
|
+
Number.isFinite(operand)
|
|
122
|
+
) {
|
|
123
|
+
return actual === operand ? 0 : actual < operand ? -1 : 1;
|
|
124
|
+
}
|
|
125
|
+
if (typeof actual === 'string' && typeof operand === 'string') {
|
|
126
|
+
return actual === operand ? 0 : actual < operand ? -1 : 1;
|
|
127
|
+
}
|
|
128
|
+
if (actual instanceof Date && operand instanceof Date) {
|
|
129
|
+
const left = actual.getTime();
|
|
130
|
+
const right = operand.getTime();
|
|
131
|
+
if (!Number.isFinite(left) || !Number.isFinite(right)) return null;
|
|
132
|
+
return left === right ? 0 : left < right ? -1 : 1;
|
|
133
|
+
}
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isCaseInsensitiveEqualityDialect(dialect: string): boolean {
|
|
138
|
+
return dialect === 'mysql' || dialect === 'mariadb';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function valuesEqual(left: unknown, right: unknown, metadataDialect: string): boolean {
|
|
142
|
+
if (isCaseInsensitiveEqualityDialect(metadataDialect) && typeof left === 'string' && typeof right === 'string') {
|
|
143
|
+
return left.toLowerCase() === right.toLowerCase();
|
|
144
|
+
}
|
|
145
|
+
return isEqual(left, right);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function coerceLikeActual(actual: unknown, metadataDialect: string): string | null {
|
|
149
|
+
if (typeof actual === 'string') return actual;
|
|
150
|
+
if (
|
|
151
|
+
(metadataDialect === 'postgres' ||
|
|
152
|
+
metadataDialect === 'mysql' ||
|
|
153
|
+
metadataDialect === 'mariadb' ||
|
|
154
|
+
metadataDialect === 'sqlite') &&
|
|
155
|
+
(typeof actual === 'number' || typeof actual === 'boolean')
|
|
156
|
+
) {
|
|
157
|
+
return String(actual);
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function foldLikeCase(text: string, metadataDialect: string): string {
|
|
163
|
+
// Match core SQL LIKE/ILIKE case semantics per metadata DB dialect (ASCII-only folding for SQLite LIKE).
|
|
164
|
+
return metadataDialect === 'sqlite' ? text.replace(/[A-Z]/g, (ch) => ch.toLowerCase()) : text.toLowerCase();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function escapeRegExpChar(ch: string): string {
|
|
168
|
+
return /[\\^$.*+?()[\]{}|]/.test(ch) ? `\\${ch}` : ch;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function buildCoreLikePattern(
|
|
172
|
+
operand: string,
|
|
173
|
+
metadataDialect: string,
|
|
174
|
+
mode: 'includes' | 'startsWith' | 'endsWith',
|
|
175
|
+
): string {
|
|
176
|
+
const escaped = operand.replace(/[_%]/g, '\\$&');
|
|
177
|
+
if (mode === 'startsWith') return `${escaped}%`;
|
|
178
|
+
if (mode === 'endsWith') return `%${escaped}`;
|
|
179
|
+
return `%${escaped}%`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function compileSqlLikePattern(patternFolded: string, metadataDialect: string): RegExp | null {
|
|
183
|
+
// Sequelize emits `LIKE '...'` on SQLite without `ESCAPE '\'`, so `\` is literal while `_` and `%` stay wildcards.
|
|
184
|
+
// PostgreSQL ILIKE and MySQL/MariaDB LIKE use `\` as their default escape character.
|
|
185
|
+
const escapeChar = metadataDialect === 'sqlite' ? null : '\\';
|
|
186
|
+
const chars = Array.from(patternFolded);
|
|
187
|
+
let source = '^';
|
|
188
|
+
let prevWildcardMany = false;
|
|
189
|
+
|
|
190
|
+
for (let idx = 0; idx < chars.length; idx += 1) {
|
|
191
|
+
const ch = chars[idx];
|
|
192
|
+
if (escapeChar !== null && ch === escapeChar) {
|
|
193
|
+
if (idx + 1 >= chars.length) {
|
|
194
|
+
if (metadataDialect === 'postgres') return null;
|
|
195
|
+
source += '\\\\';
|
|
196
|
+
prevWildcardMany = false;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
idx += 1;
|
|
200
|
+
source += escapeRegExpChar(chars[idx]);
|
|
201
|
+
prevWildcardMany = false;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (ch === '%') {
|
|
205
|
+
if (!prevWildcardMany) {
|
|
206
|
+
source += '.*';
|
|
207
|
+
prevWildcardMany = true;
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
prevWildcardMany = false;
|
|
212
|
+
source += ch === '_' ? '.' : escapeRegExpChar(ch);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
source += '$';
|
|
216
|
+
return new RegExp(source, 'su');
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function matchLikeValue(
|
|
220
|
+
actualFolded: string,
|
|
221
|
+
rawOperand: string,
|
|
222
|
+
metadataDialect: string,
|
|
223
|
+
mode: 'includes' | 'startsWith' | 'endsWith',
|
|
224
|
+
): boolean {
|
|
225
|
+
if (metadataDialect === 'mssql') {
|
|
226
|
+
const foldedOperand = foldLikeCase(rawOperand, metadataDialect);
|
|
227
|
+
if (mode === 'startsWith') return actualFolded.startsWith(foldedOperand);
|
|
228
|
+
if (mode === 'endsWith') return actualFolded.endsWith(foldedOperand);
|
|
229
|
+
return actualFolded.includes(foldedOperand);
|
|
230
|
+
}
|
|
231
|
+
const patternFolded = foldLikeCase(buildCoreLikePattern(rawOperand, metadataDialect, mode), metadataDialect);
|
|
232
|
+
const compiled = compileSqlLikePattern(patternFolded, metadataDialect);
|
|
233
|
+
return compiled !== null && compiled.test(actualFolded);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function evaluateLikeOperator(
|
|
237
|
+
actual: unknown,
|
|
238
|
+
operand: unknown,
|
|
239
|
+
metadataDialect: string,
|
|
240
|
+
mode: 'includes' | 'startsWith' | 'endsWith',
|
|
241
|
+
negated: boolean,
|
|
242
|
+
): boolean {
|
|
243
|
+
if (mode === 'includes' && operand === null) {
|
|
244
|
+
return negated ? actual !== null && actual !== undefined : actual === null || actual === undefined;
|
|
245
|
+
}
|
|
246
|
+
const actualText = coerceLikeActual(actual, metadataDialect);
|
|
247
|
+
if (actualText === null) return false;
|
|
248
|
+
const actualFolded = foldLikeCase(actualText, metadataDialect);
|
|
249
|
+
if (typeof operand === 'string') {
|
|
250
|
+
const matched = matchLikeValue(actualFolded, operand, metadataDialect, mode);
|
|
251
|
+
return negated ? !matched : matched;
|
|
252
|
+
}
|
|
253
|
+
if (Array.isArray(operand) && operand.every((item): item is string => typeof item === 'string')) {
|
|
254
|
+
if (negated) {
|
|
255
|
+
return operand.every((item) => !matchLikeValue(actualFolded, item, metadataDialect, mode));
|
|
256
|
+
}
|
|
257
|
+
return operand.some((item) => matchLikeValue(actualFolded, item, metadataDialect, mode));
|
|
258
|
+
}
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function evaluateSingleOperator(
|
|
263
|
+
actual: unknown,
|
|
264
|
+
operator: string,
|
|
265
|
+
operand: unknown,
|
|
266
|
+
metadataDialect = 'postgres',
|
|
267
|
+
): boolean {
|
|
268
|
+
if (!KNOWN_FILTER_OPERATORS.has(operator) || containsUnresolvedTemplate(operand)) return false;
|
|
269
|
+
switch (operator) {
|
|
270
|
+
case '$eq':
|
|
271
|
+
if (operand === undefined) return true;
|
|
272
|
+
if (operand === null) return actual === null || actual === undefined;
|
|
273
|
+
if (Array.isArray(operand) && !Array.isArray(actual)) {
|
|
274
|
+
return (
|
|
275
|
+
actual !== null &&
|
|
276
|
+
actual !== undefined &&
|
|
277
|
+
operand.some((item) => item !== null && item !== undefined && valuesEqual(actual, item, metadataDialect))
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
return valuesEqual(actual, operand, metadataDialect);
|
|
281
|
+
case '$ne':
|
|
282
|
+
if (operand === null) return actual !== null && actual !== undefined;
|
|
283
|
+
if (Array.isArray(operand) && !Array.isArray(actual)) {
|
|
284
|
+
if (operand.length === 0) return true;
|
|
285
|
+
if (actual === null || actual === undefined) return false;
|
|
286
|
+
if (operand.some((item) => item === null || item === undefined)) return false;
|
|
287
|
+
return !operand.some((item) => valuesEqual(actual, item, metadataDialect));
|
|
288
|
+
}
|
|
289
|
+
return !Array.isArray(actual) && (actual === null || actual === undefined)
|
|
290
|
+
? true
|
|
291
|
+
: !valuesEqual(actual, operand, metadataDialect);
|
|
292
|
+
case '$in': {
|
|
293
|
+
if (operand === null || operand === undefined) return false;
|
|
294
|
+
const list = Array.isArray(operand) ? operand : [operand];
|
|
295
|
+
return (
|
|
296
|
+
actual !== null &&
|
|
297
|
+
actual !== undefined &&
|
|
298
|
+
list.some((item) => item !== null && item !== undefined && valuesEqual(actual, item, metadataDialect))
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
case '$notIn':
|
|
302
|
+
if (!Array.isArray(operand)) return false;
|
|
303
|
+
if (actual === null || actual === undefined || operand.length === 0) return true;
|
|
304
|
+
if (operand.some((item) => item === null || item === undefined)) return false;
|
|
305
|
+
return !operand.some((item) => valuesEqual(actual, item, metadataDialect));
|
|
306
|
+
case '$exists': {
|
|
307
|
+
if (operand !== undefined && typeof operand !== 'boolean') return false;
|
|
308
|
+
const exists = Array.isArray(actual) ? actual.length > 0 : actual !== null && actual !== undefined;
|
|
309
|
+
return operand === false ? !exists : exists;
|
|
310
|
+
}
|
|
311
|
+
case '$notExists': {
|
|
312
|
+
if (operand !== undefined && typeof operand !== 'boolean') return false;
|
|
313
|
+
const exists = Array.isArray(actual) ? actual.length > 0 : actual !== null && actual !== undefined;
|
|
314
|
+
return operand === false ? exists : !exists;
|
|
315
|
+
}
|
|
316
|
+
case '$includes':
|
|
317
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, 'includes', false);
|
|
318
|
+
case '$notIncludes':
|
|
319
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, 'includes', true);
|
|
320
|
+
case '$startsWith':
|
|
321
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, 'startsWith', false);
|
|
322
|
+
case '$notStartsWith':
|
|
323
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, 'startsWith', true);
|
|
324
|
+
case '$endWith':
|
|
325
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, 'endsWith', false);
|
|
326
|
+
case '$notEndWith':
|
|
327
|
+
return evaluateLikeOperator(actual, operand, metadataDialect, 'endsWith', true);
|
|
328
|
+
case '$empty': {
|
|
329
|
+
if (operand !== undefined && typeof operand !== 'boolean') return false;
|
|
330
|
+
const empty =
|
|
331
|
+
actual === null || actual === undefined || actual === '' || (Array.isArray(actual) && !actual.length);
|
|
332
|
+
return operand === false ? !empty : empty;
|
|
333
|
+
}
|
|
334
|
+
case '$notEmpty': {
|
|
335
|
+
if (operand !== undefined && typeof operand !== 'boolean') return false;
|
|
336
|
+
const empty =
|
|
337
|
+
actual === null || actual === undefined || actual === '' || (Array.isArray(actual) && !actual.length);
|
|
338
|
+
return operand === false ? empty : !empty;
|
|
339
|
+
}
|
|
340
|
+
case '$isFalsy':
|
|
341
|
+
if (operand !== undefined && typeof operand !== 'boolean') return false;
|
|
342
|
+
return operand === false ? Boolean(actual) : !actual;
|
|
343
|
+
case '$isTruly':
|
|
344
|
+
if (operand !== undefined && typeof operand !== 'boolean') return false;
|
|
345
|
+
return operand === false ? !actual : Boolean(actual);
|
|
346
|
+
case '$gt': {
|
|
347
|
+
const cmp = compareOrdered(actual, operand);
|
|
348
|
+
return cmp !== null && cmp > 0;
|
|
349
|
+
}
|
|
350
|
+
case '$gte': {
|
|
351
|
+
const cmp = compareOrdered(actual, operand);
|
|
352
|
+
return cmp !== null && cmp >= 0;
|
|
353
|
+
}
|
|
354
|
+
case '$lt': {
|
|
355
|
+
const cmp = compareOrdered(actual, operand);
|
|
356
|
+
return cmp !== null && cmp < 0;
|
|
357
|
+
}
|
|
358
|
+
case '$lte': {
|
|
359
|
+
const cmp = compareOrdered(actual, operand);
|
|
360
|
+
return cmp !== null && cmp <= 0;
|
|
361
|
+
}
|
|
362
|
+
case '$anyOf':
|
|
363
|
+
return (
|
|
364
|
+
Array.isArray(actual) &&
|
|
365
|
+
Array.isArray(operand) &&
|
|
366
|
+
actual.some((item) => operand.some((target) => valuesEqual(item, target, metadataDialect)))
|
|
367
|
+
);
|
|
368
|
+
case '$noneOf':
|
|
369
|
+
return (
|
|
370
|
+
Array.isArray(actual) &&
|
|
371
|
+
Array.isArray(operand) &&
|
|
372
|
+
!actual.some((item) => operand.some((target) => valuesEqual(item, target, metadataDialect)))
|
|
373
|
+
);
|
|
374
|
+
case '$match':
|
|
375
|
+
return (
|
|
376
|
+
Array.isArray(actual) &&
|
|
377
|
+
isRecord(operand) &&
|
|
378
|
+
actual.some((item) => isRecord(item) && matchesCollectionFilter(item, operand, metadataDialect))
|
|
379
|
+
);
|
|
380
|
+
case '$notMatch':
|
|
381
|
+
return (
|
|
382
|
+
Array.isArray(actual) &&
|
|
383
|
+
isRecord(operand) &&
|
|
384
|
+
!actual.some((item) => isRecord(item) && matchesCollectionFilter(item, operand, metadataDialect))
|
|
385
|
+
);
|
|
386
|
+
default:
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const ARRAY_LEVEL_OPERATORS = new Set([
|
|
392
|
+
'$match',
|
|
393
|
+
'$notMatch',
|
|
394
|
+
'$anyOf',
|
|
395
|
+
'$noneOf',
|
|
396
|
+
'$empty',
|
|
397
|
+
'$notEmpty',
|
|
398
|
+
'$exists',
|
|
399
|
+
'$notExists',
|
|
400
|
+
]);
|
|
401
|
+
const CONTAINER_ARRAY_KEYS = new Set(['fields', 'indexes']);
|
|
402
|
+
const MAX_PINNED_FILTER_CONTEXTS = 10_000;
|
|
403
|
+
|
|
404
|
+
function evaluateOperatorGroup(
|
|
405
|
+
target: { values: unknown[]; traversedArray: boolean },
|
|
406
|
+
entries: Array<[string, unknown]>,
|
|
407
|
+
metadataDialect: string,
|
|
408
|
+
): boolean {
|
|
409
|
+
if (!entries.length) return true;
|
|
410
|
+
if (
|
|
411
|
+
entries.some(([operator, operand]) => !KNOWN_FILTER_OPERATORS.has(operator) || containsUnresolvedTemplate(operand))
|
|
412
|
+
) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
if (target.traversedArray && entries.every(([operator]) => NEGATED_ARRAY_OPERATORS.has(operator))) {
|
|
416
|
+
return target.values.every((value) =>
|
|
417
|
+
entries.every(([operator, operand]) => evaluateSingleOperator(value, operator, operand, metadataDialect)),
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
return target.values.some((value) =>
|
|
421
|
+
entries.every(([operator, operand]) => evaluateSingleOperator(value, operator, operand, metadataDialect)),
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function collectPositiveArrayPrefixes(filter: unknown, out: string[][], prefix: string[] = []): void {
|
|
426
|
+
if (!isRecord(filter)) return;
|
|
427
|
+
for (const [rawKey, condition] of Object.entries(filter)) {
|
|
428
|
+
if (rawKey === '$and' || rawKey === '$or') {
|
|
429
|
+
const items = Array.isArray(condition) ? condition : isRecord(condition) ? Object.values(condition) : null;
|
|
430
|
+
if (items) {
|
|
431
|
+
for (const item of items) collectPositiveArrayPrefixes(item, out, prefix);
|
|
432
|
+
}
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (rawKey === '$not' || rawKey.startsWith('$')) continue;
|
|
436
|
+
|
|
437
|
+
const parts = rawKey.split('.');
|
|
438
|
+
const lastPart = parts[parts.length - 1];
|
|
439
|
+
const hasTrailingOp = parts.length > 1 && lastPart.startsWith('$');
|
|
440
|
+
const fieldParts = hasTrailingOp ? parts.slice(0, -1) : parts;
|
|
441
|
+
if (fieldParts.some((part) => !part || part.startsWith('$'))) continue;
|
|
442
|
+
|
|
443
|
+
const fullParts = [...prefix, ...fieldParts];
|
|
444
|
+
let arrayLevelOnly = false;
|
|
445
|
+
if (hasTrailingOp) {
|
|
446
|
+
arrayLevelOnly = ARRAY_LEVEL_OPERATORS.has(lastPart);
|
|
447
|
+
} else if (isRecord(condition)) {
|
|
448
|
+
const keys = Object.keys(condition);
|
|
449
|
+
if (keys.length && keys.every((key) => KNOWN_FILTER_OPERATORS.has(key))) {
|
|
450
|
+
arrayLevelOnly = keys.every((key) => ARRAY_LEVEL_OPERATORS.has(key));
|
|
451
|
+
} else if (keys.length && keys.every((key) => !key.startsWith('$') || LOGICAL_OPERATORS.has(key))) {
|
|
452
|
+
for (let len = 1; len <= fullParts.length; len += 1) {
|
|
453
|
+
out.push(fullParts.slice(0, len));
|
|
454
|
+
}
|
|
455
|
+
collectPositiveArrayPrefixes(condition, out, fullParts);
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const maxLen = arrayLevelOnly ? fullParts.length - 1 : fullParts.length;
|
|
461
|
+
for (let len = 1; len <= maxLen; len += 1) {
|
|
462
|
+
out.push(fullParts.slice(0, len));
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function readPathWithPins(
|
|
468
|
+
record: Record<string, unknown>,
|
|
469
|
+
parts: string[],
|
|
470
|
+
pins: ReadonlyMap<string, unknown>,
|
|
471
|
+
ignoreLeafPin = false,
|
|
472
|
+
): { values: unknown[]; traversedArray: boolean } {
|
|
473
|
+
let current: unknown[] = [record];
|
|
474
|
+
let traversedArray = false;
|
|
475
|
+
for (let idx = 0; idx < parts.length; idx += 1) {
|
|
476
|
+
const part = parts[idx];
|
|
477
|
+
const pathSoFar = parts.slice(0, idx + 1).join('.');
|
|
478
|
+
if (!(ignoreLeafPin && idx === parts.length - 1) && pins.has(pathSoFar)) {
|
|
479
|
+
current = [pins.get(pathSoFar)];
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
const next: unknown[] = [];
|
|
483
|
+
for (const item of current) {
|
|
484
|
+
if (Array.isArray(item)) {
|
|
485
|
+
traversedArray = true;
|
|
486
|
+
for (const entry of item) {
|
|
487
|
+
if (isRecord(entry)) {
|
|
488
|
+
if (part in entry) next.push(entry[part]);
|
|
489
|
+
else if (isRecord(entry.options) && part in entry.options) next.push(entry.options[part]);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
} else if (isRecord(item)) {
|
|
493
|
+
if (part in item) {
|
|
494
|
+
next.push(item[part]);
|
|
495
|
+
} else if (isRecord(item.options) && part in item.options) {
|
|
496
|
+
next.push(item.options[part]);
|
|
497
|
+
} else {
|
|
498
|
+
next.push(undefined);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
current = next;
|
|
503
|
+
}
|
|
504
|
+
return { values: current.length ? current : [undefined], traversedArray };
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function expandPinsForPrefixes(
|
|
508
|
+
record: Record<string, unknown>,
|
|
509
|
+
inheritedPins: ReadonlyMap<string, unknown>,
|
|
510
|
+
candidatePrefixes: string[][],
|
|
511
|
+
): Array<Map<string, unknown>> | null {
|
|
512
|
+
const uniquePrefixes = new Map<string, string[]>();
|
|
513
|
+
for (const parts of candidatePrefixes) {
|
|
514
|
+
const key = parts.join('.');
|
|
515
|
+
if (!inheritedPins.has(key) && !uniquePrefixes.has(key)) {
|
|
516
|
+
uniquePrefixes.set(key, parts);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
const sortedPrefixes = [...uniquePrefixes.entries()].sort((a, b) => a[1].length - b[1].length);
|
|
520
|
+
let contexts: Array<Map<string, unknown>> = [new Map(inheritedPins)];
|
|
521
|
+
|
|
522
|
+
for (const [key, parts] of sortedPrefixes) {
|
|
523
|
+
const leafKey = parts[parts.length - 1];
|
|
524
|
+
const nextContexts: Array<Map<string, unknown>> = [];
|
|
525
|
+
for (const ctxPins of contexts) {
|
|
526
|
+
const rawTarget = readPathWithPins(record, parts, ctxPins, true);
|
|
527
|
+
if (rawTarget.values.length === 1 && Array.isArray(rawTarget.values[0])) {
|
|
528
|
+
const arr = rawTarget.values[0];
|
|
529
|
+
if (CONTAINER_ARRAY_KEYS.has(leafKey) || arr.some((entry) => isRecord(entry))) {
|
|
530
|
+
if (arr.length === 0) {
|
|
531
|
+
const forked = new Map(ctxPins);
|
|
532
|
+
forked.set(key, undefined);
|
|
533
|
+
nextContexts.push(forked);
|
|
534
|
+
} else {
|
|
535
|
+
for (const entry of arr) {
|
|
536
|
+
const forked = new Map(ctxPins);
|
|
537
|
+
forked.set(key, entry);
|
|
538
|
+
nextContexts.push(forked);
|
|
539
|
+
if (nextContexts.length > MAX_PINNED_FILTER_CONTEXTS) return null;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
nextContexts.push(ctxPins);
|
|
546
|
+
if (nextContexts.length > MAX_PINNED_FILTER_CONTEXTS) return null;
|
|
547
|
+
}
|
|
548
|
+
contexts = nextContexts;
|
|
549
|
+
}
|
|
550
|
+
return contexts;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function extractSubPins(pins: ReadonlyMap<string, unknown>, prefix: string): Map<string, unknown> {
|
|
554
|
+
const prefixDot = `${prefix}.`;
|
|
555
|
+
const subPins = new Map<string, unknown>();
|
|
556
|
+
for (const [key, value] of pins.entries()) {
|
|
557
|
+
if (key.startsWith(prefixDot)) {
|
|
558
|
+
subPins.set(key.slice(prefixDot.length), value);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return subPins;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function evaluateEntriesWithPins(
|
|
565
|
+
record: Record<string, unknown>,
|
|
566
|
+
pins: ReadonlyMap<string, unknown>,
|
|
567
|
+
filter: unknown,
|
|
568
|
+
metadataDialect: string,
|
|
569
|
+
): boolean {
|
|
570
|
+
if (!isRecord(filter)) return false;
|
|
571
|
+
for (const [rawKey, condition] of Object.entries(filter)) {
|
|
572
|
+
if (rawKey === '$and') {
|
|
573
|
+
const items = Array.isArray(condition) ? condition : isRecord(condition) ? Object.values(condition) : null;
|
|
574
|
+
if (!items || !items.every((item) => evaluateEntriesWithPins(record, pins, item, metadataDialect))) return false;
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
if (rawKey === '$or') {
|
|
578
|
+
const items = Array.isArray(condition) ? condition : isRecord(condition) ? Object.values(condition) : null;
|
|
579
|
+
if (!items || !items.some((item) => matchesScope(record, pins, item, metadataDialect))) return false;
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
if (rawKey === '$not') {
|
|
583
|
+
if (!isRecord(condition) || matchesScope(record, pins, condition, metadataDialect)) return false;
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
if (rawKey.startsWith('$')) return false;
|
|
587
|
+
|
|
588
|
+
const parts = rawKey.split('.');
|
|
589
|
+
const lastPart = parts[parts.length - 1];
|
|
590
|
+
const hasTrailingOp = parts.length > 1 && lastPart.startsWith('$');
|
|
591
|
+
const fieldParts = hasTrailingOp ? parts.slice(0, -1) : parts;
|
|
592
|
+
if (fieldParts.some((part) => !part || part.startsWith('$'))) return false;
|
|
593
|
+
|
|
594
|
+
if (hasTrailingOp) {
|
|
595
|
+
const target = readPathWithPins(record, fieldParts, pins, ARRAY_LEVEL_OPERATORS.has(lastPart));
|
|
596
|
+
if (!evaluateOperatorGroup(target, [[lastPart, condition]], metadataDialect)) return false;
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
if (isRecord(condition)) {
|
|
601
|
+
const entries = Object.entries(condition);
|
|
602
|
+
if (!entries.length) continue;
|
|
603
|
+
if (entries.every(([key]) => KNOWN_FILTER_OPERATORS.has(key))) {
|
|
604
|
+
const arrayLevelEntries = entries.filter(([key]) => ARRAY_LEVEL_OPERATORS.has(key));
|
|
605
|
+
const elementEntries = entries.filter(([key]) => !ARRAY_LEVEL_OPERATORS.has(key));
|
|
606
|
+
if (arrayLevelEntries.length) {
|
|
607
|
+
const arrayTarget = readPathWithPins(record, fieldParts, pins, true);
|
|
608
|
+
if (!evaluateOperatorGroup(arrayTarget, arrayLevelEntries, metadataDialect)) return false;
|
|
609
|
+
}
|
|
610
|
+
if (elementEntries.length) {
|
|
611
|
+
const elemTarget = readPathWithPins(record, fieldParts, pins, false);
|
|
612
|
+
if (!evaluateOperatorGroup(elemTarget, elementEntries, metadataDialect)) return false;
|
|
613
|
+
}
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
if (!entries.every(([key]) => !key.startsWith('$') || LOGICAL_OPERATORS.has(key))) return false;
|
|
617
|
+
|
|
618
|
+
const fieldPathKey = fieldParts.join('.');
|
|
619
|
+
const subPins = extractSubPins(pins, fieldPathKey);
|
|
620
|
+
if (pins.has(fieldPathKey)) {
|
|
621
|
+
const pinnedVal = pins.get(fieldPathKey);
|
|
622
|
+
if (!isRecord(pinnedVal) || !matchesScope(pinnedVal, subPins, condition, metadataDialect)) return false;
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
const target = readPathWithPins(record, fieldParts, pins, false);
|
|
626
|
+
const nestedMatched = target.values.some((value) => {
|
|
627
|
+
if (Array.isArray(value)) {
|
|
628
|
+
return value.some((entry) => isRecord(entry) && matchesScope(entry, subPins, condition, metadataDialect));
|
|
629
|
+
}
|
|
630
|
+
return isRecord(value) && matchesScope(value, subPins, condition, metadataDialect);
|
|
631
|
+
});
|
|
632
|
+
if (!nestedMatched) return false;
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
const target = readPathWithPins(record, fieldParts, pins, false);
|
|
637
|
+
if (!evaluateOperatorGroup(target, [['$eq', condition]], metadataDialect)) return false;
|
|
638
|
+
}
|
|
639
|
+
return true;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
function matchesScope(
|
|
643
|
+
record: Record<string, unknown>,
|
|
644
|
+
inheritedPins: ReadonlyMap<string, unknown>,
|
|
645
|
+
filter: unknown,
|
|
646
|
+
metadataDialect: string,
|
|
647
|
+
): boolean {
|
|
648
|
+
if (!isRecord(filter)) return false;
|
|
649
|
+
const prefixes: string[][] = [];
|
|
650
|
+
collectPositiveArrayPrefixes(filter, prefixes);
|
|
651
|
+
const expanded = expandPinsForPrefixes(record, inheritedPins, prefixes);
|
|
652
|
+
if (!expanded) return false;
|
|
653
|
+
return expanded.some((pins) => evaluateEntriesWithPins(record, pins, filter, metadataDialect));
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function matchesCollectionFilter(
|
|
657
|
+
record: Record<string, unknown>,
|
|
658
|
+
filter: unknown,
|
|
659
|
+
metadataDialect = 'postgres',
|
|
660
|
+
): boolean {
|
|
661
|
+
return matchesScope(record, new Map(), filter, metadataDialect);
|
|
662
|
+
}
|
|
663
|
+
|
|
69
664
|
function extractSql(values: Record<string, unknown> | undefined): unknown {
|
|
70
665
|
if (!values) return undefined;
|
|
71
666
|
if (values.sql !== undefined) return values.sql;
|
|
@@ -93,7 +688,17 @@ function prepareFields(values: Record<string, unknown>): void {
|
|
|
93
688
|
values.timestamps = false;
|
|
94
689
|
}
|
|
95
690
|
const mapped: FieldValues[] = values.fields.map((field: FieldValues | string) => {
|
|
96
|
-
if (typeof field !== 'string')
|
|
691
|
+
if (typeof field !== 'string') {
|
|
692
|
+
if (isRecord(field.settings)) {
|
|
693
|
+
if (field.elementType === undefined && typeof field.settings.elementType === 'string') {
|
|
694
|
+
field.elementType = field.settings.elementType;
|
|
695
|
+
}
|
|
696
|
+
if (field.dataType === undefined && typeof field.settings.dataType === 'string') {
|
|
697
|
+
field.dataType = field.settings.dataType;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
return applyPostgresArrayDataType(field);
|
|
701
|
+
}
|
|
97
702
|
const preset = PRESET_FIELDS[field];
|
|
98
703
|
if (!preset) throw new PostgresValidationError('Unknown preset field {{name}}', { name: field });
|
|
99
704
|
return cloneDeep(preset);
|
|
@@ -469,7 +1074,8 @@ export class PluginDataSourcePostgresServer extends Plugin {
|
|
|
469
1074
|
async (ctx: Context, next: Next) => {
|
|
470
1075
|
const { resourceName, actionName, params } = ctx.action || {};
|
|
471
1076
|
if (resourceName === 'dataSources.collections') {
|
|
472
|
-
const
|
|
1077
|
+
const dataSourceKey = ctx.action?.sourceId || params?.associatedIndex;
|
|
1078
|
+
const dataSource = dataSourceKey ? this.app.dataSourceManager.get(dataSourceKey) : undefined;
|
|
473
1079
|
if (dataSource instanceof PostgresDataSource) {
|
|
474
1080
|
if (actionName === 'create' || actionName === 'destroy') {
|
|
475
1081
|
await this.mutateCollection(ctx, dataSource, next);
|
|
@@ -525,6 +1131,106 @@ export class PluginDataSourcePostgresServer extends Plugin {
|
|
|
525
1131
|
);
|
|
526
1132
|
}
|
|
527
1133
|
|
|
1134
|
+
private async resolveCollectionsToDestroy(ctx: Context, source: PostgresDataSource): Promise<string[]> {
|
|
1135
|
+
const params = ctx.action?.params || {};
|
|
1136
|
+
const permission = (
|
|
1137
|
+
ctx as {
|
|
1138
|
+
permission?: {
|
|
1139
|
+
parsedParams?: { filter?: unknown };
|
|
1140
|
+
can?: { params?: { filter?: unknown } };
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
).permission;
|
|
1144
|
+
const rawFilter = params.filter;
|
|
1145
|
+
const scopeFilter = permission?.parsedParams?.filter ?? permission?.can?.params?.filter;
|
|
1146
|
+
const hasRawFilter = isValidFilter(rawFilter);
|
|
1147
|
+
const hasScopeFilter = isValidFilter(scopeFilter);
|
|
1148
|
+
const effectiveFilter =
|
|
1149
|
+
hasRawFilter && hasScopeFilter && !isEqual(rawFilter, scopeFilter)
|
|
1150
|
+
? { $and: [rawFilter, scopeFilter] }
|
|
1151
|
+
: hasRawFilter
|
|
1152
|
+
? rawFilter
|
|
1153
|
+
: hasScopeFilter
|
|
1154
|
+
? scopeFilter
|
|
1155
|
+
: undefined;
|
|
1156
|
+
|
|
1157
|
+
const hasFilterByTk = params.filterByTk !== undefined && params.filterByTk !== null;
|
|
1158
|
+
let requestedNames: string[] | undefined;
|
|
1159
|
+
if (hasFilterByTk) {
|
|
1160
|
+
if (Array.isArray(params.filterByTk) && params.filterByTk.length === 0) {
|
|
1161
|
+
return [];
|
|
1162
|
+
}
|
|
1163
|
+
requestedNames = names(params.filterByTk);
|
|
1164
|
+
} else if (!effectiveFilter) {
|
|
1165
|
+
throw new PostgresValidationError('A collection name is required');
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
const manager = source.collectionManager;
|
|
1169
|
+
if (!effectiveFilter) {
|
|
1170
|
+
for (const name of requestedNames ?? []) {
|
|
1171
|
+
if (!manager.getCollection(name)) {
|
|
1172
|
+
ctx.throw(404, translate(ctx, 'Collection {{name}} does not exist', { name }));
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
return requestedNames ?? [];
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
const db = this.app.db || ctx.db;
|
|
1179
|
+
const metadataDialect = db?.sequelize?.getDialect?.() ?? 'postgres';
|
|
1180
|
+
const collectionsRepo = db?.getRepository?.('dataSourcesCollections') as
|
|
1181
|
+
| { find?: (options: Record<string, unknown>) => Promise<Model[]> }
|
|
1182
|
+
| undefined;
|
|
1183
|
+
|
|
1184
|
+
if (collectionsRepo && typeof collectionsRepo.find === 'function') {
|
|
1185
|
+
const baseFilter: Record<string, unknown> = {
|
|
1186
|
+
dataSourceKey: source.name,
|
|
1187
|
+
...(requestedNames ? { name: { $in: requestedNames } } : {}),
|
|
1188
|
+
};
|
|
1189
|
+
const persistedAll = await collectionsRepo.find({ filter: baseFilter, context: ctx });
|
|
1190
|
+
const persistedNames = new Set(persistedAll.map((row) => String(row.get('name'))));
|
|
1191
|
+
const persistedMatched = await collectionsRepo.find({
|
|
1192
|
+
filter: { $and: [baseFilter, effectiveFilter] },
|
|
1193
|
+
context: ctx,
|
|
1194
|
+
});
|
|
1195
|
+
const matchedSet = new Set(persistedMatched.map((row) => String(row.get('name'))));
|
|
1196
|
+
|
|
1197
|
+
for (const collection of manager.getCollections()) {
|
|
1198
|
+
if (persistedNames.has(collection.name)) continue;
|
|
1199
|
+
if (requestedNames && !requestedNames.includes(collection.name)) continue;
|
|
1200
|
+
const record: Record<string, unknown> = {
|
|
1201
|
+
...collection.options,
|
|
1202
|
+
name: collection.name,
|
|
1203
|
+
dataSourceKey: source.name,
|
|
1204
|
+
dataSources: { key: source.name, type: DATA_SOURCE_TYPE },
|
|
1205
|
+
options: collection.options,
|
|
1206
|
+
fields: collection.getFields().map((field) => field.options),
|
|
1207
|
+
};
|
|
1208
|
+
if (matchesCollectionFilter(record, effectiveFilter, metadataDialect)) {
|
|
1209
|
+
matchedSet.add(collection.name);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
return requestedNames ? requestedNames.filter((name) => matchedSet.has(name)) : [...matchedSet];
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
const matchedRuntime = new Set<string>();
|
|
1217
|
+
for (const collection of manager.getCollections()) {
|
|
1218
|
+
if (requestedNames && !requestedNames.includes(collection.name)) continue;
|
|
1219
|
+
const record: Record<string, unknown> = {
|
|
1220
|
+
...collection.options,
|
|
1221
|
+
name: collection.name,
|
|
1222
|
+
dataSourceKey: source.name,
|
|
1223
|
+
dataSources: { key: source.name, type: DATA_SOURCE_TYPE },
|
|
1224
|
+
options: collection.options,
|
|
1225
|
+
fields: collection.getFields().map((field) => field.options),
|
|
1226
|
+
};
|
|
1227
|
+
if (matchesCollectionFilter(record, effectiveFilter, metadataDialect)) {
|
|
1228
|
+
matchedRuntime.add(collection.name);
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
return requestedNames ? requestedNames.filter((name) => matchedRuntime.has(name)) : [...matchedRuntime];
|
|
1232
|
+
}
|
|
1233
|
+
|
|
528
1234
|
private async mutateCollection(ctx: Context, source: PostgresDataSource, next: Next) {
|
|
529
1235
|
const manager = source.collectionManager;
|
|
530
1236
|
const { actionName, params } = ctx.action;
|
|
@@ -574,22 +1280,33 @@ export class PluginDataSourcePostgresServer extends Plugin {
|
|
|
574
1280
|
return;
|
|
575
1281
|
}
|
|
576
1282
|
|
|
577
|
-
const collectionNames =
|
|
578
|
-
|
|
1283
|
+
const collectionNames = await this.resolveCollectionsToDestroy(ctx, source);
|
|
1284
|
+
if (collectionNames.length === 0) {
|
|
1285
|
+
ctx.body = true;
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
const snapshots = collectionNames.flatMap((name) => {
|
|
579
1290
|
const collection = manager.getCollection(name);
|
|
580
|
-
if (!collection)
|
|
581
|
-
return
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
1291
|
+
if (!collection) return [];
|
|
1292
|
+
return [
|
|
1293
|
+
{
|
|
1294
|
+
name,
|
|
1295
|
+
physicalName: String(collection.options.tableName || name),
|
|
1296
|
+
isSql: Boolean(collection.options.sql),
|
|
1297
|
+
options: cloneDeep({
|
|
1298
|
+
...collection.options,
|
|
1299
|
+
tableName: String(collection.options.tableName || name),
|
|
1300
|
+
fields: collection.getFields().map((field) => field.options),
|
|
1301
|
+
}),
|
|
1302
|
+
},
|
|
1303
|
+
];
|
|
591
1304
|
});
|
|
592
1305
|
const cascade = params.cascade === true || params.cascade === 'true';
|
|
1306
|
+
const hadFilterByTk = Object.prototype.hasOwnProperty.call(params, 'filterByTk');
|
|
1307
|
+
const hadFilter = Object.prototype.hasOwnProperty.call(params, 'filter');
|
|
1308
|
+
const originalFilterByTk = params.filterByTk;
|
|
1309
|
+
const originalFilter = params.filter;
|
|
593
1310
|
try {
|
|
594
1311
|
await manager.db.sequelize.transaction(async (transaction) => {
|
|
595
1312
|
for (const snapshot of [...snapshots].sort((left, right) =>
|
|
@@ -601,15 +1318,21 @@ export class PluginDataSourcePostgresServer extends Plugin {
|
|
|
601
1318
|
});
|
|
602
1319
|
if (!snapshot.isSql) await manager.dropTable(snapshot.name, cascade, transaction);
|
|
603
1320
|
}
|
|
1321
|
+
params.filterByTk = collectionNames;
|
|
1322
|
+
delete params.filter;
|
|
604
1323
|
await next();
|
|
605
1324
|
});
|
|
606
1325
|
} catch (error) {
|
|
1326
|
+
if (hadFilterByTk) params.filterByTk = originalFilterByTk;
|
|
1327
|
+
else delete params.filterByTk;
|
|
1328
|
+
if (hadFilter) params.filter = originalFilter;
|
|
1329
|
+
else delete params.filter;
|
|
607
1330
|
for (const snapshot of snapshots) {
|
|
608
1331
|
if (!manager.hasCollection(snapshot.name)) manager.defineCollection(snapshot.options);
|
|
609
1332
|
}
|
|
610
1333
|
throw error;
|
|
611
1334
|
}
|
|
612
|
-
const fieldsRepo = this.app.db?.getRepository?.('dataSourcesFields') as
|
|
1335
|
+
const fieldsRepo = (this.app.db || ctx.db)?.getRepository?.('dataSourcesFields') as
|
|
613
1336
|
| { destroy?: (options: Record<string, unknown>) => Promise<unknown> }
|
|
614
1337
|
| undefined;
|
|
615
1338
|
if (fieldsRepo && typeof fieldsRepo.destroy === 'function') {
|
|
@@ -635,7 +1358,7 @@ export class PluginDataSourcePostgresServer extends Plugin {
|
|
|
635
1358
|
await this.sendSyncMessage({
|
|
636
1359
|
type: 'collectionRemoved',
|
|
637
1360
|
dataSourceKey: source.name,
|
|
638
|
-
collectionNames
|
|
1361
|
+
collectionNames,
|
|
639
1362
|
});
|
|
640
1363
|
}
|
|
641
1364
|
|
|
@@ -704,6 +1427,18 @@ export class PluginDataSourcePostgresServer extends Plugin {
|
|
|
704
1427
|
});
|
|
705
1428
|
}
|
|
706
1429
|
await syncFieldAction(manager, action, collectionName, fieldName, input, transaction);
|
|
1430
|
+
if (input.dataType === 'array' && isRecord(params.values)) {
|
|
1431
|
+
if (
|
|
1432
|
+
params.values.dataType === undefined ||
|
|
1433
|
+
params.values.dataType === null ||
|
|
1434
|
+
params.values.dataType === ''
|
|
1435
|
+
) {
|
|
1436
|
+
params.values.dataType = 'array';
|
|
1437
|
+
}
|
|
1438
|
+
if (params.values.elementType === undefined && typeof input.elementType === 'string') {
|
|
1439
|
+
params.values.elementType = input.elementType;
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
707
1442
|
metadataActionStarted = true;
|
|
708
1443
|
await next();
|
|
709
1444
|
});
|