baja-lite 1.0.24 → 1.0.26

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
@@ -21,7 +21,7 @@ declare const _def: unique symbol;
21
21
  declare const _comment: unique symbol;
22
22
  export declare const _sqlCache: unique symbol;
23
23
  export declare const _dao: unique symbol;
24
- export declare const _primaryDB: unique symbol;
24
+ export declare const _primaryDB = "______primaryDB_______";
25
25
  declare const _dbType: unique symbol;
26
26
  declare const _formatDialect: unique symbol;
27
27
  declare const _sqlite_version: unique symbol;
@@ -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对象
@@ -488,9 +490,9 @@ export interface SqliteRemoteInterface {
488
490
  get(inData: Uint8Array): Promise<Uint8Array>;
489
491
  raw(inData: Uint8Array): Promise<Uint8Array>;
490
492
  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>;
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
@@ -109,7 +109,7 @@ const _def = Symbol('def');
109
109
  const _comment = Symbol('comment');
110
110
  exports._sqlCache = Symbol('sqlMap');
111
111
  exports._dao = Symbol('dao');
112
- exports._primaryDB = Symbol('primaryDB');
112
+ exports._primaryDB = '______primaryDB_______';
113
113
  const _dbType = Symbol('dbType');
114
114
  const _formatDialect = Symbol('FormatDialect');
115
115
  const _sqlite_version = Symbol('sqlite_version');
@@ -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,18 @@ 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
+ if (!option.dao) {
1547
+ const db = new Sqlite(new globalThis[exports._GlobalSqlOption].BetterSqlite3(dbName, { fileMustExist: false }));
1548
+ if (globalThis[exports._dao][this[_dbType]][exports._primaryDB] === undefined) {
1549
+ globalThis[exports._dao][this[_dbType]][exports._primaryDB] = db;
1550
+ }
1551
+ globalThis[exports._dao][this[_dbType]][dbName] = db;
1552
+ option.dao = db;
1553
+ }
1547
1554
  error_1.Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
1548
1555
  // 连接共享
1549
1556
  if (skipConn === false && !option.conn) {
@@ -1576,6 +1583,15 @@ function P(skipConn = false) {
1576
1583
  }
1577
1584
  }
1578
1585
  else if (this[_dbType] === DBType.SqliteRemote) {
1586
+ if (!option.dao) {
1587
+ globalThis[exports._GlobalSqlOption].SqliteRemote.service.initDB(dbName);
1588
+ const db = new SqliteRemote(globalThis[exports._GlobalSqlOption].SqliteRemote.service, dbName);
1589
+ if (globalThis[exports._dao][this[_dbType]][exports._primaryDB] === undefined) {
1590
+ globalThis[exports._dao][this[_dbType]][exports._primaryDB] = db;
1591
+ }
1592
+ globalThis[exports._dao][this[_dbType]][dbName] = db;
1593
+ option.dao = db;
1594
+ }
1579
1595
  error_1.Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
1580
1596
  return new Promise(async (resolve, reject) => {
1581
1597
  // 连接共享
@@ -1606,6 +1622,7 @@ function P(skipConn = false) {
1606
1622
  });
1607
1623
  }
1608
1624
  else if (this[_dbType] === DBType.Mysql) {
1625
+ error_1.Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1609
1626
  return new Promise(async (resolve, reject) => {
1610
1627
  try {
1611
1628
  // 连接共享
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;
@@ -28,8 +25,8 @@ export declare abstract class SqliteRemoteClass implements SqliteRemoteInterface
28
25
  get(inData: Uint8Array): Promise<Uint8Array>;
29
26
  raw(inData: Uint8Array): Promise<Uint8Array>;
30
27
  query(inData: Uint8Array): Promise<Uint8Array>;
31
- initDB(dbName: string): Promise<void>;
32
- export(dbName: string): Promise<void>;
33
- restore(dbName: string): Promise<void>;
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
  };
@@ -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
@@ -21,7 +21,7 @@ declare const _def: unique symbol;
21
21
  declare const _comment: unique symbol;
22
22
  export declare const _sqlCache: unique symbol;
23
23
  export declare const _dao: unique symbol;
24
- export declare const _primaryDB: unique symbol;
24
+ export declare const _primaryDB = "______primaryDB_______";
25
25
  declare const _dbType: unique symbol;
26
26
  declare const _formatDialect: unique symbol;
27
27
  declare const _sqlite_version: unique symbol;
@@ -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对象
@@ -488,9 +490,9 @@ export interface SqliteRemoteInterface {
488
490
  get(inData: Uint8Array): Promise<Uint8Array>;
489
491
  raw(inData: Uint8Array): Promise<Uint8Array>;
490
492
  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>;
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
@@ -70,7 +70,7 @@ const _def = Symbol('def');
70
70
  const _comment = Symbol('comment');
71
71
  export const _sqlCache = Symbol('sqlMap');
72
72
  export const _dao = Symbol('dao');
73
- export const _primaryDB = Symbol('primaryDB');
73
+ export const _primaryDB = '______primaryDB_______';
74
74
  const _dbType = Symbol('dbType');
75
75
  const _formatDialect = Symbol('FormatDialect');
76
76
  const _sqlite_version = Symbol('sqlite_version');
@@ -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,18 @@ 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
+ if (!option.dao) {
1503
+ const db = new Sqlite(new globalThis[_GlobalSqlOption].BetterSqlite3(dbName, { fileMustExist: false }));
1504
+ if (globalThis[_dao][this[_dbType]][_primaryDB] === undefined) {
1505
+ globalThis[_dao][this[_dbType]][_primaryDB] = db;
1506
+ }
1507
+ globalThis[_dao][this[_dbType]][dbName] = db;
1508
+ option.dao = db;
1509
+ }
1503
1510
  Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!');
1504
1511
  // 连接共享
1505
1512
  if (skipConn === false && !option.conn) {
@@ -1532,6 +1539,15 @@ function P(skipConn = false) {
1532
1539
  }
1533
1540
  }
1534
1541
  else if (this[_dbType] === DBType.SqliteRemote) {
1542
+ if (!option.dao) {
1543
+ globalThis[_GlobalSqlOption].SqliteRemote.service.initDB(dbName);
1544
+ const db = new SqliteRemote(globalThis[_GlobalSqlOption].SqliteRemote.service, dbName);
1545
+ if (globalThis[_dao][this[_dbType]][_primaryDB] === undefined) {
1546
+ globalThis[_dao][this[_dbType]][_primaryDB] = db;
1547
+ }
1548
+ globalThis[_dao][this[_dbType]][dbName] = db;
1549
+ option.dao = db;
1550
+ }
1535
1551
  Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!');
1536
1552
  return new Promise(async (resolve, reject) => {
1537
1553
  // 连接共享
@@ -1562,6 +1578,7 @@ function P(skipConn = false) {
1562
1578
  });
1563
1579
  }
1564
1580
  else if (this[_dbType] === DBType.Mysql) {
1581
+ Throw.if(!option.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1565
1582
  return new Promise(async (resolve, reject) => {
1566
1583
  try {
1567
1584
  // 连接共享
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;
@@ -28,8 +25,8 @@ export declare abstract class SqliteRemoteClass implements SqliteRemoteInterface
28
25
  get(inData: Uint8Array): Promise<Uint8Array>;
29
26
  raw(inData: Uint8Array): Promise<Uint8Array>;
30
27
  query(inData: Uint8Array): Promise<Uint8Array>;
31
- initDB(dbName: string): Promise<void>;
32
- export(dbName: string): Promise<void>;
33
- restore(dbName: string): Promise<void>;
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
@@ -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.26",
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
@@ -61,7 +61,7 @@ const _def = Symbol('def');
61
61
  const _comment = Symbol('comment');
62
62
  export const _sqlCache = Symbol('sqlMap');
63
63
  export const _dao = Symbol('dao');
64
- export const _primaryDB = Symbol('primaryDB');
64
+ export const _primaryDB = '______primaryDB_______';
65
65
  const _dbType = Symbol('dbType');
66
66
  const _formatDialect = Symbol('FormatDialect');
67
67
  const _sqlite_version = Symbol('sqlite_version');
@@ -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>
554
+ execute(inData: Uint8Array): Promise<Uint8Array>;
555
555
  pluck(inData: Uint8Array): Promise<Uint8Array>;
556
556
  get(inData: Uint8Array): Promise<Uint8Array>;
557
557
  raw(inData: Uint8Array): Promise<Uint8Array>;
558
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>;
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,21 @@ 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
+ if (!option!.dao) {
1887
+ const db = new Sqlite(new globalThis[_GlobalSqlOption].BetterSqlite3(dbName as any, { fileMustExist: false }));
1888
+ if (globalThis[_dao][this[_dbType]!][_primaryDB] === undefined) {
1889
+ globalThis[_dao][this[_dbType]!][_primaryDB] = db;
1890
+ }
1891
+ globalThis[_dao][this[_dbType]!][dbName] = db;
1892
+ option!.dao = db;
1893
+ }
1894
+
1885
1895
  Throw.if(option.sync === SyncMode.Async, 'sqlite can not Async!')
1886
1896
  // 连接共享
1887
1897
  if (skipConn === false && !option!.conn) {
@@ -1909,6 +1919,16 @@ function P<T extends object>(skipConn = false) {
1909
1919
  }
1910
1920
  }
1911
1921
  } else if (this[_dbType] === DBType.SqliteRemote) {
1922
+ if (!option!.dao) {
1923
+ globalThis[_GlobalSqlOption].SqliteRemote.service.initDB(dbName);
1924
+ const db = new SqliteRemote(globalThis[_GlobalSqlOption].SqliteRemote.service, dbName as any);
1925
+ if (globalThis[_dao][this[_dbType]!][_primaryDB] === undefined) {
1926
+ globalThis[_dao][this[_dbType]!][_primaryDB] = db;
1927
+ }
1928
+ globalThis[_dao][this[_dbType]!][dbName] = db;
1929
+ option!.dao = db;
1930
+ }
1931
+
1912
1932
  Throw.if(option.sync === SyncMode.Sync, 'SqliteRemote remote can not sync!')
1913
1933
  return new Promise(async (resolve, reject) => {
1914
1934
  // 连接共享
@@ -1936,6 +1956,7 @@ function P<T extends object>(skipConn = false) {
1936
1956
  });
1937
1957
 
1938
1958
  } else if (this[_dbType] === DBType.Mysql) {
1959
+ Throw.if(!option!.dao, `not found db:${String(dbName)}(${this[_dbType]})`);
1939
1960
  return new Promise(async (resolve, reject) => {
1940
1961
  try {
1941
1962
  // 连接共享
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
  /**
@@ -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
  }