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/add config #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:16.13.0

WORKDIR /app

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 8080

CMD ["node", "server.js"]
3 changes: 2 additions & 1 deletion app/config/AuthConfig.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config();
module.exports = {
secret: "techblog-secret-key"
secret: process.env.API_SECRET_KEY,
};
11 changes: 6 additions & 5 deletions app/config/DbConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require('dotenv').config();
module.exports = {
HOST: "123.30.234.61",
USER: "tungnt",
PASSWORD: "tungnt",
DB: "postgres",
dialect: "postgres",
HOST: process.env.POSTGRES_HOST,
USER: process.env.POSTGRES_USER,
PASSWORD: process.env.POSTGRES_PASSWORD,
DB: process.env.POSTGRES_DB,
dialect: process.env.POSTGRES_DIALECT,
pool: {
max: 5,
min: 0,
Expand Down
12 changes: 0 additions & 12 deletions app/models/RoleModel.js

This file was deleted.

20 changes: 0 additions & 20 deletions app/models/UserModel.js

This file was deleted.

35 changes: 35 additions & 0 deletions app/models/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const Sequelize = require('sequelize');
module.exports = function(sequelize, Sequelize) {
return sequelize.define('comments', {
id: {
autoIncrement: true,
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true
},
deleted: {
type: Sequelize.BOOLEAN,
allowNull: true
},
user_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
},
post_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'posts',
key: 'id'
}
},
content: {
type: Sequelize.TEXT,
allowNull: false
}
});
};
21 changes: 6 additions & 15 deletions app/models/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const config = require("../config/DbConfig.js");
const Sequelize = require("sequelize");
const config = require("../config/DbConfig.js");
const initModels = require("./init-models");
const db = {};
const sequelize = new Sequelize(
config.DB,
config.USER,
Expand All @@ -16,20 +18,9 @@ const sequelize = new Sequelize(
}
}
);
const db = {};

db.sequelize = sequelize
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.user = require("../models/UserModel.js")(sequelize, Sequelize);
db.role = require("../models/RoleModel.js")(sequelize, Sequelize);
db.role.belongsToMany(db.user, {
through: "user_roles",
foreignKey: "roleId",
otherKey: "userId"
});
db.user.belongsToMany(db.role, {
through: "user_roles",
foreignKey: "userId",
otherKey: "roleId"
});
db.ROLES = ["user", "admin", "moderator"];
initModels(db.sequelize);
module.exports = db;
52 changes: 52 additions & 0 deletions app/models/init-models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const config = require("../config/DbConfig.js");
const Sequelize = require("sequelize");
var DataTypes = require("sequelize").DataTypes;

var _comments = require("./comments");
var _post_tags = require("./post_tags");
var _posts = require("./posts");
var _tags = require("./tags");
var _users = require("./users");
var _votes = require("./votes");
var _role = require("./role");

function initModels(sequelize) {
var comments = _comments(sequelize, DataTypes);
var post_tags = _post_tags(sequelize, DataTypes);
var posts = _posts(sequelize, DataTypes);
var tags = _tags(sequelize, DataTypes);
var users = _users(sequelize, DataTypes);
var votes = _votes(sequelize, DataTypes);
var role = _role(sequelize, DataTypes);

posts.belongsToMany(tags, { as: 'tag_id_tags', through: post_tags, foreignKey: "post_id", otherKey: "tag_id" });
tags.belongsToMany(posts, { as: 'post_id_posts', through: post_tags, foreignKey: "tag_id", otherKey: "post_id" });
comments.belongsTo(posts, { as: "post", foreignKey: "post_id"});
posts.hasMany(comments, { as: "comments", foreignKey: "post_id"});
post_tags.belongsTo(posts, { as: "post", foreignKey: "post_id"});
posts.hasMany(post_tags, { as: "post_tags", foreignKey: "post_id"});
votes.belongsTo(posts, { as: "post", foreignKey: "post_id"});
posts.hasMany(votes, { as: "votes", foreignKey: "post_id"});
post_tags.belongsTo(tags, { as: "tag", foreignKey: "tag_id"});
tags.hasMany(post_tags, { as: "post_tags", foreignKey: "tag_id"});
comments.belongsTo(users, { as: "user", foreignKey: "user_id"});
users.hasMany(comments, { as: "comments", foreignKey: "user_id"});
posts.belongsTo(users, { as: "user", foreignKey: "user_id"});
users.hasMany(posts, { as: "posts", foreignKey: "user_id"});
votes.belongsTo(users, { as: "user", foreignKey: "user_id"});
users.hasMany(votes, { as: "votes", foreignKey: "user_id"});
role.belongsToMany(users, { as: 'user_id_users', through: 'user_role', foreignKey: "role_id", otherKey: "user_id" });
users.belongsToMany(role, { as: 'role_id_roles', through: 'user_role', foreignKey: "user_id", otherKey: "role_id" });

return {
comments,
post_tags,
posts,
tags,
users,
votes,
};
}
module.exports = initModels;
module.exports.initModels = initModels;
module.exports.default = initModels;
23 changes: 23 additions & 0 deletions app/models/post_tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('post_tags', {
tag_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: 'tags',
key: 'id'
}
},
post_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: 'posts',
key: 'id'
}
}
});
};
Loading