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

feat: configure MongoDB with mevn generate #178

Merged
merged 9 commits into from Jul 7, 2020
23 changes: 23 additions & 0 deletions src/commands/basic/generate.js
Expand Up @@ -107,6 +107,29 @@ const generateFile = async () => {
]);
fs.writeFileSync('./server/.env', `DB_URL=${uri}`);
}

const dbFilePath = `server/src/db/mongodb.js`;
fs.copyFile('../../helpers/db', dbFilePath, (err) => {
if (err) throw err;
});

const serverFile = fs
.readFileSync('./server/src/router.js', 'utf8')
.toString()
.split('\n');

const postImportIndex = serverFile.indexOf(
serverFile.find((item) => item === ''),
);

// Include a new line to compensate the previous addition
serverFile.splice(
postImportIndex + 1,
0,
'import mongoDbInit from "./helpers/db/mongodb.js";',
'',
'mongodbInit();',
);
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
};

module.exports = generateFile;
15 changes: 15 additions & 0 deletions src/helpers/db/mongodb.js
@@ -0,0 +1,15 @@
const mongoose = require('mongoose');

const init = () => {
mongoose.connect(process.env.DB_URL).catch((err) => {
danivijay marked this conversation as resolved.
Show resolved Hide resolved
console.error('error: ' + err.stack);
process.exit(1);
});
mongoose.connection.on('open', () => {
console.log('connected to database');
});
};

mongoose.Promise = global.Promise;

export default init;
danivijay marked this conversation as resolved.
Show resolved Hide resolved