@cap-js/sqlite 2.1.3 → 2.2.1
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/CHANGELOG.md +34 -0
- package/README.md +3 -3
- package/lib/SQLiteService.js +51 -22
- package/lib/node-sqlite.js +36 -0
- package/lib/sql.js.js +99 -0
- package/package.json +11 -5
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,40 @@
|
|
|
4
4
|
- The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
5
5
|
- This project adheres to [Semantic Versioning](http://semver.org/).
|
|
6
6
|
|
|
7
|
+
## [2.2.1](https://github.com/cap-js/cds-dbs/compare/sqlite-v2.2.0...sqlite-v2.2.1) (2026-04-22)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
* sqlite generated key is named lastInsertRowid ([#1501](https://github.com/cap-js/cds-dbs/issues/1501)) ([a4d3437](https://github.com/cap-js/cds-dbs/commit/a4d34378297c8afdb13abb7e664165012c36eb8f))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Dependencies
|
|
16
|
+
|
|
17
|
+
* The following workspace dependencies were updated
|
|
18
|
+
* dependencies
|
|
19
|
+
* @cap-js/db-service bumped from ^2.9.0 to ^2.10.0
|
|
20
|
+
|
|
21
|
+
## [2.2.0](https://github.com/cap-js/cds-dbs/compare/sqlite-v2.1.3...sqlite-v2.2.0) (2026-03-09)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
|
|
26
|
+
* support for `node:sqlite` and `sql.js` ([#614](https://github.com/cap-js/cds-dbs/issues/614)) ([887d1bb](https://github.com/cap-js/cds-dbs/commit/887d1bba82347857364bc133983b9e16e054d6cd))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Fixed
|
|
30
|
+
|
|
31
|
+
* hana groupby with path expressions ([#1493](https://github.com/cap-js/cds-dbs/issues/1493)) ([920acde](https://github.com/cap-js/cds-dbs/commit/920acdee0edb538b74d6e5cb0a82a086556df85e))
|
|
32
|
+
* the combination of `iterator` and `SELECT.one` ([#1514](https://github.com/cap-js/cds-dbs/issues/1514)) ([4b28579](https://github.com/cap-js/cds-dbs/commit/4b2857920a7a57bcfc09a9b5fb765283cf8bd70b))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### Dependencies
|
|
36
|
+
|
|
37
|
+
* The following workspace dependencies were updated
|
|
38
|
+
* dependencies
|
|
39
|
+
* @cap-js/db-service bumped from ^2.8.2 to ^2.9.0
|
|
40
|
+
|
|
7
41
|
## [2.1.3](https://github.com/cap-js/cds-dbs/compare/sqlite-v2.1.2...sqlite-v2.1.3) (2026-02-03)
|
|
8
42
|
|
|
9
43
|
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ If you want to use SQLite for development, all you need to do is to install the
|
|
|
10
10
|
npm add @cap-js/sqlite -D
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
Learn more about setup and usage in the [respective database guide](https://cap.cloud.sap/docs/guides/databases
|
|
13
|
+
Learn more about setup and usage in the [respective database guide](https://cap.cloud.sap/docs/guides/databases/sqlite).
|
|
14
14
|
|
|
15
15
|
## Support
|
|
16
16
|
|
|
@@ -18,7 +18,7 @@ This project is open to feature requests/suggestions, bug reports etc. via [GitH
|
|
|
18
18
|
|
|
19
19
|
## Contribution
|
|
20
20
|
|
|
21
|
-
Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).
|
|
21
|
+
Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](../CONTRIBUTING.md).
|
|
22
22
|
|
|
23
23
|
## Versioning
|
|
24
24
|
|
|
@@ -27,7 +27,7 @@ All notable changes are documented in [CHANGELOG.md](CHANGELOG.md).
|
|
|
27
27
|
|
|
28
28
|
## Code of Conduct
|
|
29
29
|
|
|
30
|
-
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone. By participating in this project, you agree to abide by its [Code of Conduct](CODE_OF_CONDUCT.md) at all times.
|
|
30
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone. By participating in this project, you agree to abide by its [Code of Conduct](../CODE_OF_CONDUCT.md) at all times.
|
|
31
31
|
|
|
32
32
|
## Licensing
|
|
33
33
|
|
package/lib/SQLiteService.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const { SQLService } = require('@cap-js/db-service')
|
|
2
|
-
const cds = require('@sap/cds')
|
|
3
|
-
|
|
2
|
+
const cds = require('@sap/cds/lib')
|
|
3
|
+
let sqlite // sqlite driver is loaded on connect
|
|
4
|
+
|
|
4
5
|
const $session = Symbol('dbc.session')
|
|
5
6
|
const sessionVariableMap = require('./session.json') // Adjust the path as necessary for your project
|
|
6
7
|
const convStrm = require('stream/consumers')
|
|
@@ -28,21 +29,29 @@ class SQLiteService extends SQLService {
|
|
|
28
29
|
get factory() {
|
|
29
30
|
return {
|
|
30
31
|
options: this.options.pool || {},
|
|
31
|
-
create: tenant => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
32
|
+
create: async tenant => {
|
|
33
|
+
try {
|
|
34
|
+
if (!sqlite) loadSQLite(this.options.driver || this.options.credentials?.driver)
|
|
35
|
+
const database = this.url4(tenant)
|
|
36
|
+
const dbc = new sqlite(database, this.options.client || {})
|
|
37
|
+
await dbc.ready
|
|
38
|
+
|
|
39
|
+
const deterministic = { deterministic: true }
|
|
40
|
+
dbc.function('session_context', key => dbc[$session][key])
|
|
41
|
+
dbc.function('regexp', deterministic, (re, x) => (RegExp(re).test(x) ? 1 : 0))
|
|
42
|
+
dbc.function('ISO', deterministic, d => d && new Date(d).toISOString())
|
|
43
|
+
dbc.function('year', deterministic, d => d === null ? null : toDate(d).getUTCFullYear())
|
|
44
|
+
dbc.function('month', deterministic, d => d === null ? null : toDate(d).getUTCMonth() + 1)
|
|
45
|
+
dbc.function('day', deterministic, d => d === null ? null : toDate(d).getUTCDate())
|
|
46
|
+
dbc.function('hour', deterministic, d => d === null ? null : toDate(d, true).getUTCHours())
|
|
47
|
+
dbc.function('minute', deterministic, d => d === null ? null : toDate(d, true).getUTCMinutes())
|
|
48
|
+
dbc.function('second', deterministic, d => d === null ? null : toDate(d, true).getUTCSeconds())
|
|
49
|
+
if (database !== ':memory:') dbc.pragma?.('journal_mode = WAL') || dbc.exec('PRAGMA journal_mode = WAL')
|
|
50
|
+
return dbc
|
|
51
|
+
} catch (err) {
|
|
52
|
+
Promise.reject(err)
|
|
53
|
+
await new Promise(() => { })
|
|
54
|
+
}
|
|
46
55
|
},
|
|
47
56
|
destroy: dbc => dbc.close(),
|
|
48
57
|
validate: dbc => dbc.open,
|
|
@@ -106,7 +115,10 @@ class SQLiteService extends SQLService {
|
|
|
106
115
|
const pageSize = (1 << 16)
|
|
107
116
|
// Allow for both array and iterator result sets
|
|
108
117
|
const first = Array.isArray(rs) ? { done: !rs[0], value: rs[0] } : rs.next()
|
|
109
|
-
if (first.done)
|
|
118
|
+
if (first.done) {
|
|
119
|
+
yield one ? 'null' : '[]'
|
|
120
|
+
return
|
|
121
|
+
}
|
|
110
122
|
if (one) {
|
|
111
123
|
yield first.value[0]
|
|
112
124
|
// Close result set to release database connection
|
|
@@ -134,10 +146,8 @@ class SQLiteService extends SQLService {
|
|
|
134
146
|
}
|
|
135
147
|
|
|
136
148
|
async _allStream(stmt, binding_params, one, objectMode) {
|
|
137
|
-
stmt = stmt.
|
|
138
|
-
stmt.raw(true)
|
|
139
|
-
const get = stmt.get(binding_params)
|
|
140
|
-
if (!get) return []
|
|
149
|
+
stmt = stmt.iterate ? stmt : stmt.__proto__
|
|
150
|
+
stmt.raw?.(true)
|
|
141
151
|
const rs = stmt.iterate(binding_params)
|
|
142
152
|
const stream = Readable.from(objectMode ? this._iteratorObjectMode(rs) : this._iteratorRaw(rs, one), { objectMode })
|
|
143
153
|
const close = () => rs.return() // finish result set when closed early
|
|
@@ -293,4 +303,23 @@ class SQLiteService extends SQLService {
|
|
|
293
303
|
}
|
|
294
304
|
}
|
|
295
305
|
|
|
306
|
+
function loadSQLite(driver) {
|
|
307
|
+
const drivers = {
|
|
308
|
+
node: './node-sqlite.js',
|
|
309
|
+
'better-sqlite3': 'better-sqlite3',
|
|
310
|
+
'sql.js': './sql.js.js',
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (driver) {
|
|
314
|
+
sqlite = require(drivers[driver])
|
|
315
|
+
return
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
try { sqlite = require(drivers['better-sqlite3']) }
|
|
319
|
+
catch {
|
|
320
|
+
try { sqlite = require(drivers.node) }
|
|
321
|
+
catch { sqlite = require(drivers['sql.js']) }
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
296
325
|
module.exports = SQLiteService
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const { DatabaseSync } = require('node:sqlite');
|
|
2
|
+
|
|
3
|
+
class NodeSqlite extends DatabaseSync {
|
|
4
|
+
prepare(sql) {
|
|
5
|
+
const stmt = super.prepare(sql)
|
|
6
|
+
const ret = {
|
|
7
|
+
run(params) {
|
|
8
|
+
try {
|
|
9
|
+
params = Array.isArray(params) ? params : [params]
|
|
10
|
+
return stmt.run(...params)
|
|
11
|
+
} catch (err) {
|
|
12
|
+
if (err.message.indexOf('NOT NULL constraint failed:') === 0) {
|
|
13
|
+
err.code = 'SQLITE_CONSTRAINT_NOTNULL'
|
|
14
|
+
}
|
|
15
|
+
throw err
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
get(params) {
|
|
19
|
+
params = Array.isArray(params) ? params : [params]
|
|
20
|
+
return stmt.get(...params)
|
|
21
|
+
},
|
|
22
|
+
all(params) {
|
|
23
|
+
params = Array.isArray(params) ? params : [params]
|
|
24
|
+
return stmt.all(...params)
|
|
25
|
+
},
|
|
26
|
+
iterate(params) {
|
|
27
|
+
stmt.setReturnArrays(true)
|
|
28
|
+
params = Array.isArray(params) ? params : [params]
|
|
29
|
+
return stmt.iterate(...params)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return ret
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = NodeSqlite
|
package/lib/sql.js.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const initSqlJs = require('sql.js');
|
|
2
|
+
|
|
3
|
+
const init = initSqlJs({})
|
|
4
|
+
|
|
5
|
+
class WasmSqlite {
|
|
6
|
+
constructor(/*database*/) {
|
|
7
|
+
// TODO: load / store database file contents
|
|
8
|
+
this.ready = init
|
|
9
|
+
.then(SQL => {
|
|
10
|
+
this.db = new SQL.Database()
|
|
11
|
+
// polyfill for missing or mismatched default sqlite3 math functions
|
|
12
|
+
this.db.create_function('ln', x => Math.log(x))
|
|
13
|
+
this.db.create_function('log', (x) => Math.log10(x))
|
|
14
|
+
this.db.create_function('log', (x, y) => Math.log(y) / Math.log(x))
|
|
15
|
+
this.db.create_function('mod', (x, y) => x % y)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
this.memory = true
|
|
19
|
+
this.gc = new FinalizationRegistry(stmt => { stmt.free() })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
prepare(sql) {
|
|
23
|
+
const stmt = this.db.prepare(sql)
|
|
24
|
+
const ret = {
|
|
25
|
+
run(params) {
|
|
26
|
+
try {
|
|
27
|
+
stmt.bind(params)
|
|
28
|
+
stmt.step()
|
|
29
|
+
const changes = stmt.db.getRowsModified(stmt)
|
|
30
|
+
const lastInsertRowid = stmt.db.exec('SELECT last_insert_rowid()')[0]?.values[0][0]
|
|
31
|
+
return { changes, lastInsertRowid }
|
|
32
|
+
} catch (err) {
|
|
33
|
+
if (err.message.indexOf('NOT NULL constraint failed:') === 0) {
|
|
34
|
+
err.code = 'SQLITE_CONSTRAINT_NOTNULL'
|
|
35
|
+
}
|
|
36
|
+
throw err
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
get(params) {
|
|
40
|
+
const columns = stmt.getColumnNames()
|
|
41
|
+
stmt.bind(params)
|
|
42
|
+
stmt.step()
|
|
43
|
+
const row = stmt.get()
|
|
44
|
+
const ret = {}
|
|
45
|
+
for (let i = 0; i < columns.length; i++) {
|
|
46
|
+
ret[columns[i]] = row[i]
|
|
47
|
+
}
|
|
48
|
+
return ret
|
|
49
|
+
},
|
|
50
|
+
all(params) {
|
|
51
|
+
const columns = stmt.getColumnNames()
|
|
52
|
+
const ret = []
|
|
53
|
+
stmt.bind(params)
|
|
54
|
+
while (stmt.step()) {
|
|
55
|
+
const row = stmt.get()
|
|
56
|
+
const obj = {}
|
|
57
|
+
for (let i = 0; i < columns.length; i++) {
|
|
58
|
+
obj[columns[i]] = row[i]
|
|
59
|
+
}
|
|
60
|
+
ret.push(obj)
|
|
61
|
+
}
|
|
62
|
+
return ret
|
|
63
|
+
},
|
|
64
|
+
*iterate(params) {
|
|
65
|
+
stmt.bind(params)
|
|
66
|
+
while (stmt.step()) {
|
|
67
|
+
yield stmt.get()
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
this.gc.register(ret, stmt)
|
|
72
|
+
return ret
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exec(sql) {
|
|
76
|
+
try {
|
|
77
|
+
const { columns, values } = this.db.exec(sql)
|
|
78
|
+
return !Array.isArray(values) ? values : values.map(val => {
|
|
79
|
+
const ret = {}
|
|
80
|
+
for (let i = 0; i < columns.length; i++) {
|
|
81
|
+
ret[columns[i]] = val[i]
|
|
82
|
+
}
|
|
83
|
+
return ret
|
|
84
|
+
})
|
|
85
|
+
} catch (err) {
|
|
86
|
+
// REVISIT: address transaction errors
|
|
87
|
+
if (sql === 'BEGIN' || sql === 'ROLLBACK') { return }
|
|
88
|
+
throw err
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function(name, config, func) {
|
|
93
|
+
this.db.create_function(name, func || config)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
close() { this.db.close() }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = WasmSqlite
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cap-js/sqlite",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "CDS database service for SQLite",
|
|
5
5
|
"homepage": "https://github.com/cap-js/cds-dbs/tree/main/sqlite#cds-database-service-for-sqlite",
|
|
6
6
|
"repository": {
|
|
@@ -26,11 +26,17 @@
|
|
|
26
26
|
"test": "cds-test"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
29
|
+
"better-sqlite3": "^12.0.0",
|
|
30
|
+
"@cap-js/db-service": "^2.10.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@sap/cds": ">=9"
|
|
33
|
+
"@sap/cds": ">=9.8",
|
|
34
|
+
"sql.js": "^1.13.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"sql.js": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
34
40
|
},
|
|
35
41
|
"cds": {
|
|
36
42
|
"requires": {
|
|
@@ -54,4 +60,4 @@
|
|
|
54
60
|
}
|
|
55
61
|
},
|
|
56
62
|
"license": "Apache-2.0"
|
|
57
|
-
}
|
|
63
|
+
}
|