Skip to content

Commit f718a05

Browse files
rojvvauxten
authored andcommitted
Add Deno support
1 parent 25d31d3 commit f718a05

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

mod.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const path = "chdb_bun.so";
2+
3+
const { symbols: chdb } = Deno.dlopen(path, {
4+
Query: {
5+
parameters: ["buffer", "buffer"],
6+
result: "buffer",
7+
},
8+
QuerySession: {
9+
parameters: ["buffer", "buffer", "buffer"],
10+
result: "buffer",
11+
},
12+
});
13+
14+
const enc = new TextEncoder();
15+
// Standalone exported query function
16+
export function query(query: string, format: string = "CSV") {
17+
if (!query) {
18+
return "";
19+
}
20+
const result = chdb.Query(
21+
enc.encode(query + "\0"),
22+
enc.encode(format + "\0"),
23+
);
24+
if (result == null) {
25+
return "";
26+
}
27+
return new Deno.UnsafePointerView(result).getCString();
28+
}
29+
30+
// Session class with path handling
31+
class Session {
32+
path: string;
33+
isTemp: boolean;
34+
35+
query(query: string, format: string = "CSV") {
36+
if (!query) return "";
37+
return chdb.QuerySession(
38+
enc.encode(query + "\0"),
39+
enc.encode(format + "\0"),
40+
enc.encode(this.path + "\0"),
41+
);
42+
}
43+
44+
constructor(path: string = "") {
45+
if (path === "") {
46+
// Create a temporary directory
47+
this.path = Deno.makeTempDirSync();
48+
this.isTemp = true;
49+
} else {
50+
this.path = path;
51+
this.isTemp = false;
52+
}
53+
}
54+
55+
// Cleanup method to delete the temporary directory
56+
cleanup() {
57+
Deno.removeSync(this.path, { recursive: true });
58+
}
59+
}
60+
61+
export { chdb, Session };

0 commit comments

Comments
 (0)