Skip to content

Commit

Permalink
feat(lib): seed user easily ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Oct 11, 2020
1 parent 91096bd commit 3683c3b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const inquirer = require('inquirer');
const plugins = gulpLoadPlugins();
const defaultAssets = require('./config/assets');

const config = require(path.resolve('./config'));

// default node env if not define
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

Expand Down Expand Up @@ -170,6 +172,23 @@ const seedMongoose = async () => {
}
};

// Connects to Mongoose based on environment settings and seeds the database
const seedMongooseUser = async () => {
try {
const mongooseService = require(path.resolve('./lib/services/mongoose'));
await mongooseService.connect();
await mongooseService.loadModels();
const UserService = require(path.resolve('./modules/users/services/user.service'));
const seed = require(path.resolve('./lib/services/seed'));
await seed.user(config.seedDB.options.seedUser, UserService).catch((e) => {
console.log(e);
});
await mongooseService.disconnect();
} catch (err) {
console.log(err);
}
};

// Connects to an SQL database, drop and re-create the schemas
// gulp.task('seed:sequelize', (done) => {
// const sequelize = require('./lib/services/sequelize');
Expand All @@ -196,6 +215,10 @@ exports.testCoverage = testCoverage;
const seed = gulp.series(dropDB, seedMongoose);
exports.seed = seed;

// Run Mongoose Seed
const seedUser = gulp.series(seedMongooseUser);
exports.seedUser = seedUser;

// Run Mongoose drop
const drop = gulp.series(dropDB);
exports.drop = drop;
Expand Down
18 changes: 18 additions & 0 deletions lib/services/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,21 @@ module.exports.start = async (options, UserService, TaskService) => {

return result;
};

module.exports.user = async (user, UserService) => {
let pwd;
const result = [];

// Check for provided options
seedOptions = _.clone(config.seedDB.options, true);

try {
pwd = await UserService.generateRandomPassphrase();
result.push(await seedTheUser(UserService, user)(pwd));
} catch (err) {
console.log(err);
return new AppError('Error on seed start.');
}

return result;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"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:user": "gulp seedUser",
"seed:mongodump": "./scripts/db/mongodump.sh",
"seed:mongorestore": "node ./scripts/db/mongorestore.js",
"seed:mongodrop": "gulp drop",
Expand Down

0 comments on commit 3683c3b

Please sign in to comment.