-
Notifications
You must be signed in to change notification settings - Fork 25
feat: support for node:sqlite and sql.js
#614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
59bc177
Add sql.js fallback for sqlite in wasm
BobdenOs f456747
Update package.json
BobdenOs 3a56fe1
Adding sql.js test pipeline
BobdenOs e44a447
Double checking that the fallback is tested
BobdenOs 9b1281a
Adjust better-sqlite3 removal
BobdenOs 3ebf2f4
Remove debug logging
BobdenOs 1f5d2c7
Merge branch 'main' into sqlite/wasm
BobdenOs c4e2562
Merge branch 'main' into sqlite/wasm
BobdenOs 6546a7d
Remove linting warning
BobdenOs 8bc8319
merge
BobdenOs 19f4043
Bump sql.js version and fix streaming support
BobdenOs 54c4b27
Run sql.js tests after all other tests
BobdenOs 4235bf9
sync
BobdenOs 8f4a3e0
make better-sqlite3 an optional dependency
BobdenOs 15aa62b
add driver option for sqlite
BobdenOs 2123ecf
polyfill missing math functions for sql.js
BobdenOs 6644bfe
retain CDS_REQUIRES_DB_DRIVER for tests
BobdenOs a3367fa
add non default sqlite driver tests
BobdenOs 7c00fa9
Merge branch 'main' into sqlite/wasm
BobdenOs 5c17a7c
make better-sqlite3 a normal dependency again
BobdenOs 51fa585
Merge branch 'sqlite/wasm' of https://github.com/cap-js/cds-dbs into …
BobdenOs 12be223
add sql.js as dev dependency for testing
BobdenOs 94d2df2
Apply suggestion from @johannes-vogel
johannes-vogel 2f8ce65
Merge branch 'main' into sqlite/wasm
sjvans 03a4dcb
move common env variables to job level
BobdenOs d67fd65
Merge branch 'sqlite/wasm' of https://github.com/cap-js/cds-dbs into …
BobdenOs 0055a0d
Merge branch 'main' into sqlite/wasm
BobdenOs 149df0b
rm legacy
johannes-vogel 8764066
Disable builtin pool for Postgres tests
BobdenOs f8d2604
Merge branch 'sqlite/wasm' of https://github.com/cap-js/cds-dbs into …
BobdenOs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| const { DatabaseSync } = require('node:sqlite'); | ||
|
|
||
| class NodeSqlite extends DatabaseSync { | ||
| prepare(sql) { | ||
| const stmt = super.prepare(sql) | ||
| const ret = { | ||
| run(params) { | ||
| try { | ||
| params = Array.isArray(params) ? params : [params] | ||
| return stmt.run(...params) | ||
| } catch (err) { | ||
| if (err.message.indexOf('NOT NULL constraint failed:') === 0) { | ||
| err.code = 'SQLITE_CONSTRAINT_NOTNULL' | ||
| } | ||
| throw err | ||
| } | ||
| }, | ||
| get(params) { | ||
| params = Array.isArray(params) ? params : [params] | ||
| return stmt.get(...params) | ||
| }, | ||
| all(params) { | ||
| params = Array.isArray(params) ? params : [params] | ||
| return stmt.all(...params) | ||
| }, | ||
| iterate(params) { | ||
| stmt.setReturnArrays(true) | ||
| params = Array.isArray(params) ? params : [params] | ||
| return stmt.iterate(...params) | ||
| } | ||
| } | ||
| return ret | ||
| } | ||
| } | ||
|
|
||
| module.exports = NodeSqlite |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| const initSqlJs = require('sql.js'); | ||
|
|
||
| const init = initSqlJs({}) | ||
|
|
||
| class WasmSqlite { | ||
| constructor(/*database*/) { | ||
| // TODO: load / store database file contents | ||
| this.ready = init | ||
| .then(SQL => { | ||
| this.db = new SQL.Database() | ||
| // polyfill for missing or mismatched default sqlite3 math functions | ||
| this.db.create_function('ln', x => Math.log(x)) | ||
| this.db.create_function('log', (x) => Math.log10(x)) | ||
| this.db.create_function('log', (x, y) => Math.log(y) / Math.log(x)) | ||
| this.db.create_function('mod', (x, y) => x % y) | ||
| }) | ||
|
|
||
| this.memory = true | ||
| this.gc = new FinalizationRegistry(stmt => { stmt.free() }) | ||
| } | ||
|
|
||
| prepare(sql) { | ||
| const stmt = this.db.prepare(sql) | ||
| const ret = { | ||
| run(params) { | ||
| try { | ||
| stmt.bind(params) | ||
| stmt.step() | ||
| return { changes: stmt.db.getRowsModified(stmt) } | ||
| } catch (err) { | ||
| if (err.message.indexOf('NOT NULL constraint failed:') === 0) { | ||
| err.code = 'SQLITE_CONSTRAINT_NOTNULL' | ||
| } | ||
| throw err | ||
| } | ||
| }, | ||
| get(params) { | ||
| const columns = stmt.getColumnNames() | ||
| stmt.bind(params) | ||
| stmt.step() | ||
| const row = stmt.get() | ||
| const ret = {} | ||
| for (let i = 0; i < columns.length; i++) { | ||
| ret[columns[i]] = row[i] | ||
| } | ||
| return ret | ||
| }, | ||
| all(params) { | ||
| const columns = stmt.getColumnNames() | ||
| const ret = [] | ||
| stmt.bind(params) | ||
| while (stmt.step()) { | ||
| const row = stmt.get() | ||
| const obj = {} | ||
| for (let i = 0; i < columns.length; i++) { | ||
| obj[columns[i]] = row[i] | ||
| } | ||
| ret.push(obj) | ||
| } | ||
| return ret | ||
| }, | ||
| *iterate(params) { | ||
| stmt.bind(params) | ||
| while (stmt.step()) { | ||
| yield stmt.get() | ||
| } | ||
| } | ||
| } | ||
| this.gc.register(ret, stmt) | ||
| return ret | ||
| } | ||
|
|
||
| exec(sql) { | ||
| try { | ||
| const { columns, values } = this.db.exec(sql) | ||
| return !Array.isArray(values) ? values : values.map(val => { | ||
| const ret = {} | ||
| for (let i = 0; i < columns.length; i++) { | ||
| ret[columns[i]] = val[i] | ||
| } | ||
| return ret | ||
| }) | ||
| } catch (err) { | ||
| // REVISIT: address transaction errors | ||
| if (sql === 'BEGIN' || sql === 'ROLLBACK') { return } | ||
| throw err | ||
| } | ||
| } | ||
|
|
||
| function(name, config, func) { | ||
| this.db.create_function(name, func || config) | ||
| } | ||
|
|
||
| close() { this.db.close() } | ||
| } | ||
|
|
||
| module.exports = WasmSqlite |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.