|
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'); |
2 | 5 |
|
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 | + } |
6 | 26 |
|
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 | + } |
10 | 31 |
|
11 | | - return this; |
| 32 | + // Cleanup method to delete the temporary directory |
| 33 | + cleanup() { |
| 34 | + rmdirSync(this.path, { recursive: true }); |
| 35 | + } |
12 | 36 | } |
13 | 37 |
|
14 | | -module.exports = { chdb, db }; |
| 38 | +module.exports = { query, Session }; |
0 commit comments