salix/services/db/testing_fixtures.js

42 lines
1.0 KiB
JavaScript
Raw Normal View History

let mysql = require('mysql2');
let connection = mysql.createConnection({
2018-01-21 16:47:24 +00:00
multipleStatements: true,
host: 'localhost',
user: 'root',
password: '',
database: 'salix'
});
2018-01-21 16:47:24 +00:00
let errorHandler = callback => {
return error => {
if (error)
throw error;
callback();
};
};
let insertFixtures = (inserts, callback) => {
connection.query(inserts[0], errorHandler(callback));
// connection.query('SET FOREIGN_KEY_CHECKS = 1');
};
let truncate = (tables, callback) => {
let truncatesSQL = tables.reduce((accumulator, currentValue, i) => {
let sql = 'TRUNCATE TABLE ' + currentValue + '; ';
2018-01-21 16:47:24 +00:00
return (`${accumulator}${sql}`);
}, '');
connection.query(truncatesSQL.slice(0, -2), errorHandler(callback));
};
connection.connect();
module.exports = function restoreFixtures(fixturesToApply, callback) {
2018-01-21 16:47:24 +00:00
connection.query('SET FOREIGN_KEY_CHECKS = 0', () => {
truncate(fixturesToApply.tables, () => {
insertFixtures(fixturesToApply.inserts, callback);
});
});
};