upgrade sequelize
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const announcement = sequelize.define('announcement', {
|
||||
title: DataTypes.STRING,
|
||||
announcement: DataTypes.STRING,
|
||||
visible: DataTypes.BOOLEAN
|
||||
}, {})
|
||||
const sequelize = require('./index')
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
|
||||
return announcement
|
||||
}
|
||||
class Announcement extends Model {}
|
||||
|
||||
Announcement.init({
|
||||
title: DataTypes.STRING,
|
||||
announcement: DataTypes.STRING,
|
||||
visible: DataTypes.BOOLEAN
|
||||
}, { sequelize, modelName: 'announcement' })
|
||||
|
||||
module.exports = Announcement
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const APUser = sequelize.define('ap_user', {
|
||||
ap_id: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true
|
||||
},
|
||||
follower: DataTypes.BOOLEAN,
|
||||
blocked: DataTypes.BOOLEAN,
|
||||
object: DataTypes.JSON
|
||||
})
|
||||
const sequelize = require('./index')
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
|
||||
APUser.associate = function (models) {
|
||||
APUser.belongsTo(models.instance)
|
||||
APUser.hasMany(models.resource)
|
||||
}
|
||||
class APUser extends Model {}
|
||||
|
||||
return APUser
|
||||
}
|
||||
APUser.init({
|
||||
ap_id: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true
|
||||
},
|
||||
follower: DataTypes.BOOLEAN,
|
||||
blocked: DataTypes.BOOLEAN,
|
||||
object: DataTypes.JSON
|
||||
}, { sequelize, modelName: 'ap_user' })
|
||||
|
||||
module.exports = APUser
|
||||
|
||||
@@ -2,92 +2,105 @@ const config = require('config')
|
||||
const moment = require('moment-timezone')
|
||||
const htmlToText = require('html-to-text')
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Event = sequelize.define('event', {
|
||||
id: {
|
||||
allowNull: false,
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
title: DataTypes.STRING,
|
||||
slug: DataTypes.STRING,
|
||||
description: DataTypes.TEXT,
|
||||
multidate: DataTypes.BOOLEAN,
|
||||
start_datetime: {
|
||||
type: DataTypes.INTEGER,
|
||||
index: true
|
||||
},
|
||||
end_datetime: {
|
||||
type: DataTypes.INTEGER,
|
||||
index: true
|
||||
},
|
||||
image_path: DataTypes.STRING,
|
||||
is_visible: DataTypes.BOOLEAN,
|
||||
recurrent: DataTypes.JSON,
|
||||
likes: { type: DataTypes.JSON, defaultValue: [] },
|
||||
boost: { type: DataTypes.JSON, defaultValue: [] }
|
||||
}, {})
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
const sequelize = require('./index')
|
||||
|
||||
Event.associate = function (models) {
|
||||
Event.belongsTo(models.place)
|
||||
Event.belongsTo(models.user)
|
||||
Event.belongsToMany(models.tag, { through: 'event_tags' })
|
||||
Event.belongsToMany(models.notification, { through: 'event_notification' })
|
||||
Event.hasMany(models.resource)
|
||||
Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' })
|
||||
Event.belongsTo(models.event, { as: 'parent' })
|
||||
const Resource = require('./resource')
|
||||
const Notification = require('./notification')
|
||||
const Place = require('./place')
|
||||
const User = require('./user')
|
||||
const Tag = require('./tag')
|
||||
|
||||
class Event extends Model {}
|
||||
|
||||
Event.init({
|
||||
id: {
|
||||
allowNull: false,
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
title: DataTypes.STRING,
|
||||
slug: DataTypes.STRING,
|
||||
description: DataTypes.TEXT,
|
||||
multidate: DataTypes.BOOLEAN,
|
||||
start_datetime: {
|
||||
type: DataTypes.INTEGER,
|
||||
index: true
|
||||
},
|
||||
end_datetime: {
|
||||
type: DataTypes.INTEGER,
|
||||
index: true
|
||||
},
|
||||
image_path: DataTypes.STRING,
|
||||
is_visible: DataTypes.BOOLEAN,
|
||||
recurrent: DataTypes.JSON,
|
||||
likes: { type: DataTypes.JSON, defaultValue: [] },
|
||||
boost: { type: DataTypes.JSON, defaultValue: [] }
|
||||
}, { sequelize, modelName: 'event' })
|
||||
|
||||
Event.belongsTo(Place)
|
||||
Place.hasMany(Event)
|
||||
|
||||
Event.belongsTo(User)
|
||||
Event.belongsToMany(Tag, { through: 'event_tags' })
|
||||
|
||||
Event.belongsToMany(Notification, { through: 'event_notification' })
|
||||
Notification.belongsToMany(Event, { through: 'event_notification' })
|
||||
|
||||
Event.hasMany(Resource)
|
||||
Resource.belongsTo(Event)
|
||||
Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' })
|
||||
Event.belongsTo(Event, { as: 'parent' })
|
||||
|
||||
Event.prototype.toAP = function (username, locale, follower = []) {
|
||||
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_'))
|
||||
|
||||
const plainDescription = htmlToText.fromString(this.description.replace('\n', '').slice(0, 1000))
|
||||
const summary = `
|
||||
📍 ${this.place.name}
|
||||
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}
|
||||
|
||||
${plainDescription}
|
||||
|
||||
${tags.map(t => `#${t}`)}
|
||||
|
||||
`
|
||||
|
||||
const attachment = []
|
||||
if (this.image_path) {
|
||||
attachment.push({
|
||||
type: 'Document',
|
||||
mediaType: 'image/webp',
|
||||
url: `${config.baseurl}/media/${this.image_path}`,
|
||||
name: null,
|
||||
blurHash: null
|
||||
})
|
||||
}
|
||||
|
||||
Event.prototype.toAP = function (username, locale, follower = []) {
|
||||
const tags = this.tags && this.tags.map(t => t.tag.replace(/[ #]/g, '_'))
|
||||
|
||||
const plainDescription = htmlToText.fromString(this.description.replace('\n', '').slice(0, 1000))
|
||||
const summary = `
|
||||
📍 ${this.place.name}
|
||||
📅 ${moment.unix(this.start_datetime).locale(locale).format('dddd, D MMMM (HH:mm)')}
|
||||
|
||||
${plainDescription}
|
||||
|
||||
${tags.map(t => `#${t}`)}
|
||||
|
||||
`
|
||||
|
||||
const attachment = []
|
||||
if (this.image_path) {
|
||||
attachment.push({
|
||||
type: 'Document',
|
||||
mediaType: 'image/webp',
|
||||
url: `${config.baseurl}/media/${this.image_path}`,
|
||||
name: null,
|
||||
blurHash: null
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
id: `${config.baseurl}/federation/m/${this.id}`,
|
||||
name: this.title,
|
||||
url: `${config.baseurl}/event/${this.id}`,
|
||||
type: 'Event',
|
||||
startTime: moment.unix(this.start_datetime).locale(locale).format(),
|
||||
endTime: moment.unix(this.end_datetime).locale(locale).format(),
|
||||
location: {
|
||||
name: this.place.name
|
||||
},
|
||||
attachment,
|
||||
tag: tags.map(tag => ({
|
||||
type: 'Hashtag',
|
||||
name: '#' + tag,
|
||||
href: '/tags/' + tag
|
||||
})),
|
||||
published: this.createdAt,
|
||||
attributedTo: `${config.baseurl}/federation/u/${username}`,
|
||||
to: follower || [],
|
||||
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${username}/followers`],
|
||||
summary,
|
||||
sensitive: false
|
||||
}
|
||||
return {
|
||||
id: `${config.baseurl}/federation/m/${this.id}`,
|
||||
name: this.title,
|
||||
url: `${config.baseurl}/event/${this.id}`,
|
||||
type: 'Event',
|
||||
startTime: moment.unix(this.start_datetime).locale(locale).format(),
|
||||
endTime: moment.unix(this.end_datetime).locale(locale).format(),
|
||||
location: {
|
||||
name: this.place.name
|
||||
},
|
||||
attachment,
|
||||
tag: tags.map(tag => ({
|
||||
type: 'Hashtag',
|
||||
name: '#' + tag,
|
||||
href: '/tags/' + tag
|
||||
})),
|
||||
published: this.createdAt,
|
||||
attributedTo: `${config.baseurl}/federation/u/${username}`,
|
||||
to: follower || [],
|
||||
cc: ['https://www.w3.org/ns/activitystreams#Public', `${config.baseurl}/federation/u/${username}/followers`],
|
||||
summary,
|
||||
sensitive: false
|
||||
}
|
||||
|
||||
return Event
|
||||
}
|
||||
|
||||
module.exports = Event
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
'use strict'
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const event_notification = sequelize.define('event_notification', {
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
values: ['new', 'sent', 'error'],
|
||||
defaultValue: 'new',
|
||||
index: true
|
||||
}
|
||||
}, {})
|
||||
const sequelize = require('./index')
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
|
||||
return event_notification
|
||||
}
|
||||
class EventNotification extends Model {}
|
||||
|
||||
EventNotification.init({
|
||||
status: {
|
||||
type: DataTypes.ENUM,
|
||||
values: ['new', 'sent', 'error'],
|
||||
defaultValue: 'new',
|
||||
index: true
|
||||
}
|
||||
}, { sequelize, modelName: 'event_notification' })
|
||||
|
||||
module.exports = EventNotification
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
// const fs = require('fs')
|
||||
// const path = require('path')
|
||||
const Sequelize = require('sequelize')
|
||||
const basename = path.basename(__filename)
|
||||
// const basename = path.basename(__filename)
|
||||
const config = require('config')
|
||||
const consola = require('consola')
|
||||
const db = {}
|
||||
// const db = {}
|
||||
let sequelize = null
|
||||
|
||||
try {
|
||||
@@ -19,23 +19,23 @@ sequelize.authenticate().catch(e => {
|
||||
process.exit(-1)
|
||||
})
|
||||
|
||||
fs
|
||||
.readdirSync(__dirname)
|
||||
.filter(file => {
|
||||
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
|
||||
})
|
||||
.forEach(file => {
|
||||
const model = sequelize.import(path.join(__dirname, file))
|
||||
db[model.name] = model
|
||||
})
|
||||
// fs
|
||||
// .readdirSync(__dirname)
|
||||
// .filter(file => {
|
||||
// return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
|
||||
// })
|
||||
// .forEach(file => {
|
||||
// const model = sequelize.import(path.join(__dirname, file))
|
||||
// db[model.name] = model
|
||||
// })
|
||||
|
||||
Object.keys(db).forEach(modelName => {
|
||||
if (db[modelName].associate) {
|
||||
db[modelName].associate(db)
|
||||
}
|
||||
})
|
||||
// Object.keys(db).forEach(modelName => {
|
||||
// if (db[modelName].associate) {
|
||||
// db[modelName].associate(db)
|
||||
// }
|
||||
// })
|
||||
|
||||
db.sequelize = sequelize
|
||||
db.Sequelize = Sequelize
|
||||
// db.sequelize = sequelize
|
||||
// db.Sequelize = Sequelize
|
||||
|
||||
module.exports = db
|
||||
module.exports = sequelize
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
'use strict'
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Instance = sequelize.define('instance', {
|
||||
domain: {
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
name: DataTypes.STRING,
|
||||
blocked: DataTypes.BOOLEAN,
|
||||
data: DataTypes.JSON
|
||||
}, {})
|
||||
|
||||
Instance.associate = function (models) {
|
||||
Instance.hasMany(models.ap_user)
|
||||
}
|
||||
const sequelize = require('./index')
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
const APUser = require('./ap_user')
|
||||
|
||||
return Instance
|
||||
}
|
||||
class Instance extends Model {}
|
||||
|
||||
Instance.init({
|
||||
domain: {
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
name: DataTypes.STRING,
|
||||
blocked: DataTypes.BOOLEAN,
|
||||
data: DataTypes.JSON
|
||||
}, { sequelize, modelName: 'instance' })
|
||||
|
||||
Instance.hasMany(APUser)
|
||||
APUser.belongsTo(Instance)
|
||||
|
||||
module.exports = Instance
|
||||
|
||||
@@ -1,26 +1,32 @@
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Notification = sequelize.define('notification', {
|
||||
filters: DataTypes.JSON,
|
||||
email: DataTypes.STRING,
|
||||
remove_code: DataTypes.STRING,
|
||||
action: {
|
||||
type: DataTypes.ENUM,
|
||||
values: ['Create', 'Update', 'Delete']
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.ENUM,
|
||||
values: ['mail', 'admin_email', 'ap']
|
||||
}
|
||||
}, {
|
||||
indexes: [{
|
||||
unique: true,
|
||||
fields: ['action', 'type']
|
||||
}]
|
||||
})
|
||||
const sequelize = require('./index')
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
// const Event = require('./event')
|
||||
|
||||
Notification.associate = function (models) {
|
||||
Notification.belongsToMany(models.event, { through: 'event_notification' })
|
||||
class Notification extends Model {}
|
||||
|
||||
Notification.init({
|
||||
filters: DataTypes.JSON,
|
||||
email: DataTypes.STRING,
|
||||
remove_code: DataTypes.STRING,
|
||||
action: {
|
||||
type: DataTypes.ENUM,
|
||||
values: ['Create', 'Update', 'Delete']
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.ENUM,
|
||||
values: ['mail', 'admin_email', 'ap']
|
||||
}
|
||||
return Notification
|
||||
}
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
modelName: 'notification',
|
||||
indexes: [{
|
||||
unique: true,
|
||||
fields: ['action', 'type']
|
||||
}]
|
||||
})
|
||||
|
||||
// Notification.belongsToMany(Event, { through: 'event_notification' })
|
||||
|
||||
module.exports = Notification
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const OAuthClient = sequelize.define('oauth_client', {
|
||||
id: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true,
|
||||
allowNull: false
|
||||
},
|
||||
name: DataTypes.STRING,
|
||||
client_secret: DataTypes.STRING,
|
||||
scopes: DataTypes.STRING,
|
||||
redirectUris: DataTypes.STRING,
|
||||
website: DataTypes.STRING
|
||||
}, {})
|
||||
const sequelize = require('./index')
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
|
||||
return OAuthClient
|
||||
}
|
||||
class OAuthClient extends Model {}
|
||||
|
||||
OAuthClient.init({
|
||||
id: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true,
|
||||
allowNull: false
|
||||
},
|
||||
name: DataTypes.STRING,
|
||||
client_secret: DataTypes.STRING,
|
||||
scopes: DataTypes.STRING,
|
||||
redirectUris: DataTypes.STRING,
|
||||
website: DataTypes.STRING
|
||||
}, { sequelize, modelName: 'oauth_client' })
|
||||
|
||||
module.exports = OAuthClient
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const OAuthCode = sequelize.define('oauth_code', {
|
||||
authorizationCode: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true
|
||||
},
|
||||
expiresAt: DataTypes.DATE,
|
||||
scope: DataTypes.STRING,
|
||||
redirect_uri: DataTypes.STRING
|
||||
}, {})
|
||||
const sequelize = require('./index')
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
|
||||
OAuthCode.associate = function (models) {
|
||||
OAuthCode.belongsTo(models.user)
|
||||
OAuthCode.belongsTo(models.oauth_client, { as: 'client' })
|
||||
}
|
||||
const User = require('./user')
|
||||
const OAuthClient = require('./oauth_client')
|
||||
|
||||
return OAuthCode
|
||||
}
|
||||
class OAuthCode extends Model {}
|
||||
|
||||
OAuthCode.init({
|
||||
authorizationCode: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true
|
||||
},
|
||||
expiresAt: DataTypes.DATE,
|
||||
scope: DataTypes.STRING,
|
||||
redirect_uri: DataTypes.STRING
|
||||
}, { sequelize, modelName: 'oauth_code' })
|
||||
|
||||
OAuthCode.belongsTo(User)
|
||||
OAuthCode.belongsTo(OAuthClient, { as: 'client' })
|
||||
|
||||
module.exports = OAuthCode
|
||||
|
||||
@@ -1,31 +1,35 @@
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const OAuthToken = sequelize.define('oauth_token', {
|
||||
accessToken: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
primaryKey: true
|
||||
},
|
||||
accessTokenExpiresAt: {
|
||||
type: DataTypes.DATE,
|
||||
get () {
|
||||
return new Date(this.getDataValue('accesTokenExpiresAt'))
|
||||
}
|
||||
},
|
||||
refreshToken: DataTypes.STRING,
|
||||
refreshTokenExpiresAt: {
|
||||
type: DataTypes.DATE,
|
||||
get () {
|
||||
return new Date(this.getDataValue('accesTokenExpiresAt'))
|
||||
}
|
||||
},
|
||||
scope: DataTypes.STRING
|
||||
}, {})
|
||||
const sequelize = require('./index')
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
|
||||
OAuthToken.associate = function (models) {
|
||||
OAuthToken.belongsTo(models.user)
|
||||
OAuthToken.belongsTo(models.oauth_client, { as: 'client' })
|
||||
}
|
||||
const User = require('./user')
|
||||
const OAuthClient = require('./oauth_client')
|
||||
|
||||
return OAuthToken
|
||||
}
|
||||
class OAuthToken extends Model {}
|
||||
|
||||
OAuthToken.init({
|
||||
accessToken: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
primaryKey: true
|
||||
},
|
||||
accessTokenExpiresAt: {
|
||||
type: DataTypes.DATE,
|
||||
get () {
|
||||
return new Date(this.getDataValue('accesTokenExpiresAt'))
|
||||
}
|
||||
},
|
||||
refreshToken: DataTypes.STRING,
|
||||
refreshTokenExpiresAt: {
|
||||
type: DataTypes.DATE,
|
||||
get () {
|
||||
return new Date(this.getDataValue('accesTokenExpiresAt'))
|
||||
}
|
||||
},
|
||||
scope: DataTypes.STRING
|
||||
}, { sequelize, modelName: 'oauth_token' })
|
||||
|
||||
OAuthToken.belongsTo(User)
|
||||
OAuthToken.belongsTo(OAuthClient, { as: 'client' })
|
||||
|
||||
module.exports = OAuthToken
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
const sequelize = require('./index')
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
// const Event = require('./event')
|
||||
class Place extends Model {}
|
||||
|
||||
const Place = sequelize.define('place', {
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
unique: true,
|
||||
index: true,
|
||||
allowNull: false
|
||||
},
|
||||
address: DataTypes.STRING
|
||||
}, {})
|
||||
Place.init({
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
unique: true,
|
||||
index: true,
|
||||
allowNull: false
|
||||
},
|
||||
address: DataTypes.STRING
|
||||
}, { sequelize, modelName: 'place' })
|
||||
|
||||
Place.associate = function (models) {
|
||||
Place.hasMany(models.event)
|
||||
}
|
||||
// Place.hasMany(Event)
|
||||
|
||||
return Place
|
||||
}
|
||||
module.exports = Place
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Resource = sequelize.define('resource', {
|
||||
activitypub_id: {
|
||||
type: DataTypes.STRING,
|
||||
index: true,
|
||||
unique: true
|
||||
},
|
||||
hidden: DataTypes.BOOLEAN,
|
||||
data: DataTypes.JSON
|
||||
}, {})
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
const sequelize = require('./index')
|
||||
|
||||
Resource.associate = function (models) {
|
||||
// Resource.belongsTo(models.instance)
|
||||
Resource.belongsTo(models.event)
|
||||
Resource.belongsTo(models.ap_user)
|
||||
}
|
||||
// const Event = require('./event')
|
||||
const APUser = require('./ap_user')
|
||||
|
||||
return Resource
|
||||
}
|
||||
class Resource extends Model {}
|
||||
|
||||
Resource.init({
|
||||
activitypub_id: {
|
||||
type: DataTypes.STRING,
|
||||
index: true,
|
||||
unique: true
|
||||
},
|
||||
hidden: DataTypes.BOOLEAN,
|
||||
data: DataTypes.JSON
|
||||
}, { sequelize, modelName: 'resource' })
|
||||
|
||||
APUser.hasMany(Resource)
|
||||
Resource.belongsTo(APUser)
|
||||
|
||||
module.exports = Resource
|
||||
|
||||
@@ -13,3 +13,5 @@ Setting.init({
|
||||
value: DataTypes.JSON,
|
||||
is_secret: DataTypes.BOOLEAN
|
||||
}, { sequelize, modelName: 'setting' })
|
||||
|
||||
module.exports = Setting
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
// const Event = require('./event')
|
||||
const sequelize = require('./index')
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Tag = sequelize.define('tag', {
|
||||
tag: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
index: true,
|
||||
primaryKey: true
|
||||
},
|
||||
weigth: { type: DataTypes.INTEGER, defaultValue: 0, allowNull: false }
|
||||
}, {})
|
||||
class Tag extends Model {}
|
||||
|
||||
Tag.associate = function (models) {
|
||||
Tag.belongsToMany(models.event, { through: 'event_tags' })
|
||||
}
|
||||
Tag.init({
|
||||
tag: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
index: true,
|
||||
primaryKey: true
|
||||
},
|
||||
weigth: { type: DataTypes.INTEGER, defaultValue: 0, allowNull: false }
|
||||
}, { sequelize, modelName: 'tag' })
|
||||
|
||||
return Tag
|
||||
}
|
||||
// Tag.belongsToMany(Event, { through: 'event_tags' })
|
||||
|
||||
module.exports = Tag
|
||||
|
||||
@@ -1,50 +1,55 @@
|
||||
|
||||
const bcrypt = require('bcryptjs')
|
||||
const { Model, DataTypes } = require('sequelize')
|
||||
const sequelize = require('./index')
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const User = sequelize.define('user', {
|
||||
settings: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: []
|
||||
// const Event = require('./event')
|
||||
|
||||
class User extends Model {}
|
||||
|
||||
User.init({
|
||||
settings: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: []
|
||||
},
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
unique: { msg: 'error.email_taken' },
|
||||
validate: {
|
||||
notEmpty: true
|
||||
},
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
unique: { msg: 'error.email_taken' },
|
||||
validate: {
|
||||
notEmpty: true
|
||||
},
|
||||
index: true,
|
||||
allowNull: false
|
||||
},
|
||||
description: DataTypes.TEXT,
|
||||
password: DataTypes.STRING,
|
||||
recover_code: DataTypes.STRING,
|
||||
is_admin: DataTypes.BOOLEAN,
|
||||
is_active: DataTypes.BOOLEAN
|
||||
}, {
|
||||
scopes: {
|
||||
withoutPassword: {
|
||||
attributes: { exclude: ['password', 'recover_code'] }
|
||||
}
|
||||
index: true,
|
||||
allowNull: false
|
||||
},
|
||||
description: DataTypes.TEXT,
|
||||
password: DataTypes.STRING,
|
||||
recover_code: DataTypes.STRING,
|
||||
is_admin: DataTypes.BOOLEAN,
|
||||
is_active: DataTypes.BOOLEAN
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: 'user',
|
||||
scopes: {
|
||||
withoutPassword: {
|
||||
attributes: { exclude: ['password', 'recover_code'] }
|
||||
}
|
||||
})
|
||||
|
||||
User.associate = function (models) {
|
||||
User.hasMany(models.event)
|
||||
}
|
||||
})
|
||||
|
||||
User.prototype.comparePassword = async function (pwd) {
|
||||
if (!this.password) { return false }
|
||||
const ret = await bcrypt.compare(pwd, this.password)
|
||||
return ret
|
||||
}
|
||||
// User.hasMany(Event)
|
||||
|
||||
User.beforeSave(async (user, options) => {
|
||||
if (user.changed('password')) {
|
||||
const salt = await bcrypt.genSalt(10)
|
||||
const hash = await bcrypt.hash(user.password, salt)
|
||||
user.password = hash
|
||||
}
|
||||
})
|
||||
|
||||
return User
|
||||
User.prototype.comparePassword = async function (pwd) {
|
||||
if (!this.password) { return false }
|
||||
const ret = await bcrypt.compare(pwd, this.password)
|
||||
return ret
|
||||
}
|
||||
|
||||
User.beforeSave(async (user, options) => {
|
||||
if (user.changed('password')) {
|
||||
const salt = await bcrypt.genSalt(10)
|
||||
const hash = await bcrypt.hash(user.password, salt)
|
||||
user.password = hash
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = User
|
||||
|
||||
Reference in New Issue
Block a user