-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat: Add sql.js
fallback for sqlite
in wasm
#614
Open
BobdenOs
wants to merge
9
commits into
main
Choose a base branch
from
sqlite/wasm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 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 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 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,37 @@ | ||
name: Tests WASM | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
types: [opened, synchronize, reopened, auto_merge_enabled] | ||
|
||
# Allow parallel jobs on `main`, so that each commit is tested. For PRs, run only the latest commit. | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
name: Node.js ${{ matrix.node }} | ||
|
||
strategy: | ||
fail-fast: true | ||
matrix: | ||
node: [18] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: ${{ matrix.node }} | ||
cache: 'npm' | ||
|
||
- run: npm ci | ||
# Remove better-sqlite3 to force switching to sql.js | ||
- run: npm install sql.js && rm -rf node_modules/better-sqlite3/ | ||
- run: npm test -w sqlite -- --maxWorkers=1 | ||
env: | ||
FORCE_COLOR: true |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,84 @@ | ||
const initSqlJs = require('sql.js'); | ||
|
||
const init = initSqlJs({}) | ||
|
||
class WasmSqlite { | ||
constructor(database) { | ||
Check warning on line 6 in sqlite/lib/sql.js.js GitHub Actions / Node.js 18
|
||
// TODO: load / store database file contents | ||
this.ready = init | ||
.then(SQL => { this.db = new SQL.Database() }) | ||
|
||
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: this.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 | ||
} | ||
} | ||
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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we get rid of this warning?