-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee05ab0
commit 4917832
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |