w-orm-reladb 1.1.8 → 1.1.10

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.
@@ -46,16 +46,19 @@
46
46
  <section>
47
47
  <article>
48
48
  <pre class="prettyprint source linenums"><code>import Sequelize from 'sequelize'
49
+ import cvOrmFind from 'wsemi/src/cvOrmFind.mjs'
49
50
 
50
51
 
51
52
  //查詢條件(find)之轉譯, 供select與delAll共用, 令同一find於兩函數由同一SQL謂詞決定命中
52
53
  //
53
- //原select內之cvObj以lodash之each迭代並以Op[k]直譯$鍵, 有下列缺陷而不得供破壞性之delAll使用:
54
- // 1. 不支援之運算子成為鍵'undefined', 運算子物件被交給sequelize之欄位型別stringify:
55
- // FLOAT成'NaN'、INTEGER成'[object Object]'、DATE成本地今日00:00, 致靜默無命中或刪錯列
56
- // 2. 具數值length之物件被當成陣列樣迭代而丟棄全部條件(回全表)、Symbol鍵被丟棄
57
- // 3. $regex轉LIKE而未跳脫萬用字元, 欄位層$not遇運算子物件被轉為!=
58
- //本模組改以白名單轉譯, 凡不能保證語意者一律拋錯, 規則見CLAUDE_rulebook.md之R1–R6;
54
+ //分兩階段(規則見CLAUDE_rulebook.md之R11):
55
+ // 1. 形狀檢核: 第一步經wsemi之cvOrmFind依系列白名單檢核並回傳全新之null原型深複本(檢查之值即使用之值), 之後只走訪複本;
56
+ // 白名單外之運算子、非純量之值、非條件物件之find、異常結構(存取器、Symbol鍵、洞…)一律整批reject, 不靜默忽略、不改變語意、不擴大刪除範圍
57
+ // 2. 翻譯: 本模組負責後端專屬之檢核與轉譯——欄名嚴格鍵、依欄位型別檢核純量、主鍵正規化(R8)、DATE範圍與ctx.dateNoTz、
58
+ // 空集合之欄位引用($in:[]), 見R3–R5、R8
59
+ //歷史: 原select內之cvObj以lodash之each迭代並以Op[k]直譯$鍵, 不支援之運算子成為鍵'undefined'、具數值length之物件被當成陣列樣迭代而丟棄全部條件,
60
+ //不得供破壞性之delAll使用, 1.1.7起改自寫白名單; 第33項批1起形狀層改由cvOrmFind單一擁有, 本模組刪除Symbol/Op全集、$not $nor $regex $options $exists、
61
+ //RegExp值、陣列值即$in、非物件視為{}等形狀層(破壞性變更見CLAUDE_rulebook.md與README)
59
62
  //主鍵值之正規化(normPk)亦屬本模組, 供主鍵函數與主鍵欄條件共用, 規則見R8
60
63
 
61
64
 
@@ -63,8 +66,17 @@
63
66
  let Op = Sequelize.Op
64
67
 
65
68
 
66
- //sequelize之運算子Symbol全集, 非其成員之Symbol會被sequelize靜默丟棄, 故一律拒收
67
- let setOp = new Set(Object.values(Op))
69
+ //形狀檢核之上限(傳給cvOrmFind): $and/$or長度999——SQLite平鋪$or/$and恰1000項即Expression tree is too large(DB錯誤), 本套件使用SQLite故下調(預設1000);
70
+ //其餘上限用預設: 邏輯巢狀深度32、$in/$nin長度10000、節點總數100000、欄名128
71
+ let optShape = { maxList: 999 }
72
+
73
+
74
+ //$and/$or清單(含同一條件物件內並列欄位之隱含AND)項數超過此值者, 遞迴二分為平衡樹再交sequelize:
75
+ //SQLite之運算式樹深度上限1000(Expression tree is too large), 平鋪之a OR b OR …為左深樹(深度隨項數線性成長), select因欄位帶表別名(`t`.`c`)而較delAll少容1項
76
+ //(實測平鋪$or: select最多998項、delAll最多999項, 999項時select報DB錯誤而delAll照常刪除, 違反select命中即delAll刪除之對象一致);
77
+ //巢狀層數亦會累加(32層 × 每層33項平鋪即超限, 外審sol F1), 另有巢狀遞迴上限(實測右深巢狀OR約830層, SQLITE_ERROR: Recursion limit);
78
+ //二分後每個清單之樹深僅約log2(項數)+3, 邏輯巢狀上限32層下全樹深度恆遠低於兩種上限, select與delAll皆可執行
79
+ let nFlat = 4
68
80
 
69
81
 
70
82
  //比較運算子, 值須為純量(null僅$eq與$ne)
@@ -78,32 +90,6 @@ let mCmp = {
78
90
  }
79
91
 
80
92
 
81
- //sequelize之Symbol運算子對應之$名稱, 令呼叫端以Symbol給條件時亦經同一檢核與轉譯
82
- let mSymCmp = new Map([
83
- [Op.eq, '$eq'],
84
- [Op.ne, '$ne'],
85
- [Op.gt, '$gt'],
86
- [Op.gte, '$gte'],
87
- [Op.lt, '$lt'],
88
- [Op.lte, '$lte'],
89
- [Op.in, '$in'],
90
- [Op.notIn, '$nin'],
91
- [Op.not, '$not'],
92
- ])
93
-
94
-
95
- //欄位層不接受之Symbol: 邏輯運算子於sequelize欄位層會提前返回而丟棄同層其他條件
96
- let setSymFieldNo = new Set([Op.and, Op.or])
97
-
98
-
99
- //LIKE之跳脫字元, 選用於各方言字串常值內皆無特殊意義者, 令ESCAPE子句不需依方言跳脫
100
- let cEsc = '!'
101
-
102
-
103
- //巢狀深度上限, 防循環參照與過深之條件
104
- let nDepthMax = 32
105
-
106
-
107
93
  //欄位型別分類, 以sequelize之型別類別判定(各方言之型別皆繼承之), 另以type.key補判
108
94
  //註: DataTypes.DOUBLE之key為'DOUBLE PRECISION', 只比對key會漏判而不檢型別
109
95
  let DT = Sequelize.DataTypes
@@ -134,52 +120,73 @@ let reIsoTz = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2})(:(\d{2})(\.\d+)?)?(Z|
134
120
  let reDateOnly = /^(\d{4})-(\d{2})-(\d{2})$/
135
121
 
136
122
 
123
+ //無時區之ISO 8601日期時間字串(秒與小數秒可省), 僅呼叫端以ctx.dateNoTz明示'local'時收
124
+ let reIsoNoTz = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?$/
125
+
126
+
137
127
  /**
138
- * 判定年月日是否為曆法上存在之日期, 如2026-02-30不存在
128
+ * 依主機時區解析無時區之ISO 8601日期時間字串為Date, 不合法(格式不符、曆法上不存在、主機時區下不存在)者回傳null
129
+ * 註: 不用Date.parse, 因帶空白分隔等寫法屬實作定義; 以本地時間建構後逐欄回驗, 如2026-02-30、24:00:00、60秒、夏令跳過之時刻皆為null
130
+ * 註: 小數秒取前三位(截斷至毫秒); 夏令重疊之時刻取較早者(Date之行為)
139
131
  *
140
132
  * @ignore
141
133
  */
142
- function isCalendarDate(y, m, d) {
143
- let t = new Date(Date.UTC(Number(y), Number(m) - 1, Number(d)))
144
- return t.getUTCFullYear() === Number(y) &amp;&amp; t.getUTCMonth() === Number(m) - 1 &amp;&amp; t.getUTCDate() === Number(d)
134
+ function parseNoTzLocal(s) {
135
+ let m = s.match(reIsoNoTz)
136
+ if (m === null) {
137
+ return null
138
+ }
139
+ let y = Number(m[1])
140
+ let mo = Number(m[2])
141
+ let d = Number(m[3])
142
+ let h = Number(m[4])
143
+ let mi = Number(m[5])
144
+ let sec = m[6] === undefined ? 0 : Number(m[6])
145
+ let ms = m[7] === undefined ? 0 : Number(`${m[7]}00`.slice(0, 3))
146
+ let t = new Date(y, mo - 1, d, h, mi, sec, ms)
147
+ if (t.getFullYear() !== y || t.getMonth() !== mo - 1 || t.getDate() !== d || t.getHours() !== h || t.getMinutes() !== mi || t.getSeconds() !== sec || t.getMilliseconds() !== ms) {
148
+ return null
149
+ }
150
+ if (!inDateRange(t.getTime())) {
151
+ return null //本地時間通過回驗, 但換算UTC後年份超出1–9999(如負偏移時區之9999-12-31晚間)
152
+ }
153
+ return t
145
154
  }
146
155
 
147
156
 
148
157
  /**
149
- * 拋出條件錯誤
158
+ * 判定年月日是否為曆法上存在之日期, 如2026-02-30不存在; 年須在1以上(年0000不收)
159
+ * 註: 不用Date.UTC(y,..)建構, 因其把0–99年映射為1900–1999而使年0001–0099之字串被誤拒; 以setUTCFullYear設定完整年份
150
160
  *
151
161
  * @ignore
152
162
  */
153
- function err(msg, path) {
154
- throw new Error(`invalid find: ${msg}${path ? ` at [${path}]` : ''}`)
163
+ function isCalendarDate(y, m, d) {
164
+ let yn = Number(y)
165
+ if (yn &lt; 1) {
166
+ return false
167
+ }
168
+ let t = new Date(0)
169
+ t.setUTCFullYear(yn, Number(m) - 1, Number(d))
170
+ return t.getUTCFullYear() === yn &amp;&amp; t.getUTCMonth() === Number(m) - 1 &amp;&amp; t.getUTCDate() === Number(d)
155
171
  }
156
172
 
157
173
 
158
174
  /**
159
- * 是否為一般物件: 原型為Object.prototype或null者
160
- * 註: 不以toString判定, 因帶Symbol.toStringTag之一般物件會被誤判為非物件, 而class實例、Object.create(proto)會被誤判為一般物件,
161
- * 其條件可能放在原型上而取不到自有鍵, 被當成{}而刪全表
175
+ * 拋出條件錯誤
162
176
  *
163
177
  * @ignore
164
178
  */
165
- function isPlainObj(v) {
166
- if (v === null || typeof v !== 'object') {
167
- return false
168
- }
169
- let p = Object.getPrototypeOf(v)
170
- return p === Object.prototype || p === null
179
+ function err(msg, path) {
180
+ throw new Error(`invalid find: ${msg}${path ? ` at [${path}]` : ''}`)
171
181
  }
172
182
 
173
183
 
174
184
  /**
175
- * 恆真與恆假之謂詞, 以明確之SQL表示, 不以空條件表示
176
- * 註: sequelize會把空條件(如{}、空$and)於OR、NOT內濾除, 致恆真被當成不存在而改變語意(如$nor:[{}]成全部)
185
+ * 恆假之謂詞, 以明確之SQL表示, 不以空條件表示
186
+ * 註: sequelize會把空條件於OR、NOT內濾除而改變語意; 本模組僅用於$in:[]之f IS NULL AND 1 = 0(仍引用欄位, 見R5)
177
187
  *
178
188
  * @ignore
179
189
  */
180
- function sqlTrue() {
181
- return Sequelize.literal('1 = 1')
182
- }
183
190
  function sqlFalse() {
184
191
  return Sequelize.literal('1 = 0')
185
192
  }
@@ -220,6 +227,149 @@ function getColKind(attrs, field) {
220
227
  }
221
228
 
222
229
 
230
+ /**
231
+ * 取model屬性之資料庫欄名: attr.field(字串)優先, 否則為屬性名(sequelize define後恆有field, 手造attrs可能缺)
232
+ *
233
+ * @ignore
234
+ */
235
+ function colOf(attrs, k) {
236
+ return attrs[k] &amp;&amp; typeof attrs[k].field === 'string' ? attrs[k].field : k
237
+ }
238
+
239
+
240
+ /**
241
+ * 建立attrs之索引: 資料庫欄名 → 屬性名陣列(依定義順序), 供欄名解析與sequelize反查之核對
242
+ *
243
+ * @ignore
244
+ */
245
+ function buildAttrIdx(attrs) {
246
+ let byCol = new Map()
247
+ for (let k of Object.keys(attrs)) {
248
+ let c = colOf(attrs, k)
249
+ let a = byCol.get(c)
250
+ if (a === undefined) {
251
+ a = []
252
+ byCol.set(c, a)
253
+ }
254
+ a.push(k)
255
+ }
256
+ return { byCol }
257
+ }
258
+
259
+
260
+ /**
261
+ * 核對attrs為sequelize之rawAttributes形狀: 一般物件(原型為Object.prototype或null)、無Symbol鍵、自有字串鍵皆可列舉
262
+ * 註: sequelize以rawAttributes[鍵]讀取(含原型鏈、Symbol鍵、不可列舉者), 而cvFind以Object.keys建索引、以自有屬性核對; 三者所見之屬性集合不同時,
263
+ * 反查核對與sequelize實際取用之定義分歧(如與Op.and相撞之Symbol屬性使sequelize把邏輯鍵映射為欄而條件消失); 真正之rawAttributes恆符合
264
+ *
265
+ * @ignore
266
+ */
267
+ function chkAttrs(attrs) {
268
+ let p = typeof attrs === 'object' ? Object.getPrototypeOf(attrs) : undefined
269
+ if (!(p === Object.prototype || p === null) || Object.getOwnPropertySymbols(attrs).length > 0 || Object.getOwnPropertyNames(attrs).length !== Object.keys(attrs).length) {
270
+ 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')
271
+ }
272
+ }
273
+
274
+
275
+ /**
276
+ * 核對欄名可安全交sequelize: 下列名稱會被sequelize於轉成SQL前改寫或重新解讀, 使資料庫實際比對之欄不是此名, 一律拒收
277
+ * 註: 空字串者(sequelize之_joinKeyValue遇空鍵只回值, 條件成常數而delAll刪全表)、含反引號/雙引號/單引號/方括號者(quoteIdentifier依方言刪除之, 如sqlite之`val`ue`成value)、含點者(JSON路徑、表.欄)、
278
+ * 前後皆$者($x$被當成欄引用; sequelize之isColString只比首尾字元, 故含換行者與單一$亦是, 不以正規式判定以免.不匹配換行)、
279
+ * Object.prototype之成員名與__proto__(sequelize以一般物件查表與賦值: 查到函數而不依欄型序列化、賦值__proto__使條件遺失而刪全表)
280
+ *
281
+ * @ignore
282
+ */
283
+ function chkColName(n, path) {
284
+ if (n === '' || n in Object.prototype || n.indexOf('.') >= 0 || /[`"'[\]]/.test(n) || (n[0] === '$' &amp;&amp; n[n.length - 1] === '$')) {
285
+ err(`unsupported column name [${n}]`, path)
286
+ }
287
+ }
288
+
289
+
290
+ /**
291
+ * 屬性是否為VIRTUAL(非資料庫欄)
292
+ *
293
+ * @ignore
294
+ */
295
+ function isVirtualAttr(at) {
296
+ let t = at &amp;&amp; at.type
297
+ return !!t &amp;&amp; (t instanceof DT.VIRTUAL || t.key === 'VIRTUAL')
298
+ }
299
+
300
+
301
+ /**
302
+ * 將條件之欄名解析為model之屬性名(嚴格鍵): 僅收屬性名完全相符、或資料庫欄名(attr.field)完全相符且唯一者, 其餘一律拒收, 不模擬資料庫之識別字解析
303
+ * 註: 欄名經三方各自解析: cvFind(判型別、主鍵、DATE值)、sequelize(屬性名映射欄名、以映射後之名稱反查屬性型別、引用層改寫字元)、資料庫(依定序折疊);
304
+ * 完全相符之名稱無論資料庫如何折疊皆解析為自己, 故只收完全相符者(另核對sequelize之反查與改寫, 見chkColName與下文), 使三方之解析必然一致, 見CLAUDE_rulebook.md之R4欄名解析
305
+ * 註: 以model之attrs為準, attrs缺少之欄與VIRTUAL屬性皆拒收; attrs未給者回傳null(不解析, 欄位型別為any, 交資料庫處置)
306
+ *
307
+ * @ignore
308
+ */
309
+ function resolveAttr(attrs, idx, field, path) {
310
+ if (!attrs) {
311
+ return null
312
+ }
313
+ chkColName(field, path)
314
+
315
+ //①屬性名完全相符; ②資料庫欄名完全相符(多於一個者無從判定)
316
+ let name = null
317
+ if (Object.prototype.hasOwnProperty.call(attrs, field)) {
318
+ name = field
319
+ }
320
+ else {
321
+ let hits = idx.byCol.get(field) || []
322
+ if (hits.length > 1) {
323
+ err(`ambiguous column name [${field}]`, path)
324
+ }
325
+ if (hits.length === 1) {
326
+ name = hits[0]
327
+ }
328
+ }
329
+
330
+ //其餘拒收; 提示僅供診斷(不分大小寫相同者), 不影響解析
331
+ if (name === null || isVirtualAttr(attrs[name])) {
332
+ let lf = field.toLowerCase()
333
+ let near = Object.keys(attrs).find((k) => !isVirtualAttr(attrs[k]) &amp;&amp; (k.toLowerCase() === lf || colOf(attrs, k).toLowerCase() === lf))
334
+ err(`unknown column name [${field}]${near === undefined ? '' : `, did you mean [${near}]?`}`, path)
335
+ }
336
+
337
+ //field已設者須為字串(sequelize保留原值並以String(field)映射, 退回屬性名則二者指向不同欄); 空字串由chkColName拒收
338
+ let at = attrs[name]
339
+ if (at &amp;&amp; at.field !== undefined &amp;&amp; typeof at.field !== 'string') {
340
+ err(`unsupported column name [${name}], its field must be a string`, path)
341
+ }
342
+
343
+ //sequelize把屬性名之鍵映射為其欄名後, 以rawAttributes[欄名](名稱為該欄名之屬性)、其次fieldRawAttributesMap[欄名](欄名為該欄名之屬性, 多個取最後定義者)取得型別序列化值;
344
+ //反查所得須即本屬性, 否則值依他屬性之型別序列化(如DATE之值被INTEGER序列化為Date.toString文字, sqlite以文字比較而命中全部非NULL列), 拒收
345
+ //共用一欄者另須本屬性即最後定義者: 實體欄之型別由最後定義者決定(sync以欄名為鍵而後者覆寫), 先定義者即使反查為自己, sequelize之序列化亦與實體欄不符
346
+ let col = colOf(attrs, name)
347
+ chkColName(col, path)
348
+ let cs = idx.byCol.get(col)
349
+ let last = cs[cs.length - 1]
350
+ let s = Object.prototype.hasOwnProperty.call(attrs, col) ? col : last
351
+ if (s !== name) {
352
+ err(`ambiguous column name [${field}], its column [${col}] resolves to attribute [${s}]`, path)
353
+ }
354
+ if (last !== name) {
355
+ err(`ambiguous column name [${field}], its column [${col}] is shared with attribute [${last}], the last attribute defines the column type`, path)
356
+ }
357
+ return name
358
+ }
359
+
360
+
361
+ /**
362
+ * 判定時間(毫秒)換算UTC後年份是否在1–9999
363
+ * 註: sequelize以YYYY格式化DATE, 年10000起為5位數, sqlite以文字比較致{d:{$gt:年10000}}命中全部日期列; mssql之datetimeoffset範圍亦為0001–9999
364
+ *
365
+ * @ignore
366
+ */
367
+ function inDateRange(ms) {
368
+ let y = new Date(ms).getUTCFullYear()
369
+ return y >= 1 &amp;&amp; y &lt;= 9999
370
+ }
371
+
372
+
223
373
  /**
224
374
  * 取DECIMAL欄之scale, 未指定者為0(mssql之DECIMAL預設為(18,0)), 非DECIMAL者為null
225
375
  *
@@ -275,18 +425,23 @@ function isKindOk(v, kind, scale = null) {
275
425
  return true
276
426
  }
277
427
  if (kind === 'date') {
428
+ //所有形式換算UTC後年份須在1–9999(inDateRange)
278
429
  if (v instanceof Date) {
279
- return !isNaN(v.getTime())
430
+ return !isNaN(v.getTime()) &amp;&amp; inDateRange(v.getTime())
280
431
  }
281
432
  if (typeof v === 'number') {
282
433
  //須在Date可表示之範圍(±8.64e15毫秒)內, 超出者被stringify為'Invalid date'而以文字比較, sqlite會命中全部日期列
283
- return Number.isFinite(v) &amp;&amp; !isNaN(new Date(v).getTime())
434
+ return Number.isFinite(v) &amp;&amp; !isNaN(new Date(v).getTime()) &amp;&amp; inDateRange(v)
284
435
  }
285
436
  if (typeof v !== 'string') {
286
437
  return false
287
438
  }
288
439
  let m = v.match(reIsoTz)
289
- return m !== null &amp;&amp; isCalendarDate(m[1], m[2], m[3]) &amp;&amp; !isNaN(Date.parse(v.replace(' ', 'T')))
440
+ if (m === null || !isCalendarDate(m[1], m[2], m[3])) {
441
+ return false
442
+ }
443
+ let ms = Date.parse(v.replace(' ', 'T'))
444
+ return !isNaN(ms) &amp;&amp; inDateRange(ms)
290
445
  }
291
446
  if (kind === 'dateonly') {
292
447
  //Date物件依主機時區取日期而結果隨主機而變, 故只收日期字串
@@ -386,42 +541,31 @@ function normPk(v, kind, scale = null) {
386
541
  return { kind: 'invalid' }
387
542
  }
388
543
 
389
-
390
544
  /**
391
- * 檢核純量值: 字串、有限數值、布林、有效Date、Buffer, 以及允許時之null; 並須符合欄位型別分類
392
- * 註: 物件、陣列等不得交sequelize之型別stringify, 否則FLOAT成'NaN'、INTEGER成'[object Object]'、DATE成本地今日00:00而誤刪
545
+ * 檢核純量值: 字串、有限數值、布林、有效Date, 以及null(是否允許null已由cvOrmFind依運算子判定); 並須符合欄位型別分類
546
+ * 註: 物件、陣列等不得交sequelize之型別stringify, 否則FLOAT成'NaN'、INTEGER成'[object Object]'、DATE成本地今日00:00而誤刪;
547
+ * 形狀層(cvOrmFind)已保證僅此類值到達, 此處保留型別檢核作為fail-closed之防線(日後上游放寬時不得靜默放行)
393
548
  * 註: 主鍵欄(pk為true)之字串類、整數類與其他數值類以normPk正規化後回傳(如字串類主鍵欄之123即'123'), 無效或型別不符者拒收,
394
549
  * 令select與delAll對主鍵之認定與selectByPk、del等主鍵函數一致; 非主鍵欄不轉型
550
+ * 註: kind為date且呼叫端以ctx.dateNoTz明示'local'(dateNoTz為'local')時, 無時區日期時間字串依主機時區轉為Date回傳(見parseNoTzLocal), 其餘值照舊判定
395
551
  *
396
552
  * @ignore
397
553
  */
398
- function chkScalar(v, path, allowNull, kind = 'any', scale = null, pk = false) {
554
+ function chkScalar(v, path, kind = 'any', scale = null, pk = false, dateNoTz = undefined) {
399
555
  if (v === null) {
400
- if (allowNull) {
401
- return v
402
- }
403
- err('null is not allowed here', path)
404
- }
405
- let ok = false
406
- if (typeof v === 'string' || typeof v === 'boolean') {
407
- ok = true
556
+ return v
408
557
  }
409
- else if (typeof v === 'number') {
558
+ if (typeof v === 'number') {
410
559
  if (!Number.isFinite(v)) {
411
560
  err(`number must be finite, got [${v}]`, path)
412
561
  }
413
- ok = true
414
562
  }
415
563
  else if (v instanceof Date) {
416
564
  if (isNaN(v.getTime())) {
417
565
  err('invalid Date', path)
418
566
  }
419
- ok = true
420
- }
421
- else if (Buffer.isBuffer(v)) {
422
- ok = kind === 'any'
423
567
  }
424
- if (!ok) {
568
+ else if (typeof v !== 'string' &amp;&amp; typeof v !== 'boolean') {
425
569
  err(`value must be a string, finite number, boolean, Date or null, got [${Object.prototype.toString.call(v)}]`, path)
426
570
  }
427
571
  if (pk &amp;&amp; (kind === 'str' || kind === 'int' || kind === 'num')) {
@@ -431,408 +575,231 @@ function chkScalar(v, path, allowNull, kind = 'any', scale = null, pk = false) {
431
575
  }
432
576
  err(`value [${String(v)}] does not match column type [${kind}]${scale !== null ? ` with scale [${scale}]` : ''}`, path)
433
577
  }
578
+ if (kind === 'date' &amp;&amp; dateNoTz === 'local' &amp;&amp; typeof v === 'string') {
579
+ //呼叫端明示之無時區日期時間字串: 依主機時區解析並以Date送出, 與直接給Date同義; 不符者續由isKindOk判定(帶時區者照舊)
580
+ let t = parseNoTzLocal(v)
581
+ if (t !== null) {
582
+ return t
583
+ }
584
+ }
434
585
  if (!isKindOk(v, kind, scale)) {
435
586
  err(`value [${String(v)}] does not match column type [${kind}]${scale !== null ? ` with scale [${scale}]` : ''}`, path)
436
587
  }
588
+ if (kind === 'date' &amp;&amp; !(v instanceof Date)) {
589
+ //通過檢核之字串與epoch一律轉為Date送出: sequelize只在鍵為屬性名或欄名完全相符時依欄型序列化值, 大小寫變體鍵會原樣送出字串與epoch,
590
+ //sqlite以文字或型別序比較而命中錯列(如{D:{$gt:epoch}}刪光全部日期列); 轉為Date後與鍵之拼寫無關
591
+ return new Date(typeof v === 'string' ? Date.parse(v.replace(' ', 'T')) : v)
592
+ }
437
593
  return v
438
594
  }
439
595
 
440
596
 
441
597
  /**
442
- * 取陣列之各元素, 稀疏陣列之洞與undefined一律拒收
443
- * 註: forEach與map會略過洞, 洞若被略過則$and之子式消失而成恆真
598
+ * $in與$nin, 元素為純量(不含null, 已由cvOrmFind保證); 空$in以引用欄位之恆假表示
599
+ * 註: $nin:[]已由cvOrmFind拒收(恆真, 易因清單恰為空而刪全表); 此處再擋以免上游放寬時靜默放行
444
600
  *
445
601
  * @ignore
446
602
  */
447
- function getItems(arr, path) {
448
- let rs = []
603
+ function cvIn(field, arr, path, neg, kind, scale, pk, dateNoTz) {
604
+ let vs = []
449
605
  for (let i = 0; i &lt; arr.length; i++) {
450
- if (!(i in arr) || arr[i] === undefined) {
451
- err('array must not contain holes or undefined', `${path}.${i}`)
606
+ if (arr[i] === null) {
607
+ err('null is not allowed here', `${path}[${i}]`)
452
608
  }
453
- rs.push(arr[i])
454
- }
455
- return rs
456
- }
457
-
458
-
459
- /**
460
- * 檢核sequelize原生Symbol運算子之值: 純量、純量陣列或sequelize之literal/fn/col
461
- * 註: 原生寫法由呼叫端自負語意, 但仍不得把物件交sequelize之型別stringify
462
- *
463
- * @ignore
464
- */
465
- function chkRawSym(x, path, kind, scale, pk) {
466
- if (x instanceof Sequelize.Utils.SequelizeMethod) {
467
- return x
609
+ vs.push(chkScalar(arr[i], `${path}[${i}]`, kind, scale, pk, dateNoTz))
468
610
  }
469
- if (Array.isArray(x)) {
470
- return getItems(x, path).map((y, i) => (y instanceof Sequelize.Utils.SequelizeMethod ? y : chkScalar(y, `${path}.${i}`, true, kind, scale, pk)))
471
- }
472
- return chkScalar(x, path, true, kind, scale, pk)
473
- }
474
-
475
-
476
- /**
477
- * 將正規式來源轉為字面字串, 僅接受無未跳脫元字元者
478
- *
479
- * @ignore
480
- */
481
- function regexToLiteral(src, path) {
482
- let r = ''
483
- let meta = '\\^$.|?*+()[]{}/'
484
- for (let i = 0; i &lt; src.length; i++) {
485
- let c = src[i]
486
- if (c === '\\') {
487
- let n = src[i + 1]
488
- if (n !== undefined &amp;&amp; meta.indexOf(n) >= 0) {
489
- r += n
490
- i++
491
- continue
492
- }
493
- err(`$regex supports literal substrings only, escape [\\${n === undefined ? '' : n}] is not supported`, path)
494
- }
495
- if (meta.indexOf(c) >= 0) {
496
- err(`$regex supports literal substrings only, unescaped metacharacter [${c}] is not supported`, path)
497
- }
498
- r += c
499
- }
500
- if (r.indexOf('\u0000') >= 0) {
501
- err('$regex must not contain NUL', path)
502
- }
503
- return r
504
- }
505
-
506
-
507
- /**
508
- * 正規化正規式旗標, 僅接受i; '$i'為既有寫法, 過渡期視同i
509
- *
510
- * @ignore
511
- */
512
- function normFlags(fs, path) {
513
- if (fs === undefined || fs === null || fs === '') {
514
- return false
515
- }
516
- if (typeof fs !== 'string') {
517
- err('$options must be a string', path)
518
- }
519
- let s = fs === '$i' ? 'i' : fs
520
- for (let c of s) {
521
- if (c !== 'i') {
522
- err(`$options/flags supports [i] only, got [${fs}]`, path)
523
- }
524
- }
525
- return s.indexOf('i') >= 0
526
- }
527
-
528
-
529
- /**
530
- * 產生LIKE子字串條件, 跳脫萬用字元並帶ESCAPE子句; 不分大小寫時兩側皆以LOWER處理
531
- * 註: sqlite之LOWER只折疊ASCII, 故sqlite下給i而字面含非ASCII之大小寫字母者拒收, 不宣稱做不到之語意
532
- *
533
- * @ignore
534
- */
535
- function cvRegex(field, re, options, path, ctx, kind) {
536
- if (kind !== 'str' &amp;&amp; kind !== 'any') {
537
- err(`$regex requires a string column, column type is [${kind}]`, path)
538
- }
539
- let src
540
- let ci
541
- if (re instanceof RegExp) {
542
- if (options !== undefined) {
543
- err('$options can not be used with a RegExp $regex', path)
544
- }
545
- let flags = re.flags.replace(/[gu]/g, '') //g、u不影響是否命中
546
- src = re.source === '(?:)' ? '' : re.source //new RegExp('')之source為(?:), 即空字串
547
- ci = normFlags(flags, path)
548
- }
549
- else if (typeof re === 'string') {
550
- src = re
551
- ci = normFlags(options, path)
552
- }
553
- else {
554
- err('$regex must be a string or RegExp', path)
555
- }
556
- let lit = regexToLiteral(src, path)
557
- if (ci &amp;&amp; ctx.dialect === 'sqlite') {
558
- for (let c of lit) {
559
- if (c.charCodeAt(0) > 127 &amp;&amp; c.toLowerCase() !== c.toUpperCase()) {
560
- err(`$regex with i supports ASCII letters only on sqlite, got [${c}]`, path)
561
- }
562
- }
563
- }
564
- let pat = `%${lit.replace(/[!%_[]/g, (c) => `${cEsc}${c}`)}%`
565
- let q = ctx.escape(pat)
566
- if (ci) {
567
- //LOWER內須為資料庫之實際欄名, model之屬性名與欄名可不同
568
- let col = ctx.attrs &amp;&amp; ctx.attrs[field] &amp;&amp; ctx.attrs[field].field ? ctx.attrs[field].field : field
569
- return Sequelize.where(Sequelize.fn('LOWER', Sequelize.col(col)), Op.like, Sequelize.literal(`LOWER(${q}) ESCAPE '${cEsc}'`))
570
- }
571
- return { [field]: { [Op.like]: Sequelize.literal(`${q} ESCAPE '${cEsc}'`) } }
572
- }
573
-
574
-
575
- /**
576
- * $in與$nin, 含null者另以IS [NOT] NULL補上, 與{欄位:null}一致; 空集合以恆假/恆真表示
577
- *
578
- * @ignore
579
- */
580
- function cvIn(field, arr, path, neg, kind, scale, pk) {
581
- if (!Array.isArray(arr)) {
582
- err(`${neg ? '$nin' : '$in'} must be an array`, path)
583
- }
584
- let hasNull = false
585
- let vs = []
586
- getItems(arr, path).forEach((v, k) => {
587
- if (v === null) {
588
- hasNull = true
589
- return
590
- }
591
- vs.push(chkScalar(v, `${path}.${k}`, false, kind, scale, pk))
592
- })
593
611
  if (!neg) {
594
612
  if (vs.length === 0) {
595
- return hasNull ? { [field]: null } : sqlFalse()
613
+ //f IS NULL恆為二值(永不為UNKNOWN), 整式二值, 且欄名會出現在SQL中, 不存在之欄由資料庫回報(R5)
614
+ return { [Op.and]: [{ [field]: null }, sqlFalse()] }
596
615
  }
597
- let c = { [field]: { [Op.in]: vs } }
598
- return hasNull ? { [Op.or]: [c, { [field]: null }] } : c
616
+ return { [field]: { [Op.in]: vs } }
599
617
  }
600
618
  if (vs.length === 0) {
601
- return hasNull ? { [field]: { [Op.ne]: null } } : sqlTrue()
619
+ err('$nin with empty array is always true', path)
602
620
  }
603
- let c = { [field]: { [Op.notIn]: vs } }
604
- return hasNull ? { [Op.and]: [c, { [field]: { [Op.ne]: null } }] } : c
621
+ return { [field]: { [Op.notIn]: vs } }
605
622
  }
606
623
 
607
624
 
608
625
  /**
609
- * 欄位層條件, 回傳以AND結合之子式陣列
626
+ * 欄位層條件, 回傳以AND結合之子式陣列; v為純量、null、Date(等值)或運算子物件(null原型, 鍵為$eq $ne $gt $gte $lt $lte $in $nin)
610
627
  *
611
628
  * @ignore
612
629
  */
613
- function cvField(field, v, path, depth, ctx) {
614
- let kind = getColKind(ctx.attrs, field)
615
- let scale = getColScale(ctx.attrs, field)
616
- let pk = ctx.pk !== undefined &amp;&amp; field === ctx.pk //主鍵欄之純量依normPk正規化(R8)
617
- if (depth > nDepthMax) {
618
- err('find is too deep', path)
619
- }
620
- if (field === '' || field === '__proto__') {
621
- err(`invalid column name [${field}]`, path)
622
- }
623
- if (v === undefined) {
624
- err('value is undefined', path)
625
- }
626
- if (Array.isArray(v)) {
627
- //陣列值即$in(含null與空陣列之處置), 與MongoDB之陣列比對不同, 已文件化
628
- return [cvIn(field, v, path, false, kind, scale, pk)]
629
- }
630
- if (v instanceof RegExp) {
631
- return [cvRegex(field, v, undefined, path, ctx, kind)]
632
- }
633
- if (!isPlainObj(v)) {
634
- return [{ [field]: chkScalar(v, path, true, kind, scale, pk) }]
635
- }
636
- let ks = Reflect.ownKeys(v)
637
- if (ks.length === 0) {
638
- err('empty operator object', path)
630
+ function cvField(field, v, path, ctx) {
631
+
632
+ //欄名先解析為model之屬性名再判型別與主鍵(R4欄名解析); attrs已給而解析不到者拒收, attrs未給者為any, 交資料庫處置
633
+ let an = resolveAttr(ctx.attrs, ctx.attrIdx, field, path)
634
+ let kind = an === null ? 'any' : getColKind(ctx.attrs, an)
635
+ let scale = an === null ? null : getColScale(ctx.attrs, an)
636
+ let pk = ctx.pk !== undefined &amp;&amp; an === ctx.pk //主鍵欄之純量依normPk正規化(R8)
637
+ let dateNoTz = ctx.dateNoTz
638
+
639
+ //欄位直給(等值)
640
+ if (v === null || typeof v !== 'object' || v instanceof Date) {
641
+ return [{ [field]: chkScalar(v, path, kind, scale, pk, dateNoTz) }]
639
642
  }
643
+
644
+ //運算子物件
640
645
  let simple = {}
641
646
  let nSimple = 0
642
647
  let cs = []
643
- let hasRegex = false
644
- for (let k of ks) {
645
- let kp = `${path}.${String(k)}`
648
+ for (let k of Object.keys(v)) {
649
+ let kp = `${path}.${k}`
646
650
  let x = v[k]
647
- let name = k
648
- if (typeof k === 'symbol') {
649
- if (!setOp.has(k) || setSymFieldNo.has(k)) {
650
- err(`unsupported symbol operator [${String(k)}]`, kp)
651
+ if (k === '$in' || k === '$nin') {
652
+ if (!Array.isArray(x)) {
653
+ err(`${k} must be an array`, kp)
651
654
  }
652
- name = mSymCmp.get(k)
653
- if (name === undefined) {
654
- //其餘sequelize之Symbol運算子為呼叫端自負之原生寫法, 值經檢核後各自成一子式
655
- cs.push({ [field]: { [k]: chkRawSym(x, kp, kind, scale, pk) } })
656
- continue
657
- }
658
- }
659
- if (typeof name !== 'string' || name[0] !== '$') {
660
- err('nested object is not supported, operators must start with $', kp)
655
+ cs.push(cvIn(field, x, kp, k === '$nin', kind, scale, pk, dateNoTz))
661
656
  }
662
- if (mCmp[name] !== undefined) {
663
- let allowNull = name === '$eq' || name === '$ne'
664
- let y = chkScalar(x, kp, allowNull, kind, scale, pk)
665
- if (Object.prototype.hasOwnProperty.call(simple, mCmp[name])) {
666
- //同義之鍵(如$gt與Op.gt)各自成一子式, 不得互相覆寫
667
- cs.push({ [field]: { [mCmp[name]]: y } })
668
- }
669
- else {
670
- simple[mCmp[name]] = y
671
- nSimple++
672
- }
673
- }
674
- else if (name === '$in' || name === '$nin') {
675
- cs.push(cvIn(field, x, kp, name === '$nin', kind, scale, pk))
676
- }
677
- else if (name === '$exists') {
678
- if (x !== true &amp;&amp; x !== false &amp;&amp; x !== 0 &amp;&amp; x !== 1) {
679
- err('$exists must be a boolean, 0 or 1', kp)
680
- }
681
- cs.push(x ? { [field]: { [Op.ne]: null } } : { [field]: null })
682
- }
683
- else if (name === '$regex') {
684
- hasRegex = true
685
- cs.push(cvRegex(field, x, v.$options, kp, ctx, kind))
686
- }
687
- else if (name === '$options') {
688
- //由$regex一併處理
689
- }
690
- else if (name === '$not') {
691
- if (Array.isArray(x)) {
692
- err('$not does not accept an array', kp)
693
- }
694
- let ns = cvField(field, x, kp, depth + 1, ctx)
695
- cs.push({ [Op.not]: { [Op.and]: ns } })
657
+ else if (Object.prototype.hasOwnProperty.call(mCmp, k)) {
658
+ //同欄之比較運算子併入同一欄位物件, 以AND結合
659
+ simple[mCmp[k]] = chkScalar(x, kp, kind, scale, pk, dateNoTz)
660
+ nSimple++
696
661
  }
697
662
  else {
698
- err(`unsupported operator [${name}]`, kp)
663
+ err(`unsupported operator [${k}]`, kp)
699
664
  }
700
665
  }
701
- if (v.$options !== undefined &amp;&amp; !hasRegex) {
702
- err('$options requires $regex', path)
703
- }
704
666
  if (nSimple > 0) {
705
667
  cs.unshift({ [field]: simple })
706
668
  }
669
+ if (cs.length === 0) {
670
+ //不產生任何子式者條件即消失而成恆真, delAll會刪全表; cvOrmFind已拒收空運算子物件, 此處再擋(含非Date之物件值)
671
+ err('operator object has no operator', path)
672
+ }
707
673
  return cs
708
674
  }
709
675
 
710
676
 
711
677
  /**
712
- * 文件層條件(含頂層邏輯運算子)
678
+ * 文件層條件(含$and、$or), o為cvOrmFind之複本(null原型, 鍵僅字串)
713
679
  *
714
680
  * @ignore
715
681
  */
716
- function cvDoc(o, path, depth, ctx) {
717
- if (depth > nDepthMax) {
718
- err('find is too deep', path)
719
- }
720
- if (!isPlainObj(o)) {
721
- err('condition must be an object', path)
722
- }
723
- let cvList = (x, kp) => {
724
- if (!Array.isArray(x)) {
725
- err('logical operator requires an array', kp)
726
- }
727
- //空陣列拒收(同MongoDB), 以免呼叫端之條件清單恰為空時成為恆真而刪全表
728
- if (x.length === 0) {
729
- err('logical operator requires a nonempty array', kp)
730
- }
731
- return getItems(x, kp).map((y, i) => cvDoc(y, `${kp}.${i}`, depth + 1, ctx))
732
- }
682
+ function cvDoc(o, path, ctx) {
733
683
  let cs = []
734
- for (let k of Reflect.ownKeys(o)) {
684
+ for (let k of Object.keys(o)) {
735
685
  let x = o[k]
736
- let kp = path ? `${path}.${String(k)}` : String(k)
737
- if (typeof k === 'symbol') {
738
- if (k === Op.and) {
739
- cs.push(cvAnd(cvList(x, kp)))
740
- }
741
- else if (k === Op.or) {
742
- cs.push(cvOr(cvList(x, kp)))
686
+ let kp = `${path}.${k}`
687
+ if (k === '$and' || k === '$or') {
688
+ if (!Array.isArray(x) || x.length === 0) {
689
+ err(`${k} requires a nonempty array`, kp)
743
690
  }
744
- else if (k === Op.not) {
745
- cs.push({ [Op.not]: cvDoc(x, kp, depth + 1, ctx) })
691
+ let sub = []
692
+ for (let i = 0; i &lt; x.length; i++) {
693
+ sub.push(cvDoc(x[i], `${kp}[${i}]`, ctx))
746
694
  }
747
- else {
748
- err(`unsupported symbol operator at document level [${String(k)}]`, kp)
749
- }
750
- continue
695
+ cs.push(cvGroup(k === '$and' ? cvAnd : cvOr, sub))
751
696
  }
752
- if (k[0] === '$') {
753
- if (k === '$and') {
754
- cs.push(cvAnd(cvList(x, kp)))
755
- }
756
- else if (k === '$or') {
757
- cs.push(cvOr(cvList(x, kp)))
758
- }
759
- else if (k === '$nor') {
760
- cs.push({ [Op.not]: cvOr(cvList(x, kp)) })
761
- }
762
- else {
763
- err(`unsupported operator [${k}]`, kp)
764
- }
765
- continue
697
+ else if (k[0] === '$') {
698
+ err(`unsupported operator [${k}]`, kp)
766
699
  }
767
- cs.push(...cvField(k, x, kp, depth + 1, ctx))
700
+ else {
701
+ cs.push(...cvField(k, x, kp, ctx))
702
+ }
703
+ }
704
+ if (cs.length === 0) {
705
+ err('empty condition', path)
768
706
  }
769
- return cvAnd(cs)
707
+ return cvGroup(cvAnd, cs)
770
708
  }
771
709
 
772
710
 
773
711
  /**
774
- * 以AND結合子式, 無子式者為恆真
712
+ * 以AND結合子式
775
713
  *
776
714
  * @ignore
777
715
  */
778
716
  function cvAnd(cs) {
779
- if (cs.length === 0) {
780
- return { [Op.and]: [sqlTrue()] }
781
- }
782
717
  return { [Op.and]: cs }
783
718
  }
784
719
 
785
720
 
786
721
  /**
787
- * 以OR結合子式, 無子式者為恆假
722
+ * 以OR結合子式
788
723
  *
789
724
  * @ignore
790
725
  */
791
726
  function cvOr(cs) {
792
- if (cs.length === 0) {
793
- return { [Op.and]: [sqlFalse()] }
794
- }
795
727
  return { [Op.or]: cs }
796
728
  }
797
729
 
798
730
 
731
+ /**
732
+ * 子式過多者遞迴二分為平衡樹, 避免SQLite之運算式樹深度與遞迴上限(見nFlat); 語意不變(AND與OR皆滿足結合律, 含NULL之三值邏輯), 子式順序不變
733
+ *
734
+ * @ignore
735
+ */
736
+ function cvGroup(fn, cs) {
737
+ if (cs.length &lt;= nFlat) {
738
+ return fn(cs)
739
+ }
740
+ let k = Math.ceil(cs.length / 2)
741
+ return fn([cvGroup(fn, cs.slice(0, k)), cvGroup(fn, cs.slice(k))])
742
+ }
743
+
744
+
745
+ /**
746
+ * 檢核find之形狀(系列白名單, 由wsemi之cvOrmFind單一擁有, 規則見R11)並回傳全新之null原型深複本, 後續一律只使用複本(檢查之值即使用之值)
747
+ * 註: 未給/undefined/{}回傳空物件(全部); 其餘非條件物件、白名單外之運算子與值、異常結構一律throw Error('invalid find: 原因 at [路徑]')
748
+ * 註: maxList傳999(SQLite之運算式樹深度上限); select與delAll於載入model與建立sequelize實例之前先呼叫本函數, 使形狀不合法者不連線、不送出任何SQL
749
+ * 註: 對複本再呼叫為冪等(複本本身符合白名單)
750
+ *
751
+ * @ignore
752
+ * @param {*} find 輸入查詢條件
753
+ * @returns {Object} 回傳null原型之條件複本, 無自有鍵代表全部
754
+ */
755
+ function chkFindShape(find) {
756
+ return cvOrmFind(find, optShape)
757
+ }
758
+
759
+
799
760
  /**
800
761
  * 將查詢條件轉為sequelize之where, 供select與delAll共用, 令同一find於兩函數由同一SQL謂詞決定命中
801
762
  *
802
- * 註: 支援欄位等值與$eq、$ne、$gt、$gte、$lt、$lte、$in、$nin、$not、$regex($options僅i)、$exists, 以及$and、$or、$nor
803
- * 註: 不支援之運算子、非純量之值、與欄位型別不符之純量一律拋錯, 不得交sequelize之型別stringify(見本檔之chkScalar)
804
- * 註: NULL依SQL三值邏輯; $regex僅支援字面子字串; 非物件之find回{}(即全部), 此為現況保留
763
+ * 註: 第一步經chkFindShape(wsemi之cvOrmFind)依系列白名單檢核並取複本, 其後只走訪複本; 白名單: 欄位等值{欄:純量|null}、
764
+ * $eq $ne $gt $gte $lt $lte(純量)、$in $nin(純量陣列)、$and $or(條件物件之非空陣列); 其餘(含$not $nor $regex $options $exists、RegExp、Symbol鍵、陣列值)一律拋錯
765
+ * 註: 未給、undefined或{}回傳{}(全部); 其餘非條件物件(null、陣列、字串、數值等)拋錯, 不再視為{}
766
+ * 註: 與欄位型別不符之純量、不存在之欄一律拋錯, 不得交sequelize之型別stringify(見本檔之chkScalar); NULL依SQL三值邏輯
805
767
  *
806
768
  * @param {*} find 輸入查詢條件
807
769
  * @param {Object} ctx 輸入轉換所需之依賴物件
808
- * @param {Function} ctx.escape 輸入依方言將字串轉為SQL字串常值之函數, 通常為sequelize實例之escape
809
- * @param {Object} [ctx.attrs] 輸入model之rawAttributes, 供依欄位型別檢核純量與取得實際欄名, 未給時不檢型別
810
- * @param {String} [ctx.dialect] 輸入資料庫種類字串, 供判定方言能力(如sqlite之LOWER僅折疊ASCII)
770
+ * @param {Function} [ctx.escape] 已不使用(原供$regex之LIKE字面常值; 第33項批1移除$regex), 保留接受以相容既有呼叫端
771
+ * @param {Object} [ctx.attrs] 輸入執行where之model之rawAttributes, 供欄名解析(嚴格鍵: 鍵須與屬性名或資料庫欄名完全相符, 其餘拒收)、依欄位型別檢核純量與取得實際欄名; 未給時不解析鍵、不檢型別而交資料庫(見CLAUDE_rulebook.md之R4)
772
+ * @param {String} [ctx.dialect] 已不使用(原供判定sqlite之LOWER僅折疊ASCII), 保留接受以相容既有呼叫端
811
773
  * @param {String} [ctx.pk] 輸入主鍵欄位名, 該欄之純量條件值依normPk正規化(如字串類主鍵欄之123即'123'), 未給時不正規化
774
+ * @param {String} [ctx.dateNoTz] 輸入無時區日期時間字串之處置, 僅接受undefined(預設, 無時區者拒收)或'local'(依主機時區解析, 與以本地時間分量建構之Date同義), 其餘值拋錯; 僅作用於DATE欄, 供Jet等無時區後端之呼叫端明示啟用
812
775
  * @returns {Object} 回傳sequelize之where物件, 條件不合法時拋出錯誤
813
776
  */
814
777
  function cvFind(find, ctx) {
815
778
 
816
- //check, 非物件(undefined、null、字串、數值等純量)與陣列視為未給條件, 此為現況保留, 系列規格另案處理
817
- if (find === null || typeof find !== 'object' || Array.isArray(find)) {
818
- return {}
779
+ //check, ctx.dateNoTz須為undefined或'local', 其餘值(含拼錯、大小寫不同)拋錯, 不靜默忽略
780
+ if (ctx &amp;&amp; ctx.dateNoTz !== undefined &amp;&amp; ctx.dateNoTz !== 'local') {
781
+ throw new Error(`invalid ctx: dateNoTz must be 'local' or undefined, got [${String(ctx.dateNoTz)}]`)
819
782
  }
820
783
 
821
- //check, 非一般物件(class實例、Object.create(proto)、Date、Map等)之條件可能不在自有鍵上, 拒收而不當成{}
822
- if (!isPlainObj(find)) {
823
- err('find must be a plain object')
824
- }
784
+ //step1, 形狀檢核(R11), 其後一律只使用複本
785
+ let fd = chkFindShape(find)
825
786
 
826
787
  //check, 空物件為全部
827
- if (Reflect.ownKeys(find).length === 0) {
788
+ if (Reflect.ownKeys(fd).length === 0) {
828
789
  return {}
829
790
  }
830
791
 
831
- return cvDoc(find, '', 0, ctx)
792
+ //check, attrs須為rawAttributes形狀(見chkAttrs)
793
+ if (ctx &amp;&amp; ctx.attrs) {
794
+ chkAttrs(ctx.attrs)
795
+ }
796
+
797
+ //attrs之索引每次轉譯只建一次(內部欄位attrIdx, 不屬ctx之介面)
798
+ return cvDoc(fd, 'find', ctx &amp;&amp; ctx.attrs ? { ...ctx, attrIdx: buildAttrIdx(ctx.attrs) } : ctx)
832
799
  }
833
800
 
834
801
 
835
- export { getColKind, getColScale, normPk }
802
+ export { getColKind, getColScale, normPk, chkFindShape }
836
803
  export default cvFind
837
804
  </code></pre>
838
805
  </article>
@@ -848,7 +815,7 @@ export default cvFind
848
815
  <br class="clear">
849
816
 
850
817
  <footer>
851
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Fri Oct 16 2026 14:32:44 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
818
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Oct 19 2026 18:54:43 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
852
819
  </footer>
853
820
 
854
821
  <script>prettyPrint();</script>