@maiyunnet/kebab 6.2.0 → 6.2.2

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/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "6.2.0";
8
+ export declare const VER = "6.2.2";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  export declare const LIB_PATH: string;
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '6.2.0';
9
+ export const VER = '6.2.2';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/lib/sql.js CHANGED
@@ -703,8 +703,6 @@ export class Sql {
703
703
  * @param suf 表后缀,仅请在 field 表名时倒入后缀,前面加 # 代表要强制 AS,可能是分表查询时用
704
704
  */
705
705
  field(str, pre = '', suf = '') {
706
- let left = '';
707
- let right = '';
708
706
  const q = this._service === ESERVICE.MYSQL ? '`' : '"';
709
707
  if (Array.isArray(str)) {
710
708
  this._data.push(...str[1]);
@@ -713,21 +711,21 @@ export class Sql {
713
711
  if (typeof str === 'number') {
714
712
  str = str.toString();
715
713
  }
716
- str = str.trim(); // --- 去除前导尾随 ---
717
- str = str.replace(/ {2,}/g, ' '); // --- 去除多余的空格 ---
718
- str = str.replace(/ +([),])/g, ' $1');
719
- str = str.replace(/([(,]) +/g, '$1 ');
720
- str = str.replace(/(\W)(JOIN|WHERE|UNION)(\W)/ig, '$1$3');
714
+ str = str.trim() // --- 去除前导尾随 ---
715
+ .replace(/ {2,}/g, ' ') // --- 去除多余的空格 ---
716
+ .replace(/ +([),])/g, ' $1')
717
+ .replace(/([(,]) +/g, '$1 ')
718
+ .replace(/(\W)(JOIN|WHERE|UNION)(\W)/ig, '$1$3');
721
719
  // --- 先判断 suf 强制性 AS ---
722
- let sufAs = false;
723
- if (suf.startsWith('#')) {
720
+ const sufAs = suf.startsWith('#');
721
+ if (sufAs) {
724
722
  // --- 强制 AS ---
725
723
  suf = suf.slice(1);
726
- sufAs = true;
727
724
  }
728
725
  // --- 先判断有没有别名(也就是 as) ---
729
- const loStr = str.toLowerCase();
730
- const asPos = loStr.indexOf(' as ');
726
+ const asPos = str.toLowerCase().indexOf(' as ');
727
+ let left = '';
728
+ let right = '';
731
729
  if (asPos === -1) {
732
730
  // --- 没有 as ---
733
731
  let spacePos = str.lastIndexOf(' ');
@@ -736,21 +734,12 @@ export class Sql {
736
734
  // --- 连接符 ---
737
735
  spacePos = -1;
738
736
  }
739
- if (spacePos !== -1) {
740
- const spaceRight = str.slice(spacePos + 1);
741
- if (/^[a-zA-Z_`"][\w`"]*$/.test(spaceRight)) {
742
- // --- OK ---
743
- left = str.slice(0, spacePos);
744
- right = spaceRight;
745
- }
746
- else {
747
- left = str;
748
- right = '';
749
- }
737
+ if (spacePos !== -1 && /^[a-zA-Z_`"][\w`"]*$/.test(str.slice(spacePos + 1))) {
738
+ left = str.slice(0, spacePos);
739
+ right = str.slice(spacePos + 1);
750
740
  }
751
741
  else {
752
742
  left = str;
753
- right = '';
754
743
  }
755
744
  }
756
745
  else {
@@ -760,90 +749,60 @@ export class Sql {
760
749
  }
761
750
  if (right) {
762
751
  // --- 处理右侧 ---
752
+ const hasQuote = right.startsWith(q);
753
+ if (!left.includes('.')) {
754
+ // --- 存储表别名 ---
755
+ this._alias.push(hasQuote ? right.slice(1, -1) : right);
756
+ }
763
757
  if (this._service === ESERVICE.MYSQL) {
764
- if (right.startsWith('`')) {
765
- if (!left.includes('.')) {
766
- // --- 存储表别名 ---
767
- this._alias.push(right.slice(1, -1));
768
- }
769
- right = '`' + pre + right.slice(1);
770
- }
771
- else {
772
- if (!left.includes('.')) {
773
- // --- 存储表别名 ---
774
- this._alias.push(right);
775
- }
776
- right = '`' + pre + right + '`';
777
- }
778
- right = ' AS ' + right;
758
+ right = ' AS `' + pre + (hasQuote ? right.slice(1) : right + '`');
779
759
  }
780
760
  else {
781
- if (right.startsWith('"')) {
782
- if (!left.includes('.')) {
783
- // --- 存储表别名 ---
784
- this._alias.push(right.slice(1, -1));
785
- }
786
- if (pre) {
787
- right = `"${pre}".${right}`;
788
- }
789
- }
790
- else {
791
- if (!left.includes('.')) {
792
- // --- 存储表别名 ---
793
- this._alias.push(right);
794
- }
795
- right = (pre ? `"${pre}".` : '') + `"${right}"`;
796
- }
797
- right = ' AS ' + right;
761
+ right = ' AS ' + (pre ? `"${pre}".` : '') + (hasQuote ? right : `"${right}"`);
798
762
  }
799
763
  }
800
- else {
764
+ else if (sufAs) {
801
765
  // --- 没有右侧 ---
802
- if (sufAs) {
803
- // --- 强制 AS ---
804
- if (!left.includes('.')) {
805
- // --- 存储表别名 ---
806
- this._alias.push(left.startsWith('`') || left.startsWith('"') ? left.slice(1, -1) : left);
807
- }
808
- right = ' AS ' + this.field(left, pre);
766
+ // --- 强制 AS ---
767
+ if (!left.includes('.')) {
768
+ // --- 存储表别名 ---
769
+ this._alias.push(left.startsWith(q) ? left.slice(1, -1) : left);
809
770
  }
771
+ right = ' AS ' + this.field(left, pre);
810
772
  }
811
773
  // --- 处理 left ---
812
774
  if (/^[\w`"_.*]+$/.test(left)) {
775
+ // --- 简单字段或表名 ---
776
+ /** --- 分左右 --- */
813
777
  const l = left.split('.');
814
778
  if (l[0] === '*') {
815
779
  return '*' + right;
816
780
  }
817
- if (l[0].startsWith('`') || l[0].startsWith(`"`)) {
818
- l[0] = l[0].replace(/[`"]/g, '');
819
- }
781
+ l[0] = l[0].replace(this._service === ESERVICE.MYSQL ? /`/g : /"/g, '');
820
782
  if (l[1] === undefined) {
821
- // --- xxx ---
783
+ // --- xxx,代表无 . 右侧 ---
784
+ if ((this._service === ESERVICE.MYSQL && l[0].startsWith('"')) ||
785
+ (this._service === ESERVICE.PGSQL && l[0].startsWith(`'`))) {
786
+ // --- 字符串,无需包裹 ---
787
+ return l[0] + right;
788
+ }
822
789
  if (/^[A-Z0-9_]+$/.test(l[0])) {
823
790
  // --- 纯大写是内置函数,不能加 `" ---
824
791
  return l[0] + right;
825
792
  }
826
793
  return this._service === ESERVICE.MYSQL ?
827
- '`' + pre + l[0] + suf + '`' + right :
828
- (pre ? `"${pre}".` : '') + '"' + l[0] + suf + '"' + right;
794
+ `\`${pre}${l[0]}${suf}\`${right}` :
795
+ (pre ? `"${pre}".` : '') + `"${l[0]}${suf}"${right}`;
829
796
  }
830
797
  // --- x.xxx ---
831
798
  // --- 只有在此模式才知道 . 前面的一定是表名,因此自动加 sql 级的 _pre ---
832
- const w = l[1] === '*'
833
- ? '*' :
834
- (q + ((l[1].startsWith('`') || l[1].startsWith('"')) ?
835
- l[1].slice(1, -1) :
836
- l[1]) + q);
799
+ const w = l[1] === '*' ? '*' : q + (l[1].startsWith(q) ? l[1].slice(1, -1) : l[1]) + q;
837
800
  return this._service === ESERVICE.MYSQL ?
838
- '`' + this._pre + l[0] + suf + '`.' + w + right :
839
- (this._pre ? `"${this._pre}".` : '') + '"' + l[0] + suf + '".' + w + right;
840
- }
841
- else {
842
- // return left.replace(/([(, ])([a-zA-Z`"_][\w`"_.]*)(?=[), ])/g, (
843
- return left.replace(/(^|[(, ])([a-zA-Z`"_][\w`"_.]*)(?=[), ]|$)/g, (t, t1, t2) => {
844
- return t1 + this.field(t2, pre, suf);
845
- }) + right;
801
+ `\`${this._pre}${l[0]}${suf}\`.${w}${right}` :
802
+ (this._pre ? `"${this._pre}".` : '') + `"${l[0]}${suf}".${w}${right}`;
846
803
  }
804
+ // --- 复杂字段或表达式 ---
805
+ return left.replace(/(^|[(, ])([a-zA-Z`"_][\w`"_.]*)(?=[), ]|$)/g, (t, t1, t2) => t1 + this.field(t2, pre, suf)) + right;
847
806
  }
848
807
  /**
849
808
  * --- 判断传入值是否是 field,还是别的对象 ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "6.2.0",
3
+ "version": "6.2.2",
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": [
@@ -13,7 +13,7 @@
13
13
  "license": "Apache-2.0",
14
14
  "types": "./index.d.ts",
15
15
  "scripts": {
16
- "build-md-doc": "bash ./utils/generate-api-doc-md.sh"
16
+ "build-doc": "bash ./utils/build-doc.sh"
17
17
  },
18
18
  "bin": {
19
19
  "kebab": "./bin/kebab.js"
@@ -38,14 +38,14 @@
38
38
  "pg": "^8.16.3",
39
39
  "ssh2": "^1.17.0",
40
40
  "svg-captcha": "^1.4.0",
41
- "tencentcloud-sdk-nodejs": "^4.1.149"
41
+ "tencentcloud-sdk-nodejs": "^4.1.152"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@litert/eslint-plugin-rules": "^0.3.1",
45
45
  "@types/ejs": "^3.1.5",
46
46
  "@types/node": "^24.10.1",
47
47
  "@types/pg": "^8.15.6",
48
- "typedoc": "^0.28.14",
48
+ "typedoc": "^0.28.15",
49
49
  "typedoc-plugin-markdown": "^4.9.0",
50
50
  "typescript": "^5.9.3"
51
51
  }
package/sys/mod.d.ts CHANGED
@@ -78,7 +78,6 @@ export default class Mod {
78
78
  'key': string;
79
79
  'list': string[];
80
80
  };
81
- 'raw'?: boolean;
82
81
  'pre'?: string;
83
82
  });
84
83
  /** --- 创建字段对象 --- */
@@ -118,7 +117,6 @@ export default class Mod {
118
117
  * @param opt 选项
119
118
  */
120
119
  static removeByWhere(db: lDb.Pool | lDb.Transaction, where: string | kebab.Json, opt?: {
121
- 'raw'?: boolean;
122
120
  'pre'?: string;
123
121
  'ctr'?: sCtr.Ctr;
124
122
  'index'?: string | string[];
@@ -132,7 +130,6 @@ export default class Mod {
132
130
  * @param opt 选项
133
131
  */
134
132
  static removeByWhereSql(db: lDb.Pool | lDb.Transaction, where: string | kebab.Json, opt?: {
135
- 'raw'?: boolean;
136
133
  'pre'?: string;
137
134
  'ctr'?: sCtr.Ctr;
138
135
  'index'?: string;
@@ -147,7 +144,6 @@ export default class Mod {
147
144
  * @param opt 选项
148
145
  */
149
146
  static updateByWhere(db: lDb.Pool | lDb.Transaction, data: kebab.Json, where: string | kebab.Json, opt?: {
150
- 'raw'?: boolean;
151
147
  'pre'?: string;
152
148
  'ctr'?: sCtr.Ctr;
153
149
  'index'?: string | string[];
@@ -162,7 +158,6 @@ export default class Mod {
162
158
  * @param opt 选项
163
159
  */
164
160
  static updateByWhereSql(db: lDb.Pool | lDb.Transaction, data: kebab.Json, where: string | kebab.Json, opt?: {
165
- 'raw'?: boolean;
166
161
  'pre'?: string;
167
162
  'ctr'?: sCtr.Ctr;
168
163
  'index'?: string;
@@ -205,7 +200,6 @@ export default class Mod {
205
200
  */
206
201
  static where<T extends Mod>(db: lDb.Pool | lDb.Transaction, s?: string | kebab.Json, opt?: {
207
202
  'ctr'?: sCtr.Ctr;
208
- 'raw'?: boolean;
209
203
  'pre'?: string;
210
204
  'index'?: string | string[];
211
205
  'alias'?: string;
@@ -233,7 +227,6 @@ export default class Mod {
233
227
  static find<T extends Mod>(db: lDb.Pool | lDb.Transaction, val: string | number | null, opt?: {
234
228
  'ctr'?: sCtr.Ctr;
235
229
  'lock'?: boolean;
236
- 'raw'?: boolean;
237
230
  'pre'?: string;
238
231
  'index'?: string | string[];
239
232
  /** --- 通过 key 字段获取,默认为 false,即从主键获取 --- */
@@ -241,7 +234,6 @@ export default class Mod {
241
234
  }): Promise<false | null | (T & Record<string, any>)>;
242
235
  static one(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt: {
243
236
  'ctr'?: sCtr.Ctr;
244
- 'raw'?: boolean;
245
237
  'pre'?: string;
246
238
  'index'?: string | string[];
247
239
  'select'?: string | string[];
@@ -250,7 +242,6 @@ export default class Mod {
250
242
  }): Promise<false | null | Record<string, any>>;
251
243
  static one<T extends Mod>(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt: {
252
244
  'ctr'?: sCtr.Ctr;
253
- 'raw'?: boolean;
254
245
  'pre'?: string;
255
246
  'index'?: string | string[];
256
247
  'select'?: string | string[];
@@ -265,7 +256,6 @@ export default class Mod {
265
256
  */
266
257
  static oneArray(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt?: {
267
258
  'ctr'?: sCtr.Ctr;
268
- 'raw'?: boolean;
269
259
  'pre'?: string;
270
260
  'index'?: string | string[];
271
261
  'select'?: string | string[];
@@ -278,7 +268,6 @@ export default class Mod {
278
268
  */
279
269
  static primarys(db: lDb.Pool | lDb.Transaction, where?: string | kebab.Json, opt?: {
280
270
  'ctr'?: sCtr.Ctr;
281
- 'raw'?: boolean;
282
271
  'pre'?: string;
283
272
  'index'?: string;
284
273
  }): Promise<any[] | false>;
package/sys/mod.js CHANGED
@@ -374,7 +374,6 @@ export default class Mod {
374
374
  'ctr': opt.ctr,
375
375
  'pre': opt.pre,
376
376
  'where': s,
377
- 'raw': opt.raw,
378
377
  'index': opt.index,
379
378
  'contain': opt.contain,
380
379
  'alias': opt.alias,
@@ -411,7 +410,6 @@ export default class Mod {
411
410
  'where': [{
412
411
  [opt.key ? this._$key : this._$primary]: val,
413
412
  }],
414
- 'raw': opt.raw,
415
413
  'index': opt.index,
416
414
  }).first(opt.lock);
417
415
  }
@@ -430,7 +428,6 @@ export default class Mod {
430
428
  'ctr': opt.ctr,
431
429
  'pre': opt.pre,
432
430
  'where': s,
433
- 'raw': opt.raw
434
431
  });
435
432
  if (opt.by) {
436
433
  o.by(opt.by[0], opt.by[1]);
@@ -445,7 +442,6 @@ export default class Mod {
445
442
  'ctr': opt.ctr,
446
443
  'pre': opt.pre,
447
444
  'where': s,
448
- 'raw': opt.raw,
449
445
  'index': item,
450
446
  });
451
447
  if (opt.by) {
@@ -2422,9 +2422,12 @@ Result:<pre id="result">Nothing.</pre>`);
2422
2422
  <b>getSql() :</b> ${s}<br>
2423
2423
  <b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
2424
2424
  <b>format() :</b> ${sql.format(s, sd)}<hr>`);
2425
- s = sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \'%Y-%m\') as time'], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') }).getSql();
2425
+ const data = this._get['s'] === 'pgsql' ?
2426
+ [`IFNULL(user.nick, '') nick`, `IFNULL(user.nick, 'a2') nick2`] :
2427
+ [`IFNULL(user.nick, "") nick`, `IFNULL(user.nick, "a2") nick2`];
2428
+ s = sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \'%Y-%m\') as time', ...data], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') }).getSql();
2426
2429
  sd = sql.getData();
2427
- echo.push(`<pre>sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \\'%Y-%m\\') as time'], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') });</pre>
2430
+ echo.push(`<pre>sql.select(['SUM(user.age) age', 'UTC_TIMESTAMP', 'FROM_UNIXTIME(user.time, \\'%Y-%m\\') as time', \`${data[0]}\`, \`${data[1]}\`], 'order').leftJoin('user', { 'order.user_id': lSql.column('user.id') });</pre>
2428
2431
  <b>getSql() :</b> ${s}<br>
2429
2432
  <b>getData():</b> <pre>${JSON.stringify(sd, undefined, 4)}</pre>
2430
2433
  <b>format() :</b> ${sql.format(s, sd)}<hr>`);