1+ const { DataTypes } = require ( 'sequelize' ) ;
2+
3+ module . exports = ( sequelize ) => {
4+ const SharedContent = sequelize . define ( 'SharedContent' , {
5+ id : {
6+ type : DataTypes . CHAR ( 36 ) ,
7+ defaultValue : DataTypes . UUIDV4 ,
8+ primaryKey : true
9+ } ,
10+ user_id : {
11+ type : DataTypes . CHAR ( 36 ) ,
12+ allowNull : false ,
13+ references : {
14+ model : 'users' ,
15+ key : 'id'
16+ }
17+ } ,
18+ content_type : {
19+ type : DataTypes . ENUM ( 'topic' , 'quiz' ) ,
20+ allowNull : false
21+ } ,
22+ content_url : {
23+ type : DataTypes . STRING ( 500 ) ,
24+ allowNull : false
25+ } ,
26+ title : {
27+ type : DataTypes . STRING ( 255 ) ,
28+ allowNull : false
29+ } ,
30+ description : {
31+ type : DataTypes . TEXT ,
32+ allowNull : true
33+ } ,
34+ tags : {
35+ type : DataTypes . JSON ,
36+ allowNull : true ,
37+ defaultValue : [ ]
38+ } ,
39+ likes_count : {
40+ type : DataTypes . INTEGER ,
41+ defaultValue : 0
42+ } ,
43+ comments_count : {
44+ type : DataTypes . INTEGER ,
45+ defaultValue : 0
46+ } ,
47+ created_at : {
48+ type : DataTypes . DATE ,
49+ allowNull : false ,
50+ defaultValue : DataTypes . NOW
51+ } ,
52+ updated_at : {
53+ type : DataTypes . DATE ,
54+ allowNull : false ,
55+ defaultValue : DataTypes . NOW
56+ }
57+ } , {
58+ tableName : 'shared_content' ,
59+ timestamps : true ,
60+ underscored : true ,
61+ createdAt : 'created_at' ,
62+ updatedAt : 'updated_at' ,
63+ indexes : [
64+ {
65+ fields : [ 'user_id' ]
66+ } ,
67+ {
68+ fields : [ 'content_type' ]
69+ } ,
70+ {
71+ fields : [ 'created_at' ]
72+ }
73+ ]
74+ } ) ;
75+
76+ SharedContent . associate = ( models ) => {
77+ SharedContent . belongsTo ( models . User , {
78+ foreignKey : 'user_id' ,
79+ as : 'author'
80+ } ) ;
81+
82+ SharedContent . hasMany ( models . SharedContentLike , {
83+ foreignKey : 'shared_content_id' ,
84+ as : 'likes'
85+ } ) ;
86+
87+ SharedContent . hasMany ( models . SharedContentComment , {
88+ foreignKey : 'shared_content_id' ,
89+ as : 'comments'
90+ } ) ;
91+ } ;
92+
93+ return SharedContent ;
94+ } ;
0 commit comments