1. 程式人生 > >Node.js ORM框架Sequlize之表間關係

Node.js ORM框架Sequlize之表間關係

  1 /**
  2  * 大家就按照我的步驟來,一點一點,要有耐心哦
  3  * 我相信,最後肯定有你想要的!加油
  4  */
  5 //引入框架
  6 const Sequelize = require('sequelize');
  7 //建立ORM例項
  8 const sequelize = new Sequelize('sequlizedb', 'root', 'guoguo',
  9     {
 10         'dialect': 'mysql',  // 資料庫使用mysql
 11     }
 12 );
 13 //驗證連線
 14 sequelize
 15     .authenticate()
16 .then(() => { 17 console.log('連結成功'); 18 }) 19 .catch((error) => { 20 console.log('連結失敗' + error); 21 }) 22 //模型的建立 23 24 const User = sequelize.define('user', { 25 name: Sequelize.STRING, 26 age: Sequelize.INTEGER, 27 }, { 28 freezeTableName: true
, 29 }); 30 31 // User.create({ 32 // name: 'guo', 33 // age: 25 34 // }) 35 // .then((result) => { 36 // console.log('=======新增成功==================='); 37 // console.log(result); 38 // console.log('=========================='); 39 40 // }) 41 // .catch((error) => {
42 // console.log('=========================='); 43 // console.log('新增失敗' + error); 44 // console.log('=========================='); 45 46 // }); 47 48 // const Role=sequelize.define('role',{ 49 // name:{ 50 // type:sequelize.STRING, 51 // } 52 // }, 53 // {freezeTableName:true}); 54 55 56 const Message = sequelize.define('message', { 57 text: Sequelize.STRING, 58 }, { 59 freezeTableName: true, 60 }); 61 62 const Image = sequelize.define('image', { 63 url: Sequelize.STRING, 64 }, { 65 freezeTableName: true, 66 }); 67 //刪除表 68 // sequelize.drop() 69 // .then((logging)=>{ 70 // console.log('=========================='); 71 // console.log('刪除成功!'+logging); 72 // console.log('=========================='); 73 74 // }) 75 // .catch((error)=>{ 76 // console.log('=========================='); 77 // console.log('刪除失敗'+error); 78 // console.log('=========================='); 79 80 // }); 81 82 //建立關係 83 // Message.belongsTo(User); 84 // Message.hasMany(Image); 85 //同步到資料庫 86 // sequelize.sync({ 87 // force: true, 88 // }).then(() => { 89 // console.log('=========================='); 90 // console.log('同步成功'); 91 // console.log('=========================='); 92 93 // }).catch(() => { 94 // console.log('=========================='); 95 // console.log('同步失敗'); 96 // console.log('=========================='); 97 98 // }); 99 100 //cudr 101 function addUers(name, age) { 102 User.create({ 103 name: name, 104 age: age, 105 }).then((log) => { 106 log = JSON.stringify(log); 107 console.log('=========================='); 108 console.log('增加使用者成功' + log); 109 console.log('=========================='); 110 111 }).catch((error) => { 112 console.log('=========================='); 113 console.log('增加使用者失敗' + error); 114 console.log('=========================='); 115 116 }); 117 118 } 119 function addMessage(userId, text) { 120 Message.create({ 121 text: text, 122 userId: userId, 123 }).then((log) => { 124 log = JSON.stringify(log); 125 console.log('=========================='); 126 console.log('增加成功!' + log); 127 console.log('=========================='); 128 129 }).catch((error) => { 130 console.log('=========================='); 131 console.log('增加失敗!' + error); 132 console.log('=========================='); 133 134 }); 135 } 136 function addImage(messageId, imageUrl) { 137 Image.create({ 138 url: imageUrl, 139 messageId: messageId, 140 }).then((log) => { 141 log = JSON.stringify(log); 142 console.log('=========================='); 143 console.log('新增圖片成功' + log); 144 console.log('=========================='); 145 146 }).catch((error) => { 147 console.log('=========================='); 148 console.log('新增圖片失敗' + error); 149 console.log('=========================='); 150 151 }); 152 } 153 //測試 154 //addUers('楊雪嬌',22); 155 //addMessage(2, '楊雪嬌發來的訊息3'); 156 157 // addImage(5,'http://3.png'); 158 // addImage(6,'http://4.png'); 159 // addImage(2,'http://2.png'); 160 // // 161 function getAllMessage() { 162 Message.findAll({ 163 where: { 164 userId: 2 165 }, 166 include: [ 167 { 168 model: User, 169 attributes: [ 170 'id', 171 'name', 172 ], 173 }, 174 { 175 model: Image, 176 attributes: [ 177 'id', 178 'url' 179 ] 180 } 181 ], 182 }).then((result) => { 183 result = JSON.stringify(result); 184 console.log('=========================='); 185 console.log(result); 186 console.log('=========================='); 187 188 189 }).catch((error) => { 190 console.log('=========================='); 191 console.log('查詢失敗' + error); 192 console.log('=========================='); 193 194 }); 195 } 196 //測試 197 //getAllMessage(); 198 //刪除訊息 199 function delMessage(userId, messageId) { 200 Message.destroy({ 201 where: { 202 userId: userId, 203 id: messageId, 204 }, 205 206 }).then((log) => { 207 log = JSON.stringify(log); 208 console.log('=========================='); 209 console.log('刪除訊息成功!' + log); 210 console.log('=========================='); 211 212 }).catch((error) => { 213 console.log('=========================='); 214 console.log('刪除訊息失敗!' + error); 215 console.log('=========================='); 216 217 }); 218 } 219 //測試 220 //測試發現問題 如果不設定級聯 則,從屬message表的image表記錄不會刪除,而只是出現對應messageId 為NULL的現象 221 //delMessage(2,4); 222 223 const Role = sequelize.define('role', { 224 name: { 225 type: Sequelize.STRING, allowNull: true, 226 } 227 }, { 228 freezeTableName: true, 229 }); 230 231 232 //對於單個模型的同步 233 // Role.sync().then((log) => { 234 // log = JSON.stringify(log); 235 // console.log('=========================='); 236 // console.log('Role表資料同步成功' + log); 237 // console.log('=========================='); 238 // Role.create({ 239 // name: '管理員' 240 // }).then((log) => { 241 // log = JSON.stringify(log); 242 // console.log('=========================='); 243 // console.log('新增的資料為' + log); 244 // console.log('=========================='); 245 246 // }).catch((error) => { 247 // console.log('=========================='); 248 // console.log('新增資料失敗' + error); 249 // console.log('=========================='); 250 251 // }); 252 253 // }).catch((error) => { 254 // console.log('=========================='); 255 // console.log('Role模型與表資料同步失敗' + error); 256 // console.log('=========================='); 257 258 // }); 259 260 //定義User1模型 261 const User1 = sequelize.define('user1', { 262 name: { 263 type: Sequelize.STRING, 264 validate: { 265 notEmpty: true, 266 len: [2, 30], 267 } 268 }, 269 age: { 270 type: Sequelize.STRING, 271 defaultValue: 21, 272 validate: { 273 isInt: { 274 msg: '年齡必須是整數!', 275 } 276 } 277 278 }, 279 email: { 280 type: Sequelize.STRING, 281 validate: { 282 isEmail: true, 283 } 284 }, 285 userpicture: Sequelize.STRING, 286 }, { 287 freezeTableName: true, 288 }); 289 // 290 //同步User1模型 291 // User1.sync().then((log) => { 292 // log = JSON.stringify(log); 293 // console.log('=========================='); 294 // console.log('User1表資料同步成功' + log); 295 // console.log('=========================='); 296 // }).catch((error) => { 297 // console.log('=========================='); 298 // console.log('User1模型與表資料同步失敗' + error); 299 // console.log('=========================='); 300 // }); 301 302 function addUser1(userInfo) { 303 User1.create({ 304 name: userInfo.name, 305 age:userInfo.age, 306 email:userInfo.email, 307 }).then((log) => { 308 log = JSON.stringify(log); 309 console.log('=========================='); 310 console.log('新增的資料為' + log); 311 console.log('=========================='); 312 313 }).catch((error) => { 314 console.log('=========================='); 315 console.log('新增資料失敗' + error); 316 console.log('=========================='); 317 318 }); 319 } 320 const userInfo={ 321 name:'郭東生', 322 //age:0.1,//Validation error: 年齡必須是整數! 323 age:22, 324 email:'[email protected]', 325 //email:'7758',//Validation error: Validation isEmail on email failed 326 } 327 addUser1(userInfo);

相關推薦

Node.js ORM框架Sequlize關係

1 /** 2 * 大家就按照我的步驟來,一點一點,要有耐心哦 3 * 我相信,最後肯定有你想要的!加油 4 */ 5 //引入框架 6 const Sequelize = require('sequelize'); 7 //建立ORM例項 8 const sequel

Node.js ORM框架Sequelize(一)搭建服務

服務 l數據庫 數據 需要 log alt mysq 執行 準備 需要準備的環境:node.js mysql 1、在本地創建一個文件夾 2、執行npm init 創建成一個Node包,執行成功後文件夾裏會有一個json文件 3、安裝Sequelize 4、在

前端 —— node.js摸爬打滾路(一)

turn lan name resp function oba ack val 括號 安裝: window下的安裝,node.js直接上官網下載:https://nodejs.org/en/ 選擇LTS,也就是版本號比較低的穩定版,下載下來後運行下載的文件進行安裝; 通

Node.js日誌框架選型比較:Bunyan

npm 上下文環境 函數 -a ise -m 方式 情況 產生 前一篇Node.js日誌框架選型比較:WinstonBunyanBunyan(by Trent Mick)是另外一個值得考慮的日誌框架,以稍微不同的方式處理結構化,機器可讀性被重點對待。其結果是,bunya

node.js學習筆記二版本問題

nodejs targe tle 下一個 .cn blank 網站 mage 功能 一、版本說明 進入node.js官網https://nodejs.org/en/download/ 點擊上面的【All download options】進入到所有下載列表的地址 下載地

10個最好的 Node.js MVC 框架

.org 使用外部 itl asc ner png 友好 right 另一個 Node.js 是最流行的 JavaScript 服務端平臺,它允許建立可擴展的 Web 應用程序。Node.js 包含不同類型的框架,如 MVC 框架、全棧框架、REST API 以及大量的服

ORM框架學習EF

pla cep serve hang linq cor lis remove adapter 首先推薦一篇很好的EF文章翻譯,可以系統的學習一遍。 《Entity Framework 6 Recipes》中文翻譯系列 EF使用體會 優點: 可以省去Ado.net復雜的管

Node.js的基本使用express

decimal round line object c ron port variable require sta 1.基本使用 var express = require(‘express‘) var app = express() app.get(‘/‘, func

Hibernate關系

關系數據庫 creat arc shc vat arr 單向 ring array ManyToOne 多對一,是最常見的表間關系,對應關系數據庫中的外鍵關系。通常用於建立子實體和其父實體的關聯關系 @Entity(name = "Person") pu

Node.js Express 框架學習

轉載:http://javascript.ruanyifeng.com/nodejs/express.html#toc0 感覺很牛的樣子,不過覺得對初學者沒太大用,裡面很多例子用的api都沒有詳細的說明。為了學習備份,所以拷貝過來。 Express框架 來自《JavaScrip

Node.js Express 框架

Node.js Express 框架 Express 是一個為Node.js設計的web開發框架,它基於nodejs平臺。 。。。 Express 簡介 Express是一個簡潔而靈活的node.js Web應用框架, 提供了一系列強大特性幫助你建立各種Web應用,和豐富的HTTP工具。 使用Exp

node.js Web 框架Koa2

koa是Express的下一代基於Node.js的web框架,目前有1.x和2.0兩個版本。 歷史 1. Express Express是第一代最流行的web框架,它對Node.js的http進行了封裝,用起來如下: var express = require('expres

node.js與mysql學習

準備工作 1.利用npm進行mysql安裝 2.視覺化工具navcat mysql 正文開始 新建一個js檔案 var us=require("./data-user.js");//資料庫的一個表 var mysql= require('mysql'); //連線

node.js專案框架搭建

一、使用Node搭建伺服器專案 1.//全域性安裝 express sudo npm install -g express-generator 2.//使用ejs建立工程 express -e NodeTest 3.//進入工程 cd NodeTest 4.//下載專案所需

Node.js Express框架

Express 簡介 Express 是一個簡潔而靈活的 node.js Web應用框架, 提供了一系列強大特性幫助你建立各種 Web 應用,和豐富的 HTTP 工具。 使用 Express 可以快速地搭建一個完整功能的網站。 Express 框架核心特性: 可以設定中介軟體

Node.js實戰(十一)Buffer

JavaScript 語言自身只有字串資料型別,沒有二進位制資料型別。 但在處理像TCP流或檔案流時,必須使用到二進位制資料。因此在 Node.js中,定義了一個 Buffer 類,該類用來建立一個專門存放二進位制資料的快取區。 在 Node.js 中,Buffer 類是隨 Node 核心一起釋出的核心庫

Node.js實戰(十二)Stream

Stream 是一個抽象介面,Node 中有很多物件實現了這個介面。例如,對http 伺服器發起請求的request 物件就是一個 Stream,還有stdout(標準輸出)。 Node.js,Stream 有四種流型別: Readable - 可讀操作。 Writable&nb

Mongodb 的ORM框架 Morphia Updating

interface Datastore { ... /** updates all entities found with the operations*/ <T> UpdateResults<T> update(Query<T

Mongodb 的ORM框架 Morphia 使用Morphia(對映物件)

    Mapping Objects(對映物件)           我們一旦註解了我們的物件,將有很大繁重的工作要做。現在我們要做的就是需要建立一個Morphia例項,告訴Morphia我們要對映那些類,然後我們就可以     開始在Mongo文件和Java物件之間映射了

Orm框架開發NewExpression合併問題

之前都是看別人寫部落格,自己沒有寫部落格的習慣.在工作的過程中,總是會碰到許多的技術問題.有很多時候想記錄下來,後面一直有許多的問題等著解決.總想著等系統完成了,再回頭總結下.往往結果就把這事拋到腦後了. 總覺得不能一直這樣哈.今天簡單記一下吧.有表達不清楚的地方,多多包涵. 最近在研究.net orm框