Skip to content

Commit

Permalink
Merge pull request #2 from farmer00317558/main
Browse files Browse the repository at this point in the history
rewrite with typescript
  • Loading branch information
lmangani authored Dec 23, 2023
2 parents 8475a5b + 344af28 commit c7d23ac
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 66 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/bun-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ name: chDB-bun
on:
pull_request:
paths-ignore:
- '**/.md'
- "**/.md"
push:
branches: [ "main" ]
branches: ["main"]
paths-ignore:
- '**/.md'
- "**/.md"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Fetch library
run: |
- uses: actions/checkout@v3
- name: Fetch library
run: |
sudo wget https://github.com/metrico/libchdb/releases/latest/download/libchdb.zip
sudo unzip libchdb.zip
sudo mv libchdb.so /usr/lib/libchdb.so
sudo ldconfig
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- run: bun install
- run: bun example.js
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- run: bun install
- run: bun example.ts
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules

/lib/libchdb_bun.dylib
/lib/libchdb.so
/lib/index.js
/lib/index.d.ts
Binary file modified bun.lockb
Binary file not shown.
8 changes: 4 additions & 4 deletions example.js → example.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { db, chdb } from '.';
import { db, chdb } from ".";

const conn = new db('CSV', '/tmp')
const conn = new db("CSV", "/tmp");
var result;

// Test query
result = conn.query("SELECT version(), chdb()");
console.log(result)
console.log(result);

// Test session
conn.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
result = conn.session("SELECT hello()", "CSV");
console.log(result)
console.log(result);
34 changes: 0 additions & 34 deletions index.js

This file was deleted.

39 changes: 39 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { dlopen, FFIType, suffix, CString, ptr } from "bun:ffi";

const path = `lib/libchdb_bun.${suffix}`;

const { symbols: chdb } = dlopen(path, {
Execute: {
args: [FFIType.cstring, FFIType.cstring],
returns: FFIType.cstring,
},
ExecuteSession: {
args: [FFIType.cstring, FFIType.cstring, FFIType.cstring],
returns: FFIType.cstring,
},
});

class db {
format: string;
path: string;
query(query: string, format: string = "CSV") {
if (!query) {
return "";
}
return chdb.Execute(Buffer.from(query + "\0"), Buffer.from(format + "\0"));
}
session(query: string, format: string = "CSV", path: string = "/tmp") {
if (!query) return "";
return chdb.ExecuteSession(
Buffer.from(query + "\0"),
Buffer.from(format + "\0"),
Buffer.from(path + "\0")
);
}
constructor(format: string = "JSONCompact", path: string = ".") {
this.format = format;
this.path = path;
}
}

export { chdb, db };
7 changes: 5 additions & 2 deletions lib/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## libchdb `Execute` function binding
```
gcc -shared -fPIC -o libchdb_bun.so libchdb_bun.c -lchdb

```bash
# build the dynamic library

./build.sh
```
8 changes: 8 additions & 0 deletions lib/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if [ "$(uname)" == "Darwin" ]; then
gcc -dynamiclib -o libchdb_bun.dylib -L. -lchdb libchdb_bun.c
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
gcc -shared -fPIC -o libchdb_bun.so libchdb_bun.c -L. -lchdb
else
echo "Unsupported operating system"
exit 1
fi
35 changes: 26 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
{
"name": "chdb-bun",
"version": "1.0.4",
"module": "index.js",
"type": "module",
"author": "Lorenzo Mangani <[email protected]>",
"license": "Apache2.0",
"module": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "cd lib && gcc -shared -fPIC -o libchdb_bun.so libchdb_bun.c -lchdb"
"build:lib": "cd lib && ./build.sh",
"build:ts": "bun build index.ts --target=bun --outfile=lib/index.js --sourcemap=inline && tsc --declaration --emitDeclarationOnly --types bun-types --declarationDir lib index.ts",
"build": "bun run build:ts && bun run build:lib"
},
"type": "module",
"devDependencies": {
"bun-types": "^0.5.0"
"bun-types": "^1.0.19",
"typescript": "^5.3.3"
},
"directories":{
"lib": "lib"
}
"directories": {
"lib": "lib"
},
"files": [
"lib"
],
"maintainers": [
{
"name": "Farmer Sun",
"email": "[email protected]"
},
{
"name": "Lorenzo Mangani",
"email": "[email protected]"
}
],
"author": "Lorenzo Mangani <[email protected]>",
"license": "Apache2.0"
}
9 changes: 3 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
{
"compilerOptions": {
"lib": [
"ESNext"
],
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"allowJs": false,
"types": [
"bun-types" // add Bun global
]
}
}
}

0 comments on commit c7d23ac

Please sign in to comment.