baja-lite 1.0.23 → 1.0.25

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,3 +1,2 @@
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.AppendRomote = exports.BootRomote = void 0;
3
+ 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) {
@@ -19,7 +19,7 @@ const BootRomote = async function (options) {
19
19
  }
20
20
  if (options.SqliteRemote && options.SqliteRemote.db) {
21
21
  if (typeof options.SqliteRemote.db === 'string') {
22
- await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
22
+ 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);
24
24
  }
25
25
  else {
@@ -37,14 +37,13 @@ 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;
40
+ // export const AppendRomote = async function (dbName: string) {
41
+ // if (!globalThis[_dao][DBType.SqliteRemote][dbName]) {
42
+ // globalThis[_GlobalSqlOption].SqliteRemote.service.initDB(dbName);
43
+ // const db = new SqliteRemote(globalThis[_GlobalSqlOption].SqliteRemote.service, dbName);
44
+ // if (globalThis[_dao][DBType.SqliteRemote][_primaryDB] === undefined) {
45
+ // globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
46
+ // }
47
+ // globalThis[_dao][DBType.SqliteRemote][dbName] = db;
48
+ // }
49
+ // }
package/cjs/boot.js CHANGED
@@ -76,14 +76,13 @@ const Boot = async function (options) {
76
76
  }
77
77
  }
78
78
  if (options.Sqlite) {
79
- const BetterSqlite3 = await Promise.resolve().then(() => __importStar(require('better-sqlite3')));
80
79
  if (typeof options.Sqlite === 'string') {
81
- globalThis[sql_1._dao][sql_1.DBType.Sqlite][sql_1._primaryDB] = new sql_1.Sqlite(new BetterSqlite3.default(options.Sqlite, { fileMustExist: false }));
80
+ globalThis[sql_1._dao][sql_1.DBType.Sqlite][sql_1._primaryDB] = new sql_1.Sqlite(new options.BetterSqlite3(options.Sqlite, { fileMustExist: false }));
82
81
  }
83
82
  else {
84
83
  let flag = false;
85
84
  for (const [key, fileName] of Object.entries(options.Sqlite)) {
86
- const db = new sql_1.Sqlite(new BetterSqlite3.default(fileName, { fileMustExist: false }));
85
+ const db = new sql_1.Sqlite(new options.BetterSqlite3(fileName, { fileMustExist: false }));
87
86
  if (flag === false) {
88
87
  globalThis[sql_1._dao][sql_1.DBType.Sqlite][sql_1._primaryDB] = db;
89
88
  flag = true;
@@ -94,13 +93,13 @@ const Boot = async function (options) {
94
93
  }
95
94
  if (options.SqliteRemote && options.SqliteRemote.db) {
96
95
  if (typeof options.SqliteRemote.db === 'string') {
97
- await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
96
+ options.SqliteRemote.service.initDB(options.SqliteRemote.db);
98
97
  globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] = new sql_1.SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
99
98
  }
100
99
  else {
101
100
  let flag = false;
102
101
  for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
103
- await options.SqliteRemote.service.initDB(fileName);
102
+ options.SqliteRemote.service.initDB(fileName);
104
103
  const db = new sql_1.SqliteRemote(options.SqliteRemote.service, fileName);
105
104
  if (flag === false) {
106
105
  globalThis[sql_1._dao][sql_1.DBType.SqliteRemote][sql_1._primaryDB] = db;
package/cjs/sql.d.ts CHANGED
@@ -432,6 +432,8 @@ export interface GlobalSqlOption extends GlobalSqlOptionForWeb {
432
432
  ```
433
433
  */
434
434
  Redis?: Record<string, Record<string, any>> | Record<string, any>;
435
+ /** sqlite数据库驱动初始化函数 */
436
+ BetterSqlite3?: any;
435
437
  /**
436
438
  * 读取查询语句时,是否扫描JS文件?
437
439
  * JS文件需要默认导出一个 SqlModel对象
@@ -483,14 +485,15 @@ export interface PageQuery<L> {
483
485
  }
484
486
  /** sqlite electron服务端需要支持的接口 */
485
487
  export interface SqliteRemoteInterface {
486
- execute(inData: Uint8Array): Promise<Uint8Array>;
487
- pluck(inData: Uint8Array): Promise<Uint8Array>;
488
- get(inData: Uint8Array): Promise<Uint8Array>;
489
- raw(inData: Uint8Array): Promise<Uint8Array>;
490
- query(inData: Uint8Array): Promise<Uint8Array>;
491
- initDB(dbName: string): Promise<void>;
492
- export(dbName: string): Promise<void>;
493
- restore(dbName: string, fromName: string): Promise<void>;
488
+ execute(inData: Uint8Array): Uint8Array;
489
+ pluck(inData: Uint8Array): Uint8Array;
490
+ get(inData: Uint8Array): Uint8Array;
491
+ raw(inData: Uint8Array): Uint8Array;
492
+ query(inData: Uint8Array): Uint8Array;
493
+ initDB(dbName: string): void;
494
+ export(dbName: string, exportPath: string): Promise<void>;
495
+ restore(dbName: string, importPath: string): void;
496
+ close(dbName?: string): void;
494
497
  }
495
498
  interface Connection {
496
499
  [_daoConnection]: any;
@@ -600,12 +603,12 @@ export declare class SqliteRemote implements Dao {
600
603
  transaction<T = any>(sync: SyncMode.Async, fn: (conn: Connection) => Promise<T>, conn?: Connection | null): Promise<T | null>;
601
604
  close(sync: SyncMode.Sync): void;
602
605
  close(sync: SyncMode.Async): Promise<void>;
603
- backup(sync: SyncMode.Sync, name: string): void;
604
- backup(sync: SyncMode.Async, name: string): Promise<void>;
606
+ backup(sync: SyncMode.Sync, exportPath: string): void;
607
+ backup(sync: SyncMode.Async, exportPath: string): Promise<void>;
605
608
  remove(sync: SyncMode.Sync): void;
606
609
  remove(sync: SyncMode.Async): Promise<void>;
607
- restore(sync: SyncMode.Sync, name: string): void;
608
- restore(sync: SyncMode.Async, name: string): Promise<void>;
610
+ restore(sync: SyncMode.Sync, importPath: string): void;
611
+ restore(sync: SyncMode.Async, importPath: string): Promise<void>;
609
612
  }
610
613
  export type SqlMapper = ([string, string[], any?])[];
611
614
  export type SqlMappers = Record<string, SqlMapper>;
package/cjs/sql.js CHANGED
@@ -974,53 +974,21 @@ class SqliteRemote {
974
974
  }
975
975
  close(sync) {
976
976
  if (sync === SyncMode.Async) {
977
- return new Promise(async (resolve, reject) => {
978
- try {
979
- await this[_daoConnection].close();
980
- }
981
- catch (error) {
982
- reject(error);
983
- }
984
- });
977
+ return this[_daoDB]?.close(this[_sqliteRemoteName]);
985
978
  }
986
979
  ;
987
980
  }
988
- backup(sync, name) {
981
+ backup(sync, exportPath) {
989
982
  if (sync === SyncMode.Async) {
990
- return new Promise(async (resolve, reject) => {
991
- try {
992
- await this[_daoConnection].backup(this[_sqliteRemoteName], name);
993
- }
994
- catch (error) {
995
- reject(error);
996
- }
997
- });
983
+ return this[_daoDB]?.export(this[_sqliteRemoteName], exportPath);
998
984
  }
999
985
  ;
1000
986
  }
1001
987
  remove(sync) {
1002
- if (sync === SyncMode.Async) {
1003
- return new Promise(async (resolve, reject) => {
1004
- try {
1005
- await this[_daoConnection].remove();
1006
- }
1007
- catch (error) {
1008
- reject(error);
1009
- }
1010
- });
1011
- }
1012
- ;
1013
988
  }
1014
- restore(sync, name) {
989
+ restore(sync, importPath) {
1015
990
  if (sync === SyncMode.Async) {
1016
- return new Promise(async (resolve, reject) => {
1017
- try {
1018
- await this[_daoConnection].restore(this[_sqliteRemoteName], name);
1019
- }
1020
- catch (error) {
1021
- reject(error);
1022
- }
1023
- });
991
+ return this[_daoDB]?.restore(this[_sqliteRemoteName], importPath);
1024
992
  }
1025
993
  ;
1026
994
  }
@@ -1571,11 +1539,16 @@ function P(skipConn = false) {
1571
1539
  // option
1572
1540
  const option = args[0] = Object.assign({}, globalThis[exports._GlobalSqlOption], this[_SqlOption], args[0]);
1573
1541
  option.sync ?? (option.sync = SyncMode.Async);
1542
+ option.tableName = option?.tableName ?? this[_tableName];
1574
1543
  const dbName = option?.dbName ?? this[_daoDBName] ?? exports._primaryDB;
1575
1544
  option.dao = globalThis[exports._dao][this[_dbType]][dbName];
1576
- error_1.Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1577
- option.tableName = option?.tableName ?? this[_tableName];
1578
1545
  if (this[_dbType] === DBType.Sqlite) {
1546
+ const db = new Sqlite(new globalThis[exports._GlobalSqlOption].BetterSqlite3(dbName, { fileMustExist: false }));
1547
+ if (globalThis[exports._dao][this[_dbType]][exports._primaryDB] === undefined) {
1548
+ globalThis[exports._dao][this[_dbType]][exports._primaryDB] = db;
1549
+ }
1550
+ globalThis[exports._dao][this[_dbType]][dbName] = db;
1551
+ option.dao = db;
1579
1552
  error_1.Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
1580
1553
  // 连接共享
1581
1554
  if (skipConn === false && !option.conn) {
@@ -1608,6 +1581,13 @@ function P(skipConn = false) {
1608
1581
  }
1609
1582
  }
1610
1583
  else if (this[_dbType] === DBType.SqliteRemote) {
1584
+ globalThis[exports._GlobalSqlOption].SqliteRemote.service.initDB(dbName);
1585
+ const db = new SqliteRemote(globalThis[exports._GlobalSqlOption].SqliteRemote.service, dbName);
1586
+ if (globalThis[exports._dao][this[_dbType]][exports._primaryDB] === undefined) {
1587
+ globalThis[exports._dao][this[_dbType]][exports._primaryDB] = db;
1588
+ }
1589
+ globalThis[exports._dao][this[_dbType]][dbName] = db;
1590
+ option.dao = db;
1611
1591
  error_1.Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
1612
1592
  return new Promise(async (resolve, reject) => {
1613
1593
  // 连接共享
@@ -1638,6 +1618,7 @@ function P(skipConn = false) {
1638
1618
  });
1639
1619
  }
1640
1620
  else if (this[_dbType] === DBType.Mysql) {
1621
+ error_1.Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1641
1622
  return new Promise(async (resolve, reject) => {
1642
1623
  try {
1643
1624
  // 连接共享
package/cjs/sqlite.d.ts CHANGED
@@ -5,10 +5,7 @@ export declare abstract class SqliteRemoteClass implements SqliteRemoteInterface
5
5
  abstract getStoreName(dbName: string): string;
6
6
  /** 导入时,备份源文件路径 */
7
7
  abstract getBackName(dbName: string): string;
8
- /** 实现导出时选择文件路径方法 */
9
- abstract getExportPath(): Promise<string | undefined | null>;
10
- /** 实现入时选择文件路径方法 */
11
- abstract getImportPath(): Promise<string | undefined | null>;
8
+ abstract BetterSqlite3: any;
12
9
  /** 实现复制 */
13
10
  abstract cpSync(from: string, to: string, option?: {
14
11
  force: true;
@@ -23,13 +20,13 @@ export declare abstract class SqliteRemoteClass implements SqliteRemoteInterface
23
20
  */
24
21
  abstract setMod(name: string): void;
25
22
  abstract trace: boolean;
26
- execute(inData: Uint8Array): Promise<Uint8Array>;
27
- pluck(inData: Uint8Array): Promise<Uint8Array>;
28
- get(inData: Uint8Array): Promise<Uint8Array>;
29
- raw(inData: Uint8Array): Promise<Uint8Array>;
30
- query(inData: Uint8Array): Promise<Uint8Array>;
31
- initDB(dbName: string): Promise<void>;
32
- export(dbName: string): Promise<void>;
33
- restore(dbName: string): Promise<void>;
23
+ execute(inData: Uint8Array): Uint8Array;
24
+ pluck(inData: Uint8Array): Uint8Array;
25
+ get(inData: Uint8Array): Uint8Array;
26
+ raw(inData: Uint8Array): Uint8Array;
27
+ query(inData: Uint8Array): Uint8Array;
28
+ initDB(dbName: string): void;
29
+ export(dbName: string, exportPath: string): Promise<void>;
30
+ restore(dbName: string, importPath: string): void;
34
31
  close(dbName?: string): void;
35
32
  }
package/cjs/sqlite.js CHANGED
@@ -1,27 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
4
  };
@@ -34,7 +11,7 @@ class SqliteRemoteClass {
34
11
  constructor() {
35
12
  this.dbList = {};
36
13
  }
37
- async execute(inData) {
14
+ execute(inData) {
38
15
  const [dbName, sql, params] = (0, msgpack_1.decode)(inData);
39
16
  sql_1.logger.debug(sql, params ?? '');
40
17
  try {
@@ -61,7 +38,7 @@ class SqliteRemoteClass {
61
38
  throw error;
62
39
  }
63
40
  }
64
- async pluck(inData) {
41
+ pluck(inData) {
65
42
  const [dbName, sql, params] = (0, msgpack_1.decode)(inData);
66
43
  sql_1.logger.debug(sql, params ?? '');
67
44
  try {
@@ -84,7 +61,7 @@ class SqliteRemoteClass {
84
61
  throw error;
85
62
  }
86
63
  }
87
- async get(inData) {
64
+ get(inData) {
88
65
  const [dbName, sql, params] = (0, msgpack_1.decode)(inData);
89
66
  sql_1.logger.debug(sql, params ?? '');
90
67
  try {
@@ -102,7 +79,7 @@ class SqliteRemoteClass {
102
79
  throw error;
103
80
  }
104
81
  }
105
- async raw(inData) {
82
+ raw(inData) {
106
83
  const [dbName, sql, params] = (0, msgpack_1.decode)(inData);
107
84
  sql_1.logger.debug(sql, params ?? '');
108
85
  try {
@@ -124,7 +101,7 @@ class SqliteRemoteClass {
124
101
  throw error;
125
102
  }
126
103
  }
127
- async query(inData) {
104
+ query(inData) {
128
105
  const [dbName, sql, params] = (0, msgpack_1.decode)(inData);
129
106
  sql_1.logger.debug(sql, params ?? '');
130
107
  try {
@@ -146,10 +123,9 @@ class SqliteRemoteClass {
146
123
  throw error;
147
124
  }
148
125
  }
149
- async initDB(dbName) {
126
+ initDB(dbName) {
150
127
  if (!this.dbList[dbName]) {
151
- const BetterSqlite3 = await Promise.resolve().then(() => __importStar(require('better-sqlite3')));
152
- this.dbList[dbName] = new BetterSqlite3.default(this.getStoreName(dbName), { fileMustExist: false });
128
+ this.dbList[dbName] = new this.BetterSqlite3(this.getStoreName(dbName), { fileMustExist: false });
153
129
  this.dbList[dbName].pragma('journal_mode = WAL');
154
130
  this.dbList[dbName].exec(`
155
131
  CREATE TABLE IF NOT EXISTS DUAL ( ______id INTEGER NOT NULL, PRIMARY KEY ( ______id ));
@@ -163,34 +139,30 @@ class SqliteRemoteClass {
163
139
  `);
164
140
  }
165
141
  }
166
- async export(dbName) {
167
- const exportPath = await this.getExportPath();
168
- if (exportPath) {
169
- await this.dbList[dbName].backup(exportPath);
170
- }
142
+ async export(dbName, exportPath) {
143
+ await this.dbList[dbName].backup(exportPath);
171
144
  }
172
- async restore(dbName) {
173
- const importPath = await this.getImportPath();
174
- if (importPath) {
175
- if (this.dbList[dbName]) {
176
- this.dbList[dbName].close();
177
- this.dbList[dbName] = null;
178
- }
179
- const nn = this.getStoreName(dbName);
180
- this.cpSync(nn, this.getBackName(dbName));
181
- this.cpSync(importPath, nn, { force: true });
182
- this.setMod(nn);
183
- this.initDB(dbName);
145
+ restore(dbName, importPath) {
146
+ if (this.dbList[dbName]) {
147
+ this.dbList[dbName].close();
148
+ this.dbList[dbName] = null;
184
149
  }
150
+ const nn = this.getStoreName(dbName);
151
+ this.cpSync(nn, this.getBackName(dbName));
152
+ this.cpSync(importPath, nn, { force: true });
153
+ this.setMod(nn);
154
+ this.initDB(dbName);
185
155
  }
186
156
  close(dbName) {
187
157
  if (dbName) {
188
158
  this.dbList[dbName]?.close();
159
+ this.dbList[dbName] = null;
189
160
  }
190
161
  else {
191
162
  for (const db of Object.values(this.dbList)) {
192
163
  db.close();
193
164
  }
165
+ this.dbList = {};
194
166
  }
195
167
  }
196
168
  }
@@ -1,3 +1,2 @@
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
@@ -16,7 +16,7 @@ export const BootRomote = async function (options) {
16
16
  }
17
17
  if (options.SqliteRemote && options.SqliteRemote.db) {
18
18
  if (typeof options.SqliteRemote.db === 'string') {
19
- await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
19
+ options.SqliteRemote.service.initDB(options.SqliteRemote.db);
20
20
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
21
21
  }
22
22
  else {
@@ -33,13 +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
- };
36
+ // export const AppendRomote = async function (dbName: string) {
37
+ // if (!globalThis[_dao][DBType.SqliteRemote][dbName]) {
38
+ // 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
@@ -50,14 +50,13 @@ export const Boot = async function (options) {
50
50
  }
51
51
  }
52
52
  if (options.Sqlite) {
53
- const BetterSqlite3 = await import('better-sqlite3');
54
53
  if (typeof options.Sqlite === 'string') {
55
- globalThis[_dao][DBType.Sqlite][_primaryDB] = new Sqlite(new BetterSqlite3.default(options.Sqlite, { fileMustExist: false }));
54
+ globalThis[_dao][DBType.Sqlite][_primaryDB] = new Sqlite(new options.BetterSqlite3(options.Sqlite, { fileMustExist: false }));
56
55
  }
57
56
  else {
58
57
  let flag = false;
59
58
  for (const [key, fileName] of Object.entries(options.Sqlite)) {
60
- const db = new Sqlite(new BetterSqlite3.default(fileName, { fileMustExist: false }));
59
+ const db = new Sqlite(new options.BetterSqlite3(fileName, { fileMustExist: false }));
61
60
  if (flag === false) {
62
61
  globalThis[_dao][DBType.Sqlite][_primaryDB] = db;
63
62
  flag = true;
@@ -68,13 +67,13 @@ export const Boot = async function (options) {
68
67
  }
69
68
  if (options.SqliteRemote && options.SqliteRemote.db) {
70
69
  if (typeof options.SqliteRemote.db === 'string') {
71
- await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
70
+ options.SqliteRemote.service.initDB(options.SqliteRemote.db);
72
71
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
73
72
  }
74
73
  else {
75
74
  let flag = false;
76
75
  for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
77
- await options.SqliteRemote.service.initDB(fileName);
76
+ options.SqliteRemote.service.initDB(fileName);
78
77
  const db = new SqliteRemote(options.SqliteRemote.service, fileName);
79
78
  if (flag === false) {
80
79
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
package/es/sql.d.ts CHANGED
@@ -432,6 +432,8 @@ export interface GlobalSqlOption extends GlobalSqlOptionForWeb {
432
432
  ```
433
433
  */
434
434
  Redis?: Record<string, Record<string, any>> | Record<string, any>;
435
+ /** sqlite数据库驱动初始化函数 */
436
+ BetterSqlite3?: any;
435
437
  /**
436
438
  * 读取查询语句时,是否扫描JS文件?
437
439
  * JS文件需要默认导出一个 SqlModel对象
@@ -483,14 +485,15 @@ export interface PageQuery<L> {
483
485
  }
484
486
  /** sqlite electron服务端需要支持的接口 */
485
487
  export interface SqliteRemoteInterface {
486
- execute(inData: Uint8Array): Promise<Uint8Array>;
487
- pluck(inData: Uint8Array): Promise<Uint8Array>;
488
- get(inData: Uint8Array): Promise<Uint8Array>;
489
- raw(inData: Uint8Array): Promise<Uint8Array>;
490
- query(inData: Uint8Array): Promise<Uint8Array>;
491
- initDB(dbName: string): Promise<void>;
492
- export(dbName: string): Promise<void>;
493
- restore(dbName: string, fromName: string): Promise<void>;
488
+ execute(inData: Uint8Array): Uint8Array;
489
+ pluck(inData: Uint8Array): Uint8Array;
490
+ get(inData: Uint8Array): Uint8Array;
491
+ raw(inData: Uint8Array): Uint8Array;
492
+ query(inData: Uint8Array): Uint8Array;
493
+ initDB(dbName: string): void;
494
+ export(dbName: string, exportPath: string): Promise<void>;
495
+ restore(dbName: string, importPath: string): void;
496
+ close(dbName?: string): void;
494
497
  }
495
498
  interface Connection {
496
499
  [_daoConnection]: any;
@@ -600,12 +603,12 @@ export declare class SqliteRemote implements Dao {
600
603
  transaction<T = any>(sync: SyncMode.Async, fn: (conn: Connection) => Promise<T>, conn?: Connection | null): Promise<T | null>;
601
604
  close(sync: SyncMode.Sync): void;
602
605
  close(sync: SyncMode.Async): Promise<void>;
603
- backup(sync: SyncMode.Sync, name: string): void;
604
- backup(sync: SyncMode.Async, name: string): Promise<void>;
606
+ backup(sync: SyncMode.Sync, exportPath: string): void;
607
+ backup(sync: SyncMode.Async, exportPath: string): Promise<void>;
605
608
  remove(sync: SyncMode.Sync): void;
606
609
  remove(sync: SyncMode.Async): Promise<void>;
607
- restore(sync: SyncMode.Sync, name: string): void;
608
- restore(sync: SyncMode.Async, name: string): Promise<void>;
610
+ restore(sync: SyncMode.Sync, importPath: string): void;
611
+ restore(sync: SyncMode.Async, importPath: string): Promise<void>;
609
612
  }
610
613
  export type SqlMapper = ([string, string[], any?])[];
611
614
  export type SqlMappers = Record<string, SqlMapper>;
package/es/sql.js CHANGED
@@ -932,53 +932,21 @@ export class SqliteRemote {
932
932
  }
933
933
  close(sync) {
934
934
  if (sync === SyncMode.Async) {
935
- return new Promise(async (resolve, reject) => {
936
- try {
937
- await this[_daoConnection].close();
938
- }
939
- catch (error) {
940
- reject(error);
941
- }
942
- });
935
+ return this[_daoDB]?.close(this[_sqliteRemoteName]);
943
936
  }
944
937
  ;
945
938
  }
946
- backup(sync, name) {
939
+ backup(sync, exportPath) {
947
940
  if (sync === SyncMode.Async) {
948
- return new Promise(async (resolve, reject) => {
949
- try {
950
- await this[_daoConnection].backup(this[_sqliteRemoteName], name);
951
- }
952
- catch (error) {
953
- reject(error);
954
- }
955
- });
941
+ return this[_daoDB]?.export(this[_sqliteRemoteName], exportPath);
956
942
  }
957
943
  ;
958
944
  }
959
945
  remove(sync) {
960
- if (sync === SyncMode.Async) {
961
- return new Promise(async (resolve, reject) => {
962
- try {
963
- await this[_daoConnection].remove();
964
- }
965
- catch (error) {
966
- reject(error);
967
- }
968
- });
969
- }
970
- ;
971
946
  }
972
- restore(sync, name) {
947
+ restore(sync, importPath) {
973
948
  if (sync === SyncMode.Async) {
974
- return new Promise(async (resolve, reject) => {
975
- try {
976
- await this[_daoConnection].restore(this[_sqliteRemoteName], name);
977
- }
978
- catch (error) {
979
- reject(error);
980
- }
981
- });
949
+ return this[_daoDB]?.restore(this[_sqliteRemoteName], importPath);
982
950
  }
983
951
  ;
984
952
  }
@@ -1527,11 +1495,16 @@ function P(skipConn = false) {
1527
1495
  // option
1528
1496
  const option = args[0] = Object.assign({}, globalThis[_GlobalSqlOption], this[_SqlOption], args[0]);
1529
1497
  option.sync ?? (option.sync = SyncMode.Async);
1498
+ option.tableName = option?.tableName ?? this[_tableName];
1530
1499
  const dbName = option?.dbName ?? this[_daoDBName] ?? _primaryDB;
1531
1500
  option.dao = globalThis[_dao][this[_dbType]][dbName];
1532
- Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1533
- option.tableName = option?.tableName ?? this[_tableName];
1534
1501
  if (this[_dbType] === DBType.Sqlite) {
1502
+ const db = new Sqlite(new globalThis[_GlobalSqlOption].BetterSqlite3(dbName, { fileMustExist: false }));
1503
+ if (globalThis[_dao][this[_dbType]][_primaryDB] === undefined) {
1504
+ globalThis[_dao][this[_dbType]][_primaryDB] = db;
1505
+ }
1506
+ globalThis[_dao][this[_dbType]][dbName] = db;
1507
+ option.dao = db;
1535
1508
  Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
1536
1509
  // 连接共享
1537
1510
  if (skipConn === false && !option.conn) {
@@ -1564,6 +1537,13 @@ function P(skipConn = false) {
1564
1537
  }
1565
1538
  }
1566
1539
  else if (this[_dbType] === DBType.SqliteRemote) {
1540
+ globalThis[_GlobalSqlOption].SqliteRemote.service.initDB(dbName);
1541
+ const db = new SqliteRemote(globalThis[_GlobalSqlOption].SqliteRemote.service, dbName);
1542
+ if (globalThis[_dao][this[_dbType]][_primaryDB] === undefined) {
1543
+ globalThis[_dao][this[_dbType]][_primaryDB] = db;
1544
+ }
1545
+ globalThis[_dao][this[_dbType]][dbName] = db;
1546
+ option.dao = db;
1567
1547
  Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
1568
1548
  return new Promise(async (resolve, reject) => {
1569
1549
  // 连接共享
@@ -1594,6 +1574,7 @@ function P(skipConn = false) {
1594
1574
  });
1595
1575
  }
1596
1576
  else if (this[_dbType] === DBType.Mysql) {
1577
+ Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1597
1578
  return new Promise(async (resolve, reject) => {
1598
1579
  try {
1599
1580
  // 连接共享
package/es/sqlite.d.ts CHANGED
@@ -5,10 +5,7 @@ export declare abstract class SqliteRemoteClass implements SqliteRemoteInterface
5
5
  abstract getStoreName(dbName: string): string;
6
6
  /** 导入时,备份源文件路径 */
7
7
  abstract getBackName(dbName: string): string;
8
- /** 实现导出时选择文件路径方法 */
9
- abstract getExportPath(): Promise<string | undefined | null>;
10
- /** 实现入时选择文件路径方法 */
11
- abstract getImportPath(): Promise<string | undefined | null>;
8
+ abstract BetterSqlite3: any;
12
9
  /** 实现复制 */
13
10
  abstract cpSync(from: string, to: string, option?: {
14
11
  force: true;
@@ -23,13 +20,13 @@ export declare abstract class SqliteRemoteClass implements SqliteRemoteInterface
23
20
  */
24
21
  abstract setMod(name: string): void;
25
22
  abstract trace: boolean;
26
- execute(inData: Uint8Array): Promise<Uint8Array>;
27
- pluck(inData: Uint8Array): Promise<Uint8Array>;
28
- get(inData: Uint8Array): Promise<Uint8Array>;
29
- raw(inData: Uint8Array): Promise<Uint8Array>;
30
- query(inData: Uint8Array): Promise<Uint8Array>;
31
- initDB(dbName: string): Promise<void>;
32
- export(dbName: string): Promise<void>;
33
- restore(dbName: string): Promise<void>;
23
+ execute(inData: Uint8Array): Uint8Array;
24
+ pluck(inData: Uint8Array): Uint8Array;
25
+ get(inData: Uint8Array): Uint8Array;
26
+ raw(inData: Uint8Array): Uint8Array;
27
+ query(inData: Uint8Array): Uint8Array;
28
+ initDB(dbName: string): void;
29
+ export(dbName: string, exportPath: string): Promise<void>;
30
+ restore(dbName: string, importPath: string): void;
34
31
  close(dbName?: string): void;
35
32
  }
package/es/sqlite.js CHANGED
@@ -5,7 +5,7 @@ export class SqliteRemoteClass {
5
5
  constructor() {
6
6
  this.dbList = {};
7
7
  }
8
- async execute(inData) {
8
+ execute(inData) {
9
9
  const [dbName, sql, params] = decode(inData);
10
10
  logger.debug(sql, params ?? '');
11
11
  try {
@@ -32,7 +32,7 @@ export class SqliteRemoteClass {
32
32
  throw error;
33
33
  }
34
34
  }
35
- async pluck(inData) {
35
+ pluck(inData) {
36
36
  const [dbName, sql, params] = decode(inData);
37
37
  logger.debug(sql, params ?? '');
38
38
  try {
@@ -55,7 +55,7 @@ export class SqliteRemoteClass {
55
55
  throw error;
56
56
  }
57
57
  }
58
- async get(inData) {
58
+ get(inData) {
59
59
  const [dbName, sql, params] = decode(inData);
60
60
  logger.debug(sql, params ?? '');
61
61
  try {
@@ -73,7 +73,7 @@ export class SqliteRemoteClass {
73
73
  throw error;
74
74
  }
75
75
  }
76
- async raw(inData) {
76
+ raw(inData) {
77
77
  const [dbName, sql, params] = decode(inData);
78
78
  logger.debug(sql, params ?? '');
79
79
  try {
@@ -95,7 +95,7 @@ export class SqliteRemoteClass {
95
95
  throw error;
96
96
  }
97
97
  }
98
- async query(inData) {
98
+ query(inData) {
99
99
  const [dbName, sql, params] = decode(inData);
100
100
  logger.debug(sql, params ?? '');
101
101
  try {
@@ -117,10 +117,9 @@ export class SqliteRemoteClass {
117
117
  throw error;
118
118
  }
119
119
  }
120
- async initDB(dbName) {
120
+ initDB(dbName) {
121
121
  if (!this.dbList[dbName]) {
122
- const BetterSqlite3 = await import('better-sqlite3');
123
- this.dbList[dbName] = new BetterSqlite3.default(this.getStoreName(dbName), { fileMustExist: false });
122
+ this.dbList[dbName] = new this.BetterSqlite3(this.getStoreName(dbName), { fileMustExist: false });
124
123
  this.dbList[dbName].pragma('journal_mode = WAL');
125
124
  this.dbList[dbName].exec(`
126
125
  CREATE TABLE IF NOT EXISTS DUAL ( ______id INTEGER NOT NULL, PRIMARY KEY ( ______id ));
@@ -134,34 +133,30 @@ export class SqliteRemoteClass {
134
133
  `);
135
134
  }
136
135
  }
137
- async export(dbName) {
138
- const exportPath = await this.getExportPath();
139
- if (exportPath) {
140
- await this.dbList[dbName].backup(exportPath);
141
- }
136
+ async export(dbName, exportPath) {
137
+ await this.dbList[dbName].backup(exportPath);
142
138
  }
143
- async restore(dbName) {
144
- const importPath = await this.getImportPath();
145
- if (importPath) {
146
- if (this.dbList[dbName]) {
147
- this.dbList[dbName].close();
148
- this.dbList[dbName] = null;
149
- }
150
- const nn = this.getStoreName(dbName);
151
- this.cpSync(nn, this.getBackName(dbName));
152
- this.cpSync(importPath, nn, { force: true });
153
- this.setMod(nn);
154
- this.initDB(dbName);
139
+ restore(dbName, importPath) {
140
+ if (this.dbList[dbName]) {
141
+ this.dbList[dbName].close();
142
+ this.dbList[dbName] = null;
155
143
  }
144
+ const nn = this.getStoreName(dbName);
145
+ this.cpSync(nn, this.getBackName(dbName));
146
+ this.cpSync(importPath, nn, { force: true });
147
+ this.setMod(nn);
148
+ this.initDB(dbName);
156
149
  }
157
150
  close(dbName) {
158
151
  if (dbName) {
159
152
  this.dbList[dbName]?.close();
153
+ this.dbList[dbName] = null;
160
154
  }
161
155
  else {
162
156
  for (const db of Object.values(this.dbList)) {
163
157
  db.close();
164
158
  }
159
+ this.dbList = {};
165
160
  }
166
161
  }
167
162
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baja-lite",
3
- "version": "1.0.23",
3
+ "version": "1.0.25",
4
4
  "description": "some util for self",
5
5
  "homepage": "https://github.com/void-soul/util-man",
6
6
  "repository": {
@@ -17,7 +17,7 @@ export const BootRomote = async function (options: GlobalSqlOptionForWeb) {
17
17
  }
18
18
  if (options.SqliteRemote && options.SqliteRemote.db) {
19
19
  if (typeof options.SqliteRemote.db === 'string') {
20
- await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
20
+ options.SqliteRemote.service.initDB(options.SqliteRemote.db);
21
21
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
22
22
  } else {
23
23
  let flag = false;
@@ -34,13 +34,13 @@ export const BootRomote = async function (options: GlobalSqlOptionForWeb) {
34
34
  }
35
35
  }
36
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
- }
37
+ // export const AppendRomote = async function (dbName: string) {
38
+ // if (!globalThis[_dao][DBType.SqliteRemote][dbName]) {
39
+ // 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
@@ -50,13 +50,12 @@ export const Boot = async function (options: GlobalSqlOption) {
50
50
  }
51
51
  }
52
52
  if (options.Sqlite) {
53
- const BetterSqlite3 = await import('better-sqlite3');
54
53
  if (typeof options.Sqlite === 'string') {
55
- globalThis[_dao][DBType.Sqlite][_primaryDB] = new Sqlite(new BetterSqlite3.default(options.Sqlite, { fileMustExist: false }));
54
+ globalThis[_dao][DBType.Sqlite][_primaryDB] = new Sqlite(new options.BetterSqlite3(options.Sqlite, { fileMustExist: false }));
56
55
  } else {
57
56
  let flag = false;
58
57
  for (const [key, fileName] of Object.entries(options.Sqlite)) {
59
- const db = new Sqlite(new BetterSqlite3.default(fileName, { fileMustExist: false }));
58
+ const db = new Sqlite(new options.BetterSqlite3(fileName, { fileMustExist: false }));
60
59
  if (flag === false) {
61
60
  globalThis[_dao][DBType.Sqlite][_primaryDB] = db;
62
61
  flag = true;
@@ -67,12 +66,12 @@ export const Boot = async function (options: GlobalSqlOption) {
67
66
  }
68
67
  if (options.SqliteRemote && options.SqliteRemote.db) {
69
68
  if (typeof options.SqliteRemote.db === 'string') {
70
- await options.SqliteRemote.service.initDB(options.SqliteRemote.db);
69
+ options.SqliteRemote.service.initDB(options.SqliteRemote.db);
71
70
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
72
71
  } else {
73
72
  let flag = false;
74
73
  for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
75
- await options.SqliteRemote.service.initDB(fileName);
74
+ options.SqliteRemote.service.initDB(fileName);
76
75
  const db = new SqliteRemote(options.SqliteRemote.service, fileName);
77
76
  if (flag === false) {
78
77
  globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
package/src/sql.ts CHANGED
@@ -493,8 +493,8 @@ export interface GlobalSqlOption extends GlobalSqlOptionForWeb {
493
493
  ```
494
494
  */
495
495
  Redis?: Record<string, Record<string, any>> | Record<string, any>;
496
-
497
-
496
+ /** sqlite数据库驱动初始化函数 */
497
+ BetterSqlite3?: any;
498
498
  /**
499
499
  * 读取查询语句时,是否扫描JS文件?
500
500
  * JS文件需要默认导出一个 SqlModel对象
@@ -551,14 +551,15 @@ export interface PageQuery<L> {
551
551
  // #region 数据方言
552
552
  /** sqlite electron服务端需要支持的接口 */
553
553
  export interface SqliteRemoteInterface {
554
- execute(inData: Uint8Array): | Promise<Uint8Array>
555
- pluck(inData: Uint8Array): Promise<Uint8Array>;
556
- get(inData: Uint8Array): Promise<Uint8Array>;
557
- raw(inData: Uint8Array): Promise<Uint8Array>;
558
- query(inData: Uint8Array): Promise<Uint8Array>;
559
- initDB(dbName: string): Promise<void>;
560
- export(dbName: string): Promise<void>;
561
- restore(dbName: string, fromName: string): Promise<void>;
554
+ execute(inData: Uint8Array):Uint8Array;
555
+ pluck(inData: Uint8Array):Uint8Array;
556
+ get(inData: Uint8Array): Uint8Array;
557
+ raw(inData: Uint8Array): Uint8Array;
558
+ query(inData: Uint8Array): Uint8Array;
559
+ initDB(dbName: string): void;
560
+ export(dbName: string, exportPath: string): Promise<void>;
561
+ restore(dbName: string, importPath: string): void;
562
+ close(dbName?: string): void;
562
563
  };
563
564
  interface Connection {
564
565
  [_daoConnection]: any;
@@ -1277,55 +1278,29 @@ export class SqliteRemote implements Dao {
1277
1278
  close(sync: SyncMode.Async): Promise<void>;
1278
1279
  close(sync: SyncMode): Promise<void> | void {
1279
1280
  if (sync === SyncMode.Async) {
1280
- return new Promise(async (resolve, reject) => {
1281
- try {
1282
- await this[_daoConnection].close();
1283
- } catch (error) {
1284
- reject(error);
1285
- }
1286
- });
1281
+ return this[_daoDB]?.close(this[_sqliteRemoteName]);
1287
1282
  };
1288
1283
  }
1289
1284
 
1290
- backup(sync: SyncMode.Sync, name: string): void;
1291
- backup(sync: SyncMode.Async, name: string): Promise<void>;
1292
- backup(sync: SyncMode, name: string): Promise<void> | void {
1285
+ backup(sync: SyncMode.Sync, exportPath: string): void;
1286
+ backup(sync: SyncMode.Async, exportPath: string): Promise<void>;
1287
+ backup(sync: SyncMode, exportPath: string): Promise<void> | void {
1293
1288
  if (sync === SyncMode.Async) {
1294
- return new Promise(async (resolve, reject) => {
1295
- try {
1296
- await this[_daoConnection].backup(this[_sqliteRemoteName], name);
1297
- } catch (error) {
1298
- reject(error);
1299
- }
1300
- });
1289
+ return this[_daoDB]?.export(this[_sqliteRemoteName], exportPath);
1301
1290
  };
1302
1291
  }
1303
1292
 
1304
1293
  remove(sync: SyncMode.Sync): void;
1305
1294
  remove(sync: SyncMode.Async): Promise<void>;
1306
1295
  remove(sync: SyncMode): Promise<void> | void {
1307
- if (sync === SyncMode.Async) {
1308
- return new Promise(async (resolve, reject) => {
1309
- try {
1310
- await this[_daoConnection].remove();
1311
- } catch (error) {
1312
- reject(error);
1313
- }
1314
- });
1315
- };
1296
+
1316
1297
  }
1317
1298
 
1318
- restore(sync: SyncMode.Sync, name: string): void;
1319
- restore(sync: SyncMode.Async, name: string): Promise<void>;
1320
- restore(sync: SyncMode, name: string): Promise<void> | void {
1299
+ restore(sync: SyncMode.Sync, importPath: string): void;
1300
+ restore(sync: SyncMode.Async, importPath: string): Promise<void>;
1301
+ restore(sync: SyncMode, importPath: string): Promise<void> | void {
1321
1302
  if (sync === SyncMode.Async) {
1322
- return new Promise(async (resolve, reject) => {
1323
- try {
1324
- await this[_daoConnection].restore(this[_sqliteRemoteName], name);
1325
- } catch (error) {
1326
- reject(error);
1327
- }
1328
- });
1303
+ return this[_daoDB]?.restore(this[_sqliteRemoteName], importPath, );
1329
1304
  };
1330
1305
  }
1331
1306
  }
@@ -1902,11 +1877,19 @@ function P<T extends object>(skipConn = false) {
1902
1877
  // option
1903
1878
  const option = args[0] = Object.assign({}, globalThis[_GlobalSqlOption], this[_SqlOption], args[0]) as (MethodOption & { sync?: SyncMode; });
1904
1879
  option.sync ??= SyncMode.Async;
1880
+ option!.tableName = option?.tableName ?? this[_tableName];
1881
+
1905
1882
  const dbName = option?.dbName ?? this[_daoDBName] ?? _primaryDB;
1906
1883
  option!.dao = globalThis[_dao][this[_dbType]!][dbName] as Dao;
1907
- Throw.if(!option!.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1908
- option!.tableName = option?.tableName ?? this[_tableName];
1884
+
1909
1885
  if (this[_dbType] === DBType.Sqlite) {
1886
+ const db = new Sqlite(new globalThis[_GlobalSqlOption].BetterSqlite3(dbName as any, { fileMustExist: false }));
1887
+ if (globalThis[_dao][this[_dbType]!][_primaryDB] === undefined) {
1888
+ globalThis[_dao][this[_dbType]!][_primaryDB] = db;
1889
+ }
1890
+ globalThis[_dao][this[_dbType]!][dbName] = db;
1891
+ option!.dao = db;
1892
+
1910
1893
  Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!')
1911
1894
  // 连接共享
1912
1895
  if (skipConn === false && !option!.conn) {
@@ -1934,6 +1917,14 @@ function P<T extends object>(skipConn = false) {
1934
1917
  }
1935
1918
  }
1936
1919
  } else if (this[_dbType] === DBType.SqliteRemote) {
1920
+ globalThis[_GlobalSqlOption].SqliteRemote.service.initDB(dbName);
1921
+ const db = new SqliteRemote(globalThis[_GlobalSqlOption].SqliteRemote.service, dbName as any);
1922
+ if (globalThis[_dao][this[_dbType]!][_primaryDB] === undefined) {
1923
+ globalThis[_dao][this[_dbType]!][_primaryDB] = db;
1924
+ }
1925
+ globalThis[_dao][this[_dbType]!][dbName] = db;
1926
+ option!.dao = db;
1927
+
1937
1928
  Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!')
1938
1929
  return new Promise(async (resolve, reject) => {
1939
1930
  // 连接共享
@@ -1961,6 +1952,7 @@ function P<T extends object>(skipConn = false) {
1961
1952
  });
1962
1953
 
1963
1954
  } else if (this[_dbType] === DBType.Mysql) {
1955
+ Throw.if(!option!.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1964
1956
  return new Promise(async (resolve, reject) => {
1965
1957
  try {
1966
1958
  // 连接共享
package/src/sqlite.ts CHANGED
@@ -7,10 +7,9 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
7
7
  abstract getStoreName(dbName: string): string;
8
8
  /** 导入时,备份源文件路径 */
9
9
  abstract getBackName(dbName: string): string;
10
- /** 实现导出时选择文件路径方法 */
11
- abstract getExportPath(): Promise<string | undefined | null>;
12
- /** 实现入时选择文件路径方法 */
13
- abstract getImportPath(): Promise<string | undefined | null>;
10
+ abstract BetterSqlite3: any;
11
+
12
+
14
13
  /** 实现复制 */
15
14
  abstract cpSync(from: string, to: string, option?: { force: true }): void;
16
15
  /**
@@ -23,7 +22,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
23
22
  */
24
23
  abstract setMod(name: string): void;
25
24
  abstract trace: boolean;
26
- async execute(inData: Uint8Array): Promise<Uint8Array> {
25
+ execute(inData: Uint8Array): Uint8Array {
27
26
  const [dbName, sql, params] = decode(inData) as [dbName: string, sql?: string | undefined, params?: any];
28
27
  logger.debug(sql, params ?? '');
29
28
  try {
@@ -46,7 +45,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
46
45
  throw error;
47
46
  }
48
47
  }
49
- async pluck(inData: Uint8Array): Promise<Uint8Array> {
48
+ pluck(inData: Uint8Array): Uint8Array {
50
49
  const [dbName, sql, params] = decode(inData) as [dbName: string, sql?: string | undefined, params?: any];
51
50
  logger.debug(sql, params ?? '');
52
51
  try {
@@ -65,7 +64,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
65
64
  throw error;
66
65
  }
67
66
  }
68
- async get(inData: Uint8Array): Promise<Uint8Array> {
67
+ get(inData: Uint8Array): Uint8Array {
69
68
  const [dbName, sql, params] = decode(inData) as [dbName: string, sql?: string | undefined, params?: any];
70
69
  logger.debug(sql, params ?? '');
71
70
  try {
@@ -82,7 +81,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
82
81
  throw error;
83
82
  }
84
83
  }
85
- async raw(inData: Uint8Array): Promise<Uint8Array> {
84
+ raw(inData: Uint8Array): Uint8Array {
86
85
  const [dbName, sql, params] = decode(inData) as [dbName: string, sql?: string | undefined, params?: any];
87
86
  logger.debug(sql, params ?? '');
88
87
  try {
@@ -100,7 +99,7 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
100
99
  throw error;
101
100
  }
102
101
  }
103
- async query(inData: Uint8Array): Promise<Uint8Array> {
102
+ query(inData: Uint8Array): Uint8Array {
104
103
  const [dbName, sql, params] = decode(inData) as [dbName: string, sql?: string | undefined, params?: any];
105
104
  logger.debug(sql, params ?? '');
106
105
  try {
@@ -118,10 +117,9 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
118
117
  throw error;
119
118
  }
120
119
  }
121
- async initDB(dbName: string): Promise<void> {
120
+ initDB(dbName: string) {
122
121
  if (!this.dbList[dbName]) {
123
- const BetterSqlite3 = await import('better-sqlite3');
124
- this.dbList[dbName] = new BetterSqlite3.default(this.getStoreName(dbName), { fileMustExist: false });
122
+ this.dbList[dbName] = new this.BetterSqlite3(this.getStoreName(dbName), { fileMustExist: false });
125
123
  this.dbList[dbName].pragma('journal_mode = WAL');
126
124
  this.dbList[dbName].exec(`
127
125
  CREATE TABLE IF NOT EXISTS DUAL ( ______id INTEGER NOT NULL, PRIMARY KEY ( ______id ));
@@ -135,33 +133,29 @@ export abstract class SqliteRemoteClass implements SqliteRemoteInterface {
135
133
  `);
136
134
  }
137
135
  }
138
- async export(dbName: string): Promise<void> {
139
- const exportPath = await this.getExportPath();
140
- if (exportPath) {
141
- await this.dbList[dbName].backup(exportPath);
142
- }
136
+ async export(dbName: string, exportPath: string): Promise<void> {
137
+ await this.dbList[dbName].backup(exportPath);
143
138
  }
144
- async restore(dbName: string): Promise<void> {
145
- const importPath = await this.getImportPath();
146
- if (importPath) {
147
- if (this.dbList[dbName]) {
148
- this.dbList[dbName].close();
149
- this.dbList[dbName] = null;
150
- }
151
- const nn = this.getStoreName(dbName);
152
- this.cpSync(nn, this.getBackName(dbName));
153
- this.cpSync(importPath, nn, { force: true });
154
- this.setMod(nn);
155
- this.initDB(dbName);
139
+ restore(dbName: string, importPath: string) {
140
+ if (this.dbList[dbName]) {
141
+ this.dbList[dbName].close();
142
+ this.dbList[dbName] = null;
156
143
  }
144
+ const nn = this.getStoreName(dbName);
145
+ this.cpSync(nn, this.getBackName(dbName));
146
+ this.cpSync(importPath, nn, { force: true });
147
+ this.setMod(nn);
148
+ this.initDB(dbName);
157
149
  }
158
150
  close(dbName?: string) {
159
151
  if (dbName) {
160
152
  this.dbList[dbName]?.close();
153
+ this.dbList[dbName] = null;
161
154
  } else {
162
155
  for (const db of Object.values(this.dbList)) {
163
156
  db.close();
164
157
  }
158
+ this.dbList = {};
165
159
  }
166
160
  }
167
161
  }