gurumian / simpledb

sqlite3 nodejs binding

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SQLite3 nodejs binding. It's simple to use and supports the both asynchronous and synchronous manners.

Node.js CI Dependencies FOSSA Status

Prerequisites

apt install libsqlite3-dev

(or brew)

You may need to install cmake-js and typescript

npm i -g cmake-js
npm i -g typescript

Install

npm i simpledbc

Example

See example/

CREATE

const {Connection} = require('simpledbc');

let conn = new Connection(db)
let stmt = conn.createStatement()
let query = `CREATE TABLE ${table}(idx INTEGER PRIMARY KEY AUTOINCREMENT, passwd TEXT, date DATETIME);`
stmt.execute(query)
.then(res => {
  console.log(res);
})

INSERT

let stmt = conn.createStatement();
let password = Math.round(Math.random() * 1000);
let query =  `INSERT INTO ${table} (passwd, date) VALUES(${password},datetime(\'now\',\'localtime\'));`;
stmt.execute(query)
.then(res => {
  console.log(res);
})

SELECT

let stmt = conn.createStatement();
let query =`SELECT idx, passwd, date FROM ${table}`;
stmt.execute(query)
.then(res => {
  while(res.next()) {
    console.log(res.data);
    // console.log(res.obj);
  }
})

UPDATE

let stmt = conn.createStatement();
let password = 'new password';
let query = `UPDATE ${table} set passwd=\'${password}\', date=datetime(\'now\',\'localtime\') WHERE idx=1;`;
stmt.execute(query)
.then(res => {
  if(res) {
    console.log(`successfully updated to ${password}`);
  }
})

DELETE

let id = 1;
let stmt = conn.createStatement();
let query = `DELETE FROM admin WHERE idx=${id};`;
stmt.execute(query)
.then(res => {
  // 
})

async / await

You can use it like this as well.

async function removeAsync(conn) {
  let id = 2;
  let stmt = conn.createStatement();
  let query = `DELETE FROM admin WHERE idx=${id};`;
  let res = await stmt.execute(query)
  console.log(`removeAsync ${res}`);
}

(Optional) Native only build

cd native;
mkdir build; cd build
cmake .. && make

License

FOSSA Status

About

sqlite3 nodejs binding

License:MIT License


Languages

Language:C++ 47.9%Language:JavaScript 34.4%Language:TypeScript 9.2%Language:CMake 8.5%