最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Node.js ORM框架Sequlize之表間關(guān)系

 更新時(shí)間:2017年07月24日 08:24:12   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇淺談Node.js ORM框架Sequlize之表間關(guān)系。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Sequelize模型之間存在關(guān)聯(lián)關(guān)系,這些關(guān)系代表了數(shù)據(jù)庫(kù)中對(duì)應(yīng)表之間的主/外鍵關(guān)系?;谀P完P(guān)系可以實(shí)現(xiàn)關(guān)聯(lián)表之間的連接查詢、更新、刪除等操作。本文將通過(guò)一個(gè)示例,介紹模型的定義,創(chuàng)建模型關(guān)聯(lián)關(guān)系,模型與關(guān)聯(lián)關(guān)系同步數(shù)據(jù)庫(kù),及關(guān)系模型的增、刪、改、查操作。

數(shù)據(jù)庫(kù)中的表之間存在一定的關(guān)聯(lián)關(guān)系,表之間的關(guān)系基于主/外鍵進(jìn)行關(guān)聯(lián)、創(chuàng)建約束等。關(guān)系表中的數(shù)據(jù)分為1對(duì)1(1:1)、1對(duì)多(1:M)、多對(duì)多(N:M)三種關(guān)聯(lián)關(guān)系。

在Sequelize中建立關(guān)聯(lián)關(guān)系,通過(guò)調(diào)用模型(源模型)的belongsTo、hasOne、hasMany、belongsToMany方法,再將要建立關(guān)系的模型(目標(biāo)模型)做為參數(shù)傳入即可。這些方法會(huì)按以下規(guī)則創(chuàng)建關(guān)聯(lián)關(guān)系:

hasOne - 與目標(biāo)模型建立1:1關(guān)聯(lián)關(guān)系,關(guān)聯(lián)關(guān)系(外鍵)存在于目標(biāo)模型中。

belongsTo - 與目標(biāo)模型建立1:1關(guān)聯(lián)關(guān)系,關(guān)聯(lián)關(guān)系(外鍵)存在于源模型中。

hasMany - 與目標(biāo)模型建立1:N關(guān)聯(lián)關(guān)系,關(guān)聯(lián)關(guān)系(外鍵)存在于目標(biāo)模型中。

belongsToMany - 與目標(biāo)模型建立N:M關(guān)聯(lián)關(guān)系,會(huì)通過(guò)sourceId和targetId創(chuàng)建交叉表。

為了能夠清楚說(shuō)明模型關(guān)系的定義及關(guān)系模型的使用,我們定義如下4個(gè)模型對(duì)象:

用戶(User)-與其它模型存在1:1、1:N、N:M

用戶登錄信息(UserCheckin)-與User存在1:1關(guān)系

用戶地址(UserAddress)-與User存在N:1關(guān)系

角色(Role)-與User存在N:M關(guān)系

這幾個(gè)模型的E-R結(jié)構(gòu)如下:

接下來(lái)上代碼,代碼和瓷土不符,請(qǐng)注意!

代碼寫(xiě)的有點(diǎn)low,沒(méi)辦法,!

/**
 * 大家就按照我的步驟來(lái),一點(diǎn)一點(diǎn),要有耐心哦
 * 我相信,最后肯定有你想要的!加油
 */
//引入框架
const Sequelize = require('sequelize');
//創(chuàng)建ORM實(shí)例
const sequelize = new Sequelize('sequlizedb', 'root', 'guoguo',
 {
  'dialect': 'mysql', // 數(shù)據(jù)庫(kù)使用mysql
 }
);
//驗(yàn)證連接
sequelize
 .authenticate()
 .then(() => {
  console.log('鏈接成功');
 })
 .catch((error) => {
  console.log('鏈接失敗' + error);
 })
//模型的創(chuàng)建

const User = sequelize.define('user', {
 name: Sequelize.STRING,
 age: Sequelize.INTEGER,
}, {
  freezeTableName: true,
 });

// User.create({
//  name: 'guo',
//  age: 25
// })
//  .then((result) => {
//   console.log('=======添加成功===================');
//   console.log(result);
//   console.log('==========================');

//  })
//  .catch((error) => {
//   console.log('==========================');
//   console.log('添加失敗' + error);
//   console.log('==========================');

//  });

// const Role=sequelize.define('role',{
//  name:{
//   type:sequelize.STRING,
//  }
// },
// {freezeTableName:true});


const Message = sequelize.define('message', {
 text: Sequelize.STRING,
}, {
  freezeTableName: true,
 });

const Image = sequelize.define('image', {
 url: Sequelize.STRING,
}, {
  freezeTableName: true,
 });
//刪除表
// sequelize.drop()
// .then((logging)=>{
//  console.log('==========================');
//  console.log('刪除成功!'+logging);
//  console.log('==========================');

// })
// .catch((error)=>{
//  console.log('==========================');
//  console.log('刪除失敗'+error);
//  console.log('==========================');

// });

//建立關(guān)系
// Message.belongsTo(User);
// Message.hasMany(Image);
//同步到數(shù)據(jù)庫(kù)
// sequelize.sync({
//  force: true,
// }).then(() => {
//  console.log('==========================');
//  console.log('同步成功');
//  console.log('==========================');

// }).catch(() => {
//  console.log('==========================');
//  console.log('同步失敗');
//  console.log('==========================');

// });

//cudr
function addUers(name, age) {
 User.create({
  name: name,
  age: age,
 }).then((log) => {
  log = JSON.stringify(log);
  console.log('==========================');
  console.log('增加用戶成功' + log);
  console.log('==========================');

 }).catch((error) => {
  console.log('==========================');
  console.log('增加用戶失敗' + error);
  console.log('==========================');

 });

}
function addMessage(userId, text) {
 Message.create({
  text: text,
  userId: userId,
 }).then((log) => {
  log = JSON.stringify(log);
  console.log('==========================');
  console.log('增加成功!' + log);
  console.log('==========================');

 }).catch((error) => {
  console.log('==========================');
  console.log('增加失?。? + error);
  console.log('==========================');

 });
}
function addImage(messageId, imageUrl) {
 Image.create({
  url: imageUrl,
  messageId: messageId,
 }).then((log) => {
  log = JSON.stringify(log);
  console.log('==========================');
  console.log('添加圖片成功' + log);
  console.log('==========================');

 }).catch((error) => {
  console.log('==========================');
  console.log('添加圖片失敗' + error);
  console.log('==========================');

 });
}
//測(cè)試
//addUers('楊雪嬌',22);
//addMessage(2, '楊雪嬌發(fā)來(lái)的消息3');

// addImage(5,'http://3.png');
// addImage(6,'http://4.png');
// addImage(2,'http://2.png');
// //
function getAllMessage() {
 Message.findAll({
  where: {
   userId: 2
  },
  include: [
   {
    model: User,
    attributes: [
     'id',
     'name',
    ],
   },
   {
    model: Image,
    attributes: [
     'id',
     'url'
    ]
   }
  ],
 }).then((result) => {
  result = JSON.stringify(result);
  console.log('==========================');
  console.log(result);
  console.log('==========================');


 }).catch((error) => {
  console.log('==========================');
  console.log('查詢失敗' + error);
  console.log('==========================');

 });
}
//測(cè)試
//getAllMessage();
//刪除消息
function delMessage(userId, messageId) {
 Message.destroy({
  where: {
   userId: userId,
   id: messageId,
  },

 }).then((log) => {
  log = JSON.stringify(log);
  console.log('==========================');
  console.log('刪除消息成功!' + log);
  console.log('==========================');

 }).catch((error) => {
  console.log('==========================');
  console.log('刪除消息失敗!' + error);
  console.log('==========================');

 });
}
//測(cè)試
//測(cè)試發(fā)現(xiàn)問(wèn)題 如果不設(shè)置級(jí)聯(lián) 則,從屬message表的image表記錄不會(huì)刪除,而只是出現(xiàn)對(duì)應(yīng)messageId 為NULL的現(xiàn)象
//delMessage(2,4);

const Role = sequelize.define('role', {
 name: {
  type: Sequelize.STRING, allowNull: true,
 }
}, {
  freezeTableName: true,
 });


//對(duì)于單個(gè)模型的同步
// Role.sync().then((log) => {
//  log = JSON.stringify(log);
//  console.log('==========================');
//  console.log('Role表數(shù)據(jù)同步成功' + log);
//  console.log('==========================');
//  Role.create({
//   name: '管理員'
//  }).then((log) => {
//   log = JSON.stringify(log);
//   console.log('==========================');
//   console.log('添加的數(shù)據(jù)為' + log);
//   console.log('==========================');

//  }).catch((error) => {
//   console.log('==========================');
//   console.log('添加數(shù)據(jù)失敗' + error);
//   console.log('==========================');

//  });

// }).catch((error) => {
//  console.log('==========================');
//  console.log('Role模型與表數(shù)據(jù)同步失敗' + error);
//  console.log('==========================');

// });

//定義User1模型
const User1 = sequelize.define('user1', {
 name: {
  type: Sequelize.STRING,
  validate: {
   notEmpty: true,
   len: [2, 30],
  }
 },
 age: {
  type: Sequelize.STRING,
  defaultValue: 21,
  validate: {
   isInt: {
    msg: '年齡必須是整數(shù)!',
   }
  }

 },
 email: {
  type: Sequelize.STRING,
  validate: {
   isEmail: true,
  }
 },
 userpicture: Sequelize.STRING,
}, {
  freezeTableName: true,
 });
//
//同步User1模型
// User1.sync().then((log) => {
//  log = JSON.stringify(log);
//  console.log('==========================');
//  console.log('User1表數(shù)據(jù)同步成功' + log);
//  console.log('==========================');
// }).catch((error) => {
//  console.log('==========================');
//  console.log('User1模型與表數(shù)據(jù)同步失敗' + error);
//  console.log('==========================');
// });

function addUser1(userInfo) {
 User1.create({
  name: userInfo.name,
  age:userInfo.age,
  email:userInfo.email,
 }).then((log) => {
  log = JSON.stringify(log);
  console.log('==========================');
  console.log('添加的數(shù)據(jù)為' + log);
  console.log('==========================');

 }).catch((error) => {
  console.log('==========================');
  console.log('添加數(shù)據(jù)失敗' + error);
  console.log('==========================');

 });
}
const userInfo={
 name:'郭東生',
 //age:0.1,//Validation error: 年齡必須是整數(shù)!
 age:22,
 email:'7758@qq.com',
 //email:'7758',//Validation error: Validation isEmail on email failed
}
addUser1(userInfo);

以上這篇淺談Node.js ORM框架Sequlize之表間關(guān)系就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 安裝nvm并使用nvm安裝nodejs及配置環(huán)境變量的全過(guò)程

    安裝nvm并使用nvm安裝nodejs及配置環(huán)境變量的全過(guò)程

    有時(shí)候使用nvm管理node會(huì)發(fā)現(xiàn)無(wú)法使用node或npm,主要原因是環(huán)境變量沒(méi)有配置成功,下面這篇文章主要給大家介紹了關(guān)于安裝nvm并使用nvm安裝nodejs及配置環(huán)境變量的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • Nodejs下用submit提交表單提示cannot post錯(cuò)誤的解決方法

    Nodejs下用submit提交表單提示cannot post錯(cuò)誤的解決方法

    這篇文章主要介紹了Nodejs下用submit提交表單提示cannot post錯(cuò)誤的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-11-11
  • 基于nodejs的微信JS-SDK簡(jiǎn)單應(yīng)用實(shí)現(xiàn)

    基于nodejs的微信JS-SDK簡(jiǎn)單應(yīng)用實(shí)現(xiàn)

    這篇文章主要介紹了基于nodejs的微信JS-SDK簡(jiǎn)單應(yīng)用實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-05-05
  • 使用travis-ci如何持續(xù)部署node.js應(yīng)用詳解

    使用travis-ci如何持續(xù)部署node.js應(yīng)用詳解

    最近在學(xué)習(xí)使用 travis-ci 對(duì)項(xiàng)目進(jìn)行持續(xù)集成測(cè)試,所以下面這篇文章主要給大家介紹了關(guān)于使用travis-ci如何持續(xù)部署node.js應(yīng)用的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-07-07
  • 理解Koa2中的async&await的用法

    理解Koa2中的async&await的用法

    這篇文章主要介紹了理解Koa2中的async&await的用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • node.js實(shí)現(xiàn)的裝飾者模式示例

    node.js實(shí)現(xiàn)的裝飾者模式示例

    這篇文章主要介紹了node.js實(shí)現(xiàn)的裝飾者模式,簡(jiǎn)單說(shuō)明了裝飾者模式的原理、功能并結(jié)合實(shí)例形式給出了node.js裝飾者模式的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2017-09-09
  • 詳解Koa中更方便簡(jiǎn)單發(fā)送響應(yīng)的方式

    詳解Koa中更方便簡(jiǎn)單發(fā)送響應(yīng)的方式

    這篇文章主要介紹了詳解Koa中更方便簡(jiǎn)單發(fā)送響應(yīng)的方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 一文詳解Node.contain?函數(shù)兼容處理

    一文詳解Node.contain?函數(shù)兼容處理

    這篇文章主要為大家介紹了Node.contain?函數(shù)兼容處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • node中的cookie的具體使用

    node中的cookie的具體使用

    這篇文章主要介紹了node中的cookie的具體使用,詳細(xì)的介紹了什么是cookie和cookie的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • npm包發(fā)布和刪除的超詳細(xì)教程

    npm包發(fā)布和刪除的超詳細(xì)教程

    npm是JavaScript的包管理器,也是世界上最大的軟件注冊(cè)中心,下面這篇文章主要給大家介紹了關(guān)于npm包發(fā)布和刪除的超詳細(xì)教程,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02

最新評(píng)論

金沙县| 唐海县| 朔州市| 衡水市| 沅江市| 察雅县| 图木舒克市| 习水县| 东城区| 太谷县| 文水县| 宁津县| 孟连| 德阳市| 贡觉县| 邛崃市| 巴东县| 武冈市| 自贡市| 南乐县| 额济纳旗| 达州市| 鄢陵县| 宣城市| 阿巴嘎旗| 龙井市| 玉田县| 盐津县| 襄樊市| 新巴尔虎右旗| 泾川县| 荥阳市| 高尔夫| 梅州市| 芮城县| 荥经县| 内丘县| 阳朔县| 铁岭县| 张家界市| 都昌县|