Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scripts): add missing scripts 🐛 #845

Merged
merged 1 commit into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"test:coveralls": "cross-env NODE_ENV=test gulp testCoverage && cat ./coverage/lcov.info | coveralls",
"seed:dev": "cross-env NODE_ENV=development gulp seed",
"seed:prod": "cross-env NODE_ENV=production gulp seed",
"seed:mongodump": "./scripts/data/mongoDump.sh",
"seed:mongorestore": "node ./scripts/data/mongoRestore.js",
"seed:mongodump": "./scripts/db/mongoDump.sh",
"seed:mongorestore": "node ./scripts/db/mongoRestore.js",
"seed:mongodrop": "gulp drop",
"generate:sllCerts": "scripts/generate-ssl-certs.sh",
"lint": "gulp lint",
Expand Down
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/data/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/data/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/data/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();