Updated from sqlite to sqlite3 now all functions are async so we wait for response.

This commit is contained in:
2018-03-06 19:16:10 +01:00
parent e6a8515432
commit 34a97c069b

View File

@@ -1,12 +1,12 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const sqlite = require('sqlite'); const sqlite3 = require('sqlite3').verbose();
class SqliteDatabase { class SqliteDatabase {
constructor(host) { constructor(host) {
this.host = host; this.host = host;
this.connection = sqlite; this.connection = this.connect()
this.schemaDirectory = path.join(__dirname, 'schemas'); this.schemaDirectory = path.join(__dirname, 'schemas');
} }
@@ -14,10 +14,12 @@ 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() { async connect() {
return Promise.resolve() return Promise.resolve()
.then(() => sqlite.open(this.host)) .then(() => {
.then(() => sqlite.exec('pragma foreign_keys = on;')); this.connection = new sqlite3.Database(this.host);
this.connection.run('PRAGMA foreign_keys=on')
});
} }
/** /**
@@ -27,7 +29,13 @@ class SqliteDatabase {
* @returns {Promise} * @returns {Promise}
*/ */
run(sql, parameters) { run(sql, parameters) {
return this.connection.run(sql, parameters); return new Promise((resolve, reject) => {
this.connection.run(sql, parameters, (error, result) => {
if (error)
reject(error);
resolve(result)
});
});
} }
/** /**
@@ -36,8 +44,15 @@ class SqliteDatabase {
* @param {Array} parameters in the SQL query * @param {Array} parameters in the SQL query
* @returns {Promise} * @returns {Promise}
*/ */
all(sql, parameters) { async all(sql, parameters) {
return this.connection.all(sql, parameters); return new Promise((resolve, reject) => {
this.connection.all(sql, parameters, (err, rows) => {
if (err) {
reject(err);
}
resolve(rows);
})
})
} }
/** /**
@@ -46,8 +61,16 @@ class SqliteDatabase {
* @param {Array} parameters in the SQL query * @param {Array} parameters in the SQL query
* @returns {Promise} * @returns {Promise}
*/ */
get(sql, parameters) { async get(sql, parameters) {
return this.connection.get(sql, parameters); return new Promise((resolve, reject) => {
this.connection.get(sql, parameters, (err, rows) => {
if (err) {
reject(err);
}
console.log('rows', rows)
resolve(rows);
})
})
} }
/** /**
@@ -56,8 +79,10 @@ class SqliteDatabase {
* @param {Array} parameters in the SQL query * @param {Array} parameters in the SQL query
* @returns {Promise} * @returns {Promise}
*/ */
execute(sql) { async execute(sql) {
return this.connection.exec(sql); return new Promise(resolve => {
resolve(this.connection.exec(sql));
})
} }
/** /**