salix/services/db/testing_fixtures.js

39 lines
938 B
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 = async function(sqlStatements) {
try {
if (sqlStatements.deletes.length)
await connection.query(sqlStatements.deletes);
if (sqlStatements.inserts.length)
await connection.query(sqlStatements.inserts);
if (sqlStatements.updates.length)
await connection.query(sqlStatements.updates);
} catch (error) {
errorHandler(error);
}
2018-01-21 16:47:24 +00:00
};
connection.connect();
module.exports = function restoreFixtures(sqlStatements) {
2018-01-21 16:47:24 +00:00
connection.query('SET FOREIGN_KEY_CHECKS = 0', () => {
insertFixtures(sqlStatements);
2018-01-21 16:47:24 +00:00
});
};