Skip to content

Commit 447532e

Browse files
committed
Update boilerplate
1 parent 3aa9c81 commit 447532e

27 files changed

+3953
-2963
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
coverage
2+
coverage
3+
.DS_Store

Dockerfile.dev

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
FROM node:13.2.0-stretch-slim
1+
FROM node:10.19.0-buster-slim
22

3-
LABEL Name=boilerplate-node-postgres-dev Version=1.0.0
3+
LABEL Name=boilerplate-backend Version=1.0.0
44

55
EXPOSE 5000
66
#Adjust localtime to your needs
77
#Set locale
88
ENV DEBIAN_FRONTEND=noninteractive
99

10-
RUN apt-get update && apt-get install -y --no-install-recommends \
11-
locales \
12-
tzdata \
13-
ca-certificates \
14-
libpq-dev \
15-
&& sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
16-
&& echo 'LANG="en_US.UTF-8"'>/etc/default/locale \
17-
&& dpkg-reconfigure --frontend=noninteractive locales \
18-
&& update-locale LANG=en_US.UTF-8 \
19-
&& apt-get clean && rm -rf /var/lib/apt/lists/*
20-
2110
RUN mkdir /app && chown node:node /app
2211

2312
USER node
@@ -32,4 +21,4 @@ ENV PATH=/app/node_modules/.bin:$PATH
3221

3322
WORKDIR /app/node_app
3423

35-
CMD ["npm", "run", "dev"]
24+
CMD ["npm", "run", "dev"]

README.md

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
1-
# Node.js/Express.js/Postgresql/ boilerplate with Docker
1+
# Run project
22

3-
**Stack**:
3+
docker-compose -f docker-compose-dev.yml up --build
44

5-
- Node
6-
- Express
7-
- Nodemon
8-
- Pg
9-
- Sequelize
10-
- Jest
11-
- Supertest
12-
- Eslint
13-
- Prettier
14-
- Docker
15-
- Docker-compose
5+
# Access to container
166

17-
> Editor recomended is "Visual Studio Code"
7+
docker exec -ti -u root #container_id /bin/bash
188

19-
**Run the boilerplate**
9+
# Generate model
2010

21-
1. Execute in terminal `docker-compose up --build` in the root _boilerplate-node-express-postgresql-with-docker_ folder.
11+
npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string
2212

23-
2. In other terminal execute `npm install` in the root folder _boilerplate-node-express-postgresql-with-docker_ to install local dependencies for the good practices of code, dependencies like a eslint, prettier, pre-commit, etc.
13+
# Migrate
2414

25-
**Visual Studio Code**
26-
27-
For the good practices of code, activate the option `Editor: Format On Save` in the **setting** section.
15+
sequelize db:migrate --url "postgres://hackademy_user:tupass@postgres:5432/hackademydbtest"

apidoc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "Caritbot API documentation",
3+
"version": "1.0.0",
4+
"description": "API Caritbot Application",
5+
"template": {
6+
"forceLanguage": "en"
7+
}
8+
}

config/config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
PORT: process.env.PORT,
3+
DB: {
4+
username: process.env.DB_USER,
5+
password: process.env.DB_PASSWORD,
6+
database: process.env.DB_NAME,
7+
host: process.env.DB_HOST,
8+
port: process.env.DB_PORT,
9+
dialect: 'postgres',
10+
logging: false
11+
}
12+
};

config/database.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Sequelize from 'sequelize';
2+
import { DB } from './config';
3+
4+
export default new Sequelize(DB.database, DB.username, DB.password, {
5+
host: DB.host,
6+
port: DB.port,
7+
dialect: DB.dialect
8+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
firstName: {
12+
type: Sequelize.STRING
13+
},
14+
lastName: {
15+
type: Sequelize.STRING
16+
},
17+
email: {
18+
type: Sequelize.STRING
19+
},
20+
createdAt: {
21+
allowNull: false,
22+
type: Sequelize.DATE
23+
},
24+
updatedAt: {
25+
allowNull: false,
26+
type: Sequelize.DATE
27+
}
28+
});
29+
},
30+
down: (queryInterface, Sequelize) => {
31+
return queryInterface.dropTable('Users');
32+
}
33+
};

database/models/index.js

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

database/models/user.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
module.exports = (sequelize, DataTypes) => {
3+
const User = sequelize.define(
4+
'User',
5+
{
6+
firstName: DataTypes.STRING,
7+
lastName: DataTypes.STRING,
8+
email: DataTypes.STRING
9+
},
10+
{}
11+
);
12+
User.associate = function(models) {
13+
// associations can be defined here
14+
};
15+
return User;
16+
};

docker-compose-dev.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
version: '3.3'
2+
services:
3+
postgres:
4+
image: 'postgres:12.2'
5+
ports:
6+
- 5434:5432
7+
environment:
8+
- POSTGRES_USER=user_db
9+
- POSTGRES_DB=boilerplate_db
10+
- POSTGRES_PASSWORD=tupass
11+
volumes:
12+
- 'pgdata-boilerplate:/var/lib/postgresql/data'
13+
14+
api:
15+
depends_on:
16+
- postgres
17+
build:
18+
dockerfile: Dockerfile.dev
19+
context: .
20+
restart: unless-stopped
21+
ports:
22+
- '5050:5000'
23+
volumes:
24+
- .:/app/node_app
25+
- ./package.json:/app/package.json
26+
- ./package-lock.json:/app/package-lock.json
27+
- notused:/app/node_modules
28+
environment:
29+
- TZ=America/Mexico_City
30+
- LC_CTYPE=en_US.UTF-8
31+
- LC_ALL=en_US.UTF-8
32+
- DB_USER=user_db
33+
- PORT=5000
34+
- DB_HOST=postgres
35+
- DB_NAME=boilerplate_db
36+
- DB_PASSWORD=tupass
37+
- DB_PORT=5432
38+
39+
volumes:
40+
pgdata-boilerplate:
41+
notused:

0 commit comments

Comments
 (0)