baja-lite 1.0.24 → 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,14 @@ 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;
494
496
  close(dbName?: string): void;
495
497
  }
496
498
  interface Connection {
@@ -601,12 +603,12 @@ export declare class SqliteRemote implements Dao {
601
603
  transaction<T = any>(sync: SyncMode.Async, fn: (conn: Connection) => Promise<T>, conn?: Connection | null): Promise<T | null>;
602
604
  close(sync: SyncMode.Sync): void;
603
605
  close(sync: SyncMode.Async): Promise<void>;
604
- backup(sync: SyncMode.Sync, name: string): void;
605
- 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>;
606
608
  remove(sync: SyncMode.Sync): void;
607
609
  remove(sync: SyncMode.Async): Promise<void>;
608
- restore(sync: SyncMode.Sync, name: string): void;
609
- 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>;
610
612
  }
611
613
  export type SqlMapper = ([string, string[], any?])[];
612
614
  export type SqlMappers = Record<string, SqlMapper>;
package/cjs/sql.js CHANGED
@@ -978,17 +978,17 @@ class SqliteRemote {
978
978
  }
979
979
  ;
980
980
  }
981
- backup(sync, name) {
981
+ backup(sync, exportPath) {
982
982
  if (sync === SyncMode.Async) {
983
- return this[_daoDB]?.export(this[_sqliteRemoteName]);
983
+ return this[_daoDB]?.export(this[_sqliteRemoteName], exportPath);
984
984
  }
985
985
  ;
986
986
  }
987
987
  remove(sync) {
988
988
  }
989
- restore(sync, name) {
989
+ restore(sync, importPath) {
990
990
  if (sync === SyncMode.Async) {
991
- return this[_daoDB]?.restore(this[_sqliteRemoteName], name);
991
+ return this[_daoDB]?.restore(this[_sqliteRemoteName], importPath);
992
992
  }
993
993
  ;
994
994
  }
@@ -1539,11 +1539,16 @@ function P(skipConn = false) {
1539
1539
  // option
1540
1540
  const option = args[0] = Object.assign({}, globalThis[exports._GlobalSqlOption], this[_SqlOption], args[0]);
1541
1541
  option.sync ?? (option.sync = SyncMode.Async);
1542
+ option.tableName = option?.tableName ?? this[_tableName];
1542
1543
  const dbName = option?.dbName ?? this[_daoDBName] ?? exports._primaryDB;
1543
1544
  option.dao = globalThis[exports._dao][this[_dbType]][dbName];
1544
- error_1.Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1545
- option.tableName = option?.tableName ?? this[_tableName];
1546
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;
1547
1552
  error_1.Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
1548
1553
  // 连接共享
1549
1554
  if (skipConn === false && !option.conn) {
@@ -1576,6 +1581,13 @@ function P(skipConn = false) {
1576
1581
  }
1577
1582
  }
1578
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;
1579
1591
  error_1.Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
1580
1592
  return new Promise(async (resolve, reject) => {
1581
1593
  // 连接共享
@@ -1606,6 +1618,7 @@ function P(skipConn = false) {
1606
1618
  });
1607
1619
  }
1608
1620
  else if (this[_dbType] === DBType.Mysql) {
1621
+ error_1.Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1609
1622
  return new Promise(async (resolve, reject) => {
1610
1623
  try {
1611
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,14 @@ 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;
494
496
  close(dbName?: string): void;
495
497
  }
496
498
  interface Connection {
@@ -601,12 +603,12 @@ export declare class SqliteRemote implements Dao {
601
603
  transaction<T = any>(sync: SyncMode.Async, fn: (conn: Connection) => Promise<T>, conn?: Connection | null): Promise<T | null>;
602
604
  close(sync: SyncMode.Sync): void;
603
605
  close(sync: SyncMode.Async): Promise<void>;
604
- backup(sync: SyncMode.Sync, name: string): void;
605
- 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>;
606
608
  remove(sync: SyncMode.Sync): void;
607
609
  remove(sync: SyncMode.Async): Promise<void>;
608
- restore(sync: SyncMode.Sync, name: string): void;
609
- 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>;
610
612
  }
611
613
  export type SqlMapper = ([string, string[], any?])[];
612
614
  export type SqlMappers = Record<string, SqlMapper>;
package/es/sql.js CHANGED
@@ -936,17 +936,17 @@ export class SqliteRemote {
936
936
  }
937
937
  ;
938
938
  }
939
- backup(sync, name) {
939
+ backup(sync, exportPath) {
940
940
  if (sync === SyncMode.Async) {
941
- return this[_daoDB]?.export(this[_sqliteRemoteName]);
941
+ return this[_daoDB]?.export(this[_sqliteRemoteName], exportPath);
942
942
  }
943
943
  ;
944
944
  }
945
945
  remove(sync) {
946
946
  }
947
- restore(sync, name) {
947
+ restore(sync, importPath) {
948
948
  if (sync === SyncMode.Async) {
949
- return this[_daoDB]?.restore(this[_sqliteRemoteName], name);
949
+ return this[_daoDB]?.restore(this[_sqliteRemoteName], importPath);
950
950
  }
951
951
  ;
952
952
  }
@@ -1495,11 +1495,16 @@ function P(skipConn = false) {
1495
1495
  // option
1496
1496
  const option = args[0] = Object.assign({}, globalThis[_GlobalSqlOption], this[_SqlOption], args[0]);
1497
1497
  option.sync ?? (option.sync = SyncMode.Async);
1498
+ option.tableName = option?.tableName ?? this[_tableName];
1498
1499
  const dbName = option?.dbName ?? this[_daoDBName] ?? _primaryDB;
1499
1500
  option.dao = globalThis[_dao][this[_dbType]][dbName];
1500
- Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1501
- option.tableName = option?.tableName ?? this[_tableName];
1502
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;
1503
1508
  Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
1504
1509
  // 连接共享
1505
1510
  if (skipConn === false && !option.conn) {
@@ -1532,6 +1537,13 @@ function P(skipConn = false) {
1532
1537
  }
1533
1538
  }
1534
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;
1535
1547
  Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
1536
1548
  return new Promise(async (resolve, reject) => {
1537
1549
  // 连接共享
@@ -1562,6 +1574,7 @@ function P(skipConn = false) {
1562
1574
  });
1563
1575
  }
1564
1576
  else if (this[_dbType] === DBType.Mysql) {
1577
+ Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1565
1578
  return new Promise(async (resolve, reject) => {
1566
1579
  try {
1567
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.24",
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,14 @@ 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
562
  close(dbName?: string): void;
563
563
  };
564
564
  interface Connection {
@@ -1282,11 +1282,11 @@ export class SqliteRemote implements Dao {
1282
1282
  };
1283
1283
  }
1284
1284
 
1285
- backup(sync: SyncMode.Sync, name: string): void;
1286
- backup(sync: SyncMode.Async, name: string): Promise<void>;
1287
- 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 {
1288
1288
  if (sync === SyncMode.Async) {
1289
- return this[_daoDB]?.export(this[_sqliteRemoteName]);
1289
+ return this[_daoDB]?.export(this[_sqliteRemoteName], exportPath);
1290
1290
  };
1291
1291
  }
1292
1292
 
@@ -1296,11 +1296,11 @@ export class SqliteRemote implements Dao {
1296
1296
 
1297
1297
  }
1298
1298
 
1299
- restore(sync: SyncMode.Sync, name: string): void;
1300
- restore(sync: SyncMode.Async, name: string): Promise<void>;
1301
- 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 {
1302
1302
  if (sync === SyncMode.Async) {
1303
- return this[_daoDB]?.restore(this[_sqliteRemoteName], name);
1303
+ return this[_daoDB]?.restore(this[_sqliteRemoteName], importPath, );
1304
1304
  };
1305
1305
  }
1306
1306
  }
@@ -1877,11 +1877,19 @@ function P<T extends object>(skipConn = false) {
1877
1877
  // option
1878
1878
  const option = args[0] = Object.assign({}, globalThis[_GlobalSqlOption], this[_SqlOption], args[0]) as (MethodOption & { sync?: SyncMode; });
1879
1879
  option.sync ??= SyncMode.Async;
1880
+ option!.tableName = option?.tableName ?? this[_tableName];
1881
+
1880
1882
  const dbName = option?.dbName ?? this[_daoDBName] ?? _primaryDB;
1881
1883
  option!.dao = globalThis[_dao][this[_dbType]!][dbName] as Dao;
1882
- Throw.if(!option!.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1883
- option!.tableName = option?.tableName ?? this[_tableName];
1884
+
1884
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
+
1885
1893
  Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!')
1886
1894
  // 连接共享
1887
1895
  if (skipConn === false && !option!.conn) {
@@ -1909,6 +1917,14 @@ function P<T extends object>(skipConn = false) {
1909
1917
  }
1910
1918
  }
1911
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
+
1912
1928
  Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!')
1913
1929
  return new Promise(async (resolve, reject) => {
1914
1930
  // 连接共享
@@ -1936,6 +1952,7 @@ function P<T extends object>(skipConn = false) {
1936
1952
  });
1937
1953
 
1938
1954
  } else if (this[_dbType] === DBType.Mysql) {
1955
+ Throw.if(!option!.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1939
1956
  return new Promise(async (resolve, reject) => {
1940
1957
  try {
1941
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
  }