@sqb/sqljs 4.20.6 → 4.21.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sqb/sqljs",
3
3
  "description": "SQB serialization extension for sql.js driver",
4
- "version": "4.20.6",
4
+ "version": "4.21.0",
5
5
  "author": "Panates",
6
6
  "license": "Apache-2.0",
7
7
  "dependencies": {
@@ -9,29 +9,24 @@
9
9
  "tslib": "^2.8.1"
10
10
  },
11
11
  "peerDependencies": {
12
- "@sqb/builder": "^4.20.6",
13
- "@sqb/connect": "^4.20.6",
14
- "@sqb/sqlite-dialect": "^4.20.6",
12
+ "@sqb/builder": "^4.21.0",
13
+ "@sqb/connect": "^4.21.0",
14
+ "@sqb/sqlite-dialect": "^4.21.0",
15
15
  "sql.js": "^1.13.0"
16
16
  },
17
17
  "type": "module",
18
+ "module": "./index.js",
19
+ "types": "./index.d.ts",
18
20
  "exports": {
19
21
  ".": {
20
- "import": {
21
- "types": "./types/index.d.ts",
22
- "default": "./esm/index.js"
23
- },
24
- "require": {
25
- "types": "./types/index.d.cts",
26
- "default": "./cjs/index.js"
27
- },
28
- "default": "./esm/index.js"
22
+ "types": "./index.d.ts",
23
+ "default": "./index.js"
29
24
  },
30
25
  "./package.json": "./package.json"
31
26
  },
32
- "main": "./cjs/index.js",
33
- "module": "./esm/index.js",
34
- "types": "./types/index.d.ts",
27
+ "engines": {
28
+ "node": ">=20.0"
29
+ },
35
30
  "contributors": [
36
31
  "Eray Hanoglu <e.hanoglu@panates.com>",
37
32
  "Ilker Gurelli <i.gurelli@panates.com>"
@@ -41,17 +36,6 @@
41
36
  "url": "https://github.com/panates/sqb.git",
42
37
  "directory": "packages/sqljs"
43
38
  },
44
- "engines": {
45
- "node": ">=18.0"
46
- },
47
- "files": [
48
- "bin/",
49
- "cjs/",
50
- "esm/",
51
- "types/",
52
- "LICENSE",
53
- "README.md"
54
- ],
55
39
  "keywords": [
56
40
  "sqb",
57
41
  "sqlite",
package/cjs/index.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const connect_1 = require("@sqb/connect");
4
- const sqljs_adapter_js_1 = require("./sqljs-adapter.js");
5
- connect_1.AdapterRegistry.register(new sqljs_adapter_js_1.SqljsAdapter());
package/cjs/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
@@ -1,69 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SqljsAdapter = void 0;
4
- exports.closeMemoryDatabase = closeMemoryDatabase;
5
- const tslib_1 = require("tslib");
6
- require("@sqb/sqlite-dialect");
7
- const fs_1 = tslib_1.__importDefault(require("fs"));
8
- const path_1 = tslib_1.__importDefault(require("path"));
9
- const putil_promisify_1 = tslib_1.__importDefault(require("putil-promisify"));
10
- const sql_js_1 = tslib_1.__importDefault(require("sql.js"));
11
- const sqljs_connection_js_1 = require("./sqljs-connection.js");
12
- const dbCache = new Map();
13
- class SqljsAdapter {
14
- driver = 'sqljs';
15
- dialect = 'sqlite';
16
- features = {
17
- cursor: true,
18
- // fetchAsString: [DataType.DATE, DataType.TIMESTAMP, DataType.TIMESTAMPTZ]
19
- };
20
- async connect(config) {
21
- if (!config.database)
22
- throw new Error('You must provide sqlite database file for sql.js driver');
23
- let dbName = '';
24
- let isMemory = false;
25
- const m = config.database.match(/^(:memory:)(\w+)?$/);
26
- if (m) {
27
- isMemory = true;
28
- dbName = config.database;
29
- }
30
- else {
31
- dbName = path_1.default.resolve(config.database);
32
- }
33
- let intlDb = dbCache.get(dbName);
34
- if (intlDb) {
35
- intlDb._refCount++;
36
- }
37
- else {
38
- const SQL = await (0, sql_js_1.default)();
39
- if (isMemory) {
40
- intlDb = new SQL.Database();
41
- intlDb._refCount = 0;
42
- }
43
- else {
44
- const buf = await putil_promisify_1.default.fromCallback(cb => fs_1.default.readFile(dbName, cb));
45
- intlDb = new SQL.Database(buf);
46
- intlDb._refCount = 1;
47
- }
48
- dbCache.set(dbName, intlDb);
49
- }
50
- const _intlDb = intlDb;
51
- return new sqljs_connection_js_1.SqljsConnection(_intlDb, () => {
52
- if (isMemory)
53
- return;
54
- if (--_intlDb._refCount <= 0) {
55
- _intlDb.close();
56
- dbCache.delete(dbName);
57
- }
58
- });
59
- }
60
- }
61
- exports.SqljsAdapter = SqljsAdapter;
62
- async function closeMemoryDatabase(name) {
63
- const memoryDbName = name || ':memory:';
64
- const memDb = dbCache.get(memoryDbName);
65
- if (memDb) {
66
- dbCache.delete(memoryDbName);
67
- memDb.close();
68
- }
69
- }
@@ -1,147 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SqljsConnection = void 0;
4
- const sqljs_cursor_js_1 = require("./sqljs-cursor.js");
5
- class SqljsConnection {
6
- _onClose;
7
- intlcon;
8
- constructor(db, _onClose) {
9
- this._onClose = _onClose;
10
- this.intlcon = db;
11
- }
12
- get sessionId() {
13
- return 0;
14
- }
15
- async close() {
16
- if (this.intlcon) {
17
- this.intlcon = undefined;
18
- await this._onClose();
19
- }
20
- }
21
- async reset() {
22
- return this.rollback();
23
- }
24
- async startTransaction() {
25
- assertDefined(this.intlcon);
26
- try {
27
- this.intlcon.exec('BEGIN TRANSACTION;');
28
- }
29
- catch (e) {
30
- if (e instanceof Error && e.message.match(/within a transaction/))
31
- return;
32
- throw e;
33
- }
34
- }
35
- async commit() {
36
- assertDefined(this.intlcon);
37
- try {
38
- this.intlcon.exec('COMMIT;');
39
- }
40
- catch (e) {
41
- if (e instanceof Error && e.message.match(/no transaction/))
42
- return;
43
- throw e;
44
- }
45
- }
46
- async rollback() {
47
- assertDefined(this.intlcon);
48
- try {
49
- this.intlcon.exec('ROLLBACK;');
50
- }
51
- catch (e) {
52
- if (e instanceof Error && e.message.match(/no transaction/))
53
- return;
54
- throw e;
55
- }
56
- }
57
- async test() {
58
- assertDefined(this.intlcon);
59
- this.intlcon.exec('select 1');
60
- }
61
- async execute(query) {
62
- assertDefined(this.intlcon);
63
- if (!query.autoCommit)
64
- await this.startTransaction();
65
- const out = {};
66
- let params;
67
- if (query.params) {
68
- const prms = query.params;
69
- params = Object.keys(prms).reduce((obj, k) => {
70
- obj[':' + k] = prms[k];
71
- return obj;
72
- }, {});
73
- }
74
- const m = query.sql.match(/\b(insert into|update|delete from)\b ("?\w+"?)/i);
75
- if (m) {
76
- const stmt = this.intlcon.prepare(query.sql);
77
- stmt.run(params);
78
- stmt.free();
79
- out.rowsAffected = this.intlcon.getRowsModified();
80
- if (query.autoCommit)
81
- await this.commit();
82
- if (out.rowsAffected === 1 && query.returningFields) {
83
- const selectFields = query.returningFields.map(x => x.field + (x.alias ? ' as ' + x.alias : ''));
84
- let sql = `select ${selectFields.join(',')} from ${m[2]}\n`;
85
- // Emulate insert into ... returning
86
- if (m[1].toLowerCase() === 'insert into') {
87
- sql += 'where rowid=last_insert_rowid();';
88
- const r = this.intlcon.exec(sql);
89
- if (r.length) {
90
- out.fields = this._convertFields(r[0].columns);
91
- out.rows = r[0].values;
92
- out.rowType = 'array';
93
- }
94
- return out;
95
- }
96
- // Emulate update ... returning
97
- if (m[1].toLowerCase() === 'update') {
98
- const m2 = query.sql.match(/where (.+)/);
99
- query = { ...query };
100
- query.sql = sql + (m2 ? ' where ' + m2[1] : '');
101
- }
102
- else
103
- return out;
104
- }
105
- }
106
- let stmt = this.intlcon.prepare(query.sql, query.params);
107
- try {
108
- const colNames = stmt.getColumnNames();
109
- if (colNames && colNames.length) {
110
- out.fields = this._convertFields(colNames);
111
- const rowType = query.objectRows ? 'object' : 'array';
112
- out.rowType = rowType;
113
- const cursor = new sqljs_cursor_js_1.SqljsCursor(stmt, { rowType });
114
- if (query.cursor) {
115
- out.cursor = cursor;
116
- stmt = undefined;
117
- }
118
- else
119
- out.rows = await cursor.fetch(query.fetchRows || 100);
120
- }
121
- return out;
122
- }
123
- finally {
124
- if (stmt)
125
- stmt.free();
126
- }
127
- }
128
- _convertFields(fields) {
129
- const result = [];
130
- for (let i = 0; i < fields.length; i++) {
131
- const v = fields[i];
132
- const o = {
133
- fieldName: v,
134
- dataType: 'any',
135
- jsType: 'any',
136
- _inf: { name: v },
137
- };
138
- result.push(o);
139
- }
140
- return result;
141
- }
142
- }
143
- exports.SqljsConnection = SqljsConnection;
144
- function assertDefined(d) {
145
- if (d == null)
146
- throw new Error('Invalid data');
147
- }
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SqljsCursor = void 0;
4
- class SqljsCursor {
5
- _stmt;
6
- _rowType;
7
- constructor(stmt, opts) {
8
- this._rowType = opts.rowType;
9
- this._stmt = stmt;
10
- }
11
- get isClosed() {
12
- return !this._stmt;
13
- }
14
- get rowType() {
15
- return this._rowType;
16
- }
17
- async close() {
18
- if (!this._stmt)
19
- return;
20
- this._stmt.free();
21
- this._stmt = undefined;
22
- }
23
- async fetch(nRows) {
24
- if (!this._stmt)
25
- return;
26
- const stmt = this._stmt;
27
- const rows = [];
28
- while (nRows-- && stmt.step()) {
29
- rows.push(this.rowType === 'object' ? stmt.getAsObject() : stmt.get());
30
- }
31
- return rows;
32
- }
33
- }
34
- exports.SqljsCursor = SqljsCursor;
package/esm/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
package/types/index.d.cts DELETED
@@ -1 +0,0 @@
1
- export {};
File without changes
File without changes
File without changes
File without changes
File without changes