@tachybase/plugin-data-source-common 0.23.8

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.
Files changed (126) hide show
  1. package/.turbo/turbo-build.log +14 -0
  2. package/LICENSE +201 -0
  3. package/README.md +1 -0
  4. package/client.d.ts +2 -0
  5. package/client.js +1 -0
  6. package/dist/client/features/rest-api/collection-add/AddCollection.d.ts +2 -0
  7. package/dist/client/features/rest-api/collection-add/AddCollectionAction.component.d.ts +2 -0
  8. package/dist/client/features/rest-api/collection-add/CreateCollection.schema.d.ts +389 -0
  9. package/dist/client/features/rest-api/collection-add/CreateCollection.view.d.ts +2 -0
  10. package/dist/client/features/rest-api/collection-add/PreviewComponent.d.ts +2 -0
  11. package/dist/client/features/rest-api/collection-add/PreviewFields.d.ts +5 -0
  12. package/dist/client/features/rest-api/collection-add/RequestActionItems.provider.d.ts +2 -0
  13. package/dist/client/features/rest-api/collection-add/getSchemaRequestAction.d.ts +57 -0
  14. package/dist/client/features/rest-api/collection-add/useActionCreateCollection.d.ts +3 -0
  15. package/dist/client/features/rest-api/collection-delete/DeleteCollection.d.ts +2 -0
  16. package/dist/client/features/rest-api/collection-delete/DeleteCollectionAction.component.d.ts +5 -0
  17. package/dist/client/features/rest-api/collection-delete/useBulkDestroyActionAndRefreshCM.d.ts +3 -0
  18. package/dist/client/features/rest-api/collection-delete/useDestroyActionAndRefreshCM.d.ts +3 -0
  19. package/dist/client/features/rest-api/collection-edit/EditCollection.d.ts +2 -0
  20. package/dist/client/features/rest-api/collection-edit/EditCollectionAction.component.d.ts +2 -0
  21. package/dist/client/features/rest-api/collection-edit/EditCollectionForm.view.d.ts +2 -0
  22. package/dist/client/features/rest-api/collection-edit/useActionEditCollection.d.ts +3 -0
  23. package/dist/client/features/rest-api/constants/mapListve.d.ts +7 -0
  24. package/dist/client/features/rest-api/constants/requestHeaderList.d.ts +4 -0
  25. package/dist/client/features/rest-api/contexts/RequestForm.context.d.ts +11 -0
  26. package/dist/client/features/rest-api/contexts/ResponseInfo.context.d.ts +12 -0
  27. package/dist/client/features/rest-api/form-data-source/DataSourceSettingsForm.d.ts +2 -0
  28. package/dist/client/features/rest-api/form-data-source/DataSourceSettingsForm.schema.d.ts +172 -0
  29. package/dist/client/features/rest-api/kit.d.ts +4 -0
  30. package/dist/client/features/rest-api/request-configs/debug-area/Debug.provider.d.ts +2 -0
  31. package/dist/client/features/rest-api/request-configs/debug-area/Debug.schema.d.ts +164 -0
  32. package/dist/client/features/rest-api/request-configs/debug-area/Debug.view.d.ts +2 -0
  33. package/dist/client/features/rest-api/request-configs/debug-area/DebugComponent.d.ts +2 -0
  34. package/dist/client/features/rest-api/request-configs/debug-area/components/AlertError.d.ts +2 -0
  35. package/dist/client/features/rest-api/request-configs/debug-area/components/DebugResponse.schema.d.ts +21 -0
  36. package/dist/client/features/rest-api/request-configs/debug-area/components/DebugResponse.view.d.ts +2 -0
  37. package/dist/client/features/rest-api/request-configs/debug-area/components/DebugResponseTabs.d.ts +2 -0
  38. package/dist/client/features/rest-api/request-configs/debug-area/components/DebugResponseTabs.items.d.ts +6 -0
  39. package/dist/client/features/rest-api/request-configs/debug-area/components/ExtractFieldMetadata.d.ts +2 -0
  40. package/dist/client/features/rest-api/request-configs/debug-area/components/Headers.schema.d.ts +60 -0
  41. package/dist/client/features/rest-api/request-configs/debug-area/components/RequestHeaders.view.d.ts +2 -0
  42. package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseBody.schema.d.ts +21 -0
  43. package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseBody.view.d.ts +2 -0
  44. package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseHeaders.view.d.ts +2 -0
  45. package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseTab.d.ts +2 -0
  46. package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseTab.item.d.ts +6 -0
  47. package/dist/client/features/rest-api/request-configs/debug-area/components/TooltipContainer.d.ts +2 -0
  48. package/dist/client/features/rest-api/request-configs/debug-area/schemas/getSchemaAction.d.ts +88 -0
  49. package/dist/client/features/rest-api/request-configs/debug-area/schemas/getSchemaParam.d.ts +1 -0
  50. package/dist/client/features/rest-api/request-configs/debug-area/scopes/useCancelAction.d.ts +3 -0
  51. package/dist/client/features/rest-api/request-configs/debug-area/scopes/useDebugAction.d.ts +3 -0
  52. package/dist/client/features/rest-api/request-configs/method-path/FieldMethod.schema.d.ts +23 -0
  53. package/dist/client/features/rest-api/request-configs/method-path/FieldMethod.view.d.ts +2 -0
  54. package/dist/client/features/rest-api/request-configs/method-path/FieldPath.schema.d.ts +35 -0
  55. package/dist/client/features/rest-api/request-configs/method-path/FieldPath.view.d.ts +2 -0
  56. package/dist/client/features/rest-api/request-configs/method-path/MethodPathComponent.d.ts +2 -0
  57. package/dist/client/features/rest-api/request-configs/request-tab/RequestBody.schema.d.ts +124 -0
  58. package/dist/client/features/rest-api/request-configs/request-tab/RequestBody.view.d.ts +2 -0
  59. package/dist/client/features/rest-api/request-configs/request-tab/RequestHeaders.schema.d.ts +77 -0
  60. package/dist/client/features/rest-api/request-configs/request-tab/RequestHeaders.view.d.ts +2 -0
  61. package/dist/client/features/rest-api/request-configs/request-tab/RequestParams.schema.d.ts +81 -0
  62. package/dist/client/features/rest-api/request-configs/request-tab/RequestParams.view.d.ts +2 -0
  63. package/dist/client/features/rest-api/request-configs/request-tab/RequestTab.d.ts +2 -0
  64. package/dist/client/features/rest-api/request-configs/request-tab/RequestTab.items.d.ts +6 -0
  65. package/dist/client/features/rest-api/request-configs/request-tab/debounceClick.util.d.ts +2 -0
  66. package/dist/client/features/rest-api/request-configs/request-transformer/ResponseTransformerComponent.d.ts +2 -0
  67. package/dist/client/features/rest-api/scopes/useVariableOptions.d.ts +1 -0
  68. package/dist/client/features/rest-api/utils/filterObjectWithMethodAndPath.d.ts +1 -0
  69. package/dist/client/features/rest-api/utils/filterTree.d.ts +1 -0
  70. package/dist/client/features/rest-api/utils/getRequestActions.d.ts +1 -0
  71. package/dist/client/features/rest-api/utils/getRequestValues.d.ts +1 -0
  72. package/dist/client/features/rest-api/utils/responseTransformerAe.d.ts +4 -0
  73. package/dist/client/features/rest-api/utils/setFormValue.d.ts +1 -0
  74. package/dist/client/forms/msql.d.ts +2 -0
  75. package/dist/client/forms/pg.d.ts +2 -0
  76. package/dist/client/index.d.ts +6 -0
  77. package/dist/client/index.js +86 -0
  78. package/dist/client/locale.d.ts +4 -0
  79. package/dist/externalVersion.js +16 -0
  80. package/dist/index.d.ts +2 -0
  81. package/dist/index.js +39 -0
  82. package/dist/locale/en-US.json +32 -0
  83. package/dist/locale/zh-CN.json +32 -0
  84. package/dist/server/http/plugin.d.ts +10 -0
  85. package/dist/server/http/plugin.js +78 -0
  86. package/dist/server/http/services/http-api-repository.d.ts +18 -0
  87. package/dist/server/http/services/http-api-repository.js +125 -0
  88. package/dist/server/http/services/http-collection-manager.d.ts +6 -0
  89. package/dist/server/http/services/http-collection-manager.js +36 -0
  90. package/dist/server/http/services/http-collection.d.ts +7 -0
  91. package/dist/server/http/services/http-collection.js +270 -0
  92. package/dist/server/http/services/http-data-source.d.ts +10 -0
  93. package/dist/server/http/services/http-data-source.js +67 -0
  94. package/dist/server/http/services/http-request-builder.d.ts +3 -0
  95. package/dist/server/http/services/http-request-builder.js +30 -0
  96. package/dist/server/http/services/transform-response.d.ts +1 -0
  97. package/dist/server/http/services/transform-response.js +185 -0
  98. package/dist/server/http/services/type-interface-map.d.ts +182 -0
  99. package/dist/server/http/services/type-interface-map.js +246 -0
  100. package/dist/server/http/services/utils.d.ts +2 -0
  101. package/dist/server/http/services/utils.js +49 -0
  102. package/dist/server/index.d.ts +1 -0
  103. package/dist/server/index.js +33 -0
  104. package/dist/server/mysql/database-introspector.d.ts +27 -0
  105. package/dist/server/mysql/database-introspector.js +254 -0
  106. package/dist/server/mysql/mysql-collection-manager.d.ts +3 -0
  107. package/dist/server/mysql/mysql-collection-manager.js +29 -0
  108. package/dist/server/mysql/mysql-data-source.d.ts +7 -0
  109. package/dist/server/mysql/mysql-data-source.js +111 -0
  110. package/dist/server/mysql/type-interface-map.d.ts +193 -0
  111. package/dist/server/mysql/type-interface-map.js +257 -0
  112. package/dist/server/pg/database-introspector.d.ts +54 -0
  113. package/dist/server/pg/database-introspector.js +286 -0
  114. package/dist/server/pg/postgres-collection-manager.d.ts +3 -0
  115. package/dist/server/pg/postgres-collection-manager.js +29 -0
  116. package/dist/server/pg/postgres-data-source.d.ts +8 -0
  117. package/dist/server/pg/postgres-data-source.js +127 -0
  118. package/dist/server/pg/type-interface-map.d.ts +183 -0
  119. package/dist/server/pg/type-interface-map.js +243 -0
  120. package/dist/server/pg/utils.d.ts +1 -0
  121. package/dist/server/pg/utils.js +48 -0
  122. package/dist/server/plugin.d.ts +13 -0
  123. package/dist/server/plugin.js +60 -0
  124. package/package.json +32 -0
  125. package/server.d.ts +2 -0
  126. package/server.js +1 -0
@@ -0,0 +1,257 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var type_interface_map_exports = {};
19
+ __export(type_interface_map_exports, {
20
+ default: () => type_interface_map_default
21
+ });
22
+ module.exports = __toCommonJS(type_interface_map_exports);
23
+ const typeInterfaceMap = {
24
+ array: "",
25
+ belongsTo: "",
26
+ belongsToMany: "",
27
+ boolean: () => {
28
+ return {
29
+ interface: "checkbox",
30
+ uiSchema: {
31
+ type: "boolean",
32
+ "x-component": "Checkbox"
33
+ }
34
+ };
35
+ },
36
+ context: "",
37
+ date: () => {
38
+ return {
39
+ interface: "datetime",
40
+ uiSchema: {
41
+ "x-component": "DatePicker",
42
+ "x-component-props": {
43
+ dateFormat: "YYYY-MM-DD",
44
+ showTime: false
45
+ }
46
+ }
47
+ };
48
+ },
49
+ hasMany: "",
50
+ hasOne: "",
51
+ json: () => {
52
+ return {
53
+ interface: "json",
54
+ uiSchema: {
55
+ "x-component": "Input.JSON",
56
+ "x-component-props": {
57
+ autoSize: {
58
+ minRows: 5
59
+ // maxRows: 20,
60
+ }
61
+ },
62
+ default: null
63
+ }
64
+ };
65
+ },
66
+ jsonb: () => {
67
+ return {
68
+ interface: "json",
69
+ uiSchema: {
70
+ "x-component": "Input.JSON",
71
+ "x-component-props": {
72
+ autoSize: {
73
+ minRows: 5
74
+ // maxRows: 20,
75
+ }
76
+ },
77
+ default: null
78
+ }
79
+ };
80
+ },
81
+ integer: () => ({
82
+ interface: "integer",
83
+ // name,
84
+ uiSchema: {
85
+ type: "number",
86
+ // title,
87
+ "x-component": "InputNumber",
88
+ "x-component-props": {
89
+ stringMode: true,
90
+ step: "1"
91
+ },
92
+ "x-validator": "integer"
93
+ }
94
+ }),
95
+ bigInt: (columnInfo) => {
96
+ return {
97
+ interface: "integer",
98
+ uiSchema: {
99
+ "x-component": "InputNumber",
100
+ "x-component-props": {
101
+ style: {
102
+ width: "100%"
103
+ }
104
+ }
105
+ }
106
+ };
107
+ },
108
+ float: () => {
109
+ return {
110
+ interface: "number",
111
+ uiSchema: {
112
+ type: "number",
113
+ // title,
114
+ "x-component": "InputNumber",
115
+ "x-component-props": {
116
+ stringMode: true,
117
+ step: "1"
118
+ }
119
+ }
120
+ };
121
+ },
122
+ double: () => {
123
+ return {
124
+ interface: "number",
125
+ uiSchema: {
126
+ type: "number",
127
+ // title,
128
+ "x-component": "InputNumber",
129
+ "x-component-props": {
130
+ stringMode: true,
131
+ step: "1"
132
+ }
133
+ }
134
+ };
135
+ },
136
+ real: () => {
137
+ return {
138
+ interface: "number",
139
+ uiSchema: {
140
+ type: "number",
141
+ // title,
142
+ "x-component": "InputNumber",
143
+ "x-component-props": {
144
+ stringMode: true,
145
+ step: "1"
146
+ }
147
+ }
148
+ };
149
+ },
150
+ decimal: () => {
151
+ return {
152
+ interface: "number",
153
+ uiSchema: {
154
+ type: "number",
155
+ // title,
156
+ "x-component": "InputNumber",
157
+ "x-component-props": {
158
+ stringMode: true,
159
+ step: "1"
160
+ }
161
+ }
162
+ };
163
+ },
164
+ password: () => ({
165
+ interface: "password",
166
+ hidden: true,
167
+ // name,
168
+ uiSchema: {
169
+ type: "string",
170
+ // title,
171
+ "x-component": "Password"
172
+ }
173
+ }),
174
+ radio: "",
175
+ set: "",
176
+ sort: "",
177
+ string: (columnInfo) => {
178
+ const type = columnInfo.type;
179
+ if (type.startsWith("ENUM")) {
180
+ return {
181
+ interface: "select",
182
+ uiSchema: {
183
+ type: "string",
184
+ "x-component": "Select",
185
+ enum: type.replace("ENUM(", "").replace(")", "").replace(/'/g, "").split(",").map((item) => ({
186
+ label: item,
187
+ value: item
188
+ }))
189
+ }
190
+ };
191
+ }
192
+ return {
193
+ interface: "input",
194
+ uiSchema: {
195
+ "x-component": "Input",
196
+ "x-component-props": {
197
+ style: {
198
+ width: "100%"
199
+ }
200
+ }
201
+ }
202
+ };
203
+ },
204
+ text: () => {
205
+ return {
206
+ interface: "textarea",
207
+ // name,
208
+ uiSchema: {
209
+ type: "string",
210
+ "x-component": "Input.TextArea"
211
+ }
212
+ };
213
+ },
214
+ time: () => ({
215
+ interface: "time",
216
+ // name,
217
+ uiSchema: {
218
+ type: "string",
219
+ "x-component": "TimePicker",
220
+ "x-component-props": {
221
+ format: "HH:mm:ss"
222
+ }
223
+ }
224
+ }),
225
+ uid: () => {
226
+ return {
227
+ interface: "input",
228
+ uiSchema: {
229
+ "x-component": "Input",
230
+ "x-component-props": {
231
+ style: {
232
+ width: "100%"
233
+ }
234
+ }
235
+ }
236
+ };
237
+ },
238
+ uuid: () => {
239
+ return {
240
+ interface: "uuid",
241
+ uiSchema: {
242
+ "x-component": "Input",
243
+ "x-component-props": {
244
+ style: {
245
+ width: "100%"
246
+ }
247
+ }
248
+ }
249
+ };
250
+ },
251
+ virtual: "",
252
+ point: "",
253
+ polygon: "",
254
+ lineString: "",
255
+ circle: ""
256
+ };
257
+ var type_interface_map_default = typeInterfaceMap;
@@ -0,0 +1,54 @@
1
+ import { EventEmitter } from 'events';
2
+ export declare class DatabaseIntrospector extends EventEmitter {
3
+ db: any;
4
+ typeInterfaceMap: any;
5
+ constructor(options: any);
6
+ getCollections(options?: {}): Promise<any[]>;
7
+ getPrimaryKeysOfTable(tableInfo: any): Promise<any>;
8
+ getCollection(options: any): Promise<any>;
9
+ loadCollection(options: any): void;
10
+ loadCollections(options: any): void;
11
+ tableInfoToCollectionOptions(tableInfo: any): {
12
+ name: any;
13
+ title: any;
14
+ schema: any;
15
+ tableName: any;
16
+ };
17
+ collectionOptionsByFields(fields: any): {
18
+ timestamps: boolean;
19
+ autoGenId: boolean;
20
+ filterTargetKey: any;
21
+ };
22
+ mergeLocalDataIntoCollectionOptions(collectionOptions: any, localData: any): any;
23
+ columnInfoToFieldOptions(columnsInfo: any, columnName: any, indexes: any): {
24
+ rawType: any;
25
+ name: any;
26
+ type: any;
27
+ possibleTypes: any[];
28
+ allowNull: any;
29
+ primaryKey: any;
30
+ unique: boolean;
31
+ autoIncrement: boolean;
32
+ } | {
33
+ rawType: any;
34
+ name: any;
35
+ type: any;
36
+ possibleTypes?: undefined;
37
+ allowNull: any;
38
+ primaryKey: any;
39
+ unique: boolean;
40
+ autoIncrement: boolean;
41
+ } | {
42
+ rawType: any;
43
+ name: any;
44
+ supported: boolean;
45
+ };
46
+ getDefaultInterfaceByType(columnsInfo: any, columnName: any, type: any): any;
47
+ columnAttribute(columnsInfo: any, columnName: any, indexes: any): {
48
+ type: any;
49
+ allowNull: any;
50
+ primaryKey: any;
51
+ unique: boolean;
52
+ autoIncrement: boolean;
53
+ };
54
+ }
@@ -0,0 +1,286 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var database_introspector_exports = {};
29
+ __export(database_introspector_exports, {
30
+ DatabaseIntrospector: () => DatabaseIntrospector
31
+ });
32
+ module.exports = __toCommonJS(database_introspector_exports);
33
+ var import_events = require("events");
34
+ var import_database = require("@tachybase/database");
35
+ var import_module_data_source = require("@tachybase/module-data-source");
36
+ var import_lodash = __toESM(require("lodash"));
37
+ var import_utils = require("./utils");
38
+ class DatabaseIntrospector extends import_events.EventEmitter {
39
+ db;
40
+ typeInterfaceMap;
41
+ constructor(options) {
42
+ super();
43
+ this.db = options.db;
44
+ this.typeInterfaceMap = options.typeInterfaceMap;
45
+ }
46
+ async getCollections(options = {}) {
47
+ let tableList = await this.db.sequelize.getQueryInterface().showAllTables();
48
+ const views = (await this.db.queryInterface.listViews()).map((view) => view.name);
49
+ tableList = tableList.concat(views);
50
+ if (this.db.options.tablePrefix) {
51
+ tableList = tableList.filter((tableName) => {
52
+ return tableName.startsWith(this.db.options.tablePrefix);
53
+ });
54
+ }
55
+ const batchSize = 5;
56
+ const results = [];
57
+ for (let i = 0; i < tableList.length; i += batchSize) {
58
+ const batch = tableList.slice(i, i + batchSize);
59
+ const batchResults = await Promise.all(
60
+ batch.map(async (tableName) => {
61
+ var _a;
62
+ const tableInfo = {
63
+ tableName
64
+ };
65
+ this.emit("loadMessage", {
66
+ message: `load table ${tableName}`
67
+ });
68
+ const collectionOptions = this.tableInfoToCollectionOptions(tableInfo);
69
+ const localOptions = (_a = options.localData) == null ? void 0 : _a[collectionOptions.name];
70
+ try {
71
+ return await this.getCollection({
72
+ tableInfo,
73
+ localOptions,
74
+ mergedOptions: views.includes(tableName) ? { view: true, template: "view" } : {}
75
+ });
76
+ } catch (e) {
77
+ if (e.message.includes("No description found for")) {
78
+ return false;
79
+ }
80
+ this.db.logger.error(`load table ${tableName} failed, ${e.message}`);
81
+ return false;
82
+ }
83
+ })
84
+ );
85
+ results.push(...batchResults);
86
+ }
87
+ return results.filter(Boolean);
88
+ }
89
+ async getPrimaryKeysOfTable(tableInfo) {
90
+ const sql = `
91
+ SELECT
92
+ pg_attribute.attname,
93
+ format_type(pg_attribute.atttypid, pg_attribute.atttypmod)
94
+ FROM pg_index, pg_class, pg_attribute, pg_namespace
95
+ WHERE
96
+ pg_class.oid = '"${tableInfo.tableName}"'::regclass AND
97
+ indrelid = pg_class.oid AND
98
+ nspname = '${tableInfo.schema || "public"}' AND
99
+ pg_class.relnamespace = pg_namespace.oid AND
100
+ pg_attribute.attrelid = pg_class.oid AND
101
+ pg_attribute.attnum = any(pg_index.indkey)
102
+ AND indisprimary
103
+ `;
104
+ return await this.db.sequelize.query(sql, {
105
+ type: "SELECT"
106
+ });
107
+ }
108
+ async getCollection(options) {
109
+ const { tableInfo } = options;
110
+ const columnsInfo = await this.db.sequelize.getQueryInterface().describeTable(tableInfo);
111
+ const primaryKeys = await this.getPrimaryKeysOfTable(tableInfo);
112
+ Object.keys(columnsInfo).forEach((columnName) => {
113
+ const columnInfo = columnsInfo[columnName];
114
+ const primaryKey = primaryKeys.find((key) => key.attname === columnName);
115
+ if (primaryKey) {
116
+ columnInfo.primaryKey = true;
117
+ }
118
+ });
119
+ const collectionOptions = this.tableInfoToCollectionOptions(tableInfo);
120
+ const constraints = await this.db.sequelize.query((0, import_utils.showUniqueIndexes)(tableInfo), {
121
+ type: "SELECT"
122
+ });
123
+ try {
124
+ const fields = Object.keys(columnsInfo).map((columnName) => {
125
+ return this.columnInfoToFieldOptions(columnsInfo, columnName, constraints);
126
+ });
127
+ const unsupportedFields = fields.filter((field) => {
128
+ return field.supported === false;
129
+ });
130
+ const supportFields = fields.filter((field) => {
131
+ return field.supported !== false;
132
+ });
133
+ const remoteCollectionInfo = {
134
+ ...collectionOptions,
135
+ ...options == null ? void 0 : options.mergedOptions,
136
+ ...this.collectionOptionsByFields(supportFields),
137
+ fields: [...supportFields]
138
+ };
139
+ if (unsupportedFields.length) {
140
+ remoteCollectionInfo.unsupportedFields = unsupportedFields;
141
+ }
142
+ const finalOptions = this.mergeLocalDataIntoCollectionOptions(remoteCollectionInfo, options.localOptions);
143
+ if (finalOptions.view && !finalOptions.filterTargetKey && supportFields.find((field) => field.name === "id")) {
144
+ finalOptions.filterTargetKey = "id";
145
+ }
146
+ return finalOptions;
147
+ } catch (e) {
148
+ throw new Error(`table ${tableInfo.tableName} introspection error: ${e.message}`);
149
+ }
150
+ }
151
+ loadCollection(options) {
152
+ this.db.collection({
153
+ ...options,
154
+ introspected: true
155
+ });
156
+ }
157
+ loadCollections(options) {
158
+ options.collections.forEach((collection) => {
159
+ this.loadCollection(collection);
160
+ });
161
+ }
162
+ tableInfoToCollectionOptions(tableInfo) {
163
+ const tableName = tableInfo.tableName;
164
+ let name = tableName;
165
+ if (this.db.options.tablePrefix) {
166
+ name = tableName.replace(this.db.options.tablePrefix, "");
167
+ }
168
+ return {
169
+ name,
170
+ title: name,
171
+ schema: tableInfo.schema,
172
+ tableName
173
+ };
174
+ }
175
+ collectionOptionsByFields(fields) {
176
+ const options = {
177
+ timestamps: false,
178
+ autoGenId: false,
179
+ filterTargetKey: void 0
180
+ };
181
+ const autoIncrementField = fields.find((field) => field.autoIncrement);
182
+ if (autoIncrementField) {
183
+ options.filterTargetKey = autoIncrementField.name;
184
+ }
185
+ const primaryKeys = fields.filter((field) => field.primaryKey);
186
+ if (!options.filterTargetKey && primaryKeys.length === 1) {
187
+ options.filterTargetKey = primaryKeys[0].name;
188
+ }
189
+ const uniques = fields.filter((field) => field.unique);
190
+ if (!options.filterTargetKey && uniques.length === 1) {
191
+ options.filterTargetKey = uniques[0].name;
192
+ }
193
+ return options;
194
+ }
195
+ mergeLocalDataIntoCollectionOptions(collectionOptions, localData) {
196
+ var _a;
197
+ if (!localData) {
198
+ return collectionOptions;
199
+ }
200
+ const collectionFields = collectionOptions.fields || [];
201
+ const localFieldsAsObject = import_lodash.default.keyBy(localData.fields, "name");
202
+ const newFields = collectionFields.map((field) => {
203
+ const localField = localFieldsAsObject[field.name];
204
+ if (!localField) {
205
+ return field;
206
+ }
207
+ return (0, import_module_data_source.mergeOptions)(field, localField);
208
+ });
209
+ const localAssociationFields = (_a = localData.fields) == null ? void 0 : _a.filter((field) => {
210
+ return ["belongsTo", "belongsToMany", "hasMany", "hasOne"].includes(field.type);
211
+ });
212
+ if (localAssociationFields) {
213
+ newFields.push(...localAssociationFields);
214
+ }
215
+ return {
216
+ ...collectionOptions,
217
+ ...import_lodash.default.omit(localData, ["fields"]),
218
+ fields: newFields
219
+ };
220
+ }
221
+ columnInfoToFieldOptions(columnsInfo, columnName, indexes) {
222
+ const columnInfo = columnsInfo[columnName];
223
+ let fieldOptions = {
224
+ ...this.columnAttribute(columnsInfo, columnName, indexes),
225
+ ...import_database.ViewFieldInference.inferToFieldType({
226
+ dialect: this.db.options.dialect,
227
+ type: columnInfo.type,
228
+ name: columnName
229
+ }),
230
+ rawType: columnInfo.type,
231
+ name: columnName
232
+ };
233
+ const interfaceConfig = this.getDefaultInterfaceByType(columnsInfo, columnName, fieldOptions.type);
234
+ if (typeof interfaceConfig === "string") {
235
+ fieldOptions.interface = interfaceConfig;
236
+ } else {
237
+ fieldOptions = {
238
+ ...fieldOptions,
239
+ ...interfaceConfig
240
+ };
241
+ }
242
+ if (!fieldOptions.type) {
243
+ return {
244
+ rawType: columnInfo.type,
245
+ name: columnName,
246
+ supported: false
247
+ };
248
+ }
249
+ import_lodash.default.set(fieldOptions, "uiSchema.title", columnName);
250
+ return fieldOptions;
251
+ }
252
+ getDefaultInterfaceByType(columnsInfo, columnName, type) {
253
+ const interfaceConfig = this.typeInterfaceMap[type];
254
+ if (typeof interfaceConfig === "function") {
255
+ return interfaceConfig(columnsInfo[columnName]);
256
+ }
257
+ return interfaceConfig;
258
+ }
259
+ columnAttribute(columnsInfo, columnName, indexes) {
260
+ const columnInfo = columnsInfo[columnName];
261
+ const attr = {
262
+ type: columnInfo.type,
263
+ allowNull: columnInfo.allowNull,
264
+ primaryKey: columnInfo.primaryKey,
265
+ unique: false,
266
+ autoIncrement: false
267
+ };
268
+ if (columnInfo.defaultValue && typeof columnInfo.defaultValue === "string") {
269
+ const isSerial = columnInfo.defaultValue.match(/^nextval\(/);
270
+ const isUUID = columnInfo.defaultValue.match(/^uuid_generate_v4\(/);
271
+ if (isSerial || isUUID) {
272
+ attr.autoIncrement = true;
273
+ }
274
+ }
275
+ for (const index of indexes) {
276
+ if (index["is_single_column"] && index["column_name"] === columnName) {
277
+ attr.unique = true;
278
+ }
279
+ }
280
+ return attr;
281
+ }
282
+ }
283
+ // Annotate the CommonJS export names for ESM import in node:
284
+ 0 && (module.exports = {
285
+ DatabaseIntrospector
286
+ });
@@ -0,0 +1,3 @@
1
+ import { SequelizeCollectionManager } from '@tachybase/data-source';
2
+ export declare class PostgresCollectionManager extends SequelizeCollectionManager {
3
+ }
@@ -0,0 +1,29 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var postgres_collection_manager_exports = {};
19
+ __export(postgres_collection_manager_exports, {
20
+ PostgresCollectionManager: () => PostgresCollectionManager
21
+ });
22
+ module.exports = __toCommonJS(postgres_collection_manager_exports);
23
+ var import_data_source = require("@tachybase/data-source");
24
+ class PostgresCollectionManager extends import_data_source.SequelizeCollectionManager {
25
+ }
26
+ // Annotate the CommonJS export names for ESM import in node:
27
+ 0 && (module.exports = {
28
+ PostgresCollectionManager
29
+ });
@@ -0,0 +1,8 @@
1
+ import { DataSource } from '@tachybase/data-source';
2
+ import { type IDatabaseOptions } from '@tachybase/database';
3
+ import { PostgresCollectionManager } from './postgres-collection-manager';
4
+ export declare class PostgresDataSource extends DataSource {
5
+ static testConnection(options: IDatabaseOptions): Promise<boolean>;
6
+ load(options: any): Promise<void>;
7
+ createCollectionManager(options: any): PostgresCollectionManager;
8
+ }