博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
node.js学习笔记(28) node-orm进阶三
阅读量:5882 次
发布时间:2019-06-19

本文共 9710 字,大约阅读时间需要 32 分钟。

也许你想监听Model实例的创建、修改、获取或者删除,那么正好node-rom为Model定义了一系列事件。

  • afterLoad : (no parameters) Right after loading and preparing an instance to be used;
  • afterAutoFetch : (no parameters) Right after auto-fetching associations (if any), it will trigger regardless of having associations or not;
  • beforeSave : (no parameters) Right before trying to save;
  • afterSave : (bool success) Right after saving;
  • beforeCreate : (no parameters) Right before trying to save a new instance (prior to beforeSave);
  • afterCreate : (bool success) Right after saving a new instance;
  • beforeRemove : (no parameters) Right before trying to remove an instance;
  • afterRemove : (bool success) Right after removing an instance;
  • beforeValidation : (no parameters) Before all validations and prior to beforeCreate and beforeSave;

Node-orm将这些events定义为hook,钩子。

钩子实际上是一个处理消息的程序段,通过系统调用,把它挂入系统。每当特定的消息发出,在没有到达目的窗口前,钩子程序就先捕获该消息,亦即钩子函数先得到控制权。这时钩子函数即可以加工处理(改变)该消息,也可以不作处理而继续传递该消息,还可以强制结束消息的传递。

这些事件刚好可以为对model实例增删改查的前后进行拦截,然后对model实例做一些加工处理。因此,钩子这个名字正合适。

这些事件都可以通过this来调用model实例本身。

var Person = db.define("person", {    name    : String,    surname : String}, {    hooks: {        beforeCreate: function (next) {            console.log(this.name);            next();        }    }});

一共9个事件,不是before*就是after*。但从一组来看,先后顺序很好区分,比如beforeCreate和afterCreate,分别是在insert的一前一后。

但是insert并不是仅仅关联beforeCreate和afterCreate这么简单,它还关联afterLoad、beforeVlidation、beforeSave和afterSave,那么谁先谁后呢?

通过程序来区分吧。

hook-insert.js:

var orm = require("orm");orm.connect('mysql://root:root@localhost:3306/nodejs', function(err, db){    if (err) throw err;    var User = db.define("user", {        id          :{type:'serial', mapsTo:'id', unique:true, size:11},        name        :{type:'text', mapsTo:'name'},        username    :{type:'text', mapsTo:'username'},        password    :{type:'text', mapsTo:'password'},        birthday    :{type:'date', mapsTo:'birthday', time:true}    },{        validations:{            username:orm.enforce.unique('The username is already existed.')        },        hooks:{            afterLoad : function(next){                console.log('This is afterLoad');                next();            },            afterAutoFetch : function(next){                console.log('This is afterAutoFetch')                next();            },            beforeSave:function(next){                console.log('This is beforeSave')                next();            },            afterSave:function(success){                console.log('This is afterSave')            },            beforeCreate :function(next){                console.log('This is beforeCreate')                next();            },            afterCreate :function(success){                console.log('This is afterCreate')            },            beforeRemove :function(next){                console.log('This is beforeRemove')                next();            },            afterRemove :function(success){                console.log('This is afterRemove')            },            beforeValidation :function(next){                console.log('This is beforeValidation')                next();            }        }    });    var user = new User(        {            name:'e',            username:'ee',            password:'eee',            birthday:'2010-10-10 10:10:10'        }    );    user.save(function(err){        if(err){            console.log(err);        }    });});
运行hook-insert:

node hook-insert.jsThis is afterLoadThis is afterAutoFetchThis is beforeValidationThis is beforeCreateThis is beforeSaveThis is afterCreateThis is afterSave
hook-load.js:
var orm = require("orm");orm.connect('mysql://root:root@localhost:3306/nodejs', function(err, db){    if (err) throw err;    var User = db.define("user", {        id          :{type:'serial', mapsTo:'id', unique:true, size:11},        name        :{type:'text', mapsTo:'name'},        username    :{type:'text', mapsTo:'username'},        password    :{type:'text', mapsTo:'password'},        birthday    :{type:'date', mapsTo:'birthday', time:true}    },{        validations:{            username:orm.enforce.unique('The username is already existed.')        },        hooks:{            afterLoad : function(next){                console.log('This is afterLoad');                next();            },            afterAutoFetch : function(next){                console.log('This is afterAutoFetch')                next();            },            beforeSave:function(next){                console.log('This is beforeSave')                next();            },            afterSave:function(success){                console.log('This is afterSave')            },            beforeCreate :function(next){                console.log('This is beforeCreate')                next();            },            afterCreate :function(success){                console.log('This is afterCreate')            },            beforeRemove :function(next){                console.log('This is beforeRemove')                next();            },            afterRemove :function(success){                console.log('This is afterRemove')            },            beforeValidation :function(next){                console.log('This is beforeValidation')                next();            }        }    });    User.find({username:'ee'}, function(err, items) {        for(var i=0;i

运行hook-load.js:

node hook-load.jsThis is afterLoadThis is afterAutoFetch{"id":8,"name":"e","username":"ee","password":"eee","birthday":"2010-10-10T02:10:10.000Z"}

hook-update.js:

var orm = require("orm");orm.connect('mysql://root:root@localhost:3306/nodejs', function(err, db){    if (err) throw err;    var User = db.define("user", {        id          :{type:'serial', mapsTo:'id', unique:true, size:11},        name        :{type:'text', mapsTo:'name'},        username    :{type:'text', mapsTo:'username'},        password    :{type:'text', mapsTo:'password'},        birthday    :{type:'date', mapsTo:'birthday', time:true}    },{        validations:{            username:orm.enforce.unique('The username is already existed.')        },        hooks:{            afterLoad : function(next){                console.log('This is afterLoad');                next();            },            afterAutoFetch : function(next){                console.log('This is afterAutoFetch')                next();            },            beforeSave:function(next){                console.log('This is beforeSave')                next();            },            afterSave:function(success){                console.log('This is afterSave')            },            beforeCreate :function(next){                console.log('This is beforeCreate')                next();            },            afterCreate :function(success){                console.log('This is afterCreate')            },            beforeRemove :function(next){                console.log('This is beforeRemove')                next();            },            afterRemove :function(success){                console.log('This is afterRemove')            },            beforeValidation :function(next){                console.log('This is beforeValidation')                next();            }        }    });    User.find({username:'ee'}, function(err, items) {        for(var i=0;i

运行hook-update.js:

node hook-update.jsThis is afterLoadThis is afterAutoFetchThis is beforeValidationThis is beforeSaveThis is afterSave
hook-delete.js:
var orm = require("orm");orm.connect('mysql://root:root@localhost:3306/nodejs', function(err, db){    if (err) throw err;    var User = db.define("user", {        id          :{type:'serial', mapsTo:'id', unique:true, size:11},        name        :{type:'text', mapsTo:'name'},        username    :{type:'text', mapsTo:'username'},        password    :{type:'text', mapsTo:'password'},        birthday    :{type:'date', mapsTo:'birthday', time:true}    },{        validations:{            username:orm.enforce.unique('The username is already existed.')        },        hooks:{            afterLoad : function(next){                console.log('This is afterLoad');                next();            },            afterAutoFetch : function(next){                console.log('This is afterAutoFetch')                next();            },            beforeSave:function(next){                console.log('This is beforeSave')                next();            },            afterSave:function(success){                console.log('This is afterSave')            },            beforeCreate :function(next){                console.log('This is beforeCreate')                next();            },            afterCreate :function(success){                console.log('This is afterCreate')            },            beforeRemove :function(next){                console.log('This is beforeRemove')                next();            },            afterRemove :function(success){                console.log('This is afterRemove')            },            beforeValidation :function(next){                console.log('This is beforeValidation')                next();            }        }    });    User.find({username:'ff'}, function(err, items) {        for(var i=0;i
运行hook-delete.js:

node hook-delete.jsThis is afterLoadThis is afterAutoFetchThis is beforeRemoveThis is afterRemove
从上面增、删、改、查四个程序的运行结果不难看出:

跟insert有关的事件有7个,按顺序分别是afterLoad、afterAutoFetch、beforeValidation、beforeCreate、beforeSave、afterCreate、afterSave。

跟udpate有关的事件有5个,按顺序分别是afterLoad、afterAutoFetch、beforeValidation、beforeSave、afterSave。

跟selete有关的事件有2个,按顺序分别是afterLoad、afterAutoFetch。

跟delete有关的事件有2个,按顺序分别是beforeRemove、afterRemove。

另外,除了afterSave、afterCreate和afterRemove,其他事件都有next参数,next是一个回调函数用于执行下一步,如果没next,程序将就此终止。

转载地址:http://cztix.baihongyu.com/

你可能感兴趣的文章
ubuntu 13 安装SH程序
查看>>
支付宝升级延时到账功能
查看>>
ghost后只剩下一个盘的数据寻回方法
查看>>
输入输出练习
查看>>
Git commit message和工作流规范
查看>>
java面试。答案源于网上
查看>>
yii中取得CActiveDataProvider的分页信息
查看>>
我的大学
查看>>
Google翻译接口收费啦
查看>>
Debian+Apache2服务器
查看>>
MySQL库和表的操作
查看>>
shell编程:编译器、解释器 变量
查看>>
yum仓库一些简单介绍
查看>>
HashMap----工作原理
查看>>
nodejs 安装 postgresql module
查看>>
【转】iOS学习之iOS禁止Touch事件
查看>>
【小记录】解决链接libcufft_static.a库出现的错误
查看>>
两列布局的几种实现方案
查看>>
Java8新特性之Collectors
查看>>
怎么用CorelDRAW制作表格
查看>>