Mise à jour des models mysql
This commit is contained in:
+12
-4
@@ -1,15 +1,23 @@
|
|||||||
const sequelize = require('./config/sequelize');
|
const sequelize = require('./config/sequelize');
|
||||||
const ArticleModel = require('./models/article');
|
const ArticleModel = require('./models/mysql/Article');
|
||||||
const CommentModel = require('./models/comment');
|
const ArticleTageModel = require('./models/mysql/ArticleTag');
|
||||||
const TagModel = require('./models/tag');
|
const CommentModel = require('./models/mysql/Comment');
|
||||||
const UserModel = require('./models/user');
|
const FavoriteModel = require('./models/mysql/Favorite');
|
||||||
|
const FollowerModel = require('./models/mysql/Follower');
|
||||||
|
const TagModel = require('./models/mysql/Tag');
|
||||||
|
const TagListModel = require('./models/mysql/TagList');
|
||||||
|
const UserModel = require('./models/mysql/User');
|
||||||
|
|
||||||
const DB = {
|
const DB = {
|
||||||
sequelize,
|
sequelize,
|
||||||
User: UserModel(sequelize),
|
User: UserModel(sequelize),
|
||||||
Article: ArticleModel(sequelize),
|
Article: ArticleModel(sequelize),
|
||||||
|
ArticleTag: ArticleTageModel(sequelize),
|
||||||
Comment: CommentModel(sequelize),
|
Comment: CommentModel(sequelize),
|
||||||
|
Favorite: FavoriteModel(sequelize),
|
||||||
|
Follower: FollowerModel(sequelize),
|
||||||
Tag: TagModel(sequelize),
|
Tag: TagModel(sequelize),
|
||||||
|
TagList: TagListModel(sequelize),
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = DB;
|
module.exports = DB;
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
const { DataTypes, Model } = require("sequelize");
|
|
||||||
const sequelize = require("../config/sequelize");
|
|
||||||
const slug = require("slug");
|
|
||||||
|
|
||||||
class Article extends Model { }
|
|
||||||
|
|
||||||
const ArticleModel = () => {
|
|
||||||
Article.init(
|
|
||||||
{
|
|
||||||
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
|
|
||||||
slug: { type: DataTypes.STRING, allowNull: false },
|
|
||||||
title: { type: DataTypes.STRING, allowNull: false },
|
|
||||||
description: { type: DataTypes.TEXT },
|
|
||||||
body: { type: DataTypes.TEXT, allowNull: false }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sequelize,
|
|
||||||
modelName: 'Article',
|
|
||||||
tableName: 'article',
|
|
||||||
timestamps: true,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Article.beforeValidate((article) => {
|
|
||||||
article.slug =slug(`${article.title}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
|
|
||||||
});
|
|
||||||
|
|
||||||
Article.prototype.toJSONFor = function () {
|
|
||||||
return {
|
|
||||||
id: this.id,
|
|
||||||
slug: this.slug,
|
|
||||||
title: this.title,
|
|
||||||
description: this.description,
|
|
||||||
body: this.body
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
return Article;
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = ArticleModel;
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
const { DataTypes, Model } = require("sequelize");
|
|
||||||
const sequelize = require("../config/sequelize");
|
|
||||||
|
|
||||||
class Comment extends Model { }
|
|
||||||
|
|
||||||
const CommentModel = () => {
|
|
||||||
Comment.init(
|
|
||||||
{
|
|
||||||
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
|
|
||||||
body: { type: DataTypes.TEXT, allowNull: false }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sequelize,
|
|
||||||
modelName: 'Comment',
|
|
||||||
tableName: 'comment',
|
|
||||||
timestamps: true,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Comment.prototype.toJSONFor = function () {
|
|
||||||
return {
|
|
||||||
id: this.id,
|
|
||||||
body: this.body
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
return Comment;
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = CommentModel;
|
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
const { DataTypes, Model } = require("sequelize");
|
||||||
|
const sequelize = require("../../config/sequelize");
|
||||||
|
const slug = require("slug");
|
||||||
|
|
||||||
|
class Article extends Model { }
|
||||||
|
|
||||||
|
const ArticleModel = () => {
|
||||||
|
Article.init(
|
||||||
|
{
|
||||||
|
id: {
|
||||||
|
autoIncrement: true,
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true
|
||||||
|
},
|
||||||
|
slug: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
authorId: {
|
||||||
|
type: DataTypes.STRING(24),
|
||||||
|
allowNull: true,
|
||||||
|
references: {
|
||||||
|
model: 'user',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sequelize,
|
||||||
|
tableName: 'article',
|
||||||
|
timestamps: true,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
name: "PRIMARY",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "id" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "authorId",
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "authorId" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Article.beforeValidate((article) => {
|
||||||
|
article.slug = slug(`${article.title}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
|
||||||
|
});
|
||||||
|
|
||||||
|
Article.prototype.toJSONFor = function () {
|
||||||
|
return {
|
||||||
|
id: this.id,
|
||||||
|
slug: this.slug,
|
||||||
|
title: this.title,
|
||||||
|
description: this.description,
|
||||||
|
body: this.body
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return Article;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = ArticleModel;
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
const { DataTypes, Model } = require("sequelize");
|
||||||
|
const sequelize = require("../../config/sequelize");
|
||||||
|
|
||||||
|
class ArticleTag extends Model { }
|
||||||
|
|
||||||
|
const ArticleTagModel = () => {
|
||||||
|
ArticleTag.init(
|
||||||
|
{
|
||||||
|
tagName: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'tag',
|
||||||
|
key: 'name'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
articleId: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'article',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sequelize,
|
||||||
|
tableName: 'articleTag',
|
||||||
|
timestamps: false,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
name: "PRIMARY",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "tagName" },
|
||||||
|
{ name: "articleId" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "articleId",
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "articleId" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
ArticleTag.prototype.toJSONFor = function () {
|
||||||
|
return {
|
||||||
|
tagName: this.tagName,
|
||||||
|
articleId: this.articleId
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return ArticleTag;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = ArticleTagModel;
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
const { DataTypes, Model } = require("sequelize");
|
||||||
|
const sequelize = require("../../config/sequelize");
|
||||||
|
|
||||||
|
class Comment extends Model { }
|
||||||
|
|
||||||
|
const CommentModel = () => {
|
||||||
|
Comment.init(
|
||||||
|
{
|
||||||
|
id: {
|
||||||
|
autoIncrement: true,
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
authorId: {
|
||||||
|
type: DataTypes.STRING(24),
|
||||||
|
allowNull: true,
|
||||||
|
references: {
|
||||||
|
model: 'user',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
articleId: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: true,
|
||||||
|
references: {
|
||||||
|
model: 'article',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sequelize,
|
||||||
|
tableName: 'comment',
|
||||||
|
timestamps: true,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
name: "PRIMARY",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "id" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "authorId",
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "authorId" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "articleId",
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "articleId" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Comment.prototype.toJSONFor = function () {
|
||||||
|
return {
|
||||||
|
id: this.id,
|
||||||
|
body: this.body
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return Comment;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = CommentModel;
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
const { DataTypes, Model } = require("sequelize");
|
||||||
|
const sequelize = require("../../config/sequelize");
|
||||||
|
|
||||||
|
class Favorite extends Model { }
|
||||||
|
|
||||||
|
const FavoriteModel = () => {
|
||||||
|
Favorite.init(
|
||||||
|
{
|
||||||
|
userId: {
|
||||||
|
type: DataTypes.STRING(24),
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'user',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
articleId: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'article',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sequelize,
|
||||||
|
tableName: 'favorite',
|
||||||
|
timestamps: false,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
name: "PRIMARY",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "userId" },
|
||||||
|
{ name: "articleId" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "articleId",
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "articleId" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Favorite.prototype.toJSONFor = function () {
|
||||||
|
return {
|
||||||
|
userId: this.userId,
|
||||||
|
articleId: this.articleId
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return Favorite;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = FavoriteModel;
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
const { DataTypes, Model } = require("sequelize");
|
||||||
|
const sequelize = require("../../config/sequelize");
|
||||||
|
|
||||||
|
class Follower extends Model { }
|
||||||
|
|
||||||
|
const FollowerModel = () => {
|
||||||
|
Follower.init(
|
||||||
|
{
|
||||||
|
userId: {
|
||||||
|
type: DataTypes.STRING(24),
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'user',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
followerId: {
|
||||||
|
type: DataTypes.STRING(24),
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'user',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sequelize,
|
||||||
|
tableName: 'follower',
|
||||||
|
timestamps: false,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
name: "PRIMARY",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "userId" },
|
||||||
|
{ name: "followerId" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "followerId",
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "followerId" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Follower.prototype.toJSONFor = function () {
|
||||||
|
return {
|
||||||
|
userId: this.userId,
|
||||||
|
followerId: this.followerId
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return Follower;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = FollowerModel;
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
const { DataTypes, Model } = require("sequelize");
|
||||||
|
const sequelize = require("../../config/sequelize");
|
||||||
|
|
||||||
|
class Tag extends Model { }
|
||||||
|
|
||||||
|
const TagModel = () => {
|
||||||
|
Tag.init(
|
||||||
|
{
|
||||||
|
name: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sequelize,
|
||||||
|
tableName: 'tag',
|
||||||
|
timestamps: true,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
name: "PRIMARY",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "name" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Tag.prototype.toJSONFor = function () {
|
||||||
|
return {
|
||||||
|
name: this.name
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return Tag;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = TagModel;
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
const { DataTypes, Model } = require("sequelize");
|
||||||
|
const sequelize = require("../../config/sequelize");
|
||||||
|
|
||||||
|
class TagList extends Model { }
|
||||||
|
|
||||||
|
const TagListModel = () => {
|
||||||
|
TagList.init(
|
||||||
|
{
|
||||||
|
articleId: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'article',
|
||||||
|
key: 'id'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
tagName: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: false,
|
||||||
|
primaryKey: true,
|
||||||
|
references: {
|
||||||
|
model: 'tag',
|
||||||
|
key: 'name'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sequelize,
|
||||||
|
tableName: 'tagList',
|
||||||
|
timestamps: false,
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
name: "PRIMARY",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "articleId" },
|
||||||
|
{ name: "tagName" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "tagName",
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "tagName" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
TagList.prototype.toJSONFor = function () {
|
||||||
|
return {
|
||||||
|
articleId: this.articleId,
|
||||||
|
tagName: this.tagName
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return TagList;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = TagListModel;
|
||||||
@@ -1,54 +1,93 @@
|
|||||||
const { DataTypes, Model } = require("sequelize");
|
const { DataTypes, Model } = require("sequelize");
|
||||||
var crypto = require('crypto');
|
var crypto = require('crypto');
|
||||||
var jwt = require('jsonwebtoken'),
|
var jwt = require('jsonwebtoken'),
|
||||||
secret = require('../../../config').secret,
|
secret = require('../../../../config').secret,
|
||||||
session_lifetime = require('../../../config').session_lifetime;
|
session_lifetime = require('../../../../config').session_lifetime;
|
||||||
|
|
||||||
const sequelize = require("../config/sequelize");
|
const sequelize = require("../../config/sequelize");
|
||||||
//const bcrypt = require("bcrypt");
|
|
||||||
|
|
||||||
class User extends Model { }
|
class User extends Model { }
|
||||||
|
|
||||||
const UserModel = () => {
|
const UserModel = () => {
|
||||||
User.init(
|
User.init(
|
||||||
{
|
{
|
||||||
_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
|
id: {
|
||||||
email: { type: DataTypes.STRING, allowNull: false, unique: true },
|
type: DataTypes.STRING(24),
|
||||||
username: { type: DataTypes.STRING, allowNull: false, unique: true },
|
allowNull: false,
|
||||||
firstname: { type: DataTypes.STRING },
|
primaryKey: true
|
||||||
lastname: { type: DataTypes.STRING },
|
},
|
||||||
bio: { type: DataTypes.TEXT, allowNull: true },
|
email: {
|
||||||
image: { type: DataTypes.TEXT, allowNull: true },
|
type: DataTypes.STRING(255),
|
||||||
role: { type: DataTypes.STRING },
|
allowNull: false,
|
||||||
salt: { type: DataTypes.STRING, allowNull: false },
|
unique: "email"
|
||||||
hash: { type: DataTypes.TEXT, allowNull: false }
|
},
|
||||||
|
username: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: false,
|
||||||
|
unique: "username"
|
||||||
|
},
|
||||||
|
firstname: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
lastname: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
bio: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
image: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
role: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: true
|
||||||
|
},
|
||||||
|
salt: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
hash: {
|
||||||
|
type: DataTypes.TEXT,
|
||||||
|
allowNull: false
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sequelize,
|
sequelize,
|
||||||
modelName: 'User',
|
|
||||||
tableName: 'user',
|
tableName: 'user',
|
||||||
timestamps: true,
|
timestamps: true,
|
||||||
createdAt: true,
|
indexes: [
|
||||||
updatedAt: true
|
{
|
||||||
|
name: "PRIMARY",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "id" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "email",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "email" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "username",
|
||||||
|
unique: true,
|
||||||
|
using: "BTREE",
|
||||||
|
fields: [
|
||||||
|
{ name: "username" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/*
|
|
||||||
Model.prototype.matchPassword = async function (enteredPassword) {
|
|
||||||
return await bcrypt.compare(enteredPassword, this.password);
|
|
||||||
};
|
|
||||||
|
|
||||||
const DEFAULT_SALT_ROUNDS = 10;
|
|
||||||
|
|
||||||
User.addHook("beforeCreate", async (user) => {
|
|
||||||
const encryptedPassword = await bcrypt.hash(
|
|
||||||
user.password,
|
|
||||||
DEFAULT_SALT_ROUNDS
|
|
||||||
);
|
|
||||||
user.password = encryptedPassword;
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
|
|
||||||
User.prototype.validPassword = function (password) {
|
User.prototype.validPassword = function (password) {
|
||||||
let hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
let hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
||||||
return this.hash === hash;
|
return this.hash === hash;
|
||||||
@@ -64,7 +103,7 @@ const UserModel = () => {
|
|||||||
var exp = new Date(today.getTime() + (session_lifetime * 1000));
|
var exp = new Date(today.getTime() + (session_lifetime * 1000));
|
||||||
|
|
||||||
return jwt.sign({
|
return jwt.sign({
|
||||||
id: this._id,
|
id: this.id,
|
||||||
username: this.username,
|
username: this.username,
|
||||||
exp: parseInt(exp.getTime() / 1000)
|
exp: parseInt(exp.getTime() / 1000)
|
||||||
}, secret, { algorithm: 'HS256' });
|
}, secret, { algorithm: 'HS256' });
|
||||||
@@ -72,7 +111,7 @@ const UserModel = () => {
|
|||||||
|
|
||||||
User.prototype.toAuthJSON = function () {
|
User.prototype.toAuthJSON = function () {
|
||||||
return {
|
return {
|
||||||
id: this._id,
|
id: this.id,
|
||||||
email: this.email,
|
email: this.email,
|
||||||
role: this.role,
|
role: this.role,
|
||||||
token: this.generateJWT(),
|
token: this.generateJWT(),
|
||||||
@@ -89,7 +128,7 @@ const UserModel = () => {
|
|||||||
|
|
||||||
User.prototype.toJSONFor = function () {
|
User.prototype.toJSONFor = function () {
|
||||||
return {
|
return {
|
||||||
id: this._id,
|
id: this.id,
|
||||||
email: this.email,
|
email: this.email,
|
||||||
username: this.username,
|
username: this.username,
|
||||||
firstname: this.firstname,
|
firstname: this.firstname,
|
||||||
@@ -111,7 +150,7 @@ const UserModel = () => {
|
|||||||
username: this.username,
|
username: this.username,
|
||||||
image: this.image || '/assets/images/users/user.jpg',
|
image: this.image || '/assets/images/users/user.jpg',
|
||||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg',
|
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg',
|
||||||
following: user ? user.isFollowing(this._id) : false
|
following: user ? user.isFollowing(this.id) : false
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
var DataTypes = require("sequelize").DataTypes;
|
||||||
|
var _Article = require("./Article");
|
||||||
|
var _ArticleTag = require("./ArticleTag");
|
||||||
|
var _Comment = require("./Comment");
|
||||||
|
var _Favorite = require("./Favorite");
|
||||||
|
var _Follower = require("./Follower");
|
||||||
|
var _Tag = require("./Tag");
|
||||||
|
var _TagList = require("./TagList");
|
||||||
|
var _User = require("./User");
|
||||||
|
|
||||||
|
function initModels(sequelize) {
|
||||||
|
var Article = _Article(sequelize, DataTypes);
|
||||||
|
var ArticleTag = _ArticleTag(sequelize, DataTypes);
|
||||||
|
var Comment = _Comment(sequelize, DataTypes);
|
||||||
|
var Favorite = _Favorite(sequelize, DataTypes);
|
||||||
|
var Follower = _Follower(sequelize, DataTypes);
|
||||||
|
var Tag = _Tag(sequelize, DataTypes);
|
||||||
|
var TagList = _TagList(sequelize, DataTypes);
|
||||||
|
var User = _User(sequelize, DataTypes);
|
||||||
|
|
||||||
|
Article.belongsToMany(Tag, { as: 'tagNameTags', through: ArticleTag, foreignKey: "articleId", otherKey: "tagName" });
|
||||||
|
Article.belongsToMany(Tag, { as: 'tagNameTagTagLists', through: TagList, foreignKey: "articleId", otherKey: "tagName" });
|
||||||
|
Article.belongsToMany(User, { as: 'userIdUsers', through: Favorite, foreignKey: "articleId", otherKey: "userId" });
|
||||||
|
Tag.belongsToMany(Article, { as: 'articleIdArticles', through: ArticleTag, foreignKey: "tagName", otherKey: "articleId" });
|
||||||
|
Tag.belongsToMany(Article, { as: 'articleIdArticleTagLists', through: TagList, foreignKey: "tagName", otherKey: "articleId" });
|
||||||
|
User.belongsToMany(Article, { as: 'articleIdArticleFavorites', through: Favorite, foreignKey: "userId", otherKey: "articleId" });
|
||||||
|
User.belongsToMany(User, { as: 'followerIdUsers', through: Follower, foreignKey: "userId", otherKey: "followerId" });
|
||||||
|
User.belongsToMany(User, { as: 'userIdUserFollowers', through: Follower, foreignKey: "followerId", otherKey: "userId" });
|
||||||
|
ArticleTag.belongsTo(Article, { as: "article", foreignKey: "articleId"});
|
||||||
|
Article.hasMany(ArticleTag, { as: "articleTags", foreignKey: "articleId"});
|
||||||
|
Comment.belongsTo(Article, { as: "article", foreignKey: "articleId"});
|
||||||
|
Article.hasMany(Comment, { as: "comments", foreignKey: "articleId"});
|
||||||
|
Favorite.belongsTo(Article, { as: "article", foreignKey: "articleId"});
|
||||||
|
Article.hasMany(Favorite, { as: "favorites", foreignKey: "articleId"});
|
||||||
|
TagList.belongsTo(Article, { as: "article", foreignKey: "articleId"});
|
||||||
|
Article.hasMany(TagList, { as: "tagLists", foreignKey: "articleId"});
|
||||||
|
ArticleTag.belongsTo(Tag, { as: "tagNameTag", foreignKey: "tagName"});
|
||||||
|
Tag.hasMany(ArticleTag, { as: "articleTags", foreignKey: "tagName"});
|
||||||
|
TagList.belongsTo(Tag, { as: "tagNameTag", foreignKey: "tagName"});
|
||||||
|
Tag.hasMany(TagList, { as: "tagLists", foreignKey: "tagName"});
|
||||||
|
Article.belongsTo(User, { as: "author", foreignKey: "authorId"});
|
||||||
|
User.hasMany(Article, { as: "articles", foreignKey: "authorId"});
|
||||||
|
Comment.belongsTo(User, { as: "author", foreignKey: "authorId"});
|
||||||
|
User.hasMany(Comment, { as: "comments", foreignKey: "authorId"});
|
||||||
|
Favorite.belongsTo(User, { as: "user", foreignKey: "userId"});
|
||||||
|
User.hasMany(Favorite, { as: "favorites", foreignKey: "userId"});
|
||||||
|
Follower.belongsTo(User, { as: "user", foreignKey: "userId"});
|
||||||
|
User.hasMany(Follower, { as: "followers", foreignKey: "userId"});
|
||||||
|
Follower.belongsTo(User, { as: "follower", foreignKey: "followerId"});
|
||||||
|
User.hasMany(Follower, { as: "followerFollowers", foreignKey: "followerId"});
|
||||||
|
|
||||||
|
return {
|
||||||
|
Article,
|
||||||
|
ArticleTag,
|
||||||
|
Comment,
|
||||||
|
Favorite,
|
||||||
|
Follower,
|
||||||
|
Tag,
|
||||||
|
TagList,
|
||||||
|
User,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
module.exports = initModels;
|
||||||
|
module.exports.initModels = initModels;
|
||||||
|
module.exports.default = initModels;
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
const { DataTypes, Model } = require("sequelize");
|
|
||||||
const sequelize = require("../config/sequelize");
|
|
||||||
|
|
||||||
class Tag extends Model { }
|
|
||||||
|
|
||||||
const TagModel = () => {
|
|
||||||
Tag.init(
|
|
||||||
{
|
|
||||||
name: { type: DataTypes.STRING, primaryKey: true, allowNull: false }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
sequelize,
|
|
||||||
modelName: 'Tag',
|
|
||||||
tableName: 'tag',
|
|
||||||
timestamps: true,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Tag.prototype.toJSONFor = function () {
|
|
||||||
return {
|
|
||||||
name: this.name
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
return Tag;
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = TagModel;
|
|
||||||
@@ -12,18 +12,36 @@ const associate = () => {
|
|||||||
as: 'author',
|
as: 'author',
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
DB.User.belongsToMany(DB.User, { as: "followers", through: "follower", foreignKey: "userId", timestamps: false });
|
DB.Article.belongsToMany(DB.Tag, { as: 'tagNameTags', through: DB.ArticleTag, foreignKey: "articleId", otherKey: "tagName" });
|
||||||
DB.User.belongsToMany(DB.User, { as: "following", through: "follower", foreignKey: "followerId", timestamps: false });
|
DB.Article.belongsToMany(DB.Tag, { as: 'tagNameTagTagLists', through: DB.TagList, foreignKey: "articleId", otherKey: "tagName" });
|
||||||
DB.User.hasMany(DB.Article, { foreignKey: "authorId", onDelete: "CASCADE" });
|
DB.Article.belongsToMany(DB.User, { as: 'userIdUsers', through: DB.Favorite, foreignKey: "articleId", otherKey: "userId" });
|
||||||
|
DB.Tag.belongsToMany(DB.Article, { as: 'articleIdArticles', through: DB.ArticleTag, foreignKey: "tagName", otherKey: "articleId" });
|
||||||
|
DB.Tag.belongsToMany(DB.Article, { as: 'articleIdArticleTagLists', through: DB.TagList, foreignKey: "tagName", otherKey: "articleId" });
|
||||||
|
DB.User.belongsToMany(DB.Article, { as: 'articleIdArticleFavorites', through: DB.Favorite, foreignKey: "userId", otherKey: "articleId" });
|
||||||
|
DB.User.belongsToMany(DB.User, { as: 'followerIdUsers', through: DB.Follower, foreignKey: "userId", otherKey: "followerId" });
|
||||||
|
DB.User.belongsToMany(DB.User, { as: 'userIdUserFollowers', through: DB.Follower, foreignKey: "followerId", otherKey: "userId" });
|
||||||
|
DB.ArticleTag.belongsTo(DB.Article, { as: "article", foreignKey: "articleId" });
|
||||||
|
DB.Article.hasMany(DB.ArticleTag, { as: "articleTags", foreignKey: "articleId" });
|
||||||
|
DB.Comment.belongsTo(DB.Article, { as: "article", foreignKey: "articleId" });
|
||||||
|
DB.Article.hasMany(DB.Comment, { as: "comments", foreignKey: "articleId" });
|
||||||
|
DB.Favorite.belongsTo(DB.Article, { as: "article", foreignKey: "articleId" });
|
||||||
|
DB.Article.hasMany(DB.Favorite, { as: "favorites", foreignKey: "articleId" });
|
||||||
|
DB.TagList.belongsTo(DB.Article, { as: "article", foreignKey: "articleId" });
|
||||||
|
DB.Article.hasMany(DB.TagList, { as: "tagLists", foreignKey: "articleId" });
|
||||||
|
DB.ArticleTag.belongsTo(DB.Tag, { as: "tagNameTag", foreignKey: "tagName" });
|
||||||
|
DB.Tag.hasMany(DB.ArticleTag, { as: "articleTags", foreignKey: "tagName" });
|
||||||
|
DB.TagList.belongsTo(DB.Tag, { as: "tagNameTag", foreignKey: "tagName" });
|
||||||
|
DB.Tag.hasMany(DB.TagList, { as: "tagLists", foreignKey: "tagName" });
|
||||||
DB.Article.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
|
DB.Article.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
|
||||||
DB.User.hasMany(DB.Comment, { foreignKey: "authorId", onDelete: "CASCADE" });
|
DB.User.hasMany(DB.Article, { as: "articles", foreignKey: "authorId" });
|
||||||
DB.Comment.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
|
DB.Comment.belongsTo(DB.User, { as: "author", foreignKey: "authorId" });
|
||||||
DB.Article.hasMany(DB.Comment, { foreignKey: "articleId", onDelete: "CASCADE" });
|
DB.User.hasMany(DB.Comment, { as: "comments", foreignKey: "authorId" });
|
||||||
DB.Comment.belongsTo(DB.Article, { foreignKey: "articleId" });
|
DB.Favorite.belongsTo(DB.User, { as: "user", foreignKey: "userId" });
|
||||||
DB.User.belongsToMany(DB.Article, { as: "favorites", through: "favorite", timestamps: false });
|
DB.User.hasMany(DB.Favorite, { as: "favorites", foreignKey: "userId" });
|
||||||
DB.Article.belongsToMany(DB.User, { through: "favorite", foreignKey: "articleId", timestamps: false });
|
DB.Follower.belongsTo(DB.User, { as: "user", foreignKey: "userId" });
|
||||||
DB.Article.belongsToMany(DB.Tag, { through: "tagList", as: "tagLists", foreignKey: "articleId", timestamps: false, onDelete: "CASCADE" });
|
DB.User.hasMany(DB.Follower, { as: "followers", foreignKey: "userId" });
|
||||||
DB.Tag.belongsToMany(DB.Article, { through: "articleTag", uniqueKey: false, timestamps: false });
|
DB.Follower.belongsTo(DB.User, { as: "follower", foreignKey: "followerId" });
|
||||||
|
DB.User.hasMany(DB.Follower, { as: "followerFollowers", foreignKey: "followerId" });
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = associate;
|
module.exports = associate;
|
||||||
|
|||||||
Reference in New Issue
Block a user