@nocobase/data-source-manager 2.1.0-beta.13 → 2.1.0-beta.15

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.
@@ -23,4 +23,6 @@ export declare abstract class DatabaseDataSource<T extends DatabaseIntrospector
23
23
  };
24
24
  }): CollectionOptions[];
25
25
  private mergeFieldOptions;
26
+ private hasCompatibleStorageType;
27
+ private getFieldStorageType;
26
28
  }
@@ -58,19 +58,34 @@ const _DatabaseDataSource = class _DatabaseDataSource extends import_data_source
58
58
  const loadedCollection = loadedCollections[collection.name];
59
59
  if (!loadedCollection) return collection;
60
60
  const collectionFields = collection.fields || [];
61
- const loadedFieldsObj = Object.fromEntries(
62
- (loadedCollection.fields || []).map((f) => [f.columnName || f.field || f.name, f])
61
+ const loadedFieldsByName = Object.fromEntries((loadedCollection.fields || []).map((f) => [f.name, f]));
62
+ const loadedFieldsByColumn = (loadedCollection.fields || []).reduce(
63
+ (result, field) => {
64
+ const key = field.columnName || field.field || field.name;
65
+ result[key] = result[key] || [];
66
+ result[key].push(field);
67
+ return result;
68
+ },
69
+ {}
63
70
  );
71
+ const isAliasField = /* @__PURE__ */ __name((field) => Boolean((field == null ? void 0 : field.field) && field.name !== field.field), "isAliasField");
64
72
  const newFields = collectionFields.map((field) => {
65
- const loadedField = loadedFieldsObj[field.name];
73
+ var _a, _b;
74
+ const loadedField = loadedFieldsByName[field.name] || ((_a = loadedFieldsByColumn[field.columnName || field.field || field.name]) == null ? void 0 : _a.find(
75
+ (candidate) => !isAliasField(candidate)
76
+ )) || ((_b = loadedFieldsByColumn[field.columnName || field.field || field.name]) == null ? void 0 : _b[0]);
66
77
  if (!loadedField) return field;
67
78
  if (loadedField.possibleTypes) {
68
79
  loadedField.possibleTypes = field.possibleTypes;
69
80
  }
70
81
  return this.mergeFieldOptions(field, loadedField);
71
82
  });
83
+ const loadedMappedFields = (loadedCollection.fields || []).filter(
84
+ (field) => isAliasField(field) && !newFields.find((newField) => newField.name === field.name)
85
+ );
86
+ newFields.push(...loadedMappedFields);
72
87
  const loadedAssociationFields = (loadedCollection.fields || []).filter(
73
- (field) => ["belongsTo", "belongsToMany", "hasMany", "hasOne", "belongsToArray"].includes(field.type)
88
+ (field) => ["belongsTo", "belongsToMany", "hasMany", "hasOne", "belongsToArray"].includes(field.type) && !newFields.find((newField) => newField.name === field.name)
74
89
  );
75
90
  newFields.push(...loadedAssociationFields);
76
91
  return {
@@ -85,20 +100,77 @@ const _DatabaseDataSource = class _DatabaseDataSource extends import_data_source
85
100
  ...fieldOptions,
86
101
  ...modelOptions
87
102
  };
103
+ const dbSyncedKeys = ["allowNull"];
88
104
  const incomingPossibleTypes = Array.isArray(fieldOptions.possibleTypes) ? fieldOptions.possibleTypes : fieldOptions.type ? [fieldOptions.type] : [];
89
- const shouldUseIncomingType = Boolean(fieldOptions.rawType) && (modelOptions.rawType ? fieldOptions.rawType !== modelOptions.rawType : !incomingPossibleTypes.includes(modelOptions.type));
105
+ const hasCompatibleStorageType = this.hasCompatibleStorageType(fieldOptions.type, modelOptions.type);
106
+ const shouldUseIncomingType = Boolean(fieldOptions.rawType) && Boolean(modelOptions.rawType || modelOptions.type) && (modelOptions.rawType ? fieldOptions.rawType !== modelOptions.rawType && !hasCompatibleStorageType : !incomingPossibleTypes.includes(modelOptions.type) && !hasCompatibleStorageType);
90
107
  if (shouldUseIncomingType) {
91
108
  newOptions.type = fieldOptions.type;
92
109
  newOptions.interface = fieldOptions.interface;
93
110
  newOptions.rawType = fieldOptions.rawType;
94
111
  }
95
112
  for (const key of [.../* @__PURE__ */ new Set([...Object.keys(modelOptions), ...Object.keys(fieldOptions)])]) {
96
- if (modelOptions[key] === null && fieldOptions[key]) {
113
+ const shouldUseIncoming = dbSyncedKeys.includes(key) || modelOptions[key] === null && key in fieldOptions;
114
+ if (shouldUseIncoming) {
97
115
  newOptions[key] = fieldOptions[key];
98
116
  }
99
117
  }
100
118
  return newOptions;
101
119
  }
120
+ hasCompatibleStorageType(incomingType, loadedType) {
121
+ if (!incomingType || !loadedType || incomingType === loadedType) {
122
+ return false;
123
+ }
124
+ const incomingStorageType = this.getFieldStorageType(incomingType);
125
+ const loadedStorageType = this.getFieldStorageType(loadedType);
126
+ if (!incomingStorageType || !loadedStorageType) {
127
+ return false;
128
+ }
129
+ return incomingStorageType === loadedStorageType;
130
+ }
131
+ getFieldStorageType(type) {
132
+ var _a, _b, _c, _d;
133
+ if (!type) {
134
+ return;
135
+ }
136
+ const db = (_a = this.collectionManager) == null ? void 0 : _a.db;
137
+ if (!db) {
138
+ return;
139
+ }
140
+ try {
141
+ const fieldClass = db.fieldTypes.get(type);
142
+ if (!fieldClass) {
143
+ return;
144
+ }
145
+ const descriptor = Object.getOwnPropertyDescriptor(fieldClass.prototype, "dataType");
146
+ const collection = {
147
+ name: "__sync_probe__",
148
+ options: {
149
+ underscored: false
150
+ },
151
+ isView() {
152
+ return false;
153
+ }
154
+ };
155
+ const dataType = (_b = descriptor == null ? void 0 : descriptor.get) == null ? void 0 : _b.call({
156
+ options: {
157
+ type
158
+ },
159
+ database: db,
160
+ collection,
161
+ context: {
162
+ database: db,
163
+ collection
164
+ }
165
+ });
166
+ if (typeof dataType === "string") {
167
+ return dataType;
168
+ }
169
+ return (dataType == null ? void 0 : dataType.key) || ((_c = dataType == null ? void 0 : dataType.constructor) == null ? void 0 : _c.key) || ((_d = dataType == null ? void 0 : dataType.constructor) == null ? void 0 : _d.name);
170
+ } catch {
171
+ return;
172
+ }
173
+ }
102
174
  };
103
175
  __name(_DatabaseDataSource, "DatabaseDataSource");
104
176
  let DatabaseDataSource = _DatabaseDataSource;
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@nocobase/data-source-manager",
3
- "version": "2.1.0-beta.13",
3
+ "version": "2.1.0-beta.15",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./lib/index.js",
7
7
  "types": "./lib/index.d.ts",
8
8
  "dependencies": {
9
- "@nocobase/actions": "2.1.0-beta.13",
10
- "@nocobase/cache": "2.1.0-beta.13",
11
- "@nocobase/database": "2.1.0-beta.13",
12
- "@nocobase/resourcer": "2.1.0-beta.13",
13
- "@nocobase/utils": "2.1.0-beta.13",
9
+ "@nocobase/actions": "2.1.0-beta.15",
10
+ "@nocobase/cache": "2.1.0-beta.15",
11
+ "@nocobase/database": "2.1.0-beta.15",
12
+ "@nocobase/resourcer": "2.1.0-beta.15",
13
+ "@nocobase/utils": "2.1.0-beta.15",
14
14
  "@types/jsonwebtoken": "^8.5.8",
15
15
  "jsonwebtoken": "^9.0.2"
16
16
  },
@@ -19,5 +19,5 @@
19
19
  "url": "git+https://github.com/nocobase/nocobase.git",
20
20
  "directory": "packages/auth"
21
21
  },
22
- "gitHead": "691716e5f4e5f8bd3859d65bc8a29b4e3c32209b"
22
+ "gitHead": "dc1aceea6357e6ab149976c2a236fc4b6bee1370"
23
23
  }