This post will cover integrating the rust-core library created in the previous post with Node.js.
Creating bindings between Rust and Node.js is easy with neon-bindings. Install neon-bindings globally with NPM. The getting-started page has more information on neon's dependencies for any install issues.
npm install --global neon-cli
Generate a new neon project inside /cross-platform-rust-database-access
and install the neon dependencies.
neon new nodejs
cd nodejs
npm i
Before the rust-code is integrated, check to make sure neon is working as expected.
neon build --release
node lib/index.js
# hello world
Time to integrate the rust-core library. First, add rust-core to the native/cargo.toml
file.
[dependencies]
neon = "0.5.0"
rust-core = { path = "../../rust-core" }
Create the binding between Node.js and Rust by editing native/src/lib.rs
and add the code below. It creates a function
named call_database
accessible in Node.js and accepts the SQLite path as a string to pass along to rust-core.
use neon::prelude::*;
use rust_core::database_test;
fn call_database(mut cx: FunctionContext) -> JsResult<JsString> {
let database_path = cx.argument::<JsString>(0).unwrap().value();
let result = database_test(database_path);
Ok(cx.string(result))
}
register_module!(mut cx, {
cx.export_function("call_database", call_database)
});
Lastly, update the lib/index.js
file to properly call the newly created binding.
var addon = require('../native');
console.log(addon.call_database('./database.sqlite'));
Build and execute the Node.js code.
neon build --release
node lib/index.js
# Person { id: 1, name: "Ada Lovelace" }
node lib/index.js
# Person { id: 2, name: "Ada Lovelace" }
That's it! While this example is pretty trivial, it's easy to imagine how it can be built upon when creating an app.