expressCart/lib/testdata.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

const common = require('./common');
2019-06-11 15:32:44 +10:00
const{initDb} = require('./db');
const fs = require('fs');
const path = require('path');
const testData = fs.readFileSync(path.join(__dirname, '..', 'bin', 'testdata.json'), 'utf-8');
const jsonData = JSON.parse(testData);
// get config
let config = common.getConfig();
initDb(config.databaseConnectionString, (err, db) => {
Promise.all([
db.users.remove({}, {}),
db.customers.remove({}, {}),
db.products.remove({}, {}),
db.menu.remove({}, {})
])
.then(() => {
Promise.all([
db.users.insertMany(jsonData.users),
db.customers.insertMany(jsonData.customers),
2019-06-09 22:32:22 +10:00
db.products.insertMany(common.fixProductDates(jsonData.products)),
db.menu.insertOne(jsonData.menu)
])
.then(() => {
console.log('Test data complete');
process.exit();
})
.catch((err) => {
console.log('Error inserting test data', err);
2019-06-11 15:32:44 +10:00
process.exit(2);
});
})
.catch((err) => {
console.log('Error removing existing test data', err);
2019-06-11 15:32:44 +10:00
process.exit(2);
});
});