w-orm-reladb 1.1.8 → 1.1.9
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 +3 -1
- package/dist/w-orm-reladb.umd.js +2 -2
- package/dist/w-orm-reladb.umd.js.map +1 -1
- package/docs/WOrmReladb.html +14 -14
- package/docs/WOrmReladb.mjs.html +26 -3
- package/docs/cvFind.mjs.html +277 -32
- package/docs/global.html +41 -7
- package/docs/importModels.mjs.html +1 -1
- package/docs/index.html +1 -1
- package/package.json +1 -1
- package/src/WOrmReladb.mjs +25 -2
- package/src/cvFind.mjs +276 -31
- package/test/api-models.test.mjs +23 -0
- package/test/api-suite.mjs +525 -0
- package/test/unit-cvfind.test.mjs +806 -5
package/src/WOrmReladb.mjs
CHANGED
|
@@ -328,6 +328,10 @@ function WOrmReladb(opt = {}) {
|
|
|
328
328
|
* 檢查model之預設scope是否會使select與delAll之對象分歧
|
|
329
329
|
* 註: defaultScope帶limit或offset時, select依其order取前幾筆, 而sequelize之限量刪除不採order(sqlite為rowid IN子查詢),
|
|
330
330
|
* 兩者命中之數據不同而會刪錯列, 故delAll遇此類model即拒絕
|
|
331
|
+
* 註: defaultScope帶group或having時, findAll套用而destroy不套用(having過濾後只回部分列, 卻刪除全部符合where者);
|
|
332
|
+
* 帶非空之include時, findAll以INNER JOIN篩列(required)而destroy不JOIN; 兩者亦拒絕
|
|
333
|
+
* 註: defaultScope.where含Op.and時, sequelize以Object.assign合併scope與cvFind輸出之where, 後者頂層恆為Op.and而覆寫scope之條件(select與delAll皆然),
|
|
334
|
+
* delAll會刪到scope範圍外之列, 故拒絕; 其他鍵(含Op.or)與cvFind輸出之Op.and並存而以AND結合, 不受影響
|
|
331
335
|
*
|
|
332
336
|
* @ignore
|
|
333
337
|
* @param {Object} md 輸入資料表model物件
|
|
@@ -335,8 +339,27 @@ function WOrmReladb(opt = {}) {
|
|
|
335
339
|
*/
|
|
336
340
|
function chkScopeForDelAll(md) {
|
|
337
341
|
let sc = get(md, '_scope', {})
|
|
338
|
-
|
|
339
|
-
|
|
342
|
+
let bads = []
|
|
343
|
+
for (let k of ['limit', 'offset']) {
|
|
344
|
+
if (get(sc, k) !== undefined) {
|
|
345
|
+
bads.push(k)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
for (let k of ['group', 'having']) {
|
|
349
|
+
let v = get(sc, k)
|
|
350
|
+
if (v !== undefined && v !== null && !(Array.isArray(v) && v.length === 0)) {
|
|
351
|
+
bads.push(k)
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (isearr(get(sc, 'include'))) {
|
|
355
|
+
bads.push('include')
|
|
356
|
+
}
|
|
357
|
+
let w = get(sc, 'where')
|
|
358
|
+
if (w !== null && typeof w === 'object' && Object.prototype.hasOwnProperty.call(w, Sequelize.Op.and)) {
|
|
359
|
+
bads.push('where[Op.and]')
|
|
360
|
+
}
|
|
361
|
+
if (bads.length > 0) {
|
|
362
|
+
throw new Error(`model [${opt.cl}] has defaultScope with ${bads.join(', ')}, delAll can not delete the same rows as select`)
|
|
340
363
|
}
|
|
341
364
|
}
|
|
342
365
|
|
package/src/cvFind.mjs
CHANGED
|
@@ -45,8 +45,17 @@ let mSymCmp = new Map([
|
|
|
45
45
|
])
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
//欄位層不接受之Symbol: 邏輯運算子於sequelize
|
|
49
|
-
|
|
48
|
+
//欄位層不接受之Symbol: 邏輯運算子於sequelize欄位層會提前返回而丟棄同層其他條件;
|
|
49
|
+
//Op.values與Op.join不在sequelize之OperatorMap(各方言皆然), 欄位層遇之退回等值並把整個運算子物件交欄型stringify(DATEONLY成查詢當日、DATE成現在、INTEGER成'[object Object]')
|
|
50
|
+
let setSymFieldNo = new Set([Op.and, Op.or, Op.values, Op.join])
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
//sequelize以value[0]、value[1]取值之Symbol運算子, 值須為兩元素陣列(字串被逐字元取值、短陣列補NULL, NOT BETWEEN可命中全部非NULL列)
|
|
54
|
+
let setSymBetween = new Set([Op.between, Op.notBetween])
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
//sequelize以字串樣板(`${value}%`)插值之Symbol運算子: DATE欄之值(轉為Date後)會成為隨主機時區與語系而變之文字, 故不得用於DATE欄
|
|
58
|
+
let setSymInterp = new Set([Op.startsWith, Op.endsWith, Op.substring])
|
|
50
59
|
|
|
51
60
|
|
|
52
61
|
//LIKE之跳脫字元, 選用於各方言字串常值內皆無特殊意義者, 令ESCAPE子句不需依方言跳脫
|
|
@@ -87,14 +96,54 @@ let reIsoTz = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2})(:(\d{2})(\.\d+)?)?(Z|
|
|
|
87
96
|
let reDateOnly = /^(\d{4})-(\d{2})-(\d{2})$/
|
|
88
97
|
|
|
89
98
|
|
|
99
|
+
//無時區之ISO 8601日期時間字串(秒與小數秒可省), 僅呼叫端以ctx.dateNoTz明示'local'時收
|
|
100
|
+
let reIsoNoTz = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?$/
|
|
101
|
+
|
|
102
|
+
|
|
90
103
|
/**
|
|
91
|
-
*
|
|
104
|
+
* 依主機時區解析無時區之ISO 8601日期時間字串為Date, 不合法(格式不符、曆法上不存在、主機時區下不存在)者回傳null
|
|
105
|
+
* 註: 不用Date.parse, 因帶空白分隔等寫法屬實作定義; 以本地時間建構後逐欄回驗, 如2026-02-30、24:00:00、60秒、夏令跳過之時刻皆為null
|
|
106
|
+
* 註: 小數秒取前三位(截斷至毫秒); 夏令重疊之時刻取較早者(Date之行為)
|
|
107
|
+
*
|
|
108
|
+
* @ignore
|
|
109
|
+
*/
|
|
110
|
+
function parseNoTzLocal(s) {
|
|
111
|
+
let m = s.match(reIsoNoTz)
|
|
112
|
+
if (m === null) {
|
|
113
|
+
return null
|
|
114
|
+
}
|
|
115
|
+
let y = Number(m[1])
|
|
116
|
+
let mo = Number(m[2])
|
|
117
|
+
let d = Number(m[3])
|
|
118
|
+
let h = Number(m[4])
|
|
119
|
+
let mi = Number(m[5])
|
|
120
|
+
let sec = m[6] === undefined ? 0 : Number(m[6])
|
|
121
|
+
let ms = m[7] === undefined ? 0 : Number(`${m[7]}00`.slice(0, 3))
|
|
122
|
+
let t = new Date(y, mo - 1, d, h, mi, sec, ms)
|
|
123
|
+
if (t.getFullYear() !== y || t.getMonth() !== mo - 1 || t.getDate() !== d || t.getHours() !== h || t.getMinutes() !== mi || t.getSeconds() !== sec || t.getMilliseconds() !== ms) {
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
if (!inDateRange(t.getTime())) {
|
|
127
|
+
return null //本地時間通過回驗, 但換算UTC後年份超出1–9999(如負偏移時區之9999-12-31晚間)
|
|
128
|
+
}
|
|
129
|
+
return t
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 判定年月日是否為曆法上存在之日期, 如2026-02-30不存在; 年須在1以上(年0000不收)
|
|
135
|
+
* 註: 不用Date.UTC(y,..)建構, 因其把0–99年映射為1900–1999而使年0001–0099之字串被誤拒; 以setUTCFullYear設定完整年份
|
|
92
136
|
*
|
|
93
137
|
* @ignore
|
|
94
138
|
*/
|
|
95
139
|
function isCalendarDate(y, m, d) {
|
|
96
|
-
let
|
|
97
|
-
|
|
140
|
+
let yn = Number(y)
|
|
141
|
+
if (yn < 1) {
|
|
142
|
+
return false
|
|
143
|
+
}
|
|
144
|
+
let t = new Date(0)
|
|
145
|
+
t.setUTCFullYear(yn, Number(m) - 1, Number(d))
|
|
146
|
+
return t.getUTCFullYear() === yn && t.getUTCMonth() === Number(m) - 1 && t.getUTCDate() === Number(d)
|
|
98
147
|
}
|
|
99
148
|
|
|
100
149
|
|
|
@@ -173,6 +222,149 @@ function getColKind(attrs, field) {
|
|
|
173
222
|
}
|
|
174
223
|
|
|
175
224
|
|
|
225
|
+
/**
|
|
226
|
+
* 取model屬性之資料庫欄名: attr.field(字串)優先, 否則為屬性名(sequelize define後恆有field, 手造attrs可能缺)
|
|
227
|
+
*
|
|
228
|
+
* @ignore
|
|
229
|
+
*/
|
|
230
|
+
function colOf(attrs, k) {
|
|
231
|
+
return attrs[k] && typeof attrs[k].field === 'string' ? attrs[k].field : k
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 建立attrs之索引: 資料庫欄名 → 屬性名陣列(依定義順序), 供欄名解析與sequelize反查之核對
|
|
237
|
+
*
|
|
238
|
+
* @ignore
|
|
239
|
+
*/
|
|
240
|
+
function buildAttrIdx(attrs) {
|
|
241
|
+
let byCol = new Map()
|
|
242
|
+
for (let k of Object.keys(attrs)) {
|
|
243
|
+
let c = colOf(attrs, k)
|
|
244
|
+
let a = byCol.get(c)
|
|
245
|
+
if (a === undefined) {
|
|
246
|
+
a = []
|
|
247
|
+
byCol.set(c, a)
|
|
248
|
+
}
|
|
249
|
+
a.push(k)
|
|
250
|
+
}
|
|
251
|
+
return { byCol }
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 核對attrs為sequelize之rawAttributes形狀: 一般物件(原型為Object.prototype或null)、無Symbol鍵、自有字串鍵皆可列舉
|
|
257
|
+
* 註: sequelize以rawAttributes[鍵]讀取(含原型鏈、Symbol鍵、不可列舉者), 而cvFind以Object.keys建索引、以自有屬性核對; 三者所見之屬性集合不同時,
|
|
258
|
+
* 反查核對與sequelize實際取用之定義分歧(如與Op.and相撞之Symbol屬性使sequelize把邏輯鍵映射為欄而條件消失); 真正之rawAttributes恆符合
|
|
259
|
+
*
|
|
260
|
+
* @ignore
|
|
261
|
+
*/
|
|
262
|
+
function chkAttrs(attrs) {
|
|
263
|
+
let p = typeof attrs === 'object' ? Object.getPrototypeOf(attrs) : undefined
|
|
264
|
+
if (!(p === Object.prototype || p === null) || Object.getOwnPropertySymbols(attrs).length > 0 || Object.getOwnPropertyNames(attrs).length !== Object.keys(attrs).length) {
|
|
265
|
+
throw new Error('invalid ctx: attrs must be a plain object (prototype Object.prototype or null) without symbol keys and with enumerable string keys only')
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* 核對欄名可安全交sequelize: 下列名稱會被sequelize於轉成SQL前改寫或重新解讀, 使資料庫實際比對之欄不是此名, 一律拒收
|
|
272
|
+
* 註: 空字串者(sequelize之_joinKeyValue遇空鍵只回值, 條件成常數而delAll刪全表)、含反引號/雙引號/單引號/方括號者(quoteIdentifier依方言刪除之, 如sqlite之`val`ue`成value)、含點者(JSON路徑、表.欄)、
|
|
273
|
+
* 前後皆$者($x$被當成欄引用; sequelize之isColString只比首尾字元, 故含換行者與單一$亦是, 不以正規式判定以免.不匹配換行)、
|
|
274
|
+
* Object.prototype之成員名與__proto__(sequelize以一般物件查表與賦值: 查到函數而不依欄型序列化、賦值__proto__使條件遺失而刪全表)
|
|
275
|
+
*
|
|
276
|
+
* @ignore
|
|
277
|
+
*/
|
|
278
|
+
function chkColName(n, path) {
|
|
279
|
+
if (n === '' || n in Object.prototype || n.indexOf('.') >= 0 || /[`"'[\]]/.test(n) || (n[0] === '$' && n[n.length - 1] === '$')) {
|
|
280
|
+
err(`unsupported column name [${n}]`, path)
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* 屬性是否為VIRTUAL(非資料庫欄)
|
|
287
|
+
*
|
|
288
|
+
* @ignore
|
|
289
|
+
*/
|
|
290
|
+
function isVirtualAttr(at) {
|
|
291
|
+
let t = at && at.type
|
|
292
|
+
return !!t && (t instanceof DT.VIRTUAL || t.key === 'VIRTUAL')
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* 將條件之欄名解析為model之屬性名(嚴格鍵): 僅收屬性名完全相符、或資料庫欄名(attr.field)完全相符且唯一者, 其餘一律拒收, 不模擬資料庫之識別字解析
|
|
298
|
+
* 註: 欄名經三方各自解析: cvFind(判型別、主鍵、DATE值)、sequelize(屬性名映射欄名、以映射後之名稱反查屬性型別、引用層改寫字元)、資料庫(依定序折疊);
|
|
299
|
+
* 完全相符之名稱無論資料庫如何折疊皆解析為自己, 故只收完全相符者(另核對sequelize之反查與改寫, 見chkColName與下文), 使三方之解析必然一致, 見CLAUDE_rulebook.md之R4欄名解析
|
|
300
|
+
* 註: 以model之attrs為準, attrs缺少之欄與VIRTUAL屬性皆拒收; attrs未給者回傳null(不解析, 欄位型別為any, 交資料庫處置)
|
|
301
|
+
*
|
|
302
|
+
* @ignore
|
|
303
|
+
*/
|
|
304
|
+
function resolveAttr(attrs, idx, field, path) {
|
|
305
|
+
if (!attrs) {
|
|
306
|
+
return null
|
|
307
|
+
}
|
|
308
|
+
chkColName(field, path)
|
|
309
|
+
|
|
310
|
+
//①屬性名完全相符; ②資料庫欄名完全相符(多於一個者無從判定)
|
|
311
|
+
let name = null
|
|
312
|
+
if (Object.prototype.hasOwnProperty.call(attrs, field)) {
|
|
313
|
+
name = field
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
let hits = idx.byCol.get(field) || []
|
|
317
|
+
if (hits.length > 1) {
|
|
318
|
+
err(`ambiguous column name [${field}]`, path)
|
|
319
|
+
}
|
|
320
|
+
if (hits.length === 1) {
|
|
321
|
+
name = hits[0]
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
//其餘拒收; 提示僅供診斷(不分大小寫相同者), 不影響解析
|
|
326
|
+
if (name === null || isVirtualAttr(attrs[name])) {
|
|
327
|
+
let lf = field.toLowerCase()
|
|
328
|
+
let near = Object.keys(attrs).find((k) => !isVirtualAttr(attrs[k]) && (k.toLowerCase() === lf || colOf(attrs, k).toLowerCase() === lf))
|
|
329
|
+
err(`unknown column name [${field}]${near === undefined ? '' : `, did you mean [${near}]?`}`, path)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
//field已設者須為字串(sequelize保留原值並以String(field)映射, 退回屬性名則二者指向不同欄); 空字串由chkColName拒收
|
|
333
|
+
let at = attrs[name]
|
|
334
|
+
if (at && at.field !== undefined && typeof at.field !== 'string') {
|
|
335
|
+
err(`unsupported column name [${name}], its field must be a string`, path)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
//sequelize把屬性名之鍵映射為其欄名後, 以rawAttributes[欄名](名稱為該欄名之屬性)、其次fieldRawAttributesMap[欄名](欄名為該欄名之屬性, 多個取最後定義者)取得型別序列化值;
|
|
339
|
+
//反查所得須即本屬性, 否則值依他屬性之型別序列化(如DATE之值被INTEGER序列化為Date.toString文字, sqlite以文字比較而命中全部非NULL列), 拒收
|
|
340
|
+
//共用一欄者另須本屬性即最後定義者: 實體欄之型別由最後定義者決定(sync以欄名為鍵而後者覆寫), 先定義者即使反查為自己, sequelize之序列化亦與實體欄不符
|
|
341
|
+
let col = colOf(attrs, name)
|
|
342
|
+
chkColName(col, path)
|
|
343
|
+
let cs = idx.byCol.get(col)
|
|
344
|
+
let last = cs[cs.length - 1]
|
|
345
|
+
let s = Object.prototype.hasOwnProperty.call(attrs, col) ? col : last
|
|
346
|
+
if (s !== name) {
|
|
347
|
+
err(`ambiguous column name [${field}], its column [${col}] resolves to attribute [${s}]`, path)
|
|
348
|
+
}
|
|
349
|
+
if (last !== name) {
|
|
350
|
+
err(`ambiguous column name [${field}], its column [${col}] is shared with attribute [${last}], the last attribute defines the column type`, path)
|
|
351
|
+
}
|
|
352
|
+
return name
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 判定時間(毫秒)換算UTC後年份是否在1–9999
|
|
358
|
+
* 註: sequelize以YYYY格式化DATE, 年10000起為5位數, sqlite以文字比較致{d:{$gt:年10000}}命中全部日期列; mssql之datetimeoffset範圍亦為0001–9999
|
|
359
|
+
*
|
|
360
|
+
* @ignore
|
|
361
|
+
*/
|
|
362
|
+
function inDateRange(ms) {
|
|
363
|
+
let y = new Date(ms).getUTCFullYear()
|
|
364
|
+
return y >= 1 && y <= 9999
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
|
|
176
368
|
/**
|
|
177
369
|
* 取DECIMAL欄之scale, 未指定者為0(mssql之DECIMAL預設為(18,0)), 非DECIMAL者為null
|
|
178
370
|
*
|
|
@@ -228,18 +420,23 @@ function isKindOk(v, kind, scale = null) {
|
|
|
228
420
|
return true
|
|
229
421
|
}
|
|
230
422
|
if (kind === 'date') {
|
|
423
|
+
//所有形式換算UTC後年份須在1–9999(inDateRange)
|
|
231
424
|
if (v instanceof Date) {
|
|
232
|
-
return !isNaN(v.getTime())
|
|
425
|
+
return !isNaN(v.getTime()) && inDateRange(v.getTime())
|
|
233
426
|
}
|
|
234
427
|
if (typeof v === 'number') {
|
|
235
428
|
//須在Date可表示之範圍(±8.64e15毫秒)內, 超出者被stringify為'Invalid date'而以文字比較, sqlite會命中全部日期列
|
|
236
|
-
return Number.isFinite(v) && !isNaN(new Date(v).getTime())
|
|
429
|
+
return Number.isFinite(v) && !isNaN(new Date(v).getTime()) && inDateRange(v)
|
|
237
430
|
}
|
|
238
431
|
if (typeof v !== 'string') {
|
|
239
432
|
return false
|
|
240
433
|
}
|
|
241
434
|
let m = v.match(reIsoTz)
|
|
242
|
-
|
|
435
|
+
if (m === null || !isCalendarDate(m[1], m[2], m[3])) {
|
|
436
|
+
return false
|
|
437
|
+
}
|
|
438
|
+
let ms = Date.parse(v.replace(' ', 'T'))
|
|
439
|
+
return !isNaN(ms) && inDateRange(ms)
|
|
243
440
|
}
|
|
244
441
|
if (kind === 'dateonly') {
|
|
245
442
|
//Date物件依主機時區取日期而結果隨主機而變, 故只收日期字串
|
|
@@ -345,10 +542,11 @@ function normPk(v, kind, scale = null) {
|
|
|
345
542
|
* 註: 物件、陣列等不得交sequelize之型別stringify, 否則FLOAT成'NaN'、INTEGER成'[object Object]'、DATE成本地今日00:00而誤刪
|
|
346
543
|
* 註: 主鍵欄(pk為true)之字串類、整數類與其他數值類以normPk正規化後回傳(如字串類主鍵欄之123即'123'), 無效或型別不符者拒收,
|
|
347
544
|
* 令select與delAll對主鍵之認定與selectByPk、del等主鍵函數一致; 非主鍵欄不轉型
|
|
545
|
+
* 註: kind為date且呼叫端以ctx.dateNoTz明示'local'(dateNoTz為'local')時, 無時區日期時間字串依主機時區轉為Date回傳(見parseNoTzLocal), 其餘值照舊判定
|
|
348
546
|
*
|
|
349
547
|
* @ignore
|
|
350
548
|
*/
|
|
351
|
-
function chkScalar(v, path, allowNull, kind = 'any', scale = null, pk = false) {
|
|
549
|
+
function chkScalar(v, path, allowNull, kind = 'any', scale = null, pk = false, dateNoTz = undefined) {
|
|
352
550
|
if (v === null) {
|
|
353
551
|
if (allowNull) {
|
|
354
552
|
return v
|
|
@@ -384,9 +582,21 @@ function chkScalar(v, path, allowNull, kind = 'any', scale = null, pk = false) {
|
|
|
384
582
|
}
|
|
385
583
|
err(`value [${String(v)}] does not match column type [${kind}]${scale !== null ? ` with scale [${scale}]` : ''}`, path)
|
|
386
584
|
}
|
|
585
|
+
if (kind === 'date' && dateNoTz === 'local' && typeof v === 'string') {
|
|
586
|
+
//呼叫端明示之無時區日期時間字串: 依主機時區解析並以Date送出, 與直接給Date同義; 不符者續由isKindOk判定(帶時區者照舊)
|
|
587
|
+
let t = parseNoTzLocal(v)
|
|
588
|
+
if (t !== null) {
|
|
589
|
+
return t
|
|
590
|
+
}
|
|
591
|
+
}
|
|
387
592
|
if (!isKindOk(v, kind, scale)) {
|
|
388
593
|
err(`value [${String(v)}] does not match column type [${kind}]${scale !== null ? ` with scale [${scale}]` : ''}`, path)
|
|
389
594
|
}
|
|
595
|
+
if (kind === 'date' && !(v instanceof Date)) {
|
|
596
|
+
//通過檢核之字串與epoch一律轉為Date送出: sequelize只在鍵為屬性名或欄名完全相符時依欄型序列化值, 大小寫變體鍵會原樣送出字串與epoch,
|
|
597
|
+
//sqlite以文字或型別序比較而命中錯列(如{D:{$gt:epoch}}刪光全部日期列); 轉為Date後與鍵之拼寫無關
|
|
598
|
+
return new Date(typeof v === 'string' ? Date.parse(v.replace(' ', 'T')) : v)
|
|
599
|
+
}
|
|
390
600
|
return v
|
|
391
601
|
}
|
|
392
602
|
|
|
@@ -415,14 +625,14 @@ function getItems(arr, path) {
|
|
|
415
625
|
*
|
|
416
626
|
* @ignore
|
|
417
627
|
*/
|
|
418
|
-
function chkRawSym(x, path, kind, scale, pk) {
|
|
628
|
+
function chkRawSym(x, path, kind, scale, pk, dateNoTz) {
|
|
419
629
|
if (x instanceof Sequelize.Utils.SequelizeMethod) {
|
|
420
630
|
return x
|
|
421
631
|
}
|
|
422
632
|
if (Array.isArray(x)) {
|
|
423
|
-
return getItems(x, path).map((y, i) => (y instanceof Sequelize.Utils.SequelizeMethod ? y : chkScalar(y, `${path}.${i}`, true, kind, scale, pk)))
|
|
633
|
+
return getItems(x, path).map((y, i) => (y instanceof Sequelize.Utils.SequelizeMethod ? y : chkScalar(y, `${path}.${i}`, true, kind, scale, pk, dateNoTz)))
|
|
424
634
|
}
|
|
425
|
-
return chkScalar(x, path, true, kind, scale, pk)
|
|
635
|
+
return chkScalar(x, path, true, kind, scale, pk, dateNoTz)
|
|
426
636
|
}
|
|
427
637
|
|
|
428
638
|
|
|
@@ -485,7 +695,7 @@ function normFlags(fs, path) {
|
|
|
485
695
|
*
|
|
486
696
|
* @ignore
|
|
487
697
|
*/
|
|
488
|
-
function cvRegex(field, re, options, path, ctx, kind) {
|
|
698
|
+
function cvRegex(field, col, re, options, path, ctx, kind) {
|
|
489
699
|
if (kind !== 'str' && kind !== 'any') {
|
|
490
700
|
err(`$regex requires a string column, column type is [${kind}]`, path)
|
|
491
701
|
}
|
|
@@ -507,6 +717,10 @@ function cvRegex(field, re, options, path, ctx, kind) {
|
|
|
507
717
|
err('$regex must be a string or RegExp', path)
|
|
508
718
|
}
|
|
509
719
|
let lit = regexToLiteral(src, path)
|
|
720
|
+
if (ci && col[0] === '*') {
|
|
721
|
+
//sequelize之Col遇以*開頭之名稱一律回傳*, LOWER(col)成LOWER(*)
|
|
722
|
+
err(`unsupported column name [${col}], it can not be used with $regex and i`, path)
|
|
723
|
+
}
|
|
510
724
|
if (ci && ctx.dialect === 'sqlite') {
|
|
511
725
|
for (let c of lit) {
|
|
512
726
|
if (c.charCodeAt(0) > 127 && c.toLowerCase() !== c.toUpperCase()) {
|
|
@@ -517,8 +731,7 @@ function cvRegex(field, re, options, path, ctx, kind) {
|
|
|
517
731
|
let pat = `%${lit.replace(/[!%_[]/g, (c) => `${cEsc}${c}`)}%`
|
|
518
732
|
let q = ctx.escape(pat)
|
|
519
733
|
if (ci) {
|
|
520
|
-
//LOWER
|
|
521
|
-
let col = ctx.attrs && ctx.attrs[field] && ctx.attrs[field].field ? ctx.attrs[field].field : field
|
|
734
|
+
//LOWER內須為資料庫之實際欄名col, model之屬性名與欄名可不同(欄名已由resolveAttr核對可安全交sequelize)
|
|
522
735
|
return Sequelize.where(Sequelize.fn('LOWER', Sequelize.col(col)), Op.like, Sequelize.literal(`LOWER(${q}) ESCAPE '${cEsc}'`))
|
|
523
736
|
}
|
|
524
737
|
return { [field]: { [Op.like]: Sequelize.literal(`${q} ESCAPE '${cEsc}'`) } }
|
|
@@ -530,7 +743,7 @@ function cvRegex(field, re, options, path, ctx, kind) {
|
|
|
530
743
|
*
|
|
531
744
|
* @ignore
|
|
532
745
|
*/
|
|
533
|
-
function cvIn(field, arr, path, neg, kind, scale, pk) {
|
|
746
|
+
function cvIn(field, arr, path, neg, kind, scale, pk, dateNoTz) {
|
|
534
747
|
if (!Array.isArray(arr)) {
|
|
535
748
|
err(`${neg ? '$nin' : '$in'} must be an array`, path)
|
|
536
749
|
}
|
|
@@ -541,17 +754,17 @@ function cvIn(field, arr, path, neg, kind, scale, pk) {
|
|
|
541
754
|
hasNull = true
|
|
542
755
|
return
|
|
543
756
|
}
|
|
544
|
-
vs.push(chkScalar(v, `${path}.${k}`, false, kind, scale, pk))
|
|
757
|
+
vs.push(chkScalar(v, `${path}.${k}`, false, kind, scale, pk, dateNoTz))
|
|
545
758
|
})
|
|
546
759
|
if (!neg) {
|
|
547
760
|
if (vs.length === 0) {
|
|
548
|
-
return hasNull ? { [field]: null } : sqlFalse()
|
|
761
|
+
return hasNull ? { [field]: null } : { [Op.and]: [{ [field]: null }, sqlFalse()] }
|
|
549
762
|
}
|
|
550
763
|
let c = { [field]: { [Op.in]: vs } }
|
|
551
764
|
return hasNull ? { [Op.or]: [c, { [field]: null }] } : c
|
|
552
765
|
}
|
|
553
766
|
if (vs.length === 0) {
|
|
554
|
-
return hasNull ? { [field]: { [Op.ne]: null } } : sqlTrue()
|
|
767
|
+
return hasNull ? { [field]: { [Op.ne]: null } } : { [Op.or]: [{ [field]: null }, sqlTrue()] }
|
|
555
768
|
}
|
|
556
769
|
let c = { [field]: { [Op.notIn]: vs } }
|
|
557
770
|
return hasNull ? { [Op.and]: [c, { [field]: { [Op.ne]: null } }] } : c
|
|
@@ -564,9 +777,6 @@ function cvIn(field, arr, path, neg, kind, scale, pk) {
|
|
|
564
777
|
* @ignore
|
|
565
778
|
*/
|
|
566
779
|
function cvField(field, v, path, depth, ctx) {
|
|
567
|
-
let kind = getColKind(ctx.attrs, field)
|
|
568
|
-
let scale = getColScale(ctx.attrs, field)
|
|
569
|
-
let pk = ctx.pk !== undefined && field === ctx.pk //主鍵欄之純量依normPk正規化(R8)
|
|
570
780
|
if (depth > nDepthMax) {
|
|
571
781
|
err('find is too deep', path)
|
|
572
782
|
}
|
|
@@ -576,15 +786,23 @@ function cvField(field, v, path, depth, ctx) {
|
|
|
576
786
|
if (v === undefined) {
|
|
577
787
|
err('value is undefined', path)
|
|
578
788
|
}
|
|
789
|
+
|
|
790
|
+
//欄名先解析為model之屬性名再判型別與主鍵(R4欄名解析); attrs已給而解析不到者拒收, attrs未給者為any, 交資料庫處置
|
|
791
|
+
let an = resolveAttr(ctx.attrs, ctx.attrIdx, field, path)
|
|
792
|
+
let col = an === null ? field : colOf(ctx.attrs, an) //資料庫實際欄名
|
|
793
|
+
let kind = an === null ? 'any' : getColKind(ctx.attrs, an)
|
|
794
|
+
let scale = an === null ? null : getColScale(ctx.attrs, an)
|
|
795
|
+
let pk = ctx.pk !== undefined && an === ctx.pk //主鍵欄之純量依normPk正規化(R8)
|
|
796
|
+
let dateNoTz = ctx.dateNoTz
|
|
579
797
|
if (Array.isArray(v)) {
|
|
580
798
|
//陣列值即$in(含null與空陣列之處置), 與MongoDB之陣列比對不同, 已文件化
|
|
581
|
-
return [cvIn(field, v, path, false, kind, scale, pk)]
|
|
799
|
+
return [cvIn(field, v, path, false, kind, scale, pk, dateNoTz)]
|
|
582
800
|
}
|
|
583
801
|
if (v instanceof RegExp) {
|
|
584
|
-
return [cvRegex(field, v, undefined, path, ctx, kind)]
|
|
802
|
+
return [cvRegex(field, col, v, undefined, path, ctx, kind)]
|
|
585
803
|
}
|
|
586
804
|
if (!isPlainObj(v)) {
|
|
587
|
-
return [{ [field]: chkScalar(v, path, true, kind, scale, pk) }]
|
|
805
|
+
return [{ [field]: chkScalar(v, path, true, kind, scale, pk, dateNoTz) }]
|
|
588
806
|
}
|
|
589
807
|
let ks = Reflect.ownKeys(v)
|
|
590
808
|
if (ks.length === 0) {
|
|
@@ -604,8 +822,19 @@ function cvField(field, v, path, depth, ctx) {
|
|
|
604
822
|
}
|
|
605
823
|
name = mSymCmp.get(k)
|
|
606
824
|
if (name === undefined) {
|
|
825
|
+
if (kind === 'date' && setSymInterp.has(k)) {
|
|
826
|
+
err(`operator [${String(k)}] is not supported on a date column`, kp)
|
|
827
|
+
}
|
|
828
|
+
if (setSymBetween.has(k)) {
|
|
829
|
+
//值須為兩元素陣列, 元素為純量(不含null)或sequelize之方法物件
|
|
830
|
+
if (!Array.isArray(x) || x.length !== 2) {
|
|
831
|
+
err(`${String(k)} requires an array of two values`, kp)
|
|
832
|
+
}
|
|
833
|
+
cs.push({ [field]: { [k]: getItems(x, kp).map((y, i) => (y instanceof Sequelize.Utils.SequelizeMethod ? y : chkScalar(y, `${kp}.${i}`, false, kind, scale, pk, dateNoTz))) } })
|
|
834
|
+
continue
|
|
835
|
+
}
|
|
607
836
|
//其餘sequelize之Symbol運算子為呼叫端自負之原生寫法, 值經檢核後各自成一子式
|
|
608
|
-
cs.push({ [field]: { [k]: chkRawSym(x, kp, kind, scale, pk) } })
|
|
837
|
+
cs.push({ [field]: { [k]: chkRawSym(x, kp, kind, scale, pk, dateNoTz) } })
|
|
609
838
|
continue
|
|
610
839
|
}
|
|
611
840
|
}
|
|
@@ -614,7 +843,7 @@ function cvField(field, v, path, depth, ctx) {
|
|
|
614
843
|
}
|
|
615
844
|
if (mCmp[name] !== undefined) {
|
|
616
845
|
let allowNull = name === '$eq' || name === '$ne'
|
|
617
|
-
let y = chkScalar(x, kp, allowNull, kind, scale, pk)
|
|
846
|
+
let y = chkScalar(x, kp, allowNull, kind, scale, pk, dateNoTz)
|
|
618
847
|
if (Object.prototype.hasOwnProperty.call(simple, mCmp[name])) {
|
|
619
848
|
//同義之鍵(如$gt與Op.gt)各自成一子式, 不得互相覆寫
|
|
620
849
|
cs.push({ [field]: { [mCmp[name]]: y } })
|
|
@@ -625,7 +854,7 @@ function cvField(field, v, path, depth, ctx) {
|
|
|
625
854
|
}
|
|
626
855
|
}
|
|
627
856
|
else if (name === '$in' || name === '$nin') {
|
|
628
|
-
cs.push(cvIn(field, x, kp, name === '$nin', kind, scale, pk))
|
|
857
|
+
cs.push(cvIn(field, x, kp, name === '$nin', kind, scale, pk, dateNoTz))
|
|
629
858
|
}
|
|
630
859
|
else if (name === '$exists') {
|
|
631
860
|
if (x !== true && x !== false && x !== 0 && x !== 1) {
|
|
@@ -635,7 +864,7 @@ function cvField(field, v, path, depth, ctx) {
|
|
|
635
864
|
}
|
|
636
865
|
else if (name === '$regex') {
|
|
637
866
|
hasRegex = true
|
|
638
|
-
cs.push(cvRegex(field, x, v.$options, kp, ctx, kind))
|
|
867
|
+
cs.push(cvRegex(field, col, x, v.$options, kp, ctx, kind))
|
|
639
868
|
}
|
|
640
869
|
else if (name === '$options') {
|
|
641
870
|
//由$regex一併處理
|
|
@@ -654,6 +883,10 @@ function cvField(field, v, path, depth, ctx) {
|
|
|
654
883
|
if (v.$options !== undefined && !hasRegex) {
|
|
655
884
|
err('$options requires $regex', path)
|
|
656
885
|
}
|
|
886
|
+
if (cs.length === 0 && nSimple === 0) {
|
|
887
|
+
//如僅有值為undefined之$options: 不產生任何子式, 該條件即消失而成恆真, delAll會刪全表
|
|
888
|
+
err('operator object has no operator', path)
|
|
889
|
+
}
|
|
657
890
|
if (nSimple > 0) {
|
|
658
891
|
cs.unshift({ [field]: simple })
|
|
659
892
|
}
|
|
@@ -759,13 +992,19 @@ function cvOr(cs) {
|
|
|
759
992
|
* @param {*} find 輸入查詢條件
|
|
760
993
|
* @param {Object} ctx 輸入轉換所需之依賴物件
|
|
761
994
|
* @param {Function} ctx.escape 輸入依方言將字串轉為SQL字串常值之函數, 通常為sequelize實例之escape
|
|
762
|
-
* @param {Object} [ctx.attrs]
|
|
995
|
+
* @param {Object} [ctx.attrs] 輸入執行where之model之rawAttributes, 供欄名解析(嚴格鍵: 鍵須與屬性名或資料庫欄名完全相符, 其餘拒收)、依欄位型別檢核純量與取得實際欄名; 未給時不解析鍵、不檢型別而交資料庫(見CLAUDE_rulebook.md之R4)
|
|
763
996
|
* @param {String} [ctx.dialect] 輸入資料庫種類字串, 供判定方言能力(如sqlite之LOWER僅折疊ASCII)
|
|
764
997
|
* @param {String} [ctx.pk] 輸入主鍵欄位名, 該欄之純量條件值依normPk正規化(如字串類主鍵欄之123即'123'), 未給時不正規化
|
|
998
|
+
* @param {String} [ctx.dateNoTz] 輸入無時區日期時間字串之處置, 僅接受undefined(預設, 無時區者拒收)或'local'(依主機時區解析, 與以本地時間分量建構之Date同義), 其餘值拋錯; 僅作用於DATE欄, 供Jet等無時區後端之呼叫端明示啟用
|
|
765
999
|
* @returns {Object} 回傳sequelize之where物件, 條件不合法時拋出錯誤
|
|
766
1000
|
*/
|
|
767
1001
|
function cvFind(find, ctx) {
|
|
768
1002
|
|
|
1003
|
+
//check, ctx.dateNoTz須為undefined或'local', 其餘值(含拼錯、大小寫不同)拋錯, 不靜默忽略
|
|
1004
|
+
if (ctx && ctx.dateNoTz !== undefined && ctx.dateNoTz !== 'local') {
|
|
1005
|
+
throw new Error(`invalid ctx: dateNoTz must be 'local' or undefined, got [${String(ctx.dateNoTz)}]`)
|
|
1006
|
+
}
|
|
1007
|
+
|
|
769
1008
|
//check, 非物件(undefined、null、字串、數值等純量)與陣列視為未給條件, 此為現況保留, 系列規格另案處理
|
|
770
1009
|
if (find === null || typeof find !== 'object' || Array.isArray(find)) {
|
|
771
1010
|
return {}
|
|
@@ -781,7 +1020,13 @@ function cvFind(find, ctx) {
|
|
|
781
1020
|
return {}
|
|
782
1021
|
}
|
|
783
1022
|
|
|
784
|
-
|
|
1023
|
+
//check, attrs須為rawAttributes形狀(見chkAttrs)
|
|
1024
|
+
if (ctx && ctx.attrs) {
|
|
1025
|
+
chkAttrs(ctx.attrs)
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
//attrs之索引每次轉譯只建一次(內部欄位attrIdx, 不屬ctx之介面)
|
|
1029
|
+
return cvDoc(find, '', 0, ctx && ctx.attrs ? { ...ctx, attrIdx: buildAttrIdx(ctx.attrs) } : ctx)
|
|
785
1030
|
}
|
|
786
1031
|
|
|
787
1032
|
|
package/test/api-models.test.mjs
CHANGED
|
@@ -128,6 +128,29 @@ describe('models file', function() {
|
|
|
128
128
|
assert.deepStrictEqual((await wAll.select()).map((v) => v.id).sort(), ['a', 'b'])
|
|
129
129
|
})
|
|
130
130
|
|
|
131
|
+
it('model之defaultScope帶group/having、include或含Op.and之where時delAll拒絕且不刪除(select與destroy對這些選項之處置不同, 或scope之Op.and被cvFind頂層之Op.and覆寫)', async function() {
|
|
132
|
+
//rulebook R1: findAll套用scope之group/having與required include(只回部分列), destroy卻不套用而刪除全部符合where者(run05 sol F03、opus 疑慮(ii), sqlite實測 select=[2] 而 destroy=3);
|
|
133
|
+
//scope.where含Op.and者, sequelize以Object.assign合併而被cvFind輸出之頂層Op.and覆寫, scope條件消失(opus 疑慮(i))
|
|
134
|
+
let cases = [
|
|
135
|
+
['group+having', 'defaultScope: { group: [\'id\'], having: sequelize.literal(\'value > 0\') }'],
|
|
136
|
+
['required include', 'defaultScope: { include: [{ model: sequelize.define(\'uchild\', { id: { type: DataTypes.STRING, primaryKey: true } }, { tableName: \'uchild\' }), required: true }] }'],
|
|
137
|
+
['where含Op.and', 'defaultScope: { where: { [require(\'sequelize\').Op.and]: [{ name: \'x\' }] } }'],
|
|
138
|
+
]
|
|
139
|
+
for (let [i, [t, sc]] of cases.entries()) {
|
|
140
|
+
let fdm = path.join(fd, `m6${i}`)
|
|
141
|
+
fs.mkdirSync(fdm, { recursive: true })
|
|
142
|
+
fs.writeFileSync(path.join(fdm, 'users.js'), genModel(true).replace('tableName: \'users\'', `tableName: 'users', ${sc}`), 'utf8')
|
|
143
|
+
let st = path.join(fd, `m6${i}.sqlite`)
|
|
144
|
+
let w = wo({ url: 'sqlite://:', storage: st, db: 'worm', cl: 'users', fdModels: fdm })
|
|
145
|
+
await w.createStorage()
|
|
146
|
+
await w.insert([{ id: 'a', name: 'x', value: 1 }, { id: 'b', name: 'y', value: 0 }])
|
|
147
|
+
let r = await w.delAll({ value: { $gte: 0 } }).then(() => null).catch((err) => String(err.message || err))
|
|
148
|
+
assert.ok(r !== null && r.indexOf('defaultScope') >= 0, `${t}: 未reject: ${r}`)
|
|
149
|
+
let wAll = wo({ url: 'sqlite://:', storage: st, db: 'worm', cl: 'users', fdModels: path.join(fd, 'm1') })
|
|
150
|
+
assert.deepStrictEqual((await wAll.select()).map((v) => v.id).sort(), ['a', 'b'], `${t}: 資料被刪`)
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
|
|
131
154
|
it('BIGINT主鍵: 超出安全整數之數值為型別不符, 精確值以標準整數寫法之字串給予', async function() {
|
|
132
155
|
//rulebook R8: 整數類主鍵之數值須為安全整數, 否則已失精度而會命中相鄰主鍵; 超出安全整數之標準整數寫法字串保留為字串(表外細化)
|
|
133
156
|
let fdm = path.join(fd, 'm5')
|