@maiyunnet/kebab 9.26.4 → 9.26.5

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/doc/kebab-rag.md CHANGED
@@ -1794,7 +1794,7 @@ index/variables/VER.md
1794
1794
 
1795
1795
  # Variable: VER
1796
1796
 
1797
- > `const` **VER**: `"9.26.4"` = `'9.26.4'`
1797
+ > `const` **VER**: `"9.26.5"` = `'9.26.5'`
1798
1798
 
1799
1799
  Defined in: [index.ts:9](https://github.com/maiyunnet/kebab/blob/master/index.ts#L9)
1800
1800
 
@@ -135860,11 +135860,11 @@ Defined in: [sys/mod.ts:92](https://github.com/maiyunnet/kebab/blob/master/sys/m
135860
135860
 
135861
135861
  ### \_$index
135862
135862
 
135863
- > `protected` `static` **\_$index**: `string` = `''`
135863
+ > `protected` `static` **\_$index**: `string` \| `string`[] = `''`
135864
135864
 
135865
135865
  Defined in: [sys/mod.ts:86](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L86)
135866
135866
 
135867
- 若使用 _$key 并且有多个 unique 索引,这里指定 _$key 的索引名
135867
+ 自动 key 冲突目标:MySQL 为索引名;PGSQL 为唯一字段或字段数组,PG 留空使用 _$key,不支持部分或表达式索引
135868
135868
 
135869
135869
  ***
135870
135870
 
@@ -135912,7 +135912,7 @@ Defined in: [sys/mod.ts:77](https://github.com/maiyunnet/kebab/blob/master/sys/m
135912
135912
 
135913
135913
  > `protected` **\_keyGenerator**(): `string`
135914
135914
 
135915
- Defined in: [sys/mod.ts:1987](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1987)
135915
+ Defined in: [sys/mod.ts:2048](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2048)
135916
135916
 
135917
135917
  当 _key 不为空时,则依据继承此方法的方法自动生成填充 key
135918
135918
 
@@ -135936,7 +135936,7 @@ Defined in: [sys/mod.ts:1987](https://github.com/maiyunnet/kebab/blob/master/sys
135936
135936
 
135937
135937
  > **all**(): `Promise`\<`false` \| [`Rows`](Rows.md)\<`Mod`\>\>
135938
135938
 
135939
- Defined in: [sys/mod.ts:1210](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1210)
135939
+ Defined in: [sys/mod.ts:1271](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1271)
135940
135940
 
135941
135941
  ##### Returns
135942
135942
 
@@ -135946,7 +135946,7 @@ Defined in: [sys/mod.ts:1210](https://github.com/maiyunnet/kebab/blob/master/sys
135946
135946
 
135947
135947
  > **all**(`key`): `Promise`\<`false` \| `Record`\<`string`, `Mod`\>\>
135948
135948
 
135949
- Defined in: [sys/mod.ts:1211](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1211)
135949
+ Defined in: [sys/mod.ts:1272](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1272)
135950
135950
 
135951
135951
  ##### Parameters
135952
135952
 
@@ -135974,7 +135974,7 @@ Defined in: [sys/mod.ts:1211](https://github.com/maiyunnet/kebab/blob/master/sys
135974
135974
 
135975
135975
  > **allArray**(): `Promise`\<`false` \| `Record`\<`string`, `any`\>[]\>
135976
135976
 
135977
- Defined in: [sys/mod.ts:1389](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1389)
135977
+ Defined in: [sys/mod.ts:1450](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1450)
135978
135978
 
135979
135979
  ##### Returns
135980
135980
 
@@ -135984,7 +135984,7 @@ Defined in: [sys/mod.ts:1389](https://github.com/maiyunnet/kebab/blob/master/sys
135984
135984
 
135985
135985
  > **allArray**(`key`): `Promise`\<`false` \| `Record`\<`string`, `Record`\<`string`, `any`\>\>\>
135986
135986
 
135987
- Defined in: [sys/mod.ts:1390](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1390)
135987
+ Defined in: [sys/mod.ts:1451](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1451)
135988
135988
 
135989
135989
  ##### Parameters
135990
135990
 
@@ -136002,7 +136002,7 @@ Defined in: [sys/mod.ts:1390](https://github.com/maiyunnet/kebab/blob/master/sys
136002
136002
 
136003
136003
  > **allCursor**\<`T`\>(`count`, `opt`): `Promise`\<`false` \| [`ICursorPage`](../interfaces/ICursorPage.md)\<`T`\>\>
136004
136004
 
136005
- Defined in: [sys/mod.ts:1638](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1638)
136005
+ Defined in: [sys/mod.ts:1699](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1699)
136006
136006
 
136007
136007
  读取一批游标数据,多取一条判断后续记录,不使用 OFFSET,不修改原查询
136008
136008
  排序字段必须全部非空、同向,且组合唯一;SELECT 中须包含对应字段(同名或指定 keys)
@@ -136067,7 +136067,7 @@ SELECT 结果中对应的字段名,默认去掉 by 的表别名
136067
136067
 
136068
136068
  > **append**(`sql`): `this`
136069
136069
 
136070
- Defined in: [sys/mod.ts:1896](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1896)
136070
+ Defined in: [sys/mod.ts:1957](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1957)
136071
136071
 
136072
136072
  在 sql 最后追加字符串
136073
136073
 
@@ -136087,7 +136087,7 @@ Defined in: [sys/mod.ts:1896](https://github.com/maiyunnet/kebab/blob/master/sys
136087
136087
 
136088
136088
  > **by**(`c`, `d?`): `this`
136089
136089
 
136090
- Defined in: [sys/mod.ts:1852](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1852)
136090
+ Defined in: [sys/mod.ts:1913](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1913)
136091
136091
 
136092
136092
  ORDER BY
136093
136093
 
@@ -136115,7 +136115,7 @@ ORDER BY
136115
136115
 
136116
136116
  > **contain**(`contain`): `this`
136117
136117
 
136118
- Defined in: [sys/mod.ts:1905](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1905)
136118
+ Defined in: [sys/mod.ts:1966](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1966)
136119
136119
 
136120
136120
  设置闭包含数据
136121
136121
 
@@ -136143,7 +136143,7 @@ Defined in: [sys/mod.ts:1905](https://github.com/maiyunnet/kebab/blob/master/sys
136143
136143
 
136144
136144
  > **count**(): `Promise`\<`number`\>
136145
136145
 
136146
- Defined in: [sys/mod.ts:1688](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1688)
136146
+ Defined in: [sys/mod.ts:1749](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1749)
136147
136147
 
136148
136148
  根据当前条件,筛选出当前条目该有的数据条数
136149
136149
 
@@ -136157,7 +136157,7 @@ Defined in: [sys/mod.ts:1688](https://github.com/maiyunnet/kebab/blob/master/sys
136157
136157
 
136158
136158
  > **countSql**(): `string`
136159
136159
 
136160
- Defined in: [sys/mod.ts:1709](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1709)
136160
+ Defined in: [sys/mod.ts:1770](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1770)
136161
136161
 
136162
136162
  获取当前条件下的 count 的 SQL 语句
136163
136163
 
@@ -136171,15 +136171,15 @@ Defined in: [sys/mod.ts:1709](https://github.com/maiyunnet/kebab/blob/master/sys
136171
136171
 
136172
136172
  > **create**(): `Promise`\<`boolean` \| `null`\>
136173
136173
 
136174
- Defined in: [sys/mod.ts:889](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L889)
136174
+ Defined in: [sys/mod.ts:901](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L901)
136175
136175
 
136176
- 创建数据
136176
+ 创建数据,自动 key 最多尝试 3 次;PGSQL 仅忽略指定字段的唯一冲突
136177
136177
 
136178
136178
  #### Returns
136179
136179
 
136180
136180
  `Promise`\<`boolean` \| `null`\>
136181
136181
 
136182
- true-成功,false-报错,null-唯一键非 _$key 键冲突
136182
+ true-成功,false-错误或重试耗尽,null-其他唯一冲突;PGSQL 的 SQL 错误需要回滚事务
136183
136183
 
136184
136184
  ***
136185
136185
 
@@ -136187,7 +136187,7 @@ true-成功,false-报错,null-唯一键非 _$key 键冲突
136187
136187
 
136188
136188
  > **crossJoin**(`f`, `s`, `index?`, `pre?`): `this`
136189
136189
 
136190
- Defined in: [sys/mod.ts:1784](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1784)
136190
+ Defined in: [sys/mod.ts:1845](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1845)
136191
136191
 
136192
136192
  cross join 方法
136193
136193
 
@@ -136237,7 +136237,7 @@ ON 信息
136237
136237
 
136238
136238
  > **explain**(`all?`): `Promise`\<`string` \| `false`\>
136239
136239
 
136240
- Defined in: [sys/mod.ts:1515](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1515)
136240
+ Defined in: [sys/mod.ts:1576](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1576)
136241
136241
 
136242
136242
  ##### Parameters
136243
136243
 
@@ -136253,7 +136253,7 @@ Defined in: [sys/mod.ts:1515](https://github.com/maiyunnet/kebab/blob/master/sys
136253
136253
 
136254
136254
  > **explain**(`all`): `Promise`\<`false` \| `Record`\<`string`, `any`\>\>
136255
136255
 
136256
- Defined in: [sys/mod.ts:1516](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1516)
136256
+ Defined in: [sys/mod.ts:1577](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1577)
136257
136257
 
136258
136258
  ##### Parameters
136259
136259
 
@@ -136271,7 +136271,7 @@ Defined in: [sys/mod.ts:1516](https://github.com/maiyunnet/kebab/blob/master/sys
136271
136271
 
136272
136272
  > **filter**(`s`): `this`
136273
136273
 
136274
- Defined in: [sys/mod.ts:1830](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1830)
136274
+ Defined in: [sys/mod.ts:1891](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1891)
136275
136275
 
136276
136276
  筛选器
136277
136277
 
@@ -136309,7 +136309,7 @@ Defined in: [sys/mod.ts:1830](https://github.com/maiyunnet/kebab/blob/master/sys
136309
136309
 
136310
136310
  > **first**(`lock`, `array`): `Promise`\<`false` \| `Record`\<`string`, `any`\> \| `null`\>
136311
136311
 
136312
- Defined in: [sys/mod.ts:1099](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1099)
136312
+ Defined in: [sys/mod.ts:1160](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1160)
136313
136313
 
136314
136314
  ##### Parameters
136315
136315
 
@@ -136329,7 +136329,7 @@ Defined in: [sys/mod.ts:1099](https://github.com/maiyunnet/kebab/blob/master/sys
136329
136329
 
136330
136330
  > **first**(`lock?`, `array?`): `Promise`\<`false` \| `Mod` & `Record`\<`string`, `any`\> \| `null`\>
136331
136331
 
136332
- Defined in: [sys/mod.ts:1103](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1103)
136332
+ Defined in: [sys/mod.ts:1164](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1164)
136333
136333
 
136334
136334
  ##### Parameters
136335
136335
 
@@ -136351,7 +136351,7 @@ Defined in: [sys/mod.ts:1103](https://github.com/maiyunnet/kebab/blob/master/sys
136351
136351
 
136352
136352
  > **firstArray**(`lock?`): `Promise`\<`false` \| `Record`\<`string`, `any`\> \| `null`\>
136353
136353
 
136354
- Defined in: [sys/mod.ts:1143](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1143)
136354
+ Defined in: [sys/mod.ts:1204](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1204)
136355
136355
 
136356
136356
  获取数据库第一个原生对象
136357
136357
 
@@ -136373,7 +136373,7 @@ Defined in: [sys/mod.ts:1143](https://github.com/maiyunnet/kebab/blob/master/sys
136373
136373
 
136374
136374
  > **format**(`sql?`, `data?`): `string`
136375
136375
 
136376
- Defined in: [sys/mod.ts:1932](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1932)
136376
+ Defined in: [sys/mod.ts:1993](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1993)
136377
136377
 
136378
136378
  获取带 data 的 sql 语句
136379
136379
 
@@ -136401,7 +136401,7 @@ sql 语句
136401
136401
 
136402
136402
  > **fullJoin**(`f`, `s`, `index?`, `pre?`): `this`
136403
136403
 
136404
- Defined in: [sys/mod.ts:1772](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1772)
136404
+ Defined in: [sys/mod.ts:1833](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1833)
136405
136405
 
136406
136406
  full join 方法
136407
136407
 
@@ -136463,7 +136463,7 @@ Defined in: [sys/mod.ts:881](https://github.com/maiyunnet/kebab/blob/master/sys/
136463
136463
 
136464
136464
  > **getData**(): `any`[]
136465
136465
 
136466
- Defined in: [sys/mod.ts:1923](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1923)
136466
+ Defined in: [sys/mod.ts:1984](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1984)
136467
136467
 
136468
136468
  获取全部 data
136469
136469
 
@@ -136477,7 +136477,7 @@ Defined in: [sys/mod.ts:1923](https://github.com/maiyunnet/kebab/blob/master/sys
136477
136477
 
136478
136478
  > **getSql**(): `string`
136479
136479
 
136480
- Defined in: [sys/mod.ts:1916](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1916)
136480
+ Defined in: [sys/mod.ts:1977](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1977)
136481
136481
 
136482
136482
  获取 sql 语句
136483
136483
 
@@ -136491,7 +136491,7 @@ Defined in: [sys/mod.ts:1916](https://github.com/maiyunnet/kebab/blob/master/sys
136491
136491
 
136492
136492
  > **group**(`c`): `this`
136493
136493
 
136494
- Defined in: [sys/mod.ts:1861](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1861)
136494
+ Defined in: [sys/mod.ts:1922](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1922)
136495
136495
 
136496
136496
  GROUP BY
136497
136497
 
@@ -136513,7 +136513,7 @@ GROUP BY
136513
136513
 
136514
136514
  > **having**(`s`): `this`
136515
136515
 
136516
- Defined in: [sys/mod.ts:1821](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1821)
136516
+ Defined in: [sys/mod.ts:1882](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1882)
136517
136517
 
136518
136518
  筛选器
136519
136519
 
@@ -136535,7 +136535,7 @@ Defined in: [sys/mod.ts:1821](https://github.com/maiyunnet/kebab/blob/master/sys
136535
136535
 
136536
136536
  > **hint**(`h`): `this`
136537
136537
 
136538
- Defined in: [sys/mod.ts:1812](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1812)
136538
+ Defined in: [sys/mod.ts:1873](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1873)
136539
136539
 
136540
136540
  设置 MySQL 优化器 Hint
136541
136541
  内容会以 Optimizer Hint 注释语法注入到 SELECT 关键字后
@@ -136580,7 +136580,7 @@ Hint 内容
136580
136580
 
136581
136581
  > **innerJoin**(`f`, `s`, `index?`, `pre?`): `this`
136582
136582
 
136583
- Defined in: [sys/mod.ts:1760](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1760)
136583
+ Defined in: [sys/mod.ts:1821](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1821)
136584
136584
 
136585
136585
  inner join 方法
136586
136586
 
@@ -136620,7 +136620,7 @@ ON 信息
136620
136620
 
136621
136621
  > **join**(`f`, `s?`, `type?`, `index?`, `pre?`): `this`
136622
136622
 
136623
- Defined in: [sys/mod.ts:1724](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1724)
136623
+ Defined in: [sys/mod.ts:1785](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1785)
136624
136624
 
136625
136625
  #### Parameters
136626
136626
 
@@ -136664,7 +136664,7 @@ ON 信息
136664
136664
 
136665
136665
  > **langText**(`col`, `lang`): `string`
136666
136666
 
136667
- Defined in: [sys/mod.ts:1967](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1967)
136667
+ Defined in: [sys/mod.ts:2028](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2028)
136668
136668
 
136669
136669
  获取字段的可用语种文本
136670
136670
 
@@ -136692,7 +136692,7 @@ Defined in: [sys/mod.ts:1967](https://github.com/maiyunnet/kebab/blob/master/sys
136692
136692
 
136693
136693
  > **leftJoin**(`f`, `s`, `index?`, `pre?`): `this`
136694
136694
 
136695
- Defined in: [sys/mod.ts:1736](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1736)
136695
+ Defined in: [sys/mod.ts:1797](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1797)
136696
136696
 
136697
136697
  left join 方法
136698
136698
 
@@ -136732,7 +136732,7 @@ ON 信息
136732
136732
 
136733
136733
  > **limit**(`a`, `b?`): `this`
136734
136734
 
136735
- Defined in: [sys/mod.ts:1874](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1874)
136735
+ Defined in: [sys/mod.ts:1935](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1935)
136736
136736
 
136737
136737
  LIMIT
136738
136738
 
@@ -136760,7 +136760,7 @@ LIMIT
136760
136760
 
136761
136761
  > **page**(`count`, `page?`): `this`
136762
136762
 
136763
- Defined in: [sys/mod.ts:1885](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1885)
136763
+ Defined in: [sys/mod.ts:1946](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1946)
136764
136764
 
136765
136765
  分页
136766
136766
 
@@ -136788,7 +136788,7 @@ Defined in: [sys/mod.ts:1885](https://github.com/maiyunnet/kebab/blob/master/sys
136788
136788
 
136789
136789
  > **refresh**(`lock?`): `Promise`\<`boolean` \| `null`\>
136790
136790
 
136791
- Defined in: [sys/mod.ts:1011](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1011)
136791
+ Defined in: [sys/mod.ts:1069](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1069)
136792
136792
 
136793
136793
  刷新当前模型获取最新数据
136794
136794
 
@@ -136810,7 +136810,7 @@ Defined in: [sys/mod.ts:1011](https://github.com/maiyunnet/kebab/blob/master/sys
136810
136810
 
136811
136811
  > **remove**(): `Promise`\<`boolean`\>
136812
136812
 
136813
- Defined in: [sys/mod.ts:1077](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1077)
136813
+ Defined in: [sys/mod.ts:1138](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1138)
136814
136814
 
136815
136815
  移除本条目
136816
136816
 
@@ -136824,7 +136824,7 @@ Defined in: [sys/mod.ts:1077](https://github.com/maiyunnet/kebab/blob/master/sys
136824
136824
 
136825
136825
  > **rightJoin**(`f`, `s`, `index?`, `pre?`): `this`
136826
136826
 
136827
- Defined in: [sys/mod.ts:1748](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1748)
136827
+ Defined in: [sys/mod.ts:1809](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1809)
136828
136828
 
136829
136829
  right join 方法
136830
136830
 
@@ -136864,7 +136864,7 @@ ON 信息
136864
136864
 
136865
136865
  > **save**(`where?`): `Promise`\<`boolean`\>
136866
136866
 
136867
- Defined in: [sys/mod.ts:1043](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1043)
136867
+ Defined in: [sys/mod.ts:1102](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1102)
136868
136868
 
136869
136869
  更新 set 的数据到数据库,有未保存数据时才保存
136870
136870
 
@@ -136874,12 +136874,14 @@ Defined in: [sys/mod.ts:1043](https://github.com/maiyunnet/kebab/blob/master/sys
136874
136874
 
136875
136875
  `any`
136876
136876
 
136877
- 自定义筛选条件,默认根据主键筛选
136877
+ 非空筛选条件,省略时必须有主键
136878
136878
 
136879
136879
  #### Returns
136880
136880
 
136881
136881
  `Promise`\<`boolean`\>
136882
136882
 
136883
+ 是否成功,失败保留待写数据
136884
+
136883
136885
  ***
136884
136886
 
136885
136887
  ### set()
@@ -136960,7 +136962,7 @@ Defined in: [sys/mod.ts:847](https://github.com/maiyunnet/kebab/blob/master/sys/
136960
136962
 
136961
136963
  > **toArray**\<`TC`\>(): [`TOnlyProperties`](../type-aliases/TOnlyProperties.md)\<`InstanceType`\<`TC`\>\> & `Record`\<`string`, `any`\>
136962
136964
 
136963
- Defined in: [sys/mod.ts:1939](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1939)
136965
+ Defined in: [sys/mod.ts:2000](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2000)
136964
136966
 
136965
136967
  获取值对象,获取的是新创建的数组
136966
136968
 
@@ -136980,7 +136982,7 @@ Defined in: [sys/mod.ts:1939](https://github.com/maiyunnet/kebab/blob/master/sys
136980
136982
 
136981
136983
  > **total**(`f?`): `Promise`\<`number`\>
136982
136984
 
136983
- Defined in: [sys/mod.ts:1573](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1573)
136985
+ Defined in: [sys/mod.ts:1634](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1634)
136984
136986
 
136985
136987
  获取总条数,自动抛弃 LIMIT,仅用于获取数据的情况(select)
136986
136988
 
@@ -137000,7 +137002,7 @@ Defined in: [sys/mod.ts:1573](https://github.com/maiyunnet/kebab/blob/master/sys
137000
137002
 
137001
137003
  > **totalEstimate**(): `Promise`\<`number` \| `false`\>
137002
137004
 
137003
- Defined in: [sys/mod.ts:1599](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1599)
137005
+ Defined in: [sys/mod.ts:1660](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1660)
137004
137006
 
137005
137007
  从 PostgreSQL 执行计划估算查询总行数,移除外层排序和分页,不执行数据扫描
137006
137008
  请在应用游标条件前调用;非 PostgreSQL、框架分表联查或查询失败返回 false,绝不回退到 COUNT
@@ -137017,7 +137019,7 @@ Defined in: [sys/mod.ts:1599](https://github.com/maiyunnet/kebab/blob/master/sys
137017
137019
 
137018
137020
  > **union**(`f`, `type?`): `this`
137019
137021
 
137020
- Defined in: [sys/mod.ts:1154](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1154)
137022
+ Defined in: [sys/mod.ts:1215](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1215)
137021
137023
 
137022
137024
  联合查询表数据
137023
137025
 
@@ -137045,7 +137047,7 @@ Defined in: [sys/mod.ts:1154](https://github.com/maiyunnet/kebab/blob/master/sys
137045
137047
 
137046
137048
  > **unionAll**(`f`): `this`
137047
137049
 
137048
- Defined in: [sys/mod.ts:1184](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1184)
137050
+ Defined in: [sys/mod.ts:1245](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1245)
137049
137051
 
137050
137052
  所有联合查询表数据
137051
137053
 
@@ -137067,7 +137069,7 @@ Defined in: [sys/mod.ts:1184](https://github.com/maiyunnet/kebab/blob/master/sys
137067
137069
 
137068
137070
  > **unsaved**(): `boolean`
137069
137071
 
137070
- Defined in: [sys/mod.ts:1958](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1958)
137072
+ Defined in: [sys/mod.ts:2019](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2019)
137071
137073
 
137072
137074
  当前是否设置了未保存 --=
137073
137075
 
@@ -137081,7 +137083,7 @@ Defined in: [sys/mod.ts:1958](https://github.com/maiyunnet/kebab/blob/master/sys
137081
137083
 
137082
137084
  > **updates**(): `Record`\<`string`, `any`\>
137083
137085
 
137084
- Defined in: [sys/mod.ts:1947](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1947)
137086
+ Defined in: [sys/mod.ts:2008](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2008)
137085
137087
 
137086
137088
  获取当前设置要提交的数据
137087
137089
 
@@ -137095,9 +137097,11 @@ Defined in: [sys/mod.ts:1947](https://github.com/maiyunnet/kebab/blob/master/sys
137095
137097
 
137096
137098
  > **upsert**(`conflict`): `Promise`\<`boolean`\>
137097
137099
 
137098
- Defined in: [sys/mod.ts:980](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L980)
137100
+ Defined in: [sys/mod.ts:995](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L995)
137099
137101
 
137100
- 插入数据,如果存在则更新(UPSERT)
137102
+ 插入数据,如果业务唯一键已存在则更新,保留已有主键和自动 key
137103
+ PGSQL 为原子 UPSERT,自动 key 只生成一次,SQL 错误需回滚事务
137104
+ MySQL 分步执行;自动 key 有多个唯一索引时须配置 _$index,整笔原子性由调用方事务保证
137101
137105
 
137102
137106
  #### Parameters
137103
137107
 
@@ -137105,19 +137109,21 @@ Defined in: [sys/mod.ts:980](https://github.com/maiyunnet/kebab/blob/master/sys/
137105
137109
 
137106
137110
  `string` \| `string`[]
137107
137111
 
137108
- 冲突字段,不能为 _$key 或 _$primary,应该是你要判断的唯一索引字段
137112
+ 已通过 set 赋非 NULL 值的业务唯一字段,不得包含 _$key 或 _$primary;PGSQL 不支持部分或表达式索引
137109
137113
 
137110
137114
  #### Returns
137111
137115
 
137112
137116
  `Promise`\<`boolean`\>
137113
137117
 
137118
+ 是否成功;至少需要一个可更新字段,失败保留待写数据
137119
+
137114
137120
  ***
137115
137121
 
137116
137122
  ### where()
137117
137123
 
137118
137124
  > **where**(`s`): `this`
137119
137125
 
137120
- Defined in: [sys/mod.ts:1841](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1841)
137126
+ Defined in: [sys/mod.ts:1902](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1902)
137121
137127
 
137122
137128
  是 filter 的别名
137123
137129
 
@@ -138382,7 +138388,7 @@ sys/mod/interfaces/IModUnionItem.md
138382
138388
 
138383
138389
  # Interface: IModUnionItem
138384
138390
 
138385
- Defined in: [sys/mod.ts:2001](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2001)
138391
+ Defined in: [sys/mod.ts:2062](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2062)
138386
138392
 
138387
138393
  ## Properties
138388
138394
 
@@ -138390,7 +138396,7 @@ Defined in: [sys/mod.ts:2001](https://github.com/maiyunnet/kebab/blob/master/sys
138390
138396
 
138391
138397
  > **field**: `string`
138392
138398
 
138393
- Defined in: [sys/mod.ts:2002](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2002)
138399
+ Defined in: [sys/mod.ts:2063](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2063)
138394
138400
 
138395
138401
  ***
138396
138402
 
@@ -138398,7 +138404,7 @@ Defined in: [sys/mod.ts:2002](https://github.com/maiyunnet/kebab/blob/master/sys
138398
138404
 
138399
138405
  > `optional` **where?**: `any`
138400
138406
 
138401
- Defined in: [sys/mod.ts:2003](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2003)
138407
+ Defined in: [sys/mod.ts:2064](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2064)
138402
138408
 
138403
138409
  sys/mod/interfaces/IRows.md
138404
138410
  ---
@@ -138411,7 +138417,7 @@ sys/mod/interfaces/IRows.md
138411
138417
 
138412
138418
  # Interface: IRows\<T\>
138413
138419
 
138414
- Defined in: [sys/mod.ts:1995](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1995)
138420
+ Defined in: [sys/mod.ts:2056](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2056)
138415
138421
 
138416
138422
  ## Extends
138417
138423
 
@@ -138429,7 +138435,7 @@ Defined in: [sys/mod.ts:1995](https://github.com/maiyunnet/kebab/blob/master/sys
138429
138435
 
138430
138436
  > `readonly` **length**: `number`
138431
138437
 
138432
- Defined in: [sys/mod.ts:1996](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1996)
138438
+ Defined in: [sys/mod.ts:2057](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2057)
138433
138439
 
138434
138440
  ## Methods
138435
138441
 
@@ -138437,7 +138443,7 @@ Defined in: [sys/mod.ts:1996](https://github.com/maiyunnet/kebab/blob/master/sys
138437
138443
 
138438
138444
  > **item**(`index`): `T`
138439
138445
 
138440
- Defined in: [sys/mod.ts:1997](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1997)
138446
+ Defined in: [sys/mod.ts:2058](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2058)
138441
138447
 
138442
138448
  #### Parameters
138443
138449
 
@@ -138455,7 +138461,7 @@ Defined in: [sys/mod.ts:1997](https://github.com/maiyunnet/kebab/blob/master/sys
138455
138461
 
138456
138462
  > **toArray**(): `Record`\<`string`, `any`\>[]
138457
138463
 
138458
- Defined in: [sys/mod.ts:1998](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L1998)
138464
+ Defined in: [sys/mod.ts:2059](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L2059)
138459
138465
 
138460
138466
  #### Returns
138461
138467
 
package/index.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * --- 本文件用来定义每个目录实体地址的常量 ---
5
5
  */
6
6
  /** --- 当前系统版本号 --- */
7
- export declare const VER = "9.26.4";
7
+ export declare const VER = "9.26.5";
8
8
  /** --- 框架根目录,以 / 结尾 --- */
9
9
  export declare const ROOT_PATH: string;
10
10
  /** --- 框架的 LIB,以 / 结尾 --- */
package/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export const VER = '9.26.4';
8
+ export const VER = '9.26.5';
9
9
  // --- 服务端用的路径 ---
10
10
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
11
11
  /** --- /xxx/xxx --- */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.26.4",
3
+ "version": "9.26.5",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
package/sys/mod.d.ts CHANGED
@@ -45,8 +45,8 @@ export default class Mod {
45
45
  protected static _$primary: string;
46
46
  /** --- 设置后将由 _keyGenerator 函数生成唯一字段 --- */
47
47
  protected static _$key: string;
48
- /** --- 若使用 _$key 并且有多个 unique 索引,这里指定 _$key 的索引名 --- */
49
- protected static _$index: string;
48
+ /** --- 自动 key 冲突目标:MySQL 为索引名;PGSQL 为唯一字段或字段数组,PG 留空使用 _$key,不支持部分或表达式索引 --- */
49
+ protected static _$index: string | string[];
50
50
  /** --- 前缀,MySQL 时为表前缀(如 prefix_),PostgreSQL 时为 Schema 名。顺序:选项前缀 -> 本前缀 -> 配置文件前缀 --- */
51
51
  protected static _$pre?: string;
52
52
  /** --- 要 update 的内容 --- */
@@ -328,13 +328,21 @@ export default class Mod {
328
328
  */
329
329
  get(n: string): any;
330
330
  /**
331
- * --- 创建数据 ---
332
- * @return true-成功,false-报错,null-唯一键非 _$key 键冲突
331
+ * --- 收集待写字段,保留模型内的原始 JSON 值 ---
332
+ * @returns 独立的 SQL 数据对象
333
+ */
334
+ private _getUpdates;
335
+ /**
336
+ * --- 创建数据,自动 key 最多尝试 3 次;PGSQL 仅忽略指定字段的唯一冲突 ---
337
+ * @return true-成功,false-错误或重试耗尽,null-其他唯一冲突;PGSQL 的 SQL 错误需要回滚事务
333
338
  */
334
339
  create(): Promise<boolean | null>;
335
340
  /**
336
- * --- 插入数据,如果存在则更新(UPSERT) ---
337
- * @param conflict 冲突字段,不能为 _$key 或 _$primary,应该是你要判断的唯一索引字段
341
+ * --- 插入数据,如果业务唯一键已存在则更新,保留已有主键和自动 key ---
342
+ * --- PGSQL 为原子 UPSERT,自动 key 只生成一次,SQL 错误需回滚事务 ---
343
+ * --- MySQL 分步执行;自动 key 有多个唯一索引时须配置 _$index,整笔原子性由调用方事务保证 ---
344
+ * @param conflict 已通过 set 赋非 NULL 值的业务唯一字段,不得包含 _$key 或 _$primary;PGSQL 不支持部分或表达式索引
345
+ * @returns 是否成功;至少需要一个可更新字段,失败保留待写数据
338
346
  */
339
347
  upsert(conflict: string | string[]): Promise<boolean>;
340
348
  /**
@@ -344,7 +352,8 @@ export default class Mod {
344
352
  refresh(lock?: boolean): Promise<boolean | null>;
345
353
  /**
346
354
  * --- 更新 set 的数据到数据库,有未保存数据时才保存 ---
347
- * @param where 自定义筛选条件,默认根据主键筛选
355
+ * @param where 非空筛选条件,省略时必须有主键
356
+ * @returns 是否成功,失败保留待写数据
348
357
  */
349
358
  save(where?: any): Promise<boolean>;
350
359
  /**
package/sys/mod.js CHANGED
@@ -53,7 +53,7 @@ export default class Mod {
53
53
  static _$primary = 'id';
54
54
  /** --- 设置后将由 _keyGenerator 函数生成唯一字段 --- */
55
55
  static _$key = '';
56
- /** --- 若使用 _$key 并且有多个 unique 索引,这里指定 _$key 的索引名 --- */
56
+ /** --- 自动 key 冲突目标:MySQL 为索引名;PGSQL 为唯一字段或字段数组,PG 留空使用 _$key,不支持部分或表达式索引 --- */
57
57
  static _$index = '';
58
58
  /** --- 前缀,MySQL 时为表前缀(如 prefix_),PostgreSQL 时为 Schema 名。顺序:选项前缀 -> 本前缀 -> 配置文件前缀 --- */
59
59
  static _$pre;
@@ -605,17 +605,34 @@ export default class Mod {
605
605
  return this._data[n];
606
606
  }
607
607
  /**
608
- * --- 创建数据 ---
609
- * @return true-成功,false-报错,null-唯一键非 _$key 键冲突
608
+ * --- 收集待写字段,保留模型内的原始 JSON 值 ---
609
+ * @returns 独立的 SQL 数据对象
610
610
  */
611
- async create() {
612
- const cstr = this.constructor;
611
+ _getUpdates() {
613
612
  const updates = {};
614
- for (const k in this._updates) {
615
- updates[k] = this._jsonUpdates[k] ? lSql.json(this._data[k]) : this._data[k];
613
+ for (const field in this._updates) {
614
+ updates[field] = this._jsonUpdates[field] ? lSql.json(this._data[field]) : this._data[field];
616
615
  }
616
+ return updates;
617
+ }
618
+ /**
619
+ * --- 创建数据,自动 key 最多尝试 3 次;PGSQL 仅忽略指定字段的唯一冲突 ---
620
+ * @return true-成功,false-错误或重试耗尽,null-其他唯一冲突;PGSQL 的 SQL 错误需要回滚事务
621
+ */
622
+ async create() {
623
+ const cstr = this.constructor;
624
+ const updates = this._getUpdates();
625
+ const service = this._db.getService();
626
+ const keyIndex = cstr._$index;
617
627
  let r = null;
618
628
  if ((cstr._$key !== '') && (updates[cstr._$key] === undefined)) {
629
+ const keyFields = Array.isArray(keyIndex) ? keyIndex : [keyIndex || cstr._$key];
630
+ if (service === lDb.ESERVICE.PGSQL && !keyFields.includes(cstr._$key)) {
631
+ return false;
632
+ }
633
+ if (service === lDb.ESERVICE.MYSQL && Array.isArray(keyIndex)) {
634
+ return false;
635
+ }
619
636
  let count = 0;
620
637
  while (true) {
621
638
  if (count === 3) {
@@ -626,38 +643,32 @@ export default class Mod {
626
643
  this[cstr._$key] = updates[cstr._$key];
627
644
  this._sql.insert(cstr._$table + (this._index ? ('_' + this._index[0]) : ''));
628
645
  this._sql.values(updates);
646
+ if (service === lDb.ESERVICE.PGSQL) {
647
+ this._sql.append(' ON CONFLICT (' + keyFields.map(field => this._sql.field(field)).join(', ') + ') DO NOTHING');
648
+ }
629
649
  r = await this._db.execute(this._sql.getSql(), this._sql.getData());
630
650
  ++count;
631
651
  if (!r.error) {
652
+ // --- PG 的自动 key 冲突不报错,未插入时换一个候选值 ---
653
+ if (service === lDb.ESERVICE.PGSQL && r.packet?.affected === 0) {
654
+ continue;
655
+ }
632
656
  break;
633
657
  }
634
658
  if (r.error.errno === 1062) {
635
- // --- 与唯一键冲突 ---
636
- if (!cstr._$index) {
637
- // --- 没设置 index,那就当做是本次生成的 key 重复了 ---
638
- continue;
639
- }
640
- if (this._db.getService() === lDb.ESERVICE.MYSQL) {
641
- // --- MYSQL ---
642
- const match = /for key '([\w.]+)'/.exec(r.error.message);
643
- if (match?.[1].includes(cstr._$index)) {
644
- // --- key 索引重复,继续生成 key ---
659
+ if (service === lDb.ESERVICE.MYSQL) {
660
+ if (!keyIndex) {
645
661
  continue;
646
662
  }
647
- }
648
- else {
649
- // --- PGSQL ---
650
- const match = /constraint "([\w.]+)"/.exec(r.error.message);
651
- if (match?.[1].includes(cstr._$index)) {
652
- // --- key 索引重复,继续生成 key ---
663
+ const match = /for key '([\w.]+)'/.exec(r.error.message);
664
+ if (typeof keyIndex === 'string' && match?.[1].includes(keyIndex)) {
653
665
  continue;
654
666
  }
655
667
  }
656
- // --- 1062 非 index 冲突,那需要用户自行处理(可能不允许重复的邮箱) ---
668
+ // --- PG 的目标冲突已经被 SQL 处理,剩下的是其他唯一约束冲突 ---
657
669
  return null;
658
670
  }
659
671
  // --- 未处理的错误 ---
660
- const service = this._db.getService();
661
672
  lCore.log(this._ctr ?? {}, '[MOD][create0][' + cstr._$table + '] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
662
673
  if (lCore.globalConfig.debug) {
663
674
  lCore.debug('[MOD][create0][' + cstr._$table + ']', service !== null ? lDb.ESERVICE[service] : 'NONE', r);
@@ -694,34 +705,79 @@ export default class Mod {
694
705
  }
695
706
  }
696
707
  /**
697
- * --- 插入数据,如果存在则更新(UPSERT) ---
698
- * @param conflict 冲突字段,不能为 _$key 或 _$primary,应该是你要判断的唯一索引字段
708
+ * --- 插入数据,如果业务唯一键已存在则更新,保留已有主键和自动 key ---
709
+ * --- PGSQL 为原子 UPSERT,自动 key 只生成一次,SQL 错误需回滚事务 ---
710
+ * --- MySQL 分步执行;自动 key 有多个唯一索引时须配置 _$index,整笔原子性由调用方事务保证 ---
711
+ * @param conflict 已通过 set 赋非 NULL 值的业务唯一字段,不得包含 _$key 或 _$primary;PGSQL 不支持部分或表达式索引
712
+ * @returns 是否成功;至少需要一个可更新字段,失败保留待写数据
699
713
  */
700
714
  async upsert(conflict) {
701
- // --- 这里没用 mysql 的 INSERT ... ON DUPLICATE KEY UPDATE 或 pgsql 的 ON CONFLICT DO UPDATE ---
702
- // --- 因为还要处理 _$key 的自动生成 ---
703
- const res = await this.create();
704
- if (res) {
705
- // --- 创建成功 ---
706
- return true;
707
- }
708
- if (res === false) {
709
- // --- 系统错误 ---
710
- return false;
711
- }
712
- // --- res === null,唯一键冲突,执行更新 ---
713
- /** --- 冲突字段列表 --- */
715
+ const cstr = this.constructor;
714
716
  const conflicts = typeof conflict === 'string' ? [conflict] : conflict;
717
+ const inserts = this._getUpdates();
718
+ const updates = { ...inserts };
715
719
  const where = {};
720
+ if (!conflicts.length) {
721
+ return false;
722
+ }
716
723
  for (const field of conflicts) {
717
- if (!this._updates[field]) {
724
+ if (!field || field === cstr._$key || field === cstr._$primary ||
725
+ !this._updates[field] || inserts[field] === null) {
726
+ return false;
727
+ }
728
+ where[field] = inserts[field];
729
+ delete updates[field];
730
+ }
731
+ delete updates[cstr._$primary];
732
+ delete updates[cstr._$key];
733
+ if (!Object.keys(updates).length) {
734
+ return false;
735
+ }
736
+ const mysql = this._db.getService() === lDb.ESERVICE.MYSQL;
737
+ const table = cstr._$table + (this._index ? `_${this._index[0]}` : '');
738
+ const identity = [...new Set([cstr._$primary, cstr._$key].filter(field => field))];
739
+ if (mysql) {
740
+ const created = await this.create();
741
+ if (created !== null) {
742
+ return created;
743
+ }
744
+ // --- 按业务键读取最新目标,事务内锁住该行直到更新完成 ---
745
+ this._sql.select(identity, table).where(where).limit(1).lock();
746
+ }
747
+ else {
748
+ if (cstr._$key && inserts[cstr._$key] === undefined) {
749
+ inserts[cstr._$key] = this._keyGenerator();
750
+ }
751
+ this._sql.insert(table).values(inserts).upsert(updates, conflicts);
752
+ this._sql.append(` RETURNING ${identity.map(field => this._sql.field(field)).join(', ')}`);
753
+ }
754
+ const result = await this._db.query(this._sql.getSql(), this._sql.getData());
755
+ if (result.error) {
756
+ lCore.log(this._ctr ?? {}, `[MOD][upsert][${cstr._$table}] database error: ${result.error.errno}`, '-error');
757
+ return false;
758
+ }
759
+ const row = result.rows?.[0];
760
+ if (!row) {
761
+ return false;
762
+ }
763
+ if (mysql) {
764
+ // --- 同时匹配身份字段和业务键,目标变化时不回填旧身份 ---
765
+ this._sql.update(table, updates).where({ ...where, ...row });
766
+ const saved = await this._db.execute(this._sql.getSql(), this._sql.getData());
767
+ if (saved.error) {
768
+ lCore.log(this._ctr ?? {}, `[MOD][upsert][${cstr._$table}] database error: ${saved.error.errno}`, '-error');
718
769
  return false;
719
770
  }
720
- where[field] = this._data[field];
721
- delete this._updates[field];
722
- delete this._jsonUpdates[field];
771
+ if (!saved.packet?.affected) {
772
+ return false;
773
+ }
774
+ }
775
+ for (const field of identity) {
776
+ this._setProperty(field, row[field]);
723
777
  }
724
- return this.save(where);
778
+ this._updates = {};
779
+ this._jsonUpdates = {};
780
+ return true;
725
781
  }
726
782
  /**
727
783
  * --- 刷新当前模型获取最新数据 ---
@@ -756,24 +812,27 @@ export default class Mod {
756
812
  }
757
813
  /**
758
814
  * --- 更新 set 的数据到数据库,有未保存数据时才保存 ---
759
- * @param where 自定义筛选条件,默认根据主键筛选
815
+ * @param where 非空筛选条件,省略时必须有主键
816
+ * @returns 是否成功,失败保留待写数据
760
817
  */
761
818
  async save(where) {
762
819
  if (Object.keys(this._updates).length === 0) {
763
820
  return true;
764
821
  }
765
822
  const cstr = this.constructor;
766
- if (!this._data[cstr._$primary]) {
823
+ if (!this._data[cstr._$primary] && (where === undefined || where === null)) {
767
824
  lCore.log(this._ctr ?? {}, '[MOD][save] primary key not found', '-error');
768
825
  return false;
769
826
  }
770
- const updates = {};
771
- for (const k in this._updates) {
772
- updates[k] = this._jsonUpdates[k] ? lSql.json(this._data[k]) : this._data[k];
773
- }
774
- this._sql.update(cstr._$table + (this._index ? ('_' + this._index[0]) : ''), [updates]).where(where ?? {
827
+ const updates = this._getUpdates();
828
+ this._sql.update(cstr._$table + (this._index ? ('_' + this._index[0]) : ''), [updates]);
829
+ const updateSql = this._sql.getSql();
830
+ this._sql.where(where ?? {
775
831
  [cstr._$primary]: this._data[cstr._$primary]
776
832
  });
833
+ if (this._sql.getSql() === updateSql) {
834
+ return false;
835
+ }
777
836
  const r = await this._db.execute(this._sql.getSql(), this._sql.getData());
778
837
  if (r.packet === null) {
779
838
  lCore.log(this._ctr ?? {}, '[MOD][save] ' + (lText.stringifyJson(r.error?.message ?? '').slice(1, -1) + ' - ' + this._sql.format()).replaceAll('"', '""'), '-error');
@@ -12,7 +12,7 @@ CREATE TABLE `m_test` (
12
12
  `time_add` bigint NOT NULL,
13
13
  PRIMARY KEY (`id`) USING BTREE,
14
14
  INDEX `time_add`(`time_add` ASC) USING BTREE,
15
- UNIQUE INDEX `utoken`(`token` ASC) USING BTREE,
15
+ UNIQUE INDEX `token`(`token` ASC) USING BTREE,
16
16
  UNIQUE INDEX `uname`(`name` ASC) USING BTREE
17
17
  ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
18
18
  // --- PGSQL ---
@@ -41,7 +41,7 @@ export default class default_1 extends sMod {
41
41
  static _$table = 'test';
42
42
  static _$primary = 'id';
43
43
  static _$key = 'token';
44
- static _$index = 'utoken';
44
+ static _$index = 'token';
45
45
  /* eslint-enable */
46
46
  _keyGenerator() {
47
47
  return 'test_' + lCore.rand(0, 3).toString();