互動朋友圈模型設計 根據該模型生出資料庫後的表結構如下: 如有問題,請留言! ...
互動朋友圈模型設計
1 //引入框架 2 const Sequelize = require('sequelize'); 3 //創建ORM實例 4 const sequelize = new Sequelize('friendsdb', 'root', 'guoguo', 5 { 6 'dialect': 'mysql', // 資料庫使用mysql 7 } 8 ); 9 //驗證時候連接 10 sequelize.authenticate().then(()=>{ 11 console.log('=========================='); 12 console.log('連接資料庫成功!'); 13 console.log('=========================='); 14 15 }).catch((error)=>{ 16 console.log('=========================='); 17 console.log('連接資料庫失敗!'+error); 18 console.log('=========================='); 19 20 }); 21 //創建賬戶 22 const Account = sequelize.define('account', { 23 account: { 24 type: Sequelize.STRING, 25 validate: { 26 notEmpty: true, 27 isEmail: true, 28 } 29 }, 30 pwd: { 31 type: Sequelize.STRING, 32 validate: { 33 notEmpty: true, 34 len: [6, 18], 35 }, 36 } 37 }, { 38 freezeTableName: true, 39 paranoid: true, 40 }) 41 42 //創建用戶 43 const User = sequelize.define('user', { 44 picUrl: { 45 type: Sequelize.STRING, 46 validate: { 47 notEmpty: true, 48 } 49 } 50 }, { 51 freezeTableName: true, 52 paranoid: true, 53 }); 54 //創建消息 55 const Message = sequelize.define('message', { 56 content: { 57 type: Sequelize.STRING(500), 58 validate: { 59 notEmpty: true, 60 } 61 } 62 }, { 63 freezeTableName: true, 64 paranoid: true, 65 }); 66 //創建圖像 67 const Image = sequelize.define('image', { 68 picUrl: Sequelize.STRING, 69 }, { 70 freezeTableName: true, 71 paranoid: true, 72 }); 73 //創建評論 74 const Comment = sequelize.define('comment', { 75 content: { 76 type: Sequelize.STRING(500), 77 validate: { 78 notEmpty: true, 79 } 80 } 81 }, { 82 freezeTableName: true, 83 paranoid: true, 84 }); 85 86 87 //設置中間表() 88 const Friendship=sequelize.define('friendship',{ 89 id:{ 90 type:Sequelize.INTEGER, 91 autoincrement:true, 92 primaryKey:true, 93 }, 94 95 },{ 96 freezeTableName:true, 97 }); 98 99 //設置關係的時候上級聯 100 //Account.belongsTo(User,{onDelete:'CASCADE',onUpdate:'CASCADE'}); 101 User.belongsTo(Account,{onDelete:'CASCADE',onUpdate:'CASCADE'}); 102 //用戶和消息的關係 103 User.hasMany(Message,{onDelete:'CASCADE',onUpdate:'CASCADE'}); 104 Message.belongsTo(User,{onDelete:'CASCADE',onUpdate:'CASCADE'}); 105 //用戶和評論的關係 106 User.hasMany(Comment,{onDelete:'CASCADE',onUpdate:'CASCADE'}); 107 Comment.belongsTo(User,{onDelete:'CASCADE',onUpdate:'CASCADE'}); 108 //消息和圖片的關係 109 Message.hasMany(Image,{onDelete:'CASCADE',onUpdate:'CASCADE'}); 110 //消息和評論的關係 111 Message.hasMany(Comment,{onDelete:'CASCADE',onUpdate:'CASCADE'}); 112 //用戶表自身關聯 (關註與被關註) 113 User.belongsToMany(User,{as:'followed',through:Friendship,foreignKey:'followerId',onDelete:'CASCADE',onUpdate:'CASCADE'}); 114 User.belongsToMany(User,{as:'follower',through:Friendship,foreignKey:'followedId',onDelete:'CASCADE',onUpdate:'CASCADE'}); 115 116 /** 117 * //刪除資料庫 118 sequelize.drop().then(()=>{ 119 console.log('=========================='); 120 console.log('刪除成功!'); 121 console.log('=========================='); 122 123 }); 124 * 125 */ 126 127 128 //同步模型與資料庫 129 sequelize.sync();
根據該模型生出資料庫後的表結構如下:
1 mysql> desc account; 2 +-----------+--------------+------+-----+---------+----------------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +-----------+--------------+------+-----+---------+----------------+ 5 | id | int(11) | NO | PRI | NULL | auto_increment | 6 | account | varchar(255) | YES | | NULL | | 7 | pwd | varchar(255) | YES | | NULL | | 8 | createdAt | datetime | NO | | NULL | | 9 | updatedAt | datetime | NO | | NULL | | 10 | deletedAt | datetime | YES | | NULL | | 11 +-----------+--------------+------+-----+---------+----------------+ 12 6 rows in set 13 14 mysql> desc user; 15 +-----------+--------------+------+-----+---------+----------------+ 16 | Field | Type | Null | Key | Default | Extra | 17 +-----------+--------------+------+-----+---------+----------------+ 18 | id | int(11) | NO | PRI | NULL | auto_increment | 19 | picUrl | varchar(255) | YES | | NULL | | 20 | createdAt | datetime | NO | | NULL | | 21 | updatedAt | datetime | NO | | NULL | | 22 | deletedAt | datetime | YES | | NULL | | 23 | accountId | int(11) | YES | MUL | NULL | | 24 +-----------+--------------+------+-----+---------+----------------+ 25 6 rows in set 26 27 mysql> desc message; 28 +-----------+--------------+------+-----+---------+----------------+ 29 | Field | Type | Null | Key | Default | Extra | 30 +-----------+--------------+------+-----+---------+----------------+ 31 | id | int(11) | NO | PRI | NULL | auto_increment | 32 | content | varchar(500) | YES | | NULL | | 33 | createdAt | datetime | NO | | NULL | | 34 | updatedAt | datetime | NO | | NULL | | 35 | deletedAt | datetime | YES | | NULL | | 36 | userId | int(11) | YES | MUL | NULL | | 37 +-----------+--------------+------+-----+---------+----------------+ 38 6 rows in set 39 40 mysql> desc comment; 41 +-----------+--------------+------+-----+---------+----------------+ 42 | Field | Type | Null | Key | Default | Extra | 43 +-----------+--------------+------+-----+---------+----------------+ 44 | id | int(11) | NO | PRI | NULL | auto_increment | 45 | content | varchar(500) | YES | | NULL | | 46 | createdAt | datetime | NO | | NULL | | 47 | updatedAt | datetime | NO | | NULL | | 48 | deletedAt | datetime | YES | | NULL | | 49 | userId | int(11) | YES | MUL | NULL | | 50 | messageId | int(11) | YES | MUL | NULL | | 51 +-----------+--------------+------+-----+---------+----------------+ 52 7 rows in set 53 54 mysql> desc image; 55 +-----------+--------------+------+-----+---------+----------------+ 56 | Field | Type | Null | Key | Default | Extra | 57 +-----------+--------------+------+-----+---------+----------------+ 58 | id | int(11) | NO | PRI | NULL | auto_increment | 59 | picUrl | varchar(255) | YES | | NULL | | 60 | createdAt | datetime | NO | | NULL | | 61 | updatedAt | datetime | NO | | NULL | | 62 | deletedAt | datetime | YES | | NULL | | 63 | messageId | int(11) | YES | MUL | NULL | | 64 +-----------+--------------+------+-----+---------+----------------+ 65 6 rows in set 66 67 mysql> desc friendship; 68 +------------+----------+------+-----+---------+-------+ 69 | Field | Type | Null | Key | Default | Extra | 70 +------------+----------+------+-----+---------+-------+ 71 | id | int(11) | NO | PRI | 0 | | 72 | createdAt | datetime | NO | | NULL | | 73 | updatedAt | datetime | NO | | NULL | | 74 | followerId | int(11) | YES | MUL | NULL | | 75 | followedId | int(11) | YES | MUL | NULL | | 76 +------------+----------+------+-----+---------+-------+ 77 5 rows in set
如有問題,請留言!