plugin-data-source-mssql 2.0.2 → 2.0.3
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.
|
@@ -181,69 +181,34 @@ class MssqlExternalDataSource extends import_data_source_manager.SequelizeDataSo
|
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
183
|
* Set up MSSQL-specific Sequelize hooks for datetime handling.
|
|
184
|
-
* MSSQL DATETIME doesn't support timezone offset format like '+00:00'
|
|
184
|
+
* MSSQL DATETIME doesn't support timezone offset format like '+00:00',
|
|
185
|
+
* so we convert Date objects to 'YYYY-MM-DD HH:mm:ss.SSS' on save.
|
|
186
|
+
*
|
|
187
|
+
* NOTE: We intentionally do NOT transform dates in WHERE clauses.
|
|
188
|
+
* The NocoBase core date operators already produce properly formatted strings
|
|
189
|
+
* (via DatetimeNoTzField → 'YYYY-MM-DD HH:mm:ss') that MSSQL understands
|
|
190
|
+
* natively. Using Sequelize.literal() for dates in WHERE clauses would:
|
|
191
|
+
* 1. Bypass parameterized queries → no index usage → full table scans
|
|
192
|
+
* 2. Add recursive overhead to every single query
|
|
185
193
|
*/
|
|
186
194
|
setupMssqlHooks() {
|
|
187
195
|
const db = this.collectionManager.db;
|
|
188
196
|
if (!(db == null ? void 0 : db.sequelize)) return;
|
|
189
|
-
const Sequelize = db.sequelize.Sequelize;
|
|
190
|
-
if (!Sequelize) return;
|
|
191
197
|
const isDatetimeString = (value) => {
|
|
192
198
|
return /^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}(:\d{2})?)?$/.test(value);
|
|
193
199
|
};
|
|
194
200
|
db.sequelize.addHook("beforeSave", (instance) => {
|
|
195
|
-
if (instance
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
dateValue = new Date(value);
|
|
203
|
-
}
|
|
204
|
-
if (dateValue && !isNaN(dateValue.getTime())) {
|
|
205
|
-
const iso = dateValue.toISOString();
|
|
206
|
-
const mssqlDate = iso.replace("T", " ").replace("Z", "");
|
|
207
|
-
if (Sequelize && Sequelize.literal) {
|
|
208
|
-
instance.dataValues[key] = Sequelize.literal(`'${mssqlDate}'`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
201
|
+
if (!(instance == null ? void 0 : instance.dataValues)) return;
|
|
202
|
+
for (const [key, value] of Object.entries(instance.dataValues)) {
|
|
203
|
+
let dateValue = null;
|
|
204
|
+
if (value instanceof Date) {
|
|
205
|
+
dateValue = value;
|
|
206
|
+
} else if (typeof value === "string" && isDatetimeString(value)) {
|
|
207
|
+
dateValue = new Date(value);
|
|
211
208
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const transformDatesInObject = (obj, Sq) => {
|
|
215
|
-
if (!obj || typeof obj !== "object") return obj;
|
|
216
|
-
if (obj instanceof Date) {
|
|
217
|
-
if (!isNaN(obj.getTime())) {
|
|
218
|
-
const iso = obj.toISOString();
|
|
219
|
-
const mssqlDate = iso.replace("T", " ").replace("Z", "");
|
|
220
|
-
return Sq.literal(`'${mssqlDate}'`);
|
|
209
|
+
if (dateValue && !isNaN(dateValue.getTime())) {
|
|
210
|
+
instance.dataValues[key] = dateValue.toISOString().replace("T", " ").replace("Z", "");
|
|
221
211
|
}
|
|
222
|
-
return obj;
|
|
223
|
-
}
|
|
224
|
-
if (Array.isArray(obj)) {
|
|
225
|
-
return obj.map((item) => transformDatesInObject(item, Sq));
|
|
226
|
-
}
|
|
227
|
-
const result = {};
|
|
228
|
-
for (const key of Reflect.ownKeys(obj)) {
|
|
229
|
-
result[key] = transformDatesInObject(obj[key], Sq);
|
|
230
|
-
}
|
|
231
|
-
return result;
|
|
232
|
-
};
|
|
233
|
-
db.sequelize.addHook("beforeFind", (options) => {
|
|
234
|
-
if (!options || !options.where) return;
|
|
235
|
-
try {
|
|
236
|
-
options.where = transformDatesInObject(options.where, Sequelize);
|
|
237
|
-
} catch (err) {
|
|
238
|
-
console.error("[MSSQL] Error transforming dates in WHERE:", err);
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
db.sequelize.addHook("beforeCount", (options) => {
|
|
242
|
-
if (!options || !options.where) return;
|
|
243
|
-
try {
|
|
244
|
-
options.where = transformDatesInObject(options.where, Sequelize);
|
|
245
|
-
} catch (err) {
|
|
246
|
-
console.error("[MSSQL] Error transforming dates in WHERE:", err);
|
|
247
212
|
}
|
|
248
213
|
});
|
|
249
214
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"displayName": "Data source: MSSQL",
|
|
4
4
|
"displayName.ru-RU": "Источник данных: MSSQL",
|
|
5
5
|
"displayName.zh-CN": "数据源:MSSQL",
|
|
6
|
-
"version": "2.0.
|
|
6
|
+
"version": "2.0.3",
|
|
7
7
|
"description": "External MSSQL data source support for NocoBase.",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"main": "./dist/server/index.js",
|