0.1.0 • Published 3 years ago
asynqlite v0.1.0
asynqlite
Simple async/await wrapper for SQLite. Uses sqlite3 behind the scenes and
greatly simplifies the the API.
Install
npm install asynqliteExamples
SELECT some data:
const db = require('asynqlite');
(async () => {
await db.open(':memory:');
const res = await db.run('SELECT datetime() AS foo');
console.log(res[0].foo);
db.close();
})();Use a prepared statement:
const db = require('asynqlite');
(async () => {
db.open(':memory:');
await db.run('CREATE TABLE foo (bar TEXT)');
const stmt = await db.prepare('INSERT INTO foo VALUES (?)');
for (let i = 0; i < 10; i++) {
stmt.run('test ' + i);
}
await db.finalize(stmt);
const res = await db.run('SELECT rowid AS id, bar FROM foo');
console.log(res);
await db.close();
})();API
db.open(path, options)pathis a local path and filename for persistant storage,:memory:for a memory based non persistant database orundefinedfor a filesystem based non-persistant storage.optionsare one or more pipe delineateddb.OPEN_READONLY,db.OPEN_READWRITE,db.OPEN_CREATE,db.OPEN_FULLMUTEX,db.OPEN_URI,db.OPEN_SHAREDCACHE,db.OPEN_PRIVATECACHE. The default isOPEN_READWRITE | OPEN_CREATE | OPEN_FULLMUTEX.
db.run(sql (, [param, ...]))- returns an array of results (if any)sqlis the SQL statement in text to be executed.- Optional array of
paramvalues which will be substituted for?in the SQL. Example: setbarequal toawhere baz isbin the tablefooawait db.run('UPDATE foo SET bar = ? WHERE baz = ?', [ 'a', 'b' ]); - Returns an array of results. (if any)
Example:
SELECTall the rows from the tablefoo:const res = await db.run('SELECT * FROM foo'); console.log(res);
db.prepare(sql)- returns a statement objectsqlis 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 anINSERTstatement and execute it with three different sets of values:const stmt = await db.prepare('INSERT INTO foo (a, b) VALUES (?, ?)'); stmt.run('x', 1); stmt.run('y', 2); stmt.run('z', 3); await db.finalize(stmt);Note: You don't have to
awaiteachstmt.run()because theawait finalize()effectivly does this.
db.close()- Closes the database handle. (happens automatically if you don't do it explicitly)
More Info
See the examples directory for more.
License
MIT