Skip to content

Statically import libsql targets #89

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,49 @@ const {
statementColumns,
statementSafeIntegers,
rowsNext,
} = require(`@libsql/${target}`);
} = requireLibsql(target);

const SqliteError = require("./sqlite-error");

/**
* Statically require the correct libsql for the given target.
* Workaround for bundlers that don't support dynamic requires.
* e.g.: Bun single-executable build mode.
* @param {*} target
* @returns
*/
function requireLibsql(target) {
if (target === "darwin-arm64") {
return require("@libsql/darwin-arm64");
}

if (target === "darwin-x64") {
return require("@libsql/darwin-x64");
}

if (target === "linux-arm64-gnu") {
return require("@libsql/linux-arm64-gnu");
}

if (target === "linux-arm64-musl") {
return require("@libsql/linux-arm64-musl");
}

if (target === "linux-x64-gnu") {
return require("@libsql/linux-x64-gnu");
}

if (target === "linux-x64-musl") {
return require("@libsql/linux-x64-musl");
}

if (target === "win32-x64-msvc") {
return require("@libsql/win32-x64-msvc");
}

throw new Error(`Unsupported target: ${target}`);
}

/**
* Database represents a connection that can prepare and execute SQL statements.
*/
Expand Down Expand Up @@ -99,7 +138,7 @@ class Database {
prepare(sql) {
try {
const stmt = databasePrepareSync.call(this.db, sql);
return new Statement(stmt);
return new Statement(stmt);
} catch (err) {
throw new SqliteError(err.message, err.code, err.rawCode);
}
Expand Down
Loading