-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from farmer00317558/main
rewrite with typescript
- Loading branch information
Showing
10 changed files
with
100 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
} | ||
} | ||
} |