Skip to content
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(bun): add bun sqlite support #71

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e07ec80
feat: bun sqlite implementation
Hebilicious Aug 27, 2023
12990e6
test: add bun sqlite tests
Hebilicious Aug 27, 2023
111d6df
feat: add bun entrypoint
Hebilicious Aug 27, 2023
a9fa380
chore: fix build errors
Hebilicious Aug 28, 2023
f3c3552
chore: rename to bun_sqlite
Hebilicious Aug 28, 2023
3343782
chore: rename bun_sqlite classes
Hebilicious Aug 28, 2023
c59d208
chore: rename private to closed
Hebilicious Aug 28, 2023
9aaf631
refactor: use ArrayBuffer.isView
Hebilicious Aug 28, 2023
4d9cc78
refactor: use booleans
Hebilicious Aug 28, 2023
f3e1d2a
refactor: rename index to node
Hebilicious Aug 28, 2023
0dd1069
test: fix imports
Hebilicious Aug 28, 2023
7d1a444
refactor: bun tests helpers
Hebilicious Aug 28, 2023
86306ae
feat: batch statements according to line breaks
Hebilicious Aug 28, 2023
de51092
chore: test comments
Hebilicious Aug 28, 2023
0ac69f4
chore: fix node import map
Hebilicious Aug 28, 2023
5943e37
chore: specify node export
Hebilicious Aug 28, 2023
0f297b3
Merge branch 'main' into feat/add-bun-support
Hebilicious Aug 28, 2023
20df841
chore: add prettier config file
Hebilicious Aug 29, 2023
3125b88
chore: add jest types to tsconfig
Hebilicious Aug 29, 2023
4a9d9ab
refactor: extract common utilities
Hebilicious Aug 29, 2023
c4e45e7
feat: throw error on executeMultiple
Hebilicious Aug 29, 2023
b24e43c
feat: bunsqlite only support number intMode
Hebilicious Aug 29, 2023
816163f
refactor: extract integer values
Hebilicious Aug 29, 2023
0c7b827
refactor: validate file config
Hebilicious Aug 29, 2023
2a06b1e
test: fix expectBunSqliteError
Hebilicious Aug 29, 2023
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
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": true,
"printWidth": 130,
"tabWidth": 4
}
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 34 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"Jan Plhak <[email protected]>"
],
"license": "MIT",

"type": "module",
"main": "lib-cjs/node.js",
"types": "lib-esm/node.d.ts",
Expand All @@ -33,6 +32,7 @@
"deno": "./lib-esm/web.js",
"edge-light": "./lib-esm/web.js",
"netlify": "./lib-esm/web.js",
"bun": "./lib-esm/bun.js",
Hebilicious marked this conversation as resolved.
Show resolved Hide resolved
"node": "./lib-esm/node.js",
"default": "./lib-esm/node.js"
},
Expand Down Expand Up @@ -62,22 +62,47 @@
"types": "./lib-esm/web.d.ts",
"import": "./lib-esm/web.js",
"require": "./lib-cjs/web.js"
},
"./bun": {
"types": "./lib-esm/bun.d.ts",
"import": "./lib-esm/bun.js",
"require": "./lib-cjs/bun.js"
},
"./bun-sqlite": {
"types": "./lib-esm/bun_sqlite.d.ts",
"import": "./lib-esm/bun_sqlite.js",
"require": "./lib-cjs/bun_sqlite.js"
}
},
"typesVersions": {
"*": {
".": ["./lib-esm/node.d.ts"],
"http": ["./lib-esm/http.d.ts"],
"hrana": ["./lib-esm/hrana.d.ts"],
"sqlite3": ["./lib-esm/sqlite3.d.ts"],
"web": ["./lib-esm/web.d.ts"]
".": [
"./lib-esm/node.d.ts"
],
"http": [
"./lib-esm/http.d.ts"
],
"hrana": [
"./lib-esm/hrana.d.ts"
],
"sqlite3": [
"./lib-esm/sqlite3.d.ts"
],
"web": [
"./lib-esm/web.d.ts"
],
"bun": [
"./lib-esm/bun.d.ts"
],
"bun-sqlite": [
"./lib-esm/bun_sqlite.d.ts"
]
}
},
"files": [
"lib-cjs/**",
"lib-esm/**"
],

"scripts": {
"prepublishOnly": "npm run build",
"prebuild": "rm -rf ./lib-cjs ./lib-esm",
Expand All @@ -86,10 +111,10 @@
"build:esm": "tsc -p tsconfig.build-esm.json",
"postbuild": "cp package-cjs.json ./lib-cjs/package.json",
"test": "jest --runInBand",
"test:bun": "bun test bun",
"typecheck": "tsc --noEmit",
"typedoc": "rm -rf ./docs && typedoc"
},

"dependencies": {
"@libsql/hrana-client": "^0.5.0-pre.2",
"better-sqlite3": "^8.0.1",
Expand All @@ -99,6 +124,7 @@
"@types/better-sqlite3": "^7.6.3",
"@types/jest": "^29.2.5",
"@types/node": "^18.15.5",
"bun-types": "^0.8.1",
"jest": "^29.3.1",
"ts-jest": "^29.0.5",
"typedoc": "^0.23.28",
Expand Down
8 changes: 3 additions & 5 deletions src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import console from "node:console";
import { expect } from "@jest/globals";
import type { MatcherFunction } from "expect";

import type { Request, Response } from "@libsql/hrana-client";
import { fetch } from "@libsql/hrana-client";
Expand Down Expand Up @@ -449,7 +447,7 @@ describe("batch()", () => {
], "write");

const n = 100;
const promises = [];
const promises: Promise<void>[] = [];
for (let i = 0; i < n; ++i) {
const ii = i;
promises.push((async () => {
Expand Down Expand Up @@ -495,7 +493,7 @@ describe("batch()", () => {
}));

test("batch with a lot of different statements", withClient(async (c) => {
const stmts = [];
const stmts: string[] = [];
for (let i = 0; i < 1000; ++i) {
stmts.push(`SELECT ${i}`);
}
Expand All @@ -509,7 +507,7 @@ describe("batch()", () => {
const n = 20;
const m = 200;

const stmts = [];
const stmts: libsql.InStatement[] = [];
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
stmts.push({sql: `SELECT ?, ${j}`, args: [i]});
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const toBeLibsqlError: MatcherFunction<[code?: string, message?: RegExp]> =
&& (messageRe === undefined || actual.message.match(messageRe) !== null);

const message = (): string => {
const parts = [];
const parts: string[] = [];
parts.push("expected ");
parts.push(this.utils.printReceived(actual));
parts.push(pass ? " not to be " : " to be ");
Expand Down
38 changes: 38 additions & 0 deletions src/bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Config, Client } from "./api.js";
import { LibsqlError } from "./api.js";
import type { ExpandedConfig } from "./config.js";
import { expandConfig } from "./config.js";
import { supportedUrlLink } from "./util.js";

import { _createClient as _createWsClient } from "./ws.js";
import { _createClient as _createHttpClient } from "./http.js";
import { _createClient as _createBunSqliteClient } from "./bun_sqlite.js";

export * from "./api.js";

export function createClient(config: Config): Client {
return _createClient(expandConfig(config, true));
}

export const isBun = () => {
if ((globalThis as any).Bun || (globalThis as any).process?.versions?.bun) return;
throw new LibsqlError("Bun is not available", "BUN_NOT_AVAILABLE");
};

/** @private */
export function _createClient(config: ExpandedConfig): Client {
isBun();
if (config.scheme === "ws" || config.scheme === "wss") {
return _createWsClient(config);
} else if (config.scheme === "http" || config.scheme === "https") {
return _createHttpClient(config);
} else if (config.scheme === "file") {
return _createBunSqliteClient(config);
} else {
throw new LibsqlError(
'The Bun client supports "file", "libsql:", "wss:", "ws:", "https:" and "http:" URLs, ' +
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'The Bun client supports "file", "libsql:", "wss:", "ws:", "https:" and "http:" URLs, ' +
'The Bun client supports "file:", "libsql:", "wss:", "ws:", "https:" and "http:" URLs, ' +

`got ${JSON.stringify(config.scheme + ":")}. For more information, please read ${supportedUrlLink}`,
"URL_SCHEME_NOT_SUPPORTED"
);
}
}
Loading
Loading