You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simple Node.js async/await wrapper for SQLite. Uses sqlite3 behind the scenes and
greatly simplifies the the API.
Install
npm install asynqlite
Examples
SELECT some data:
constdb=require('asynqlite');(async()=>{awaitdb.open(':memory:');constres=awaitdb.run('SELECT datetime() AS foo');console.log(res[0].foo);// outputs today's datedb.close();})();
Pass parameters:
constdb=require('asynqlite');(async()=>{awaitdb.open(':memory:');constres=awaitdb.run('SELECT ? + ? as onePlusTwo',[1,2]);console.log(res[0].onePlusTwo);// output:// 3db.close();})();
Use a prepared statement:
constdb=require('asynqlite');(async()=>{db.open(':memory:');awaitdb.run('CREATE TABLE foo (bar TEXT)');conststmt=awaitdb.prepare('INSERT INTO foo VALUES (?)');for(leti=0;i<5;i++){stmt.run('test '+i);}awaitdb.finalize(stmt);constres=awaitdb.run('SELECT rowid AS id, bar FROM foo');console.log(res);// output:// [// { id: 1, bar: 'test 0' },// { id: 2, bar: 'test 1' },// { id: 3, bar: 'test 2' },// { id: 4, bar: 'test 3' },// { id: 5, bar: 'test 4' }// ]awaitdb.close();})();
API
db.open(path, options)
path is a local path and filename for persistant storage, :memory: for a memory
based non persistant database or undefined for a filesystem based non-persistant
storage.
options are one or more pipe delineated db.OPEN_READONLY, db.OPEN_READWRITE,
db.OPEN_CREATE, db.OPEN_FULLMUTEX, db.OPEN_URI, db.OPEN_SHAREDCACHE,
db.OPEN_PRIVATECACHE. The default is OPEN_READWRITE | OPEN_CREATE | OPEN_FULLMUTEX.
db.run(sql (, [param, ...])) - returns an array of results (if any)
sql is the SQL statement in text to be executed.
Optional array of param values which will be substituted for ? in the SQL.
Example: set bar equal to a where baz is b in the table foo
awaitdb.run('UPDATE foo SET bar = ? WHERE baz = ?',['a','b']);
Returns an array of results. (if any)
Example: SELECT all the rows from the table foo:
constres=awaitdb.run('SELECT * FROM foo');console.log(res);
db.prepare(sql) - returns a statement object
sql is the SQL statement in text to be executed. Parameter substitution (values of which
are to be supplied later by using the returned statement object) are designated with ?
in the SQL statement.
Example: Prepare an INSERT statement and execute it with three different sets of values:
conststmt=awaitdb.prepare('INSERT INTO foo (a, b) VALUES (?, ?)');stmt.run('x',1);stmt.run('y',2);stmt.run('z',3);awaitdb.finalize(stmt);
Note: You don't have to await each stmt.run() because the await finalize() effectivly does this.
db.finalize(stmt) - finalizes the passed statement
See db.prepare(sql) above for example usage.
db.close()
Closes the database handle. (happens automatically if you don't do it explicitly)