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/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# External PostgreSQL data source
|
|
2
2
|
|
|
3
|
-
Version 2.0.
|
|
3
|
+
Version 2.0.1 targets NocoBase 2.2.x and provides configuration for both the v1 and v2 clients. Enable the Data Source Manager and this plugin, then add an external PostgreSQL connection in Data Source Manager.
|
|
4
4
|
|
|
5
5
|
The connection form accepts a PostgreSQL schema and can load all collections or selected tables and views. Loading a table or view registers its schema for use in NocoBase without changing the PostgreSQL object. Existing datasource configurations keep the `postgres-external` type.
|
|
6
6
|
|
package/dist/externalVersion.js
CHANGED
|
@@ -30,6 +30,7 @@ __export(sql_collection_exports, {
|
|
|
30
30
|
});
|
|
31
31
|
module.exports = __toCommonJS(sql_collection_exports);
|
|
32
32
|
var import_database = require("@nocobase/database");
|
|
33
|
+
var import_sql_validator = require("../utils/sql-validator");
|
|
33
34
|
var import_sql_model = require("./sql-model");
|
|
34
35
|
class PostgresSQLCollection extends import_database.Collection {
|
|
35
36
|
constructor(options, context) {
|
|
@@ -54,8 +55,7 @@ class PostgresSQLCollection extends import_database.Collection {
|
|
|
54
55
|
return void 0;
|
|
55
56
|
}
|
|
56
57
|
modelInit() {
|
|
57
|
-
const
|
|
58
|
-
const normalizedSql = sql.endsWith(";") ? sql.slice(0, -1) : sql;
|
|
58
|
+
const normalizedSql = (0, import_sql_validator.normalizeSqlCollectionQuery)(String(this.options.sql || ""));
|
|
59
59
|
if (this.model) {
|
|
60
60
|
this.model.sql = normalizedSql;
|
|
61
61
|
return;
|
|
@@ -36,7 +36,8 @@ var import_validation_error = require("../utils/validation-error");
|
|
|
36
36
|
function selectQuery(_tableName, options, model) {
|
|
37
37
|
var _a;
|
|
38
38
|
const queryOptions = options || {};
|
|
39
|
-
if ((0, import_lodash.isEmpty)(queryOptions)) return `${model.sql}
|
|
39
|
+
if ((0, import_lodash.isEmpty)(queryOptions)) return `${model.sql}
|
|
40
|
+
;`;
|
|
40
41
|
let attributes = (_a = queryOptions.attributes) == null ? void 0 : _a.slice();
|
|
41
42
|
if (attributes) {
|
|
42
43
|
const fields = Array.from(model.collection.fields.keys());
|
|
@@ -63,7 +64,8 @@ function selectQuery(_tableName, options, model) {
|
|
|
63
64
|
}
|
|
64
65
|
const limit = this.addLimitAndOffset(queryOptions, model);
|
|
65
66
|
if (limit) clauses.push(limit);
|
|
66
|
-
return `SELECT ${selected.join(", ")} FROM (${model.sql}
|
|
67
|
+
return `SELECT ${selected.join(", ")} FROM (${model.sql}
|
|
68
|
+
) ${this.getAliasToken()} ${this.quoteIdentifier(
|
|
67
69
|
model.name
|
|
68
70
|
)}${clauses.join("")};`;
|
|
69
71
|
}
|
|
@@ -35,6 +35,7 @@ __export(field_hooks_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(field_hooks_exports);
|
|
36
36
|
var import_sequelize = require("sequelize");
|
|
37
37
|
var import_lodash = require("lodash");
|
|
38
|
+
var import_postgres_collection_manager = require("../postgres-collection-manager");
|
|
38
39
|
var import_validation_error = require("../utils/validation-error");
|
|
39
40
|
const RELATION_TYPES = /* @__PURE__ */ new Set(["belongsTo", "belongsToMany", "belongsToArray", "hasOne", "hasMany"]);
|
|
40
41
|
const RELATION_INTERFACES = {
|
|
@@ -326,6 +327,13 @@ async function syncFieldAction(manager, action, collectionName, fieldName, input
|
|
|
326
327
|
await ensureBelongsToArray(manager, collectionName, name, values, input, transaction);
|
|
327
328
|
return;
|
|
328
329
|
}
|
|
330
|
+
(0, import_postgres_collection_manager.applyPostgresArrayDataType)(values);
|
|
331
|
+
if (values.dataType === "array") {
|
|
332
|
+
input.dataType = "array";
|
|
333
|
+
if (input.elementType === void 0 && typeof values.elementType === "string") {
|
|
334
|
+
input.elementType = values.elementType;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
329
337
|
await manager.addField(collectionName, { ...values, name }, transaction);
|
|
330
338
|
return;
|
|
331
339
|
}
|
|
@@ -363,9 +371,21 @@ async function syncFieldAction(manager, action, collectionName, fieldName, input
|
|
|
363
371
|
}
|
|
364
372
|
return;
|
|
365
373
|
}
|
|
374
|
+
const effectiveType = values.type ?? field.options.type;
|
|
375
|
+
const effectiveElementType = values.elementType ?? field.options.elementType;
|
|
376
|
+
const effectiveDataType = values.dataType !== void 0 ? values.dataType : field.options.dataType;
|
|
377
|
+
if ((effectiveType === "array" || effectiveType === "set") && typeof effectiveElementType === "string" && effectiveElementType.trim().length > 0 && (effectiveDataType === void 0 || effectiveDataType === null || effectiveDataType === "")) {
|
|
378
|
+
const rawTypeIsArray = field.options.rawType === void 0 ? void 0 : (0, import_postgres_collection_manager.isPostgresPhysicalArrayRawType)(field.options.rawType);
|
|
379
|
+
const columnIsArray = rawTypeIsArray !== void 0 ? rawTypeIsArray : await manager.isPhysicalArrayColumn(collectionName, field.columnName(), transaction);
|
|
380
|
+
if (columnIsArray !== false) {
|
|
381
|
+
values.dataType = "array";
|
|
382
|
+
input.dataType = "array";
|
|
383
|
+
}
|
|
384
|
+
}
|
|
366
385
|
const changes = {};
|
|
367
386
|
for (const key of [
|
|
368
387
|
"type",
|
|
388
|
+
"dataType",
|
|
369
389
|
"allowNull",
|
|
370
390
|
"defaultValue",
|
|
371
391
|
"unique",
|