Skip to content

Commit f6066fd

Browse files
committed
add chdb contructor
1 parent 93f2bd3 commit f6066fd

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,35 @@
1616

1717
<br>
1818

19-
### Example
20-
#### Query _(query, format)_
19+
### Examples
20+
21+
#### Query Constructor
2122
```javascript
23+
const addon = require('.');
2224
const chdb = require('chdb-node');
25+
const db = new chdb.db('CSV')
26+
var result;
27+
28+
// Query (ephemeral)
29+
result = db.query("SELECT version()", "TabSeparated");
30+
console.log(result)
31+
32+
// Query Session (persistent)
33+
db.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
34+
result = db.session("SELECT hello()", "TabSeparated");
35+
console.log(result)
36+
```
37+
38+
#### Query _(query, format)_
39+
```javascript
40+
const chdb = require('chdb-node').chdb;
2341
var result = chdb.Execute('SELECT version()', 'CSV');
2442
console.log(result) // 23.6.1.1
2543
```
2644

2745
#### Session _(query, *format, *path)_
2846
```javascript
29-
const chdb = require('chdb-node');
47+
const chdb = require('chdb-node').chdb;
3048
chdb.Session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'")
3149
var result = = chdb.Session("SELECT hello();")
3250
console.log(result) // chDB

example.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
const addon = require('.');
2-
var result = addon.Execute('SELECT version()', 'TabSeparated');
2+
const db = new addon.db('CSV')
3+
var result;
4+
5+
// Test query
6+
result = db.query("SELECT version()");
7+
console.log(result)
8+
9+
// Test session
10+
db.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
11+
result = db.session("SELECT hello()", "TabSeparated");
312
console.log(result)

index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
//const chdb = require('./build/chdb.node');
22
const chdb = require('node-gyp-build')(__dirname)
3-
module.exports = chdb;
3+
4+
function db(format, path) {
5+
6+
this.format = format || 'JSONCompact';
7+
this.path = path || '.';
8+
9+
// add properties to this
10+
this.query = function(query, format){
11+
return chdb.Execute(query, format || this.format);
12+
}.bind(this);
13+
this.session = function(query, format, path) {
14+
return chdb.Session(query, format || this.format, path || this.path);
15+
}.bind(this);
16+
17+
return this; (implicitly)
18+
}
19+
20+
module.exports = { chdb, db };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chdb-node",
3-
"version": "0.11.5",
3+
"version": "0.11.6",
44
"description": "chdb bindings for nodejs",
55
"main": "index.js",
66
"scripts": {

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Execute } = require('..');
1+
const { Execute } = require('..').chdb;
22

33
describe("Execution", () => {
44
test('Execute returns correct version', () => {

0 commit comments

Comments
 (0)