node-sqlite-simple

1.0.0 • Public • Published

node-sqlite-simple

A small wrapper on node-sqlite3 providing simple bindings to most commonly used SQLite functions in standard applications.

This is based on original sqlite-wrapper by shin

Installation

npm install node-sqlite-simple

Usage

var db = require('node-sqlite-simple')('/tmp/foo.db');

Creating tables

var tableName = 'people';

db.createTable(tableName, {
        'id': {
            type: 'INTEGER',
            primary: true,
            notnull: true
        },
        'name': {
            type: 'TEXT',
            notnull: true,
            unique: true,
            default: "'John Doe'"
        },
        'city_id': {
            type: 'INTEGER',
            notnull: true,
            ref: 'cities'
        }
    }, callback);

Basic CRUD

// Single insertion
db.insert(tableName, {
        name: 'Donald Knuth',
        city_id: 42
    }, callback);

// Multiple inserts (bulk)
db.insertAll(tableName, [
        { name: 'Dennis Richie', city_id: 23 },
        { name: 'Bjarne Stoustrup', city_id: 347 }
    ], callback);

// Update
db.update(tableName, 'name=?', ['Donald Knuth'], {city_id: 378 }, callback);

// Remove
db.remove(tableName, 'city_id=?', ['666'], callback);

// Remove all from table
db.removeAll(tableName, callback);

// Select
db.select(tableName, 
    {'cities': 'cities.id=people.city_id' },
    { 'cities.name': 'city_name', 'people.name': 'person_name' },
    'people.id IS NOT NULL',
    null,
    callback,
    'people.name DESC',
    100,
    false
);

// Select with single result
db.selectOne(tableName,
    null,
    ['name'],
    'name LIKE ?',
    ['%Donald%'],
    callback
);

Shortcut method (common tasks)

// Find by id
db.find(tableName, 3, callback);

// List all elements in table
db.list(tableName, callback);

// Update by id
db.updateById(tableName, 3, { city_id: 42 }, callback);
// OR
db.updateById(tableName, { id: 3, city_id: 42 }, callback);

// Remove by id
db.removeById(tableName, 3, callback);

// Remove all elements in table
db.removeAll(tableName, callback);

Package Sidebar

Install

npm i node-sqlite-simple

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • earljon