Skip to content

Commit c6d4e43

Browse files
committed
feat: add migrations to create a default admin and update readme file
1 parent 2d7a829 commit c6d4e43

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ http://localhost:3000/admin/login
9494

9595
To login in the admin panel, you have to have registered verified user with admin role.
9696

97+
To create a default admin user you can run migrations:
98+
99+
```bash
100+
mingrate-mongo up
101+
```
102+
103+
The migrations will create a default admin user in db with creds:
104+
105+
```bash
106+
login: admin@test.com
107+
password: String_12345
108+
```
97109

98110
![Alt Text2](https://media.giphy.com/media/HdWGgOGfQa0QRLNXME/giphy.gif)
99111

generators/admin/steps/writing.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ module.exports = function() {
1717
this.destinationPath(`${rootFolder}/src/exceptions`),
1818
payload,
1919
);
20+
this.fs.copyTpl(
21+
this.templatePath(`${adminFolder}/migrations`),
22+
this.destinationPath(`${rootFolder}/migrations`),
23+
payload,
24+
);
25+
this.fs.copyTpl(
26+
this.templatePath(`${adminFolder}/migrate-mongo-config.js`),
27+
this.destinationPath(`${rootFolder}/migrate-mongo-config.js`),
28+
payload,
29+
);
2030
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require('dotenv').config();
2+
3+
module.exports = {
4+
mongodb: {
5+
url: process.env.MONGODB_URL,
6+
7+
options: {
8+
useNewUrlParser: true, // removes a deprecation warning when connecting
9+
useUnifiedTopology: true, // removes a deprecating warning when connecting
10+
// connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
11+
// socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
12+
},
13+
},
14+
15+
// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
16+
migrationsDir: 'migrations',
17+
18+
// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
19+
changelogCollectionName: 'changelog',
20+
21+
// The file extension to create migrations and search for in migration dir
22+
migrationFileExtension: '.js',
23+
24+
// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determin
25+
// if the file should be run. Requires that scripts are coded to be run multiple times.
26+
useFileHash: false,
27+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports.up = async (db) => {
2+
await db.collection('users').insertOne({
3+
email: 'admin@test.com',
4+
password: '$2a$10$JBhu1rHrGpk.wx.F6Cta1ujDgENg1XOoZKyeQCS77ZrEMaSsocYQ6',
5+
roles: ['user', 'admin'],
6+
verified: true,
7+
});
8+
};
9+
10+
module.exports.down = async (db) => {
11+
await db.collection('users').deleteOne({
12+
email: 'admin@test.com',
13+
});
14+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require('dotenv').config();
2+
3+
module.exports = {
4+
mongodb: {
5+
url: process.env.MONGODB_URL,
6+
7+
options: {
8+
useNewUrlParser: true, // removes a deprecation warning when connecting
9+
useUnifiedTopology: true, // removes a deprecating warning when connecting
10+
// connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
11+
// socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
12+
},
13+
},
14+
15+
// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
16+
migrationsDir: 'migrations',
17+
18+
// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
19+
changelogCollectionName: 'changelog',
20+
21+
// The file extension to create migrations and search for in migration dir
22+
migrationFileExtension: '.js',
23+
24+
// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determin
25+
// if the file should be run. Requires that scripts are coded to be run multiple times.
26+
useFileHash: false,
27+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports.up = async (db) => {
2+
await db.collection('users').insertOne({
3+
email: 'admin@test.com',
4+
password: '$2a$10$JBhu1rHrGpk.wx.F6Cta1ujDgENg1XOoZKyeQCS77ZrEMaSsocYQ6',
5+
roles: ['user', 'admin'],
6+
verified: true,
7+
});
8+
};
9+
10+
module.exports.down = async (db) => {
11+
await db.collection('users').deleteOne({
12+
email: 'admin@test.com',
13+
});
14+
};

0 commit comments

Comments
 (0)