@sqb/sqljs 4.0.2 → 4.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/SqljsAdapter.d.ts +1 -0
- package/dist/SqljsAdapter.js +49 -11
- package/dist/SqljsConnection.d.ts +5 -3
- package/dist/SqljsConnection.js +18 -4
- package/package.json +1 -1
package/dist/SqljsAdapter.d.ts
CHANGED
package/dist/SqljsAdapter.js
CHANGED
|
@@ -3,13 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.SqljsAdapter = void 0;
|
|
6
|
+
exports.closeMemoryDatabase = exports.SqljsAdapter = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
8
|
require("@sqb/sqlite-dialect");
|
|
10
9
|
const sql_js_1 = __importDefault(require("sql.js"));
|
|
11
10
|
const SqljsConnection_1 = require("./SqljsConnection");
|
|
12
|
-
const
|
|
11
|
+
const path_1 = __importDefault(require("path"));
|
|
12
|
+
const putil_promisify_1 = __importDefault(require("putil-promisify"));
|
|
13
|
+
const dbCache = new Map();
|
|
13
14
|
class SqljsAdapter {
|
|
14
15
|
constructor() {
|
|
15
16
|
this.driver = 'sqljs';
|
|
@@ -22,14 +23,51 @@ class SqljsAdapter {
|
|
|
22
23
|
async connect(config) {
|
|
23
24
|
if (!config.database)
|
|
24
25
|
throw new Error('You must provide sqlite database file for sql.js driver');
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
let dbName = '';
|
|
27
|
+
let isMemory = false;
|
|
28
|
+
const m = config.database.match(/^(:memory:)(\w+)?$/);
|
|
29
|
+
if (m) {
|
|
30
|
+
isMemory = true;
|
|
31
|
+
dbName = config.database;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
dbName = path_1.default.resolve(config.database);
|
|
35
|
+
}
|
|
36
|
+
let intlDb = dbCache.get(dbName);
|
|
37
|
+
if (intlDb) {
|
|
38
|
+
intlDb._refCount++;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
const SQL = await (0, sql_js_1.default)();
|
|
42
|
+
if (isMemory) {
|
|
43
|
+
intlDb = new SQL.Database();
|
|
44
|
+
intlDb._refCount = 0;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
const buf = await putil_promisify_1.default.fromCallback(cb => fs_1.default.readFile(dbName, cb));
|
|
48
|
+
intlDb = new SQL.Database(buf);
|
|
49
|
+
intlDb._refCount = 1;
|
|
50
|
+
}
|
|
51
|
+
dbCache.set(dbName, intlDb);
|
|
52
|
+
}
|
|
53
|
+
const _intlDb = intlDb;
|
|
54
|
+
return new SqljsConnection_1.SqljsConnection(_intlDb, () => {
|
|
55
|
+
if (isMemory)
|
|
56
|
+
return;
|
|
57
|
+
if (--_intlDb._refCount <= 0) {
|
|
58
|
+
_intlDb.close();
|
|
59
|
+
dbCache.delete(dbName);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
33
62
|
}
|
|
34
63
|
}
|
|
35
64
|
exports.SqljsAdapter = SqljsAdapter;
|
|
65
|
+
async function closeMemoryDatabase(name) {
|
|
66
|
+
const memoryDbName = name || ':memory:';
|
|
67
|
+
const memDb = dbCache.get(memoryDbName);
|
|
68
|
+
if (memDb) {
|
|
69
|
+
dbCache.delete(memoryDbName);
|
|
70
|
+
memDb.close();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.closeMemoryDatabase = closeMemoryDatabase;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { Database } from 'sql.js';
|
|
1
2
|
import { Adapter, QueryRequest } from '@sqb/connect';
|
|
2
3
|
export declare class SqljsConnection implements Adapter.Connection {
|
|
3
|
-
private
|
|
4
|
-
|
|
4
|
+
private _onClose;
|
|
5
|
+
private intlcon?;
|
|
6
|
+
constructor(db: Database, _onClose: Function);
|
|
5
7
|
get sessionId(): any;
|
|
6
8
|
close(): Promise<void>;
|
|
7
9
|
reset(): Promise<void>;
|
|
@@ -10,5 +12,5 @@ export declare class SqljsConnection implements Adapter.Connection {
|
|
|
10
12
|
rollback(): Promise<void>;
|
|
11
13
|
test(): Promise<void>;
|
|
12
14
|
execute(query: QueryRequest): Promise<Adapter.Response>;
|
|
13
|
-
_convertFields
|
|
15
|
+
private _convertFields;
|
|
14
16
|
}
|
package/dist/SqljsConnection.js
CHANGED
|
@@ -3,19 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SqljsConnection = void 0;
|
|
4
4
|
const SqljsCursor_1 = require("./SqljsCursor");
|
|
5
5
|
class SqljsConnection {
|
|
6
|
-
constructor(db) {
|
|
6
|
+
constructor(db, _onClose) {
|
|
7
|
+
this._onClose = _onClose;
|
|
7
8
|
this.intlcon = db;
|
|
8
9
|
}
|
|
9
10
|
get sessionId() {
|
|
10
11
|
return 0;
|
|
11
12
|
}
|
|
12
13
|
async close() {
|
|
13
|
-
|
|
14
|
+
if (this.intlcon) {
|
|
15
|
+
this.intlcon = undefined;
|
|
16
|
+
await this._onClose();
|
|
17
|
+
}
|
|
14
18
|
}
|
|
15
19
|
async reset() {
|
|
16
20
|
return this.rollback();
|
|
17
21
|
}
|
|
18
22
|
async startTransaction() {
|
|
23
|
+
assertDefined(this.intlcon);
|
|
19
24
|
try {
|
|
20
25
|
this.intlcon.exec('BEGIN TRANSACTION;');
|
|
21
26
|
}
|
|
@@ -26,6 +31,7 @@ class SqljsConnection {
|
|
|
26
31
|
}
|
|
27
32
|
}
|
|
28
33
|
async commit() {
|
|
34
|
+
assertDefined(this.intlcon);
|
|
29
35
|
try {
|
|
30
36
|
this.intlcon.exec('COMMIT;');
|
|
31
37
|
}
|
|
@@ -36,6 +42,7 @@ class SqljsConnection {
|
|
|
36
42
|
}
|
|
37
43
|
}
|
|
38
44
|
async rollback() {
|
|
45
|
+
assertDefined(this.intlcon);
|
|
39
46
|
try {
|
|
40
47
|
this.intlcon.exec('ROLLBACK;');
|
|
41
48
|
}
|
|
@@ -46,16 +53,19 @@ class SqljsConnection {
|
|
|
46
53
|
}
|
|
47
54
|
}
|
|
48
55
|
async test() {
|
|
56
|
+
assertDefined(this.intlcon);
|
|
49
57
|
this.intlcon.exec('select 1');
|
|
50
58
|
}
|
|
51
59
|
async execute(query) {
|
|
60
|
+
assertDefined(this.intlcon);
|
|
52
61
|
if (!query.autoCommit)
|
|
53
62
|
await this.startTransaction();
|
|
54
63
|
const out = {};
|
|
55
64
|
let params;
|
|
56
65
|
if (query.params) {
|
|
57
|
-
|
|
58
|
-
|
|
66
|
+
const prms = query.params;
|
|
67
|
+
params = Object.keys(prms).reduce((obj, k) => {
|
|
68
|
+
obj[':' + k] = prms[k];
|
|
59
69
|
return obj;
|
|
60
70
|
}, {});
|
|
61
71
|
}
|
|
@@ -130,3 +140,7 @@ class SqljsConnection {
|
|
|
130
140
|
}
|
|
131
141
|
}
|
|
132
142
|
exports.SqljsConnection = SqljsConnection;
|
|
143
|
+
function assertDefined(d) {
|
|
144
|
+
if (d == null)
|
|
145
|
+
throw new Error("Invalid data");
|
|
146
|
+
}
|