diff --git a/README.md b/README.md index 7ba9309..b04426c 100644 --- a/README.md +++ b/README.md @@ -16,17 +16,35 @@
-### 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 diff --git a/example.js b/example.js index 2f35cfc..374f216 100644 --- a/example.js +++ b/example.js @@ -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) diff --git a/index.js b/index.js index 85436b6..3f247ef 100644 --- a/index.js +++ b/index.js @@ -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 }; diff --git a/package.json b/package.json index 9b9b7c0..89d73e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chdb-node", - "version": "0.11.5", + "version": "0.11.6", "description": "chdb bindings for nodejs", "main": "index.js", "scripts": { diff --git a/test/test.js b/test/test.js index d15901e..2be64db 100644 --- a/test/test.js +++ b/test/test.js @@ -1,4 +1,4 @@ -const { Execute } = require('..'); +const { Execute } = require('..').chdb; describe("Execution", () => { test('Execute returns correct version', () => {