-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
39 lines (34 loc) · 1003 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const path = require('path');
const chdbNode = require(path.join(__dirname, 'build', 'Release', 'chdb_node.node'));
const { mkdtempSync, rmSync } = require('fs');
const { join } = require('path');
const os = require('os');
// Standalone exported query function
function query(query, format = "CSV") {
if (!query) {
return "";
}
return chdbNode.Query(query, format);
}
// Session class with path handling
class Session {
constructor(path = "") {
if (path === "") {
// Create a temporary directory
this.path = mkdtempSync(join(os.tmpdir(), 'tmp-chdb-node'));
this.isTemp = true;
} else {
this.path = path;
this.isTemp = false;
}
}
query(query, format = "CSV") {
if (!query) return "";
return chdbNode.QuerySession(query, format, this.path);
}
// Cleanup method to delete the temporary directory
cleanup() {
rmSync(this.path, { recursive: true }); // Replaced rmdirSync with rmSync
}
}
module.exports = { query, Session };