Skip to content

Commit

Permalink
add chdb contructor
Browse files Browse the repository at this point in the history
  • Loading branch information
lmangani committed Aug 1, 2023
1 parent 93f2bd3 commit f6066fd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,35 @@

<br>

### Example
#### Query _(query, format)_
### Examples

#### Query Constructor
```javascript
const addon = require('.');
const chdb = require('chdb-node');
const db = new chdb.db('CSV')
var result;

// Query (ephemeral)
result = db.query("SELECT version()", "TabSeparated");
console.log(result)

// Query Session (persistent)
db.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
result = db.session("SELECT hello()", "TabSeparated");
console.log(result)
```

#### Query _(query, format)_
```javascript
const chdb = require('chdb-node').chdb;
var result = chdb.Execute('SELECT version()', 'CSV');
console.log(result) // 23.6.1.1
```

#### Session _(query, *format, *path)_
```javascript
const chdb = require('chdb-node');
const chdb = require('chdb-node').chdb;
chdb.Session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'")
var result = = chdb.Session("SELECT hello();")
console.log(result) // chDB
Expand Down
11 changes: 10 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
const addon = require('.');
var result = addon.Execute('SELECT version()', 'TabSeparated');
const db = new addon.db('CSV')
var result;

// Test query
result = db.query("SELECT version()");
console.log(result)

// Test session
db.session("CREATE FUNCTION IF NOT EXISTS hello AS () -> 'chDB'");
result = db.session("SELECT hello()", "TabSeparated");
console.log(result)
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
//const chdb = require('./build/chdb.node');
const chdb = require('node-gyp-build')(__dirname)
module.exports = chdb;

function db(format, path) {

this.format = format || 'JSONCompact';
this.path = path || '.';

// add properties to this
this.query = function(query, format){
return chdb.Execute(query, format || this.format);
}.bind(this);
this.session = function(query, format, path) {
return chdb.Session(query, format || this.format, path || this.path);
}.bind(this);

return this; (implicitly)
}

module.exports = { chdb, db };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chdb-node",
"version": "0.11.5",
"version": "0.11.6",
"description": "chdb bindings for nodejs",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Execute } = require('..');
const { Execute } = require('..').chdb;

describe("Execution", () => {
test('Execute returns correct version', () => {
Expand Down

0 comments on commit f6066fd

Please sign in to comment.