diff --git a/README.md b/README.md index b97705f..dbd42f0 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,50 @@ [![chDB-node](https://github.com/chdb-io/chdb-node/actions/workflows/chdb-node-test.yml/badge.svg)](https://github.com/chdb-io/chdb-node/actions/workflows/chdb-node-test.yml) +[![npm version](https://badge.fury.io/js/chdb.svg)](https://badge.fury.io/js/chdb) # chdb-node -[chDB](https://github.com/chdb-io/chdb) nodejs bindings for fun and hacking. +[chDB](https://github.com/chdb-io/chdb) nodejs bindings. -### Status +### Install -- Experimental bindings -- Requires [`libchdb`](https://github.com/chdb-io/chdb) on the system +```bash +npm i chdb +``` + +### Usage + +```javascript +const { query, Session } = require("chdb"); + +var ret; + +// Test standalone query +ret = query("SELECT version(), 'Hello chDB', chdb()", "CSV"); +console.log("Standalone Query Result:", ret); + +// Test session query +// Create a new session instance +const session = new Session("./chdb-node-tmp"); +ret = session.query("SELECT 123", "CSV") +console.log("Session Query Result:", ret); +ret = session.query("CREATE DATABASE IF NOT EXISTS testdb;" + + "CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;"); + +session.query("USE testdb; INSERT INTO testtable VALUES (1), (2), (3);") -### Build +ret = session.query("SELECT * FROM testtable;") +console.log("Session Query Result:", ret); + +// Clean up the session +session.cleanup(); + +``` + +#### Build from source ```bash npm run libchdb npm install npm run test ``` - -### Examples - -See [example.js](example.js). - - diff --git a/binding.gyp b/binding.gyp index 71d54e8..3692a93 100644 --- a/binding.gyp +++ b/binding.gyp @@ -1,7 +1,4 @@ { - "variables": { - "openssl_fips": "", - }, "targets": [ { "target_name": "chdb_node", @@ -11,17 +8,9 @@ "." ], "libraries": [ "<(module_root_dir)/libchdb.so" ], - "conditions": [ - ['OS=="mac"', { - "ldflags": [ - "-Wl,-rpath,@loader_path/../../" - ] - }] - ], "cflags!": [ "-fno-exceptions" ], "cflags_cc!": [ "-fno-exceptions" ], "defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ] } ] -} - +} \ No newline at end of file diff --git a/fix_loader_path.sh b/fix_loader_path.sh new file mode 100755 index 0000000..68c2fa8 --- /dev/null +++ b/fix_loader_path.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +cd "$(dirname "$0")" + +if [[ $(uname -s) == "Darwin" ]]; then + install_name_tool -change libchdb.so @loader_path/../../libchdb.so build/Release/chdb_node.node + otool -L build/Release/chdb_node.node +fi diff --git a/index.js b/index.js index 5d07b46..29e5a57 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ -const chdbNode = require('./build/Release/chdb_node.node'); -const { mkdtempSync, rmdirSync } = require('fs'); +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'); @@ -31,7 +32,7 @@ class Session { // Cleanup method to delete the temporary directory cleanup() { - rmdirSync(this.path, { recursive: true }); + rmSync(this.path, { recursive: true }); // Replaced rmdirSync with rmSync } } diff --git a/package.json b/package.json index 8f88211..42afc84 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chdb", - "version": "1.1.1", + "version": "1.1.2", "description": "chDB bindings for nodejs", "main": "index.js", "repository": { @@ -11,7 +11,8 @@ "install": "npm run libchdb && npm run build", "test": "node example.js", "libchdb": "./update_libchdb.sh", - "build": "node-gyp configure build" + "fixloaderpath": "./fix_loader_path.sh", + "build": "node-gyp configure build --verbose && npm run fixloaderpath" }, "author": { "name": "chdb",