expressCart/lib/testdata.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-06-15 14:46:08 +10:00
const { getConfig } = require('./common');
const { initDb } = require('./db');
const { fixProductDates, fixDiscountDates } = require('../test/helper');
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
2019-07-12 18:06:34 +10:00
const config = getConfig();
initDb(config.databaseConnectionString, (err, db) => {
Promise.all([
2019-10-29 18:26:30 +10:00
db.users.deleteMany({}, {}),
db.customers.deleteMany({}, {}),
db.products.deleteMany({}, {}),
db.discounts.deleteMany({}, {}),
2019-10-29 18:26:30 +10:00
db.menu.deleteMany({}, {})
])
.then(() => {
Promise.all([
db.users.insertMany(jsonData.users),
db.customers.insertMany(jsonData.customers),
2019-06-15 14:46:08 +10:00
db.products.insertMany(fixProductDates(jsonData.products)),
db.discounts.insertMany(fixDiscountDates(jsonData.discounts)),
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);
});
});