plugin-data-source-postgres 2.0.1 → 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.
@@ -137,5 +137,12 @@
137
137
  "Column name": "Column name",
138
138
  "Column name is required": "Column name is required",
139
139
  "Foreign key {{foreignKey}} cannot have the same name as relation field {{name}}": "Foreign key {{foreignKey}} cannot have the same name as relation field {{name}}",
140
- "Foreign key {{collection}}.{{field}} has incompatible PostgreSQL type {{type}}": "Foreign key {{collection}}.{{field}} has incompatible PostgreSQL type {{type}}"
140
+ "Foreign key {{collection}}.{{field}} has incompatible PostgreSQL type {{type}}": "Foreign key {{collection}}.{{field}} has incompatible PostgreSQL type {{type}}",
141
+ "Saved connection": "Saved connection",
142
+ "Reuse an existing PostgreSQL connection or leave blank to create a new one": "Reuse an existing PostgreSQL connection or leave blank to create a new one",
143
+ "Create new connection": "Create new connection",
144
+ "Leave blank to reuse saved password": "Leave blank to reuse saved password",
145
+ "Leave blank to reuse saved CA certificate": "Leave blank to reuse saved CA certificate",
146
+ "Saved PostgreSQL connection {{name}} does not exist": "Saved PostgreSQL connection {{name}} does not exist",
147
+ "No saved PostgreSQL connections": "No saved PostgreSQL connections"
141
148
  }
@@ -137,5 +137,12 @@
137
137
  "Column name": "列名",
138
138
  "Column name is required": "请输入列名",
139
139
  "Foreign key {{foreignKey}} cannot have the same name as relation field {{name}}": "外键 {{foreignKey}} 不能与关系字段 {{name}} 同名",
140
- "Foreign key {{collection}}.{{field}} has incompatible PostgreSQL type {{type}}": "外键 {{collection}}.{{field}} 的 PostgreSQL 类型 {{type}} 不兼容"
140
+ "Foreign key {{collection}}.{{field}} has incompatible PostgreSQL type {{type}}": "外键 {{collection}}.{{field}} 的 PostgreSQL 类型 {{type}} 不兼容",
141
+ "Saved connection": "已保存的连接",
142
+ "Reuse an existing PostgreSQL connection or leave blank to create a new one": "复用已有的 PostgreSQL 连接,或留空以创建新连接",
143
+ "Create new connection": "创建新连接",
144
+ "Leave blank to reuse saved password": "留空以复用已保存的密码",
145
+ "Leave blank to reuse saved CA certificate": "留空以复用已保存的 CA 证书",
146
+ "Saved PostgreSQL connection {{name}} does not exist": "已保存的 PostgreSQL 连接 {{name}} 不存在",
147
+ "No saved PostgreSQL connections": "暂无已保存的 PostgreSQL 连接"
141
148
  }
@@ -585,6 +585,80 @@ function prepareFields(values) {
585
585
  }
586
586
  values.fields = mapped;
587
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
+ }
588
662
  async function localizeValidationErrors(ctx, next) {
589
663
  var _a, _b, _c, _d, _e;
590
664
  try {
@@ -826,7 +900,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
826
900
  });
827
901
  }
828
902
  async load() {
829
- var _a, _b;
903
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
830
904
  (_b = (_a = this.app.db) == null ? void 0 : _a.on) == null ? void 0 : _b.call(
831
905
  _a,
832
906
  "dataSources.afterDestroy",
@@ -851,13 +925,35 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
851
925
  );
852
926
  }
853
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
+ }
854
947
  this.app.resourceManager.registerPreActionHandler("dataSources:readTables", async (ctx, next) => {
855
- var _a2, _b2, _c;
948
+ var _a2, _b2, _c2;
856
949
  const { dataSourceKey, dbOptions } = ctx.action.params.values || {};
857
950
  if ((dbOptions == null ? void 0 : dbOptions.type) !== import_constants.DATA_SOURCE_TYPE) {
858
951
  await next();
859
952
  return;
860
953
  }
954
+ if (isRecord(dbOptions)) {
955
+ await resolveSavedConnectionOptions(ctx, dbOptions);
956
+ }
861
957
  const manager = this.app.dataSourceManager;
862
958
  const renderedOptions = ((_a2 = this.app.environment) == null ? void 0 : _a2.renderJsonTemplate) ? this.app.environment.renderJsonTemplate(dbOptions) : dbOptions;
863
959
  const probe = manager.factory.create(import_constants.DATA_SOURCE_TYPE, {
@@ -879,7 +975,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
879
975
  await probe.close();
880
976
  } catch (error) {
881
977
  if (requestFailed) {
882
- (_c = (_b2 = this.app.log) == null ? void 0 : _b2.warn) == null ? void 0 : _c.call(_b2, "[PostgreSQL] Failed to close temporary table-list connection");
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");
883
979
  } else {
884
980
  requestFailed = true;
885
981
  requestError = error;
@@ -896,7 +992,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
896
992
  });
897
993
  this.app.resourceManager.use(
898
994
  async (ctx, next) => {
899
- var _a2, _b2, _c;
995
+ var _a2, _b2, _c2;
900
996
  const { resourceName, actionName, params } = ctx.action || {};
901
997
  if (resourceName === "dataSources.collections") {
902
998
  const dataSourceKey = ((_a2 = ctx.action) == null ? void 0 : _a2.sourceId) || (params == null ? void 0 : params.associatedIndex);
@@ -933,7 +1029,7 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
933
1029
  const separator = index.indexOf(".");
934
1030
  const indexDataSourceKey = separator !== -1 ? index.slice(0, separator) : "";
935
1031
  const indexCollectionName = separator !== -1 ? index.slice(separator + 1) : "";
936
- const rawBody = (_c = ctx.request) == null ? void 0 : _c.body;
1032
+ const rawBody = (_c2 = ctx.request) == null ? void 0 : _c2.body;
937
1033
  const input = params.values || (isRecord(rawBody) ? rawBody : {});
938
1034
  const dataSourceKey = actionName === "apply" && typeof input.dataSourceKey === "string" && input.dataSourceKey ? input.dataSourceKey : indexDataSourceKey;
939
1035
  const collectionName = actionName === "apply" && typeof input.collectionName === "string" && input.collectionName ? input.collectionName : indexCollectionName;
@@ -947,6 +1043,31 @@ class PluginDataSourcePostgresServer extends import_server.Plugin {
947
1043
  },
948
1044
  { tag: "postgres-schema-actions", after: ["acl"] }
949
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
+ );
950
1071
  }
951
1072
  async resolveCollectionsToDestroy(ctx, source) {
952
1073
  var _a, _b, _c, _d, _e, _f, _g;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName.zh-CN": "数据源:外部PostgreSQL",
5
5
  "description": "Connect to external PostgreSQL databases as data sources",
6
6
  "description.zh-CN": "连接外部PostgreSQL数据库作为数据源",
7
- "version": "2.0.1",
7
+ "version": "2.1.1",
8
8
  "license": "AGPL-3.0",
9
9
  "main": "dist/server/index.js",
10
10
  "nocobase": {
@@ -45,7 +45,7 @@
45
45
  "url": "git+https://github.com/OrgGem/nocobase-beta.git",
46
46
  "directory": "packages/plugins/plugin-data-source-postgres"
47
47
  },
48
- "gitHead": "5b21f0f8498a767052d7ee569b54c3e5378ca04f",
48
+ "gitHead": "71e667dd2f5d0ccb215ad3c755ac780f221b1f3a",
49
49
  "publishConfig": {
50
50
  "access": "public",
51
51
  "registry": "https://registry.npmjs.org/"