Skip to content

Commit c31de17

Browse files
rewrite with typescript
1 parent 8475a5b commit c31de17

File tree

10 files changed

+91
-55
lines changed

10 files changed

+91
-55
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
node_modules
22

3+
/lib/libchdb_bun.dylib
4+
/lib/libchdb_bun.so
5+
/lib/libchdb_bun.dll
6+
/lib/libchdb.so
7+
/lib/index.js
8+
/lib/index.d.ts

bun.lockb

1.96 KB
Binary file not shown.

example.js renamed to example.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { db, chdb } from '.';
1+
import { db, chdb } from ".";
22

3-
const conn = new db('CSV', '/tmp')
3+
const conn = new db("CSV", "/tmp");
44
var result;
55

66
// Test query
77
result = conn.query("SELECT version(), chdb()");
8-
console.log(result)
8+
console.log(result);
99

1010
// Test session
1111
conn.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
1212
result = conn.session("SELECT hello()", "CSV");
13-
console.log(result)
13+
console.log(result);

index.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { dlopen, FFIType, suffix, CString, ptr } from "bun:ffi";
2+
3+
const path = `lib/libchdb_bun.${suffix}`;
4+
5+
const { symbols: chdb } = dlopen(path, {
6+
Execute: {
7+
args: [FFIType.cstring, FFIType.cstring],
8+
returns: FFIType.cstring,
9+
},
10+
ExecuteSession: {
11+
args: [FFIType.cstring, FFIType.cstring, FFIType.cstring],
12+
returns: FFIType.cstring,
13+
},
14+
});
15+
16+
class db {
17+
format: string;
18+
path: string;
19+
query(query: string, format: string = "CSV") {
20+
if (!query) {
21+
return "";
22+
}
23+
return chdb.Execute(Buffer.from(query + "\0"), Buffer.from(format + "\0"));
24+
}
25+
session(query: string, format: string = "CSV", path: string = "/tmp") {
26+
if (!query) return "";
27+
return chdb.ExecuteSession(
28+
Buffer.from(query + "\0"),
29+
Buffer.from(format + "\0"),
30+
Buffer.from(path + "\0")
31+
);
32+
}
33+
constructor(format: string = "JSONCompact", path: string = ".") {
34+
this.format = format;
35+
this.path = path;
36+
}
37+
}
38+
39+
export { chdb, db };

lib/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## libchdb `Execute` function binding
2-
```
3-
gcc -shared -fPIC -o libchdb_bun.so libchdb_bun.c -lchdb
2+
3+
```bash
4+
# build the dynamic library
5+
6+
./build.sh
47
```

lib/build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if [ "$(uname)" == "Darwin" ]; then
2+
gcc -dynamiclib -o libchdb_bun.dylib -L. -lchdb libchdb_bun.c
3+
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
4+
gcc -shared -fPIC -o libchdb_bun.so libchdb_bun.c -L. -lchdb
5+
else
6+
echo "Unsupported operating system"
7+
exit 1
8+
fi

lib/libchdb_bun.so

-15.9 KB
Binary file not shown.

package.json

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
{
22
"name": "chdb-bun",
33
"version": "1.0.4",
4-
"module": "index.js",
5-
"type": "module",
6-
"author": "Lorenzo Mangani <[email protected]>",
7-
"license": "Apache2.0",
4+
"module": "lib/index.js",
5+
"types": "lib/index.d.ts",
86
"scripts": {
9-
"build": "cd lib && gcc -shared -fPIC -o libchdb_bun.so libchdb_bun.c -lchdb"
7+
"build:lib": "cd lib && ./build.sh",
8+
"build:ts": "bun build index.ts --target=bun --outfile=lib/index.js --sourcemap=inline && tsc --declaration --emitDeclarationOnly --types bun-types --declarationDir lib index.ts",
9+
"build": "bun run build:ts && bun run build:lib"
1010
},
11+
"type": "module",
1112
"devDependencies": {
12-
"bun-types": "^0.5.0"
13+
"bun-types": "^1.0.19",
14+
"typescript": "^5.3.3"
1315
},
14-
"directories":{
15-
"lib": "lib"
16-
}
16+
"directories": {
17+
"lib": "lib"
18+
},
19+
"files": [
20+
"lib"
21+
],
22+
"maintainers": [
23+
{
24+
"name": "Farmer Sun",
25+
"email": "[email protected]"
26+
},
27+
{
28+
"name": "Lorenzo Mangani",
29+
"email": "[email protected]"
30+
}
31+
],
32+
"author": "Lorenzo Mangani <[email protected]>",
33+
"license": "Apache2.0"
1734
}

tsconfig.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
{
22
"compilerOptions": {
3-
"lib": [
4-
"ESNext"
5-
],
3+
"lib": ["ESNext"],
64
"module": "esnext",
75
"target": "esnext",
86
"moduleResolution": "bundler",
97
"strict": true,
108
"downlevelIteration": true,
119
"skipLibCheck": true,
12-
"jsx": "react-jsx",
1310
"allowSyntheticDefaultImports": true,
1411
"forceConsistentCasingInFileNames": true,
15-
"allowJs": true,
12+
"allowJs": false,
1613
"types": [
1714
"bun-types" // add Bun global
1815
]
1916
}
20-
}
17+
}

0 commit comments

Comments
 (0)