2015-12-03 20:43:42 -05:00
|
|
|
'use strict';
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2016-01-26 19:29:12 -05:00
|
|
|
/**
|
|
|
|
* Class that wraps database connection libraries
|
|
|
|
*
|
|
|
|
* @private
|
2016-11-10 22:10:45 -05:00
|
|
|
* @param {Promise} instance - The connection object
|
2016-01-26 19:29:12 -05:00
|
|
|
*/
|
|
|
|
class Adapter {
|
2015-12-02 13:01:31 -05:00
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Invoke an adapter
|
|
|
|
*
|
2016-01-26 19:29:12 -05:00
|
|
|
* @constructor
|
2016-11-10 22:10:45 -05:00
|
|
|
* @param {Promise} instance - Promise holding connection object
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
constructor (instance) {
|
2015-12-02 13:01:31 -05:00
|
|
|
this.instance = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-21 19:48:00 -05:00
|
|
|
* @return {Promise} - returns a promise resolving to the result of the database query
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-10 22:10:45 -05:00
|
|
|
execute (sql, params) {
|
2015-12-07 15:58:31 -05:00
|
|
|
throw new Error('Correct adapter not defined for query execution');
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-07 15:58:31 -05:00
|
|
|
|
2016-03-15 15:37:24 -04:00
|
|
|
/**
|
|
|
|
* Transform the adapter's result into a standard format
|
|
|
|
*
|
|
|
|
* @param {*} originalResult - the original result object from the driver
|
|
|
|
* @return {Result} - the new result object
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
transformResult (originalResult) {
|
2016-03-15 15:37:24 -04:00
|
|
|
throw new Error('Result transformer method not defined for current adapter');
|
|
|
|
}
|
|
|
|
|
2015-12-07 15:58:31 -05:00
|
|
|
/**
|
|
|
|
* Close the current database connection
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
close () {
|
2016-11-10 22:10:45 -05:00
|
|
|
this.instance.then(conn => conn.end());
|
2015-12-07 15:58:31 -05:00
|
|
|
}
|
2016-01-26 19:29:12 -05:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
module.exports = Adapter;
|