Skip to content

Commit

Permalink
store/sqlite: add bun support
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Sep 21, 2023
1 parent 64ce252 commit b23f926
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
30 changes: 27 additions & 3 deletions packages/store/src/drivers/sqlite/Facade.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {filter, keymap, valmap} from "runtime-compat/object";
import typemap from "./typemap.js";
import {runtime} from "runtime-compat/meta";

const is_bun = runtime === "bun";

const filter_null = results =>
results.map(result => filter(result, ([, value]) => value !== null));
Expand Down Expand Up @@ -47,20 +50,30 @@ export default class Connection {
const {where, bindings} = predicate(criteria);
const query = `select * from ${collection} ${where}`;
const statement = this.connection.prepare(query);
statement.safeIntegers(true);
if (!is_bun) {
statement.safeIntegers(true);
}
return filter_null(statement.all(bindings));
}

count(collection, criteria = {}) {
const {where, bindings} = predicate(criteria);
const query = `select count(*) from ${collection} ${where}`;
return this.connection.prepare(query).pluck(true).get(bindings);
const prepared = this.connection.prepare(query);
if (is_bun) {
const [count] = Object.values(prepared.get(bindings));
return count;
}

return prepared.pluck(true).get(bindings);
}

get(collection, primary, value) {
const query = `select * from ${collection} where ${primary}=$primary`;
const statement = this.connection.prepare(query);
statement.safeIntegers(true);
if (!is_bun) {
statement.safeIntegers(true);
}
const result = statement.get({primary: value});
return result === undefined
? result
Expand All @@ -75,6 +88,17 @@ export default class Connection {
? `(${columns.join(",")}) values (${values})`
: "default values";
const query = `insert into ${collection} ${$predicate}`;

if (is_bun) {
const $document = keymap(document, key => `$${key}`);
this.connection.prepare(query).all($document);

// get last id
const query_last_id = `select id from ${collection} order by id desc`;
const {id} = this.connection.prepare(query_last_id).get();
return {...document, id};
}

const {lastInsertRowid: id} = this.connection.prepare(query).run(document);
return {...document, id};
}
Expand Down
6 changes: 4 additions & 2 deletions packages/store/src/drivers/sqlite/driver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {numeric} from "runtime-compat/invariant";
import {filter} from "runtime-compat/object";
import {runtime} from "runtime-compat/meta";
import ident from "../ident.js";
import {peers} from "../common/exports.js";
import depend from "../../depend.js";
Expand All @@ -17,10 +18,11 @@ const defaults = {
export default ({
filename = defaults.filename,
} = {}) => async () => {
const [{default: Connection}] = await depend(on, `store:${name}`);
const $on = runtime === "bun" ? {"bun:sqlite": null} : on;
const [{default: Connection}] = await depend($on, `store:${name}`);
const pool = new Pool({
manager: {
new: () => new Connection(filename, {}),
new: () => new Connection(filename, {create: true}),
kill: connection => connection.close(),
},
});
Expand Down

0 comments on commit b23f926

Please sign in to comment.