2014-10-31 11:57:44 -04:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-11 10:41:04 -05:00
|
|
|
const Adapter = require('../Adapter');
|
2016-03-15 15:37:24 -04:00
|
|
|
const Result = require('../Result');
|
2016-03-11 10:41:04 -05:00
|
|
|
const helpers = require('../helpers');
|
|
|
|
const dbliteAdapter = require('dblite');
|
|
|
|
|
|
|
|
class Sqlite extends Adapter {
|
2016-09-14 16:50:32 -04:00
|
|
|
constructor (config) {
|
2016-03-11 10:41:04 -05:00
|
|
|
let file = (helpers.isString(config)) ? config : config.file;
|
2016-03-11 16:32:38 -05:00
|
|
|
|
2016-11-10 22:10:45 -05:00
|
|
|
const instance = Promise.resolve(dbliteAdapter(file)).then(conn => {
|
|
|
|
// Stop the stupid 'bye bye' message being output
|
|
|
|
conn.on('close', () => {});
|
|
|
|
|
|
|
|
conn.on('error', (err) => {
|
|
|
|
throw new Error(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure to actually pass on the connection!
|
|
|
|
return conn;
|
|
|
|
}).catch(e => console.error(e));
|
|
|
|
|
|
|
|
super(instance);
|
2016-03-11 10:41:04 -05:00
|
|
|
}
|
2015-12-03 20:43:42 -05:00
|
|
|
|
2016-03-15 15:37:24 -04:00
|
|
|
/**
|
|
|
|
* Transform the adapter's result into a standard format
|
|
|
|
*
|
|
|
|
* @param {*} result - original driver result object
|
|
|
|
* @return {Result} - standard result object
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
transformResult (result) {
|
2016-03-15 15:37:24 -04:00
|
|
|
return new Result(result);
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Run the sql query as a prepared statement
|
|
|
|
*
|
|
|
|
* @param {String} sql - The sql with placeholders
|
|
|
|
* @param {Array} params - The values to insert into the query
|
2016-11-10 22:10:45 -05:00
|
|
|
* @return {Promise} - Returns a promise if no callback is provided
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-10 22:10:45 -05:00
|
|
|
execute (sql, params) {
|
2016-01-26 19:29:12 -05:00
|
|
|
|
2016-11-10 22:10:45 -05:00
|
|
|
return this.instance.then(conn => {
|
|
|
|
return conn.query(sql, params, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
return Promise.resolve(this.instance).then(() => result);
|
2016-03-15 15:37:24 -04:00
|
|
|
});
|
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
2015-12-08 10:06:29 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the current database connection
|
2016-01-26 19:29:12 -05:00
|
|
|
|
2015-12-08 10:06:29 -05:00
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
close () {
|
2016-11-10 22:10:45 -05:00
|
|
|
this.instance.then(conn => conn.close());
|
2015-12-08 10:06:29 -05:00
|
|
|
}
|
2016-03-11 10:41:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Sqlite;
|