Skip to content

Commit 3890091

Browse files
committed
Fix session
1 parent d239f74 commit 3890091

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

index.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1-
const chdb = require("node-gyp-build")(__dirname);
1+
const chdbNode = require('./build/Release/chdb_node.node');
2+
const { mkdtempSync, rmdirSync } = require('fs');
3+
const { join } = require('path');
4+
const os = require('os');
25

3-
function db(format, path) {
4-
this.format = format || "JSONCompact";
5-
this.path = path || ".";
6+
// Standalone exported query function
7+
function query(query, format = "CSV") {
8+
if (!query) {
9+
return "";
10+
}
11+
return chdbNode.Query(query, format);
12+
}
13+
14+
// Session class with path handling
15+
class Session {
16+
constructor(path = "") {
17+
if (path === "") {
18+
// Create a temporary directory
19+
this.path = mkdtempSync(join(os.tmpdir(), 'tmp-chdb-node'));
20+
this.isTemp = true;
21+
} else {
22+
this.path = path;
23+
this.isTemp = false;
24+
}
25+
}
626

7-
// add properties to this
8-
this.query = (query, format) => chdb.Execute(query, format || this.format);
9-
this.session = (query, format, path) => chdb.Session(query, format || this.format, path || this.path);
27+
query(query, format = "CSV") {
28+
if (!query) return "";
29+
return chdbNode.QuerySession(query, format, this.path);
30+
}
1031

11-
return this;
32+
// Cleanup method to delete the temporary directory
33+
cleanup() {
34+
rmdirSync(this.path, { recursive: true });
35+
}
1236
}
1337

14-
module.exports = { chdb, db };
38+
module.exports = { query, Session };

0 commit comments

Comments
 (0)