Skip to content

A abstraction layer built on top of node sqlite3 that allows for easier SQL queries & promises.

License

Notifications You must be signed in to change notification settings

seanpar203/lite3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQLite3 sugar built ontop of node-sqlite3 for Node.js.

NPM

Supported platforms

The sqlite3 module which lite3 is built on works with Node.js v0.10.x, v0.12.x, v4.x, and v5.x.

Binaries for most Node versions and platforms are provided by default via node-pre-gyp.

Why use Lite3

Simplified Inserts, Updates & Deletes with a chained approach that increases readability & productivity.

lite3
  .table('people')
  .update('name=?, age=?')
  .where('id=1')
  .values('Sean', '24')

Installing

You can use npm to download and install:

  • The latest lite3 package: npm install lite3

Non Trivial Usage.

Check the Apisection to understand more.

Querying Database Table.

lite3
  .table('blog_post')
  .selectAll()
  .then(rows => console.log(rows))
  .catch(err => console.log(err));

Update Row

lite3
  .table('blog_post')
  .update('date=?, title=?')
  .where('id=?')
  .values(['04-12-2016',' Awesome New Tittle', 1], true)
  .then(changes =>  console.log(changes))
  .catch(err => console.log(err));

Create New Row

lite3
  .table('people')
  .insert('?,?,?')
  .values([null, 'Nina', 27])

Assign Table To Variables.

// Import
const lite3 = require('lite3');

// Create instance.
const lite3 = new lite3('DBname.db');

// Assign specific tables to constants.
const people = lite3.table('people');
const blog_post = lite3.table('blog_post');

people
	.insert('?,?,?')
	.values([null, 'Sean', 25])

// DB Schema: people (id INTEGER PRIMARY KEY ASC , name varchar(255), age INTEGER);
// INSERT INTO people VALUES(id=null, name='Sean', age=25)


blog_post
	.insert('?,?,?')
	.values([null, 'Random Post Title', 'Random Post Body'])
	
// DB Schema: blog_post (id INTEGER PRIMARY KEY ASC , title varchar(255), body BLOB);
// INSERT INTO blog_post VALUES(id=null, title='Random Post Title', body='Random Post Body') 

API

If you need a deeper understanding [Node-SQLite3] API Docs (https://github.com/mapbox/node-sqlite3/wiki/API) provides all the internal workings which this library is built on.

lite3(file)

/**
* Create DB Connection with SQLite3 File
* @param {string} file - Name of SQLite3 file to connect
*/
const lite = require('lite3');
const lite3 = new lite('DBname.db');

lite3.table(tbl)

/**
 * Sets lite3.tableName property to perform operations.
 * @param {string} tbl - Name of table to perform operations.
 * @returns {lite3} - The Class with lite3.tableName property now set.
 */
 
 lite3.table('users')

lite3.update(query)

/**
 *  Prepares UPDATE statement to run with lite3.values()
 * @param {string} query - Sets lite3.stmt and UPDATE query syntax.
 * @returns {lite3} - Class with lite3.stmt & lite3.queryType property set.
 */
 lite3
 .table('people')
 .update('name=?, age=?')
 .values(['Sean', 25])

lite3.insert(query)

This method can be confusing so we'll use a simple example database to simplify it.

people (id INTEGER PRIMARY KEY ASC , name varchar(255), age INTEGER);

  • passing in '?, ?, ?' as a string is the same as 'id=?, name=?, age=?'
  • Passing in null to the id parameter makes the DB handle the auto increment.
/**
*  Prepares INSERT statement to run with lite3.values()
* @param {string} query - Question marks equal to the amount of values inserting
* @returns {lite3} - Class with lite3.stmt, lite3.queryStmt and lite3.queryType property set
*/
lite3
  .table('people')
  .insert('?, ?, ?')
  .values([null, 'Sean', 25])

equivalent to:

INSERT INTO people VALUES(id=null, name='Sean', age=25)

lite3.del()

Deletes row ONLY accepting id as clause.

/**
 *  Prepares DELETE statement to run with lite3.values()
 * @returns {lite3}
 */
 lite3
	.table('people')
	.del()
	.values(1)

lite3.where(clause)

/**
* Sugar method which read like english.
* @param {string} clause - Clause to include into new lite3.stmt.
* @returns {lite3} - Class with clause set.
*/
lite3
  .table('people')
  .update('name=?, age=?')
  .where('id=?')
  .values(['Sean', 25, 1])

UPDATE people VALUES(name='Sean', age=25) WHERE id=1

lite3.values(vals, returnChange)

/**
* Runs values using lite3.stmt query created from lite3.update(), lite3.prepare() methods
* @param {string} vals - Values to update or insert, must be in order with lite3.stmt to succeed
* @param {boolean} [returnChange] - True to return promise of changed lastID and changes
*/

// .values(['Nina', 27, 4], true) - Specificy true as second param to get updated row in .then()
lite3
	.table('people')
	.update('name=?, age=?')
	.where('id=?')
	.values(['Nina', 27, 4], true)
	.then(changes => console.log(changes))
	.catch(err => console.log(err));
	
// Update row with no updated row returned.
lite3
	.table('people')
	.update('name=?, age=?')
	.where('id=?')
	.values(['Nina', 27, 4])

lite3.selectAll()

/**
* Returns a promise of SELECT * SQL query on the lite3.tableName property
*/
lite3
  .table('people')
  .selectAll()
  .then(rows => console.log(rows))
  .catch(err => console.log(err))

lite3.selectWhere(clause)

/**
* Find a speicifc row in lite3.tableName with constraint
* @param {string} clause - constraint on which row to find
*/
lite3
  .table('people')
  .selectWhere('name="Sean"')
  .then(row => console.log(row))
  .catch(err => console.log(err))

About

A abstraction layer built on top of node sqlite3 that allows for easier SQL queries & promises.

Resources

License

Stars

Watchers

Forks

Packages

No packages published