baja-lite 1.0.10 → 1.0.20

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.
@@ -1,2 +1,3 @@
1
1
  import { GlobalSqlOptionForWeb } from './sql';
2
2
  export declare const BootRomote: (options: GlobalSqlOptionForWeb) => Promise<void>;
3
+ export declare const AppendRomote: (dbName: string) => Promise<void>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BootRomote = void 0;
3
+ exports.AppendRomote = exports.BootRomote = void 0;
4
4
  const enum_1 = require("./enum");
5
5
  const sql_1 = require("./sql");
6
6
  const BootRomote = async function (options) {
@@ -17,7 +17,7 @@ const BootRomote = async function (options) {
17
17
  if (options.enums) {
18
18
  globalThis[sql_1._enums] = (0, enum_1.getEnums)(options.enums);
19
19
  }
20
- if (options.SqliteRemote) {
20
+ if (options.SqliteRemote && options.SqliteRemote.db) {
21
21
  if (typeof options.SqliteRemote.db === 'string') {
22
22
  await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
23
23
  globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] = new sql_1.SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
@@ -37,3 +37,14 @@ const BootRomote = async function (options) {
37
37
  }
38
38
  };
39
39
  exports.BootRomote = BootRomote;
40
+ const AppendRomote = async function (dbName) {
41
+ if (!globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][dbName]) {
42
+ await globalThis[sql_1._GlobalSqlOption].SqliteRemote.service.initDB(dbName);
43
+ const db = new sql_1.SqliteRemote(globalThis[sql_1._GlobalSqlOption].SqliteRemote.service, dbName);
44
+ if (globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] === undefined) {
45
+ globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] = db;
46
+ }
47
+ globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][dbName] = db;
48
+ }
49
+ };
50
+ exports.AppendRomote = AppendRomote;
package/cjs/boot.js CHANGED
@@ -92,7 +92,7 @@ const Boot = async function (options) {
92
92
  }
93
93
  }
94
94
  }
95
- if (options.SqliteRemote) {
95
+ if (options.SqliteRemote && options.SqliteRemote.db) {
96
96
  if (typeof options.SqliteRemote.db === 'string') {
97
97
  await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
98
98
  globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] = new sql_1.SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
package/cjs/sql.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { XML } from './convert-xml';
2
2
  import { ArrayList } from './list';
3
3
  import { EnumMap } from 'enum';
4
+ import { ExtensionCodec } from "@msgpack/msgpack";
5
+ export declare const extensionCodec: ExtensionCodec<undefined>;
4
6
  declare const _daoDBName: unique symbol;
5
7
  declare const _tableName: unique symbol;
6
8
  declare const _className: unique symbol;
@@ -257,7 +259,7 @@ export interface GlobalSqlOptionForWeb {
257
259
  ```
258
260
  不支持 `SqliteMemory`
259
261
  */
260
- db: Record<string, string> | string;
262
+ db?: Record<string, string> | string;
261
263
  /** 远程SQLITE接口实现,适用于Electron, 采用Ipc 的handel机制实现 */
262
264
  service: SqliteRemoteInterface;
263
265
  };
@@ -1682,9 +1684,15 @@ declare class StreamQuery<T extends object> {
1682
1684
  }): PageQuery<L> | Promise<PageQuery<L>>;
1683
1685
  excuteUpdate(option?: MethodOption & {
1684
1686
  sync?: SyncMode.Async;
1687
+ skipUndefined?: boolean;
1688
+ skipNull?: boolean;
1689
+ skipEmptyString?: boolean;
1685
1690
  }): Promise<number>;
1686
1691
  excuteUpdate(option: MethodOption & {
1687
1692
  sync: SyncMode.Sync;
1693
+ skipUndefined?: boolean;
1694
+ skipNull?: boolean;
1695
+ skipEmptyString?: boolean;
1688
1696
  }): number;
1689
1697
  excuteDelete(option?: MethodOption & {
1690
1698
  sync?: SyncMode.Async;
package/cjs/sql.js CHANGED
@@ -37,7 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  var _b, _c, _d;
38
38
  var _e, _f, _g, _h;
39
39
  Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.SqlService = exports.DB = exports.Field = exports.SqlCache = exports.SqliteRemote = exports.SqliteRemoteConnection = exports.Sqlite = exports.Mysql = exports._defOption = exports.SqliteMemory = exports.ColumnMode = exports.SqlType = exports.SelectResult = exports.TemplateResult = exports.SelectMode = exports.DeleteMode = exports.InsertMode = exports.SyncMode = exports.MapperIfUndefined = exports.DBType = exports.logger = exports._fs = exports._path = exports._EventBus = exports._GlobalSqlOption = exports._Hump = exports._enums = exports._primaryDB = exports._dao = exports._sqlCache = void 0;
40
+ exports.SqlService = exports.DB = exports.Field = exports.SqlCache = exports.SqliteRemote = exports.SqliteRemoteConnection = exports.Sqlite = exports.Mysql = exports._defOption = exports.SqliteMemory = exports.ColumnMode = exports.SqlType = exports.SelectResult = exports.TemplateResult = exports.SelectMode = exports.DeleteMode = exports.InsertMode = exports.SyncMode = exports.MapperIfUndefined = exports.DBType = exports.logger = exports._fs = exports._path = exports._EventBus = exports._GlobalSqlOption = exports._Hump = exports._enums = exports._primaryDB = exports._dao = exports._sqlCache = exports.extensionCodec = void 0;
41
41
  exports.flatData = flatData;
42
42
  exports.DeclareClass = DeclareClass;
43
43
  exports.DeclareService = DeclareService;
@@ -65,6 +65,31 @@ const list_1 = require("./list");
65
65
  const lodash_get_1 = __importDefault(require("lodash.get"));
66
66
  const msgpack_1 = require("@msgpack/msgpack");
67
67
  BigInt.prototype.toJSON = function () { return this.toString(); };
68
+ const BIGINT_EXT_TYPE = 0;
69
+ exports.extensionCodec = new msgpack_1.ExtensionCodec();
70
+ exports.extensionCodec.register({
71
+ type: BIGINT_EXT_TYPE,
72
+ encode(input) {
73
+ if (typeof input === "bigint") {
74
+ if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
75
+ return (0, msgpack_1.encode)(Number(input));
76
+ }
77
+ else {
78
+ return (0, msgpack_1.encode)(String(input));
79
+ }
80
+ }
81
+ else {
82
+ return null;
83
+ }
84
+ },
85
+ decode(data) {
86
+ const val = (0, msgpack_1.decode)(data);
87
+ if (!(typeof val === "string" || typeof val === "number")) {
88
+ throw new msgpack_1.DecodeError(`unexpected BigInt source: ${val} (${typeof val})`);
89
+ }
90
+ return BigInt(val);
91
+ },
92
+ });
68
93
  // #region 常量
69
94
  const _daoDBName = Symbol('dbName');
70
95
  const _tableName = Symbol('tableName');
@@ -781,8 +806,8 @@ class SqliteRemoteConnection {
781
806
  }
782
807
  return new Promise(async (resolve, reject) => {
783
808
  try {
784
- const data = await this[_daoConnection].execute((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params]));
785
- const { affectedRows, insertId } = (0, msgpack_1.decode)(data);
809
+ const data = await this[_daoConnection].execute((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
810
+ const { affectedRows, insertId } = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
786
811
  resolve({ affectedRows, insertId: insertId ? BigInt(insertId) : 0n });
787
812
  }
788
813
  catch (error) {
@@ -811,8 +836,8 @@ class SqliteRemoteConnection {
811
836
  }
812
837
  return new Promise(async (resolve, reject) => {
813
838
  try {
814
- const data = await this[_daoConnection].pluck((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params]));
815
- const r = (0, msgpack_1.decode)(data);
839
+ const data = await this[_daoConnection].pluck((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
840
+ const r = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
816
841
  resolve(r);
817
842
  }
818
843
  catch (error) {
@@ -841,8 +866,8 @@ class SqliteRemoteConnection {
841
866
  }
842
867
  return new Promise(async (resolve, reject) => {
843
868
  try {
844
- const data = await this[_daoConnection].pluck((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params]));
845
- const r = (0, msgpack_1.decode)(data);
869
+ const data = await this[_daoConnection].get((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
870
+ const r = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
846
871
  resolve(r);
847
872
  }
848
873
  catch (error) {
@@ -871,8 +896,8 @@ class SqliteRemoteConnection {
871
896
  }
872
897
  return new Promise(async (resolve, reject) => {
873
898
  try {
874
- const data = await this[_daoConnection].pluck((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params]));
875
- const r = (0, msgpack_1.decode)(data);
899
+ const data = await this[_daoConnection].raw((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
900
+ const r = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
876
901
  resolve(r);
877
902
  }
878
903
  catch (error) {
@@ -901,8 +926,8 @@ class SqliteRemoteConnection {
901
926
  }
902
927
  return new Promise(async (resolve, reject) => {
903
928
  try {
904
- const data = await this[_daoConnection].pluck((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params]));
905
- const r = (0, msgpack_1.decode)(data);
929
+ const data = await this[_daoConnection].query((0, msgpack_1.encode)([this[_sqliteRemoteName], sql, params], { extensionCodec: exports.extensionCodec }));
930
+ const r = (0, msgpack_1.decode)(data, { extensionCodec: exports.extensionCodec });
906
931
  resolve(r);
907
932
  }
908
933
  catch (error) {
@@ -1111,7 +1136,7 @@ class Build {
1111
1136
  datas[i] = `${datas[i]} ${datas[i].replace(/[a-zA-Z0-9]+\./, '').replace(/_([a-z])/g, (a, b, c) => b.toUpperCase())}`;
1112
1137
  }
1113
1138
  }
1114
- return datas.join(',');
1139
+ return ` ${datas.join(',')} `;
1115
1140
  };
1116
1141
  }
1117
1142
  /**
@@ -1925,9 +1950,6 @@ const Field = (config) => {
1925
1950
  if (hasDef) {
1926
1951
  __def[propertyName] = field.def;
1927
1952
  }
1928
- if (field.comment) {
1929
- __def[propertyName] = field.comment;
1930
- }
1931
1953
  Reflect.defineMetadata(_fields, __fields, object);
1932
1954
  Reflect.defineMetadata(_columns, __columns, object);
1933
1955
  Reflect.defineMetadata(_columnsNoId, __columnsNoId, object);
@@ -2577,8 +2599,7 @@ class SqlService {
2577
2599
  return result[0];
2578
2600
  }
2579
2601
  case TemplateResult.NotSureOne: {
2580
- error_1.Throw.if(!result, error);
2581
- return result[0] ?? null;
2602
+ return result && result.length > 0 ? result[0] ?? null : null;
2582
2603
  }
2583
2604
  case TemplateResult.Many: {
2584
2605
  return result;
@@ -2761,26 +2782,20 @@ class SqlService {
2761
2782
  const params = [];
2762
2783
  const sql = (0, sql_formatter_1.formatDialect)(option.sql?.replace(/\:([A-Za-z0-9._]+)/g, (txt, key) => {
2763
2784
  let V = (0, lodash_get_1.default)(_params, key);
2764
- if (V) {
2765
- if (V !== undefined) {
2766
- params.push(V);
2767
- }
2785
+ if (V !== undefined) {
2786
+ params.push(V);
2768
2787
  return '?';
2769
2788
  }
2770
2789
  const _key = (0, object_1.C2P)(key);
2771
2790
  V = (0, lodash_get_1.default)(_params, _key);
2772
- if (V) {
2773
- if (V !== undefined) {
2774
- params.push(V);
2775
- }
2791
+ if (V !== undefined) {
2792
+ params.push(V);
2776
2793
  return '?';
2777
2794
  }
2778
2795
  const __key = (0, object_1.P2C)(key);
2779
2796
  V = (0, lodash_get_1.default)(_params, __key);
2780
- if (V) {
2781
- if (V !== undefined) {
2782
- params.push(V);
2783
- }
2797
+ if (V !== undefined) {
2798
+ params.push(V);
2784
2799
  return '?';
2785
2800
  }
2786
2801
  return txt;
@@ -2808,26 +2823,20 @@ class SqlService {
2808
2823
  const params = [];
2809
2824
  const sql = (0, sql_formatter_1.formatDialect)(option.sql?.replace(/\:(\w+)/g, (txt, key) => {
2810
2825
  let V = (0, lodash_get_1.default)(_params, key);
2811
- if (V) {
2812
- if (V !== undefined) {
2813
- params.push(V);
2814
- }
2826
+ if (V !== undefined) {
2827
+ params.push(V);
2815
2828
  return '?';
2816
2829
  }
2817
2830
  const _key = (0, object_1.C2P)(key);
2818
2831
  V = (0, lodash_get_1.default)(_params, _key);
2819
- if (V) {
2820
- if (V !== undefined) {
2821
- params.push(V);
2822
- }
2832
+ if (V !== undefined) {
2833
+ params.push(V);
2823
2834
  return '?';
2824
2835
  }
2825
2836
  const __key = (0, object_1.P2C)(key);
2826
2837
  V = (0, lodash_get_1.default)(_params, __key);
2827
- if (V) {
2828
- if (V !== undefined) {
2829
- params.push(V);
2830
- }
2838
+ if (V !== undefined) {
2839
+ params.push(V);
2831
2840
  return '?';
2832
2841
  }
2833
2842
  return txt;
@@ -3501,11 +3510,11 @@ class StreamQuery {
3501
3510
  return this;
3502
3511
  }
3503
3512
  groupBy(...keys) { this._groups.push(...keys.map(key => `${this[_fields][String(key)]?.C2()}`)); return this; }
3504
- groupBy2(...keys) { this._groups.push(...keys); return this; }
3513
+ groupBy2(...keys) { this._groups.push(...keys.map(key => `${this[_fields][String(key)]?.C2()}`)); return this; }
3505
3514
  asc(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} ASC`)); return this; }
3506
- asc2(...keys) { this._orders.push(...keys.map(key => `${key} ASC`)); return this; }
3515
+ asc2(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} ASC`)); return this; }
3507
3516
  desc(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} DESC`)); return this; }
3508
- desc2(...keys) { this._orders.push(...keys.map(key => `${key} ASC`)); return this; }
3517
+ desc2(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} ASC`)); return this; }
3509
3518
  limit(startRow, pageSize) { this._startRow = startRow; this._pageSize = pageSize; return this; }
3510
3519
  page(pageNumber, pageSize) { this._startRow = ((pageNumber || 1) - 1) * pageSize; this._pageSize = pageSize; return this; }
3511
3520
  table(_table) { this._table = _table; return this; }
@@ -3527,9 +3536,16 @@ class StreamQuery {
3527
3536
  }
3528
3537
  select(...key) { this._columns.push(...(key.map(k => this[_fields][String(k)].C3()))); return this; }
3529
3538
  select2(sql, param) { this._columns.push(`${sql}`); Object.assign(this._param, param); return this; }
3530
- update(key, value) { this._updates ?? (this._updates = {}); this._updates[key] = value; return this; }
3539
+ update(key, value) { this._updates ?? (this._updates = {}); this._updates[this[_fields][String(key)]?.C2()] = value; return this; }
3531
3540
  update2(sql, param) { this._updateColumns.push(sql); Object.assign(this._param, param); return this; }
3532
- updateT(t) { this._updates ?? (this._updates = {}); Object.assign(this._updates, t); return this; }
3541
+ updateT(t) {
3542
+ this._updates ?? (this._updates = {});
3543
+ for (const [key, value] of Object.entries(t)) {
3544
+ this._updates[this[_fields][String(key)]?.C2()] = value;
3545
+ }
3546
+ Object.assign(this._updates, t);
3547
+ return this;
3548
+ }
3533
3549
  replace(key, valueToFind, valueToReplace) {
3534
3550
  const [pkey1, pkey2] = [`p${this._prefix}${this._index++}`, `p${this._prefix}${this._index++}`];
3535
3551
  this._updateColumns.push(` ${this[_fields][String(key)]?.C2()} = REPLACE(${this[_fields][String(key)]?.C2()}, :${pkey1}, :${pkey2}) `);
@@ -3679,12 +3695,15 @@ class StreamQuery {
3679
3695
  }
3680
3696
  }
3681
3697
  if (sets.length > 0) {
3682
- const sql = `UPDATE ${this._table} SET ${sets.join(',')} ${where}`;
3698
+ const sql = `UPDATE ${this._table} SET ${sets.join(',')}
3699
+ ${where ? ' WHERE ' : ''}
3700
+ ${where}
3701
+ `;
3683
3702
  if (option.sync === SyncMode.Async) {
3684
- return this._service.excute({ sync: SyncMode.Async, sql, params });
3703
+ return this._service.excute({ ...option, sync: SyncMode.Async, sql, params });
3685
3704
  }
3686
3705
  else {
3687
- return this._service.excute({ sync: SyncMode.Sync, sql, params });
3706
+ return this._service.excute({ ...option, sync: SyncMode.Sync, sql, params });
3688
3707
  }
3689
3708
  }
3690
3709
  else {
@@ -3695,12 +3714,15 @@ class StreamQuery {
3695
3714
  option ?? (option = {});
3696
3715
  option.sync ?? (option.sync = SyncMode.Async);
3697
3716
  const { where, params } = this._where();
3698
- const sql = `DELETE FROM ${this._table} ${where}`;
3717
+ const sql = `DELETE FROM ${this._table}
3718
+ ${where ? ' WHERE ' : ''}
3719
+ ${where}
3720
+ `;
3699
3721
  if (option.sync === SyncMode.Async) {
3700
- return this._service.excute({ sync: SyncMode.Async, sql, params });
3722
+ return this._service.excute({ ...option, sync: SyncMode.Async, sql, params });
3701
3723
  }
3702
3724
  else {
3703
- return this._service.excute({ sync: SyncMode.Sync, sql, params });
3725
+ return this._service.excute({ ...option, sync: SyncMode.Sync, sql, params });
3704
3726
  }
3705
3727
  }
3706
3728
  _where() {
package/cjs/sqlite.js CHANGED
@@ -39,7 +39,7 @@ class SqliteRemoteClass {
39
39
  sql_1.logger.debug(sql, params ?? '');
40
40
  try {
41
41
  if (!sql) {
42
- return (0, msgpack_1.encode)({ affectedRows: 0, insertId: 0n });
42
+ return (0, msgpack_1.encode)({ affectedRows: 0, insertId: 0n }, { extensionCodec: sql_1.extensionCodec });
43
43
  }
44
44
  ;
45
45
  if (this.trace) {
@@ -50,7 +50,7 @@ class SqliteRemoteClass {
50
50
  sql_1.logger.trace(result);
51
51
  }
52
52
  const { changes, lastInsertRowid } = result;
53
- return (0, msgpack_1.encode)({ affectedRows: changes, insertId: lastInsertRowid ? BigInt(lastInsertRowid) : 0n });
53
+ return (0, msgpack_1.encode)({ affectedRows: changes, insertId: lastInsertRowid ? BigInt(lastInsertRowid) : 0n }, { extensionCodec: sql_1.extensionCodec });
54
54
  }
55
55
  catch (error) {
56
56
  sql_1.logger.error(`
@@ -73,7 +73,7 @@ class SqliteRemoteClass {
73
73
  if (this.trace) {
74
74
  sql_1.logger.trace(sqlstring_1.default.format(sql, params));
75
75
  }
76
- return (0, msgpack_1.encode)(this.dbList[dbName].prepare(sql).pluck().get(params ?? {}));
76
+ return (0, msgpack_1.encode)(this.dbList[dbName].prepare(sql).pluck().get(params ?? {}), { extensionCodec: sql_1.extensionCodec });
77
77
  }
78
78
  catch (error) {
79
79
  sql_1.logger.error(`
@@ -91,7 +91,7 @@ class SqliteRemoteClass {
91
91
  if (this.trace) {
92
92
  sql_1.logger.trace(sqlstring_1.default.format(sql, params));
93
93
  }
94
- return (0, msgpack_1.encode)(this.dbList[dbName].prepare(sql).get(params ?? {}));
94
+ return (0, msgpack_1.encode)(this.dbList[dbName].prepare(sql).get(params ?? {}), { extensionCodec: sql_1.extensionCodec });
95
95
  }
96
96
  catch (error) {
97
97
  sql_1.logger.error(`
@@ -113,7 +113,7 @@ class SqliteRemoteClass {
113
113
  if (this.trace) {
114
114
  sql_1.logger.trace(sqlstring_1.default.format(sql, params));
115
115
  }
116
- return (0, msgpack_1.encode)(this.dbList[dbName].prepare(sql).raw().all(params ?? {}));
116
+ return (0, msgpack_1.encode)(this.dbList[dbName].prepare(sql).raw().all(params ?? {}), { extensionCodec: sql_1.extensionCodec });
117
117
  }
118
118
  catch (error) {
119
119
  sql_1.logger.error(`
@@ -135,7 +135,7 @@ class SqliteRemoteClass {
135
135
  if (this.trace) {
136
136
  sql_1.logger.trace(sqlstring_1.default.format(sql, params));
137
137
  }
138
- return (0, msgpack_1.encode)(this.dbList[dbName].prepare(sql).all(params ?? {}));
138
+ return (0, msgpack_1.encode)(this.dbList[dbName].prepare(sql).all(params ?? {}), { extensionCodec: sql_1.extensionCodec });
139
139
  }
140
140
  catch (error) {
141
141
  sql_1.logger.error(`
@@ -1,2 +1,3 @@
1
1
  import { GlobalSqlOptionForWeb } from './sql';
2
2
  export declare const BootRomote: (options: GlobalSqlOptionForWeb) => Promise<void>;
3
+ export declare const AppendRomote: (dbName: string) => Promise<void>;
package/es/boot-remote.js CHANGED
@@ -14,7 +14,7 @@ export const BootRomote = async function (options) {
14
14
  if (options.enums) {
15
15
  globalThis[_enums] = getEnums(options.enums);
16
16
  }
17
- if (options.SqliteRemote) {
17
+ if (options.SqliteRemote && options.SqliteRemote.db) {
18
18
  if (typeof options.SqliteRemote.db === 'string') {
19
19
  await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
20
20
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
@@ -33,3 +33,13 @@ export const BootRomote = async function (options) {
33
33
  }
34
34
  }
35
35
  };
36
+ export const AppendRomote = async function (dbName) {
37
+ if (!globalThis[_dao][DBType.SqliteRemote][dbName]) {
38
+ await globalThis[_GlobalSqlOption].SqliteRemote.service.initDB(dbName);
39
+ const db = new SqliteRemote(globalThis[_GlobalSqlOption].SqliteRemote.service, dbName);
40
+ if (globalThis[_dao][DBType.SqliteRemote][_primaryDB] === undefined) {
41
+ globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
42
+ }
43
+ globalThis[_dao][DBType.SqliteRemote][dbName] = db;
44
+ }
45
+ };
package/es/boot.js CHANGED
@@ -66,7 +66,7 @@ export const Boot = async function (options) {
66
66
  }
67
67
  }
68
68
  }
69
- if (options.SqliteRemote) {
69
+ if (options.SqliteRemote && options.SqliteRemote.db) {
70
70
  if (typeof options.SqliteRemote.db === 'string') {
71
71
  await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
72
72
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
package/es/sql.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { XML } from './convert-xml';
2
2
  import { ArrayList } from './list';
3
3
  import { EnumMap } from 'enum';
4
+ import { ExtensionCodec } from "@msgpack/msgpack";
5
+ export declare const extensionCodec: ExtensionCodec<undefined>;
4
6
  declare const _daoDBName: unique symbol;
5
7
  declare const _tableName: unique symbol;
6
8
  declare const _className: unique symbol;
@@ -257,7 +259,7 @@ export interface GlobalSqlOptionForWeb {
257
259
  ```
258
260
  不支持 `SqliteMemory`
259
261
  */
260
- db: Record<string, string> | string;
262
+ db?: Record<string, string> | string;
261
263
  /** 远程SQLITE接口实现,适用于Electron, 采用Ipc 的handel机制实现 */
262
264
  service: SqliteRemoteInterface;
263
265
  };
@@ -1682,9 +1684,15 @@ declare class StreamQuery<T extends object> {
1682
1684
  }): PageQuery<L> | Promise<PageQuery<L>>;
1683
1685
  excuteUpdate(option?: MethodOption & {
1684
1686
  sync?: SyncMode.Async;
1687
+ skipUndefined?: boolean;
1688
+ skipNull?: boolean;
1689
+ skipEmptyString?: boolean;
1685
1690
  }): Promise<number>;
1686
1691
  excuteUpdate(option: MethodOption & {
1687
1692
  sync: SyncMode.Sync;
1693
+ skipUndefined?: boolean;
1694
+ skipNull?: boolean;
1695
+ skipEmptyString?: boolean;
1688
1696
  }): number;
1689
1697
  excuteDelete(option?: MethodOption & {
1690
1698
  sync?: SyncMode.Async;
package/es/sql.js CHANGED
@@ -24,8 +24,33 @@ import HTML from 'html-parse-stringify';
24
24
  import { convert } from './convert-xml';
25
25
  import { ArrayList } from './list';
26
26
  import LGet from 'lodash.get';
27
- import { encode, decode } from "@msgpack/msgpack";
27
+ import { encode, decode, ExtensionCodec, DecodeError } from "@msgpack/msgpack";
28
28
  BigInt.prototype.toJSON = function () { return this.toString(); };
29
+ const BIGINT_EXT_TYPE = 0;
30
+ export const extensionCodec = new ExtensionCodec();
31
+ extensionCodec.register({
32
+ type: BIGINT_EXT_TYPE,
33
+ encode(input) {
34
+ if (typeof input === "bigint") {
35
+ if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
36
+ return encode(Number(input));
37
+ }
38
+ else {
39
+ return encode(String(input));
40
+ }
41
+ }
42
+ else {
43
+ return null;
44
+ }
45
+ },
46
+ decode(data) {
47
+ const val = decode(data);
48
+ if (!(typeof val === "string" || typeof val === "number")) {
49
+ throw new DecodeError(`unexpected BigInt source: ${val} (${typeof val})`);
50
+ }
51
+ return BigInt(val);
52
+ },
53
+ });
29
54
  // #region 常量
30
55
  const _daoDBName = Symbol('dbName');
31
56
  const _tableName = Symbol('tableName');
@@ -740,8 +765,8 @@ export class SqliteRemoteConnection {
740
765
  }
741
766
  return new Promise(async (resolve, reject) => {
742
767
  try {
743
- const data = await this[_daoConnection].execute(encode([this[_sqliteRemoteName], sql, params]));
744
- const { affectedRows, insertId } = decode(data);
768
+ const data = await this[_daoConnection].execute(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
769
+ const { affectedRows, insertId } = decode(data, { extensionCodec });
745
770
  resolve({ affectedRows, insertId: insertId ? BigInt(insertId) : 0n });
746
771
  }
747
772
  catch (error) {
@@ -770,8 +795,8 @@ export class SqliteRemoteConnection {
770
795
  }
771
796
  return new Promise(async (resolve, reject) => {
772
797
  try {
773
- const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params]));
774
- const r = decode(data);
798
+ const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
799
+ const r = decode(data, { extensionCodec });
775
800
  resolve(r);
776
801
  }
777
802
  catch (error) {
@@ -800,8 +825,8 @@ export class SqliteRemoteConnection {
800
825
  }
801
826
  return new Promise(async (resolve, reject) => {
802
827
  try {
803
- const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params]));
804
- const r = decode(data);
828
+ const data = await this[_daoConnection].get(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
829
+ const r = decode(data, { extensionCodec });
805
830
  resolve(r);
806
831
  }
807
832
  catch (error) {
@@ -830,8 +855,8 @@ export class SqliteRemoteConnection {
830
855
  }
831
856
  return new Promise(async (resolve, reject) => {
832
857
  try {
833
- const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params]));
834
- const r = decode(data);
858
+ const data = await this[_daoConnection].raw(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
859
+ const r = decode(data, { extensionCodec });
835
860
  resolve(r);
836
861
  }
837
862
  catch (error) {
@@ -860,8 +885,8 @@ export class SqliteRemoteConnection {
860
885
  }
861
886
  return new Promise(async (resolve, reject) => {
862
887
  try {
863
- const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params]));
864
- const r = decode(data);
888
+ const data = await this[_daoConnection].query(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
889
+ const r = decode(data, { extensionCodec });
865
890
  resolve(r);
866
891
  }
867
892
  catch (error) {
@@ -1068,7 +1093,7 @@ class Build {
1068
1093
  datas[i] = `${datas[i]} ${datas[i].replace(/[a-zA-Z0-9]+\./, '').replace(/_([a-z])/g, (a, b, c) => b.toUpperCase())}`;
1069
1094
  }
1070
1095
  }
1071
- return datas.join(',');
1096
+ return ` ${datas.join(',')} `;
1072
1097
  };
1073
1098
  }
1074
1099
  /**
@@ -1881,9 +1906,6 @@ export const Field = (config) => {
1881
1906
  if (hasDef) {
1882
1907
  __def[propertyName] = field.def;
1883
1908
  }
1884
- if (field.comment) {
1885
- __def[propertyName] = field.comment;
1886
- }
1887
1909
  Reflect.defineMetadata(_fields, __fields, object);
1888
1910
  Reflect.defineMetadata(_columns, __columns, object);
1889
1911
  Reflect.defineMetadata(_columnsNoId, __columnsNoId, object);
@@ -2531,8 +2553,7 @@ export class SqlService {
2531
2553
  return result[0];
2532
2554
  }
2533
2555
  case TemplateResult.NotSureOne: {
2534
- Throw.if(!result, error);
2535
- return result[0] ?? null;
2556
+ return result && result.length > 0 ? result[0] ?? null : null;
2536
2557
  }
2537
2558
  case TemplateResult.Many: {
2538
2559
  return result;
@@ -2715,26 +2736,20 @@ export class SqlService {
2715
2736
  const params = [];
2716
2737
  const sql = formatDialect(option.sql?.replace(/\:([A-Za-z0-9._]+)/g, (txt, key) => {
2717
2738
  let V = LGet(_params, key);
2718
- if (V) {
2719
- if (V !== undefined) {
2720
- params.push(V);
2721
- }
2739
+ if (V !== undefined) {
2740
+ params.push(V);
2722
2741
  return '?';
2723
2742
  }
2724
2743
  const _key = C2P(key);
2725
2744
  V = LGet(_params, _key);
2726
- if (V) {
2727
- if (V !== undefined) {
2728
- params.push(V);
2729
- }
2745
+ if (V !== undefined) {
2746
+ params.push(V);
2730
2747
  return '?';
2731
2748
  }
2732
2749
  const __key = P2C(key);
2733
2750
  V = LGet(_params, __key);
2734
- if (V) {
2735
- if (V !== undefined) {
2736
- params.push(V);
2737
- }
2751
+ if (V !== undefined) {
2752
+ params.push(V);
2738
2753
  return '?';
2739
2754
  }
2740
2755
  return txt;
@@ -2762,26 +2777,20 @@ export class SqlService {
2762
2777
  const params = [];
2763
2778
  const sql = formatDialect(option.sql?.replace(/\:(\w+)/g, (txt, key) => {
2764
2779
  let V = LGet(_params, key);
2765
- if (V) {
2766
- if (V !== undefined) {
2767
- params.push(V);
2768
- }
2780
+ if (V !== undefined) {
2781
+ params.push(V);
2769
2782
  return '?';
2770
2783
  }
2771
2784
  const _key = C2P(key);
2772
2785
  V = LGet(_params, _key);
2773
- if (V) {
2774
- if (V !== undefined) {
2775
- params.push(V);
2776
- }
2786
+ if (V !== undefined) {
2787
+ params.push(V);
2777
2788
  return '?';
2778
2789
  }
2779
2790
  const __key = P2C(key);
2780
2791
  V = LGet(_params, __key);
2781
- if (V) {
2782
- if (V !== undefined) {
2783
- params.push(V);
2784
- }
2792
+ if (V !== undefined) {
2793
+ params.push(V);
2785
2794
  return '?';
2786
2795
  }
2787
2796
  return txt;
@@ -3454,11 +3463,11 @@ class StreamQuery {
3454
3463
  return this;
3455
3464
  }
3456
3465
  groupBy(...keys) { this._groups.push(...keys.map(key => `${this[_fields][String(key)]?.C2()}`)); return this; }
3457
- groupBy2(...keys) { this._groups.push(...keys); return this; }
3466
+ groupBy2(...keys) { this._groups.push(...keys.map(key => `${this[_fields][String(key)]?.C2()}`)); return this; }
3458
3467
  asc(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} ASC`)); return this; }
3459
- asc2(...keys) { this._orders.push(...keys.map(key => `${key} ASC`)); return this; }
3468
+ asc2(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} ASC`)); return this; }
3460
3469
  desc(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} DESC`)); return this; }
3461
- desc2(...keys) { this._orders.push(...keys.map(key => `${key} ASC`)); return this; }
3470
+ desc2(...keys) { this._orders.push(...keys.map(key => `${this[_fields][String(key)]?.C2()} ASC`)); return this; }
3462
3471
  limit(startRow, pageSize) { this._startRow = startRow; this._pageSize = pageSize; return this; }
3463
3472
  page(pageNumber, pageSize) { this._startRow = ((pageNumber || 1) - 1) * pageSize; this._pageSize = pageSize; return this; }
3464
3473
  table(_table) { this._table = _table; return this; }
@@ -3480,9 +3489,16 @@ class StreamQuery {
3480
3489
  }
3481
3490
  select(...key) { this._columns.push(...(key.map(k => this[_fields][String(k)].C3()))); return this; }
3482
3491
  select2(sql, param) { this._columns.push(`${sql}`); Object.assign(this._param, param); return this; }
3483
- update(key, value) { this._updates ?? (this._updates = {}); this._updates[key] = value; return this; }
3492
+ update(key, value) { this._updates ?? (this._updates = {}); this._updates[this[_fields][String(key)]?.C2()] = value; return this; }
3484
3493
  update2(sql, param) { this._updateColumns.push(sql); Object.assign(this._param, param); return this; }
3485
- updateT(t) { this._updates ?? (this._updates = {}); Object.assign(this._updates, t); return this; }
3494
+ updateT(t) {
3495
+ this._updates ?? (this._updates = {});
3496
+ for (const [key, value] of Object.entries(t)) {
3497
+ this._updates[this[_fields][String(key)]?.C2()] = value;
3498
+ }
3499
+ Object.assign(this._updates, t);
3500
+ return this;
3501
+ }
3486
3502
  replace(key, valueToFind, valueToReplace) {
3487
3503
  const [pkey1, pkey2] = [`p${this._prefix}${this._index++}`, `p${this._prefix}${this._index++}`];
3488
3504
  this._updateColumns.push(` ${this[_fields][String(key)]?.C2()} = REPLACE(${this[_fields][String(key)]?.C2()}, :${pkey1}, :${pkey2}) `);
@@ -3632,12 +3648,15 @@ class StreamQuery {
3632
3648
  }
3633
3649
  }
3634
3650
  if (sets.length > 0) {
3635
- const sql = `UPDATE ${this._table} SET ${sets.join(',')} ${where}`;
3651
+ const sql = `UPDATE ${this._table} SET ${sets.join(',')}
3652
+ ${where ? ' WHERE ' : ''}
3653
+ ${where}
3654
+ `;
3636
3655
  if (option.sync === SyncMode.Async) {
3637
- return this._service.excute({ sync: SyncMode.Async, sql, params });
3656
+ return this._service.excute({ ...option, sync: SyncMode.Async, sql, params });
3638
3657
  }
3639
3658
  else {
3640
- return this._service.excute({ sync: SyncMode.Sync, sql, params });
3659
+ return this._service.excute({ ...option, sync: SyncMode.Sync, sql, params });
3641
3660
  }
3642
3661
  }
3643
3662
  else {
@@ -3648,12 +3667,15 @@ class StreamQuery {
3648
3667
  option ?? (option = {});
3649
3668
  option.sync ?? (option.sync = SyncMode.Async);
3650
3669
  const { where, params } = this._where();
3651
- const sql = `DELETE FROM ${this._table} ${where}`;
3670
+ const sql = `DELETE FROM ${this._table}
3671
+ ${where ? ' WHERE ' : ''}
3672
+ ${where}
3673
+ `;
3652
3674
  if (option.sync === SyncMode.Async) {
3653
- return this._service.excute({ sync: SyncMode.Async, sql, params });
3675
+ return this._service.excute({ ...option, sync: SyncMode.Async, sql, params });
3654
3676
  }
3655
3677
  else {
3656
- return this._service.excute({ sync: SyncMode.Sync, sql, params });
3678
+ return this._service.excute({ ...option, sync: SyncMode.Sync, sql, params });
3657
3679
  }
3658
3680
  }
3659
3681
  _where() {
package/es/sqlite.js CHANGED
@@ -1,4 +1,4 @@
1
- import { logger } from './sql';
1
+ import { extensionCodec, logger } from './sql';
2
2
  import Sqlstring from 'sqlstring';
3
3
  import { encode, decode } from "@msgpack/msgpack";
4
4
  export class SqliteRemoteClass {
@@ -10,7 +10,7 @@ export class SqliteRemoteClass {
10
10
  logger.debug(sql, params ?? '');
11
11
  try {
12
12
  if (!sql) {
13
- return encode({ affectedRows: 0, insertId: 0n });
13
+ return encode({ affectedRows: 0, insertId: 0n }, { extensionCodec });
14
14
  }
15
15
  ;
16
16
  if (this.trace) {
@@ -21,7 +21,7 @@ export class SqliteRemoteClass {
21
21
  logger.trace(result);
22
22
  }
23
23
  const { changes, lastInsertRowid } = result;
24
- return encode({ affectedRows: changes, insertId: lastInsertRowid ? BigInt(lastInsertRowid) : 0n });
24
+ return encode({ affectedRows: changes, insertId: lastInsertRowid ? BigInt(lastInsertRowid) : 0n }, { extensionCodec });
25
25
  }
26
26
  catch (error) {
27
27
  logger.error(`
@@ -44,7 +44,7 @@ export class SqliteRemoteClass {
44
44
  if (this.trace) {
45
45
  logger.trace(Sqlstring.format(sql, params));
46
46
  }
47
- return encode(this.dbList[dbName].prepare(sql).pluck().get(params ?? {}));
47
+ return encode(this.dbList[dbName].prepare(sql).pluck().get(params ?? {}), { extensionCodec });
48
48
  }
49
49
  catch (error) {
50
50
  logger.error(`
@@ -62,7 +62,7 @@ export class SqliteRemoteClass {
62
62
  if (this.trace) {
63
63
  logger.trace(Sqlstring.format(sql, params));
64
64
  }
65
- return encode(this.dbList[dbName].prepare(sql).get(params ?? {}));
65
+ return encode(this.dbList[dbName].prepare(sql).get(params ?? {}), { extensionCodec });
66
66
  }
67
67
  catch (error) {
68
68
  logger.error(`
@@ -84,7 +84,7 @@ export class SqliteRemoteClass {
84
84
  if (this.trace) {
85
85
  logger.trace(Sqlstring.format(sql, params));
86
86
  }
87
- return encode(this.dbList[dbName].prepare(sql).raw().all(params ?? {}));
87
+ return encode(this.dbList[dbName].prepare(sql).raw().all(params ?? {}), { extensionCodec });
88
88
  }
89
89
  catch (error) {
90
90
  logger.error(`
@@ -106,7 +106,7 @@ export class SqliteRemoteClass {
106
106
  if (this.trace) {
107
107
  logger.trace(Sqlstring.format(sql, params));
108
108
  }
109
- return encode(this.dbList[dbName].prepare(sql).all(params ?? {}));
109
+ return encode(this.dbList[dbName].prepare(sql).all(params ?? {}), { extensionCodec });
110
110
  }
111
111
  catch (error) {
112
112
  logger.error(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baja-lite",
3
- "version": "1.0.10",
3
+ "version": "1.0.20",
4
4
  "description": "some util for self",
5
5
  "homepage": "https://github.com/void-soul/util-man",
6
6
  "repository": {
@@ -30,35 +30,35 @@
30
30
  "sqlite": "node inspect ./dist/cjs/test-sqlite.js"
31
31
  },
32
32
  "dependencies": {
33
- "@msgpack/msgpack": "^3.0.0-beta2",
34
- "@types/lodash.get": "^4.4.9",
35
- "@types/shelljs": "^0.8.15",
33
+ "@msgpack/msgpack": "3.0.0-beta2",
34
+ "@types/lodash.get": "4.4.9",
35
+ "@types/shelljs": "0.8.15",
36
36
  "decimal.js": "10.4.3",
37
37
  "html-parse-stringify": "3.0.1",
38
38
  "iterare": "1.2.1",
39
- "lodash.get": "^4.4.2",
39
+ "lodash.get": "4.4.2",
40
40
  "mustache": "4.2.0",
41
- "pino": "9.2.0",
42
- "pino-pretty": "11.2.1",
41
+ "pino": "9.3.2",
42
+ "pino-pretty": "11.2.2",
43
43
  "reflect-metadata": "0.2.2",
44
- "sql-formatter": "15.3.2",
44
+ "sql-formatter": "15.4.0",
45
45
  "sqlstring": "2.3.3",
46
46
  "tslib": "2.6.3"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/better-sqlite3": "7.6.11",
50
50
  "@types/mustache": "4.2.5",
51
- "@types/node": "20.14.10",
51
+ "@types/node": "20.14.14",
52
52
  "@types/sqlstring": "2.3.2",
53
- "@typescript-eslint/eslint-plugin": "7.15.0",
54
- "@typescript-eslint/parser": "7.15.0",
55
- "better-sqlite3": "11.0.0",
53
+ "@typescript-eslint/eslint-plugin": "8.1.0",
54
+ "@typescript-eslint/parser": "8.1.0",
55
+ "better-sqlite3": "11.1.2",
56
56
  "ioredis": "5.4.1",
57
57
  "mongodb": "6.8.0",
58
- "mysql2": "3.10.2",
58
+ "mysql2": "3.11.0",
59
59
  "redlock": "5.0.0-beta.2",
60
60
  "shelljs": "0.8.5",
61
- "typescript": "5.5.3"
61
+ "typescript": "5.5.4"
62
62
  },
63
63
  "engines": {
64
64
  "node": ">=20"
@@ -15,7 +15,7 @@ export const BootRomote = async function (options: GlobalSqlOptionForWeb) {
15
15
  if (options.enums) {
16
16
  globalThis[_enums] = getEnums(options.enums);
17
17
  }
18
- if (options.SqliteRemote) {
18
+ if (options.SqliteRemote && options.SqliteRemote.db) {
19
19
  if (typeof options.SqliteRemote.db === 'string') {
20
20
  await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
21
21
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
@@ -33,3 +33,14 @@ export const BootRomote = async function (options: GlobalSqlOptionForWeb) {
33
33
  }
34
34
  }
35
35
  }
36
+
37
+ export const AppendRomote = async function (dbName: string) {
38
+ if (!globalThis[_dao][DBType.SqliteRemote][dbName]) {
39
+ await globalThis[_GlobalSqlOption].SqliteRemote.service.initDB(dbName);
40
+ const db = new SqliteRemote(globalThis[_GlobalSqlOption].SqliteRemote.service, dbName);
41
+ if (globalThis[_dao][DBType.SqliteRemote][_primaryDB] === undefined) {
42
+ globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
43
+ }
44
+ globalThis[_dao][DBType.SqliteRemote][dbName] = db;
45
+ }
46
+ }
package/src/boot.ts CHANGED
@@ -65,7 +65,7 @@ export const Boot = async function (options: GlobalSqlOption) {
65
65
  }
66
66
  }
67
67
  }
68
- if (options.SqliteRemote) {
68
+ if (options.SqliteRemote && options.SqliteRemote.db) {
69
69
  if (typeof options.SqliteRemote.db === 'string') {
70
70
  await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
71
71
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
@@ -130,4 +130,4 @@ export const Boot = async function (options: GlobalSqlOption) {
130
130
  });
131
131
  globalThis[_EventBus] = event;
132
132
  }
133
- }
133
+ }
package/src/sql.ts CHANGED
@@ -14,8 +14,34 @@ import { XML, convert } from './convert-xml';
14
14
  import { ArrayList } from './list';
15
15
  import LGet from 'lodash.get';
16
16
  import { EnumMap } from 'enum';
17
- import { encode, decode } from "@msgpack/msgpack";
17
+ import { encode, decode, ExtensionCodec, DecodeError } from "@msgpack/msgpack";
18
18
  (BigInt.prototype as any).toJSON = function () { return this.toString() }
19
+
20
+ const BIGINT_EXT_TYPE = 0;
21
+ export const extensionCodec = new ExtensionCodec();
22
+ extensionCodec.register({
23
+ type: BIGINT_EXT_TYPE,
24
+ encode(input: unknown): Uint8Array | null {
25
+ if (typeof input === "bigint") {
26
+ if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
27
+ return encode(Number(input));
28
+ } else {
29
+ return encode(String(input));
30
+ }
31
+ } else {
32
+ return null;
33
+ }
34
+ },
35
+ decode(data: Uint8Array): bigint {
36
+ const val = decode(data);
37
+ if (!(typeof val === "string" || typeof val === "number")) {
38
+ throw new DecodeError(`unexpected BigInt source: ${val} (${typeof val})`);
39
+ }
40
+ return BigInt(val);
41
+ },
42
+ });
43
+
44
+
19
45
  // #region 常量
20
46
  const _daoDBName = Symbol('dbName');
21
47
  const _tableName = Symbol('tableName');
@@ -293,7 +319,7 @@ export interface GlobalSqlOptionForWeb {
293
319
  ```
294
320
  不支持 `SqliteMemory`
295
321
  */
296
- db: Record<string, string> | string,
322
+ db?: Record<string, string> | string,
297
323
  /** 远程SQLITE接口实现,适用于Electron, 采用Ipc 的handel机制实现 */
298
324
  service: SqliteRemoteInterface
299
325
  },
@@ -1078,8 +1104,8 @@ export class SqliteRemoteConnection implements Connection {
1078
1104
  }
1079
1105
  return new Promise<{ affectedRows: number; insertId: bigint; }>(async (resolve, reject) => {
1080
1106
  try {
1081
- const data = await this[_daoConnection].execute(encode([this[_sqliteRemoteName], sql, params]));
1082
- const { affectedRows, insertId } = decode(data) as { affectedRows: number; insertId: bigint; };
1107
+ const data = await this[_daoConnection].execute(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
1108
+ const { affectedRows, insertId } = decode(data, { extensionCodec }) as { affectedRows: number; insertId: bigint; };
1083
1109
  resolve({ affectedRows, insertId: insertId ? BigInt(insertId) : 0n });
1084
1110
  } catch (error) {
1085
1111
  logger.error(`
@@ -1106,8 +1132,8 @@ export class SqliteRemoteConnection implements Connection {
1106
1132
  }
1107
1133
  return new Promise<T | null>(async (resolve, reject) => {
1108
1134
  try {
1109
- const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params]));
1110
- const r = decode(data) as T;
1135
+ const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
1136
+ const r = decode(data, { extensionCodec }) as T;
1111
1137
  resolve(r);
1112
1138
  } catch (error) {
1113
1139
  logger.error(`
@@ -1134,8 +1160,8 @@ export class SqliteRemoteConnection implements Connection {
1134
1160
  }
1135
1161
  return new Promise<T | null>(async (resolve, reject) => {
1136
1162
  try {
1137
- const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params]));
1138
- const r = decode(data) as T;
1163
+ const data = await this[_daoConnection].get(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
1164
+ const r = decode(data, { extensionCodec }) as T;
1139
1165
  resolve(r);
1140
1166
  } catch (error) {
1141
1167
  logger.error(`
@@ -1162,8 +1188,8 @@ export class SqliteRemoteConnection implements Connection {
1162
1188
  }
1163
1189
  return new Promise<T[]>(async (resolve, reject) => {
1164
1190
  try {
1165
- const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params]));
1166
- const r = decode(data) as T[];
1191
+ const data = await this[_daoConnection].raw(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
1192
+ const r = decode(data, { extensionCodec }) as T[];
1167
1193
  resolve(r);
1168
1194
  } catch (error) {
1169
1195
  logger.error(`
@@ -1190,8 +1216,8 @@ export class SqliteRemoteConnection implements Connection {
1190
1216
  }
1191
1217
  return new Promise<T[]>(async (resolve, reject) => {
1192
1218
  try {
1193
- const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params]));
1194
- const r = decode(data) as T[];
1219
+ const data = await this[_daoConnection].query(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
1220
+ const r = decode(data, { extensionCodec }) as T[];
1195
1221
  resolve(r);
1196
1222
  } catch (error) {
1197
1223
  logger.error(`
@@ -1444,7 +1470,7 @@ class Build {
1444
1470
  datas[i] = `${datas[i]} ${datas[i]!.replace(/[a-zA-Z0-9]+\./, '').replace(/_([a-z])/g, (a, b, c) => b.toUpperCase())}`;
1445
1471
  }
1446
1472
  }
1447
- return datas.join(',');
1473
+ return ` ${datas.join(',')} `;
1448
1474
  };
1449
1475
  }
1450
1476
  /**
@@ -2243,9 +2269,6 @@ export const Field = (config: FieldOption) => {
2243
2269
  if (hasDef) {
2244
2270
  __def[propertyName] = field.def;
2245
2271
  }
2246
- if (field.comment) {
2247
- __def[propertyName] = field.comment;
2248
- }
2249
2272
  Reflect.defineMetadata(_fields, __fields, object);
2250
2273
  Reflect.defineMetadata(_columns, __columns, object);
2251
2274
  Reflect.defineMetadata(_columnsNoId, __columnsNoId, object);
@@ -3000,8 +3023,7 @@ export class SqlService<T extends object> {
3000
3023
  return result[0] as T;
3001
3024
  }
3002
3025
  case TemplateResult.NotSureOne: {
3003
- Throw.if(!result, error);
3004
- return (result[0] as T) ?? null;
3026
+ return result && result.length > 0 ? (result[0] as T) ?? null : null;
3005
3027
  }
3006
3028
  case TemplateResult.Many: {
3007
3029
  return result;
@@ -3243,26 +3265,20 @@ export class SqlService<T extends object> {
3243
3265
  const params: any[] = [];
3244
3266
  const sql = formatDialect(option.sql?.replace(/\:([A-Za-z0-9._]+)/g, (txt, key) => {
3245
3267
  let V = LGet(_params, key);
3246
- if (V) {
3247
- if (V !== undefined) {
3248
- params.push(V);
3249
- }
3268
+ if (V !== undefined) {
3269
+ params.push(V);
3250
3270
  return '?';
3251
3271
  }
3252
3272
  const _key = C2P(key);
3253
3273
  V = LGet(_params, _key);
3254
- if (V) {
3255
- if (V !== undefined) {
3256
- params.push(V);
3257
- }
3274
+ if (V !== undefined) {
3275
+ params.push(V);
3258
3276
  return '?';
3259
3277
  }
3260
3278
  const __key = P2C(key);
3261
3279
  V = LGet(_params, __key);
3262
- if (V) {
3263
- if (V !== undefined) {
3264
- params.push(V);
3265
- }
3280
+ if (V !== undefined) {
3281
+ params.push(V);
3266
3282
  return '?';
3267
3283
  }
3268
3284
  return txt;
@@ -3307,26 +3323,20 @@ export class SqlService<T extends object> {
3307
3323
  const params: any[] = [];
3308
3324
  const sql = formatDialect(option.sql?.replace(/\:(\w+)/g, (txt, key) => {
3309
3325
  let V = LGet(_params, key);
3310
- if (V) {
3311
- if (V !== undefined) {
3312
- params.push(V);
3313
- }
3326
+ if (V !== undefined) {
3327
+ params.push(V);
3314
3328
  return '?';
3315
3329
  }
3316
3330
  const _key = C2P(key);
3317
3331
  V = LGet(_params, _key);
3318
- if (V) {
3319
- if (V !== undefined) {
3320
- params.push(V);
3321
- }
3332
+ if (V !== undefined) {
3333
+ params.push(V);
3322
3334
  return '?';
3323
3335
  }
3324
3336
  const __key = P2C(key);
3325
3337
  V = LGet(_params, __key);
3326
- if (V) {
3327
- if (V !== undefined) {
3328
- params.push(V);
3329
- }
3338
+ if (V !== undefined) {
3339
+ params.push(V);
3330
3340
  return '?';
3331
3341
  }
3332
3342
  return txt;
@@ -4034,15 +4044,15 @@ class StreamQuery<T extends object> {
4034
4044
  @IF_PROCEED<T>()
4035
4045
  groupBy(...keys: (keyof T)[]) { this._groups.push(...keys.map(key => `${this[_fields]![String(key)]?.C2()}`)); return this; }
4036
4046
  @IF_PROCEED<T>()
4037
- groupBy2(...keys: string[]) { this._groups.push(...keys); return this; }
4047
+ groupBy2(...keys: string[]) { this._groups.push(...keys.map(key => `${this[_fields]![String(key)]?.C2()}`)); return this; }
4038
4048
  @IF_PROCEED<T>()
4039
4049
  asc(...keys: (keyof T)[]) { this._orders.push(...keys.map(key => `${this[_fields]![String(key)]?.C2()} ASC`)); return this; }
4040
4050
  @IF_PROCEED<T>()
4041
- asc2(...keys: string[]) { this._orders.push(...keys.map(key => `${key} ASC`)); return this; }
4051
+ asc2(...keys: string[]) { this._orders.push(...keys.map(key => `${this[_fields]![String(key)]?.C2()} ASC`)); return this; }
4042
4052
  @IF_PROCEED<T>()
4043
4053
  desc(...keys: (keyof T)[]) { this._orders.push(...keys.map(key => `${this[_fields]![String(key)]?.C2()} DESC`)); return this; }
4044
4054
  @IF_PROCEED<T>()
4045
- desc2(...keys: string[]) { this._orders.push(...keys.map(key => `${key} ASC`)); return this; }
4055
+ desc2(...keys: string[]) { this._orders.push(...keys.map(key => `${this[_fields]![String(key)]?.C2()} ASC`)); return this; }
4046
4056
  @IF_PROCEED<T>()
4047
4057
  limit(startRow: number, pageSize: number) { this._startRow = startRow; this._pageSize = pageSize; return this; }
4048
4058
  @IF_PROCEED<T>()
@@ -4078,11 +4088,18 @@ class StreamQuery<T extends object> {
4078
4088
  @IF_PROCEED<T>()
4079
4089
  select2(sql: string, param?: Record<string, any>) { this._columns.push(`${sql}`); Object.assign(this._param, param); return this; }
4080
4090
  @IF_PROCEED<T>()
4081
- update(key: keyof T, value: T[keyof T]) { this._updates ??= {}; this._updates[key] = value; return this; }
4091
+ update(key: keyof T, value: T[keyof T]) { this._updates ??= {}; this._updates[this[_fields]![String(key)]?.C2()!] = value; return this; }
4082
4092
  @IF_PROCEED<T>()
4083
4093
  update2(sql: string, param?: Record<string, any>) { this._updateColumns.push(sql); Object.assign(this._param, param); return this; }
4084
4094
  @IF_PROCEED<T>()
4085
- updateT(t: Partial<T>) { this._updates ??= {}; Object.assign(this._updates, t); return this; }
4095
+ updateT(t: Partial<T>) {
4096
+ this._updates ??= {};
4097
+ for (const [key, value] of Object.entries(t)) {
4098
+ this._updates[this[_fields]![String(key)]?.C2()!] = value;
4099
+ }
4100
+ Object.assign(this._updates, t);
4101
+ return this;
4102
+ }
4086
4103
  @IF_PROCEED<T>()
4087
4104
  replace(key: keyof T, valueToFind: T[keyof T], valueToReplace: T[keyof T]) {
4088
4105
  const [pkey1, pkey2] = [`p${this._prefix}${this._index++}`, `p${this._prefix}${this._index++}`];
@@ -4227,10 +4244,10 @@ class StreamQuery<T extends object> {
4227
4244
  });
4228
4245
  }
4229
4246
  }
4230
- excuteUpdate(option?: MethodOption & { sync?: SyncMode.Async }): Promise<number>;
4231
- excuteUpdate(option: MethodOption & { sync: SyncMode.Sync }): number;
4247
+ excuteUpdate(option?: MethodOption & { sync?: SyncMode.Async; skipUndefined?: boolean; skipNull?: boolean; skipEmptyString?: boolean; }): Promise<number>;
4248
+ excuteUpdate(option: MethodOption & { sync: SyncMode.Sync; skipUndefined?: boolean; skipNull?: boolean; skipEmptyString?: boolean; }): number;
4232
4249
  @IF_EXEC<T>(0)
4233
- excuteUpdate(option?: MethodOption & { sync?: SyncMode }): number | Promise<number> {
4250
+ excuteUpdate(option?: MethodOption & { sync?: SyncMode; skipUndefined?: boolean; skipNull?: boolean; skipEmptyString?: boolean; }): number | Promise<number> {
4234
4251
  option ??= {};
4235
4252
  option.sync ??= SyncMode.Async;
4236
4253
  const { where, params } = this._where();
@@ -4243,11 +4260,14 @@ class StreamQuery<T extends object> {
4243
4260
  }
4244
4261
  }
4245
4262
  if (sets.length > 0) {
4246
- const sql = `UPDATE ${this._table} SET ${sets.join(',')} ${where}`;
4263
+ const sql = `UPDATE ${this._table} SET ${sets.join(',')}
4264
+ ${where ? ' WHERE ' : ''}
4265
+ ${where}
4266
+ `;
4247
4267
  if (option.sync === SyncMode.Async) {
4248
- return this._service.excute({ sync: SyncMode.Async, sql, params });
4268
+ return this._service.excute({ ...option, sync: SyncMode.Async, sql, params });
4249
4269
  } else {
4250
- return this._service.excute({ sync: SyncMode.Sync, sql, params });
4270
+ return this._service.excute({ ...option, sync: SyncMode.Sync, sql, params });
4251
4271
  }
4252
4272
  } else {
4253
4273
  return 0;
@@ -4260,11 +4280,14 @@ class StreamQuery<T extends object> {
4260
4280
  option ??= {};
4261
4281
  option.sync ??= SyncMode.Async;
4262
4282
  const { where, params } = this._where();
4263
- const sql = `DELETE FROM ${this._table} ${where}`;
4283
+ const sql = `DELETE FROM ${this._table}
4284
+ ${where ? ' WHERE ' : ''}
4285
+ ${where}
4286
+ `;
4264
4287
  if (option.sync === SyncMode.Async) {
4265
- return this._service.excute({ sync: SyncMode.Async, sql, params });
4288
+ return this._service.excute({ ...option, sync: SyncMode.Async, sql, params });
4266
4289
  } else {
4267
- return this._service.excute({ sync: SyncMode.Sync, sql, params });
4290
+ return this._service.excute({ ...option, sync: SyncMode.Sync, sql, params });
4268
4291
  }
4269
4292
  }
4270
4293
  private _where() {
package/src/sqlite.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SqliteRemoteInterface, logger } from './sql';
1
+ import { SqliteRemoteInterface, extensionCodec, logger } from './sql';
2
2
  import Sqlstring from 'sqlstring';
3
3
  import { encode, decode } from "@msgpack/msgpack";
4
4
  export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
@@ -27,7 +27,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
27
27
  const [dbName, sql, params] = decode(inData) as [dbName: string, sql?: string | undefined, params?: any];
28
28
  logger.debug(sql, params ?? '');
29
29
  try {
30
- if (!sql) { return encode({ affectedRows: 0, insertId: 0n }); };
30
+ if (!sql) { return encode({ affectedRows: 0, insertId: 0n }, { extensionCodec }); };
31
31
  if (this.trace) {
32
32
  logger.trace(Sqlstring.format(sql!, params));
33
33
  }
@@ -36,7 +36,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
36
36
  logger.trace(result);
37
37
  }
38
38
  const { changes, lastInsertRowid } = result;
39
- return encode({ affectedRows: changes, insertId: lastInsertRowid ? BigInt(lastInsertRowid) : 0n });
39
+ return encode({ affectedRows: changes, insertId: lastInsertRowid ? BigInt(lastInsertRowid) : 0n }, { extensionCodec });
40
40
  } catch (error) {
41
41
  logger.error(`
42
42
  error: ${error},
@@ -55,7 +55,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
55
55
  if (this.trace) {
56
56
  logger.trace(Sqlstring.format(sql!, params));
57
57
  }
58
- return encode(this.dbList[dbName].prepare(sql).pluck().get(params ?? {}));
58
+ return encode(this.dbList[dbName].prepare(sql).pluck().get(params ?? {}), { extensionCodec });
59
59
  } catch (error) {
60
60
  logger.error(`
61
61
  error: ${error},
@@ -72,7 +72,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
72
72
  if (this.trace) {
73
73
  logger.trace(Sqlstring.format(sql!, params));
74
74
  }
75
- return encode(this.dbList[dbName].prepare(sql).get(params ?? {}));
75
+ return encode(this.dbList[dbName].prepare(sql).get(params ?? {}), { extensionCodec });
76
76
  } catch (error) {
77
77
  logger.error(`
78
78
  error: ${error},
@@ -90,7 +90,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
90
90
  if (this.trace) {
91
91
  logger.trace(Sqlstring.format(sql!, params));
92
92
  }
93
- return encode(this.dbList[dbName].prepare(sql).raw().all(params ?? {}));
93
+ return encode(this.dbList[dbName].prepare(sql).raw().all(params ?? {}), { extensionCodec });
94
94
  } catch (error) {
95
95
  logger.error(`
96
96
  error: ${error},
@@ -108,7 +108,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
108
108
  if (this.trace) {
109
109
  logger.trace(Sqlstring.format(sql!, params));
110
110
  }
111
- return encode(this.dbList[dbName].prepare(sql).all(params ?? {}));
111
+ return encode(this.dbList[dbName].prepare(sql).all(params ?? {}), { extensionCodec });
112
112
  } catch (error) {
113
113
  logger.error(`
114
114
  error: ${error},