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

Add Deno support #5

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions example.deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Run with `deno run --unstable-ffi -A example.deno.ts`
import { query, Session } from "./mod.ts";

// Create a new session instance
const session = new Session("./chdb-bun-tmp");
let result;

// Test standalone query
result = query("SELECT version(), 'Hello chDB', chdb()", "CSV");
console.log(result);

// Test session query
session.query("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'", "CSV");
result = session.query("SELECT hello()", "CSV");
console.log(result);

// Clean up the session
session.cleanup();
65 changes: 65 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const path = "chdb_bun.so";

const { symbols: chdb } = Deno.dlopen(path, {
Query: {
parameters: ["buffer", "buffer"],
result: "buffer",
},
QuerySession: {
parameters: ["buffer", "buffer", "buffer"],
result: "buffer",
},
});

const enc = new TextEncoder();
// Standalone exported query function
export function query(query: string, format: string = "CSV") {
if (!query) {
return "";
}
const result = chdb.Query(
enc.encode(query + "\0"),
enc.encode(format + "\0"),
);
if (result == null) {
return "";
}
return new Deno.UnsafePointerView(result).getCString();
}

// Session class with path handling
class Session {
path: string;
isTemp: boolean;

query(query: string, format: string = "CSV") {
if (!query) return "";
const result = chdb.QuerySession(
enc.encode(query + "\0"),
enc.encode(format + "\0"),
enc.encode(this.path + "\0"),
);
if (result == null) {
return "";
}
return new Deno.UnsafePointerView(result).getCString();
}

constructor(path: string = "") {
if (path === "") {
// Create a temporary directory
this.path = Deno.makeTempDirSync();
this.isTemp = true;
} else {
this.path = path;
this.isTemp = false;
}
}

// Cleanup method to delete the temporary directory
cleanup() {
Deno.removeSync(this.path, { recursive: true });
}
}

export { chdb, Session };
Loading