Removed connect, connection is now a new sqlite instance, async run function, check for errors when executing and setup and teardown now return promises.
This commit is contained in:
@@ -3,10 +3,9 @@ const path = require('path');
|
|||||||
const sqlite3 = require('sqlite3').verbose();
|
const sqlite3 = require('sqlite3').verbose();
|
||||||
|
|
||||||
class SqliteDatabase {
|
class SqliteDatabase {
|
||||||
|
|
||||||
constructor(host) {
|
constructor(host) {
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.connection = this.connect()
|
this.connection = new sqlite3.Database(this.host);
|
||||||
this.schemaDirectory = path.join(__dirname, 'schemas');
|
this.schemaDirectory = path.join(__dirname, 'schemas');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -14,11 +13,11 @@ class SqliteDatabase {
|
|||||||
* Connect to the database.
|
* Connect to the database.
|
||||||
* @returns {Promise} succeeds if connection was established
|
* @returns {Promise} succeeds if connection was established
|
||||||
*/
|
*/
|
||||||
connect() {
|
// connect() {
|
||||||
let database = new sqlite3.Database(this.host);
|
// let database = ;
|
||||||
this.connection = database;
|
// this.connection = database;
|
||||||
return database;
|
// return database;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run a SQL query against the database.
|
* Run a SQL query against the database.
|
||||||
@@ -26,7 +25,7 @@ class SqliteDatabase {
|
|||||||
* @param {Array} parameters in the SQL query
|
* @param {Array} parameters in the SQL query
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
run(sql, parameters) {
|
async run(sql, parameters) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.connection.run(sql, parameters, (error, result) => {
|
this.connection.run(sql, parameters, (error, result) => {
|
||||||
if (error)
|
if (error)
|
||||||
@@ -78,7 +77,13 @@ class SqliteDatabase {
|
|||||||
*/
|
*/
|
||||||
async execute(sql) {
|
async execute(sql) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
resolve(this.connection.exec(sql));
|
this.connection.exec(sql, (err, database) => {
|
||||||
|
if (err) {
|
||||||
|
console.log('ERROR: ', err);
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +93,7 @@ class SqliteDatabase {
|
|||||||
*/
|
*/
|
||||||
setUp() {
|
setUp() {
|
||||||
const setupSchema = this.readSqlFile('setup.sql');
|
const setupSchema = this.readSqlFile('setup.sql');
|
||||||
return this.execute(setupSchema);
|
return Promise.resolve(this.execute(setupSchema));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -97,7 +102,7 @@ class SqliteDatabase {
|
|||||||
*/
|
*/
|
||||||
tearDown() {
|
tearDown() {
|
||||||
const tearDownSchema = this.readSqlFile('tearDown.sql');
|
const tearDownSchema = this.readSqlFile('tearDown.sql');
|
||||||
return this.execute(tearDownSchema);
|
return Promise.resolve(this.execute(tearDownSchema));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user