Skip to content

Commit

Permalink
fix(scripts): fix file name 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Jun 21, 2020
1 parent ee05ab0 commit 4917832
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions scripts/db/mongodump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# NODE_ENV=xxx ./scripts/data/dump.sh

if [ -z ${NODE_ENV+x} ]; then NODE_ENV=development; fi

db=$(grep -i 'mongodb://' ./config/defaults/${NODE_ENV}.js | awk -F/ '{print $NF}' | rev | cut -c3- | rev )

mongodump -d ${db} --out=./scripts/db/dump
55 changes: 55 additions & 0 deletions scripts/db/mongorestore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Module dependencies
*/

const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
const bson = require('bson');

const config = require(path.resolve('./config'));
const mongooseService = require(path.resolve('./lib/services/mongoose'));

const exceptions = ['uploads'];

/**
* Work
*/

const seedData = async () => {
try {
console.log(chalk.bold.green('Start Seed Dump by update items if differents'));

// connect to mongo
await mongooseService.connect();
await mongooseService.loadModels();

const database = config.db.uri.split('/')[config.db.uri.split('/').length - 1];
console.log(chalk.bold.green(`database selected: ${database}`));

fs.readdirSync(path.resolve(`./scripts/db/dump/${database}`)).forEach((file) => {
if (file.slice(-4) === 'bson' && !exceptions.includes(file.split('.')[0])) {
const collection = file.slice(0, -5);

const buffer = fs.readFileSync(path.resolve(`./scripts/db/dump/${database}/${collection}.bson`));
let bfIdx = 0;
const items = [];
while (bfIdx < buffer.length) bfIdx = bson.deserializeStream(buffer, bfIdx, 1, items, items.length);

const Service = require(path.resolve(`./modules/${collection}/services/${collection}.data.service`));
Service.import(items, ['_id']);

console.log(chalk.blue(`Database Seeding ${collection} : ${items.length}`));
}
});
} catch (err) {
console.log(chalk.bold.red(`Error ${err}`));
}

setTimeout(() => {
console.log(chalk.bold.green('Finish adding items to mongoDB'));
process.exit(0);
}, 5000);
};

seedData();

0 comments on commit 4917832

Please sign in to comment.