Skip to content

Commit d099a5b

Browse files
authored
Merge pull request #2 from Codaisseur/setup-db
Setup db
2 parents 7070417 + 46c36c1 commit d099a5b

8 files changed

+2258
-49
lines changed

README.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,50 @@ cd YOUR_PROJECT_NAME
2626
npm install
2727
```
2828

29-
5. start server with `nodemon` (recommended for development)
29+
5. Configure your database in `config/config.json`
30+
31+
The default assumes a postgres database with
32+
33+
- username: postgres
34+
- password: secret
35+
36+
```json
37+
// config/config.json
38+
{
39+
"development": {
40+
"username": "postgres",
41+
"password": "secret",
42+
"database": "YOUR_PROJECT_NAME_HERE_development",
43+
"host": "localhost",
44+
"dialect": "postgres",
45+
"operatorsAliases": "0"
46+
}
47+
}
48+
```
49+
50+
6. Create database, run migrations & seed data
51+
52+
`package.json` contains a script for this
53+
54+
```bash
55+
npm run initdev
56+
```
57+
58+
Or run the commands seperately
59+
60+
```bash
61+
npx sequelize-cli db:create
62+
npx sequelize-cli db:migrate
63+
npx sequelize-cli db:seed:all
64+
```
65+
66+
7. start server with `nodemon` (recommended for development)
3067

3168
```
3269
npm run dev
3370
```
3471

35-
6. or start normally
72+
8. or start normally
3673

3774
```
3875
npm start

config/config.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"development": {
3+
"username": "postgres",
4+
"password": "secret",
5+
"database": "YOUR_PROJECT_NAME_HERE_development",
6+
"host": "localhost",
7+
"dialect": "postgres",
8+
"operatorsAliases": "0"
9+
},
10+
"test": {
11+
"username": "root",
12+
"password": null,
13+
"database": "database_test",
14+
"host": "127.0.0.1",
15+
"dialect": "postgres",
16+
"operatorsAliases": "0"
17+
},
18+
"production": {
19+
"username": "root",
20+
"password": null,
21+
"database": "database_production",
22+
"host": "127.0.0.1",
23+
"dialect": "postgres",
24+
"operatorsAliases": "0"
25+
}
26+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"use strict";
2+
module.exports = {
3+
up: (queryInterface, Sequelize) => {
4+
return queryInterface.createTable("users", {
5+
id: {
6+
allowNull: false,
7+
autoIncrement: true,
8+
primaryKey: true,
9+
type: Sequelize.INTEGER
10+
},
11+
name: {
12+
type: Sequelize.STRING,
13+
allowNull: false
14+
},
15+
email: {
16+
type: Sequelize.STRING,
17+
unique: true,
18+
allowNull: false
19+
},
20+
password: {
21+
type: Sequelize.STRING,
22+
allowNull: false
23+
},
24+
createdAt: {
25+
allowNull: false,
26+
type: Sequelize.DATE
27+
},
28+
updatedAt: {
29+
allowNull: false,
30+
type: Sequelize.DATE
31+
}
32+
});
33+
},
34+
down: (queryInterface, Sequelize) => {
35+
return queryInterface.dropTable("users");
36+
}
37+
};

models/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const Sequelize = require('sequelize');
6+
const basename = path.basename(__filename);
7+
const env = process.env.NODE_ENV || 'development';
8+
const config = require(__dirname + '/../config/config.json')[env];
9+
const db = {};
10+
11+
let sequelize;
12+
if (config.use_env_variable) {
13+
sequelize = new Sequelize(process.env[config.use_env_variable], config);
14+
} else {
15+
sequelize = new Sequelize(config.database, config.username, config.password, config);
16+
}
17+
18+
fs
19+
.readdirSync(__dirname)
20+
.filter(file => {
21+
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
22+
})
23+
.forEach(file => {
24+
const model = sequelize['import'](path.join(__dirname, file));
25+
db[model.name] = model;
26+
});
27+
28+
Object.keys(db).forEach(modelName => {
29+
if (db[modelName].associate) {
30+
db[modelName].associate(db);
31+
}
32+
});
33+
34+
db.sequelize = sequelize;
35+
db.Sequelize = Sequelize;
36+
37+
module.exports = db;

models/user.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
module.exports = (sequelize, DataTypes) => {
3+
const user = sequelize.define(
4+
"user",
5+
{
6+
name: {
7+
type: DataTypes.STRING,
8+
allowNull: false
9+
},
10+
email: {
11+
type: DataTypes.STRING,
12+
unique: true,
13+
allowNull: false
14+
},
15+
password: {
16+
type: DataTypes.STRING,
17+
allowNull: false
18+
}
19+
},
20+
{}
21+
);
22+
user.associate = function(models) {
23+
// associations can be defined here
24+
};
25+
return user;
26+
};

0 commit comments

Comments
 (0)