simpledbc
TypeScript icon, indicating that this package has built-in type declarations

0.4.13 • Public • Published

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

Package Sidebar

Install

npm i simpledbc

Weekly Downloads

13

Version

0.4.13

License

MIT

Unpacked Size

80.3 kB

Total Files

43

Last publish

Collaborators

  • buttonfly