baja-lite 1.0.10 → 1.0.16
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/cjs/sql.d.ts +2 -0
- package/cjs/sql.js +45 -15
- package/cjs/sqlite.js +6 -6
- package/es/sql.d.ts +2 -0
- package/es/sql.js +45 -15
- package/es/sqlite.js +7 -7
- package/package.json +13 -13
- package/src/sql.ts +46 -15
- package/src/sqlite.ts +7 -7
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;
|
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].
|
|
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].
|
|
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].
|
|
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) {
|
|
@@ -2577,8 +2602,7 @@ class SqlService {
|
|
|
2577
2602
|
return result[0];
|
|
2578
2603
|
}
|
|
2579
2604
|
case TemplateResult.NotSureOne: {
|
|
2580
|
-
|
|
2581
|
-
return result[0] ?? null;
|
|
2605
|
+
return result && result.length > 0 ? result[0] ?? null : null;
|
|
2582
2606
|
}
|
|
2583
2607
|
case TemplateResult.Many: {
|
|
2584
2608
|
return result;
|
|
@@ -3679,7 +3703,10 @@ class StreamQuery {
|
|
|
3679
3703
|
}
|
|
3680
3704
|
}
|
|
3681
3705
|
if (sets.length > 0) {
|
|
3682
|
-
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
3706
|
+
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
3707
|
+
${where ? ' WHERE ' : ''}
|
|
3708
|
+
${where}
|
|
3709
|
+
`;
|
|
3683
3710
|
if (option.sync === SyncMode.Async) {
|
|
3684
3711
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3685
3712
|
}
|
|
@@ -3695,7 +3722,10 @@ class StreamQuery {
|
|
|
3695
3722
|
option ?? (option = {});
|
|
3696
3723
|
option.sync ?? (option.sync = SyncMode.Async);
|
|
3697
3724
|
const { where, params } = this._where();
|
|
3698
|
-
const sql = `DELETE FROM ${this._table}
|
|
3725
|
+
const sql = `DELETE FROM ${this._table}
|
|
3726
|
+
${where ? ' WHERE ' : ''}
|
|
3727
|
+
${where}
|
|
3728
|
+
`;
|
|
3699
3729
|
if (option.sync === SyncMode.Async) {
|
|
3700
3730
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3701
3731
|
}
|
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(`
|
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;
|
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].
|
|
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].
|
|
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].
|
|
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) {
|
|
@@ -2531,8 +2556,7 @@ export class SqlService {
|
|
|
2531
2556
|
return result[0];
|
|
2532
2557
|
}
|
|
2533
2558
|
case TemplateResult.NotSureOne: {
|
|
2534
|
-
|
|
2535
|
-
return result[0] ?? null;
|
|
2559
|
+
return result && result.length > 0 ? result[0] ?? null : null;
|
|
2536
2560
|
}
|
|
2537
2561
|
case TemplateResult.Many: {
|
|
2538
2562
|
return result;
|
|
@@ -3632,7 +3656,10 @@ class StreamQuery {
|
|
|
3632
3656
|
}
|
|
3633
3657
|
}
|
|
3634
3658
|
if (sets.length > 0) {
|
|
3635
|
-
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
3659
|
+
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
3660
|
+
${where ? ' WHERE ' : ''}
|
|
3661
|
+
${where}
|
|
3662
|
+
`;
|
|
3636
3663
|
if (option.sync === SyncMode.Async) {
|
|
3637
3664
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3638
3665
|
}
|
|
@@ -3648,7 +3675,10 @@ class StreamQuery {
|
|
|
3648
3675
|
option ?? (option = {});
|
|
3649
3676
|
option.sync ?? (option.sync = SyncMode.Async);
|
|
3650
3677
|
const { where, params } = this._where();
|
|
3651
|
-
const sql = `DELETE FROM ${this._table}
|
|
3678
|
+
const sql = `DELETE FROM ${this._table}
|
|
3679
|
+
${where ? ' WHERE ' : ''}
|
|
3680
|
+
${where}
|
|
3681
|
+
`;
|
|
3652
3682
|
if (option.sync === SyncMode.Async) {
|
|
3653
3683
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
3654
3684
|
}
|
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.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"description": "some util for self",
|
|
5
5
|
"homepage": "https://github.com/void-soul/util-man",
|
|
6
6
|
"repository": {
|
|
@@ -30,16 +30,16 @@
|
|
|
30
30
|
"sqlite": "node inspect ./dist/cjs/test-sqlite.js"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@msgpack/msgpack": "
|
|
34
|
-
"@types/lodash.get": "
|
|
35
|
-
"@types/shelljs": "
|
|
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": "
|
|
39
|
+
"lodash.get": "4.4.2",
|
|
40
40
|
"mustache": "4.2.0",
|
|
41
|
-
"pino": "9.2
|
|
42
|
-
"pino-pretty": "11.2.
|
|
41
|
+
"pino": "9.3.2",
|
|
42
|
+
"pino-pretty": "11.2.2",
|
|
43
43
|
"reflect-metadata": "0.2.2",
|
|
44
44
|
"sql-formatter": "15.3.2",
|
|
45
45
|
"sqlstring": "2.3.3",
|
|
@@ -48,17 +48,17 @@
|
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/better-sqlite3": "7.6.11",
|
|
50
50
|
"@types/mustache": "4.2.5",
|
|
51
|
-
"@types/node": "
|
|
51
|
+
"@types/node": "22.0.3",
|
|
52
52
|
"@types/sqlstring": "2.3.2",
|
|
53
|
-
"@typescript-eslint/eslint-plugin": "
|
|
54
|
-
"@typescript-eslint/parser": "
|
|
55
|
-
"better-sqlite3": "11.
|
|
53
|
+
"@typescript-eslint/eslint-plugin": "8.0.0",
|
|
54
|
+
"@typescript-eslint/parser": "8.0.0",
|
|
55
|
+
"better-sqlite3": "11.1.2",
|
|
56
56
|
"ioredis": "5.4.1",
|
|
57
57
|
"mongodb": "6.8.0",
|
|
58
|
-
"mysql2": "3.
|
|
58
|
+
"mysql2": "3.11.0",
|
|
59
59
|
"redlock": "5.0.0-beta.2",
|
|
60
60
|
"shelljs": "0.8.5",
|
|
61
|
-
"typescript": "5.5.
|
|
61
|
+
"typescript": "5.5.4"
|
|
62
62
|
},
|
|
63
63
|
"engines": {
|
|
64
64
|
"node": ">=20"
|
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');
|
|
@@ -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].
|
|
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].
|
|
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].
|
|
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(`
|
|
@@ -3000,8 +3026,7 @@ export class SqlService<T extends object> {
|
|
|
3000
3026
|
return result[0] as T;
|
|
3001
3027
|
}
|
|
3002
3028
|
case TemplateResult.NotSureOne: {
|
|
3003
|
-
|
|
3004
|
-
return (result[0] as T) ?? null;
|
|
3029
|
+
return result && result.length > 0 ? (result[0] as T) ?? null : null;
|
|
3005
3030
|
}
|
|
3006
3031
|
case TemplateResult.Many: {
|
|
3007
3032
|
return result;
|
|
@@ -4243,7 +4268,10 @@ class StreamQuery<T extends object> {
|
|
|
4243
4268
|
}
|
|
4244
4269
|
}
|
|
4245
4270
|
if (sets.length > 0) {
|
|
4246
|
-
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
4271
|
+
const sql = `UPDATE ${this._table} SET ${sets.join(',')}
|
|
4272
|
+
${where ? ' WHERE ' : ''}
|
|
4273
|
+
${where}
|
|
4274
|
+
`;
|
|
4247
4275
|
if (option.sync === SyncMode.Async) {
|
|
4248
4276
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
4249
4277
|
} else {
|
|
@@ -4260,7 +4288,10 @@ class StreamQuery<T extends object> {
|
|
|
4260
4288
|
option ??= {};
|
|
4261
4289
|
option.sync ??= SyncMode.Async;
|
|
4262
4290
|
const { where, params } = this._where();
|
|
4263
|
-
const sql = `DELETE FROM ${this._table}
|
|
4291
|
+
const sql = `DELETE FROM ${this._table}
|
|
4292
|
+
${where ? ' WHERE ' : ''}
|
|
4293
|
+
${where}
|
|
4294
|
+
`;
|
|
4264
4295
|
if (option.sync === SyncMode.Async) {
|
|
4265
4296
|
return this._service.excute({ sync: SyncMode.Async, sql, params });
|
|
4266
4297
|
} else {
|
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},
|