-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalbum.js
49 lines (41 loc) · 916 Bytes
/
album.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict'
const Sequelize = require('sequelize')
class Album extends Sequelize.Model {
static init (sequelize, DataTypes) {
return super.init({
name: DataTypes.STRING,
description: DataTypes.STRING,
public: DataTypes.BOOLEAN,
parentId: DataTypes.INTEGER,
previewId: DataTypes.INTEGER
}, {
sequelize,
tableName: 'albums'
})
}
static associate (models) {
this.belongsToMany(models.Media, {
through: 'media_to_albums',
as: 'media'
})
this.belongsTo(models.Media, {
as: 'preview',
foreignKey: 'previewId'
})
this.hasMany(models.Album, {
as: 'children',
foreignKey: 'parentId'
})
this.belongsTo(models.Album, {
as: 'parent',
foreignKey: 'parentId'
})
}
toJSON () {
return {
...this.get(),
url: `/albums/${this.id}`
}
}
}
module.exports = Album