Node.js ORM框架:ORM2
2015-07-14 15:33
543 查看
项目地址:https://github.com/dresende/node-orm2
支持的数据库:
・ MySQL & MariaDB・ PostgreSQL・ Amazon Redshift・ SQLite安装
1. npm install orm连接数据库
1. var orm = require("orm");2. 3. orm.connect("mysql://username:password@host/database", function (err, db) {4. //...5. });定义模型
1. var Person = db.define("person", {2. name : String,3. surname : String,4. age : Number, // FLOAT5. male : Boolean,6. continent : [ "Europe", "America", "Asia", "Africa", "Australia", "Antartica" ], // ENUM type7. photo : Buffer, // BLOB/BINARY8. data : Object // JSON encoded9. }, {10. methods: {11. fullName: function () {12. return this.name + ' ' + this.surname;13. }14. },15. validations: {16. age: orm.enforce.ranges.number(18, undefined, "under-age")17. }18. });查找
1. Person.find({ surname: "Doe" }, function (err, people) {2. // SQL: "SELECT * FROM person WHERE surname = 'Doe'"3. 4. console.log("People found: %d", people.length);5. console.log("First person: %s, age %d", people[0].fullName(), people[0].age);6. 7. people[0].age = 16;8. people[0].save(function (err) {9. // err.msg = "under-age";10. });11. });删除和同步表
1. db.drop(function () {2. // 删除所有表3. 4. Person.sync(function () {5. // 创建Person表6. });7. });创建
1. var newRecord = {};2. newRecord.id = 1;3. newRecord.name = "John"4. Person.create(newRecord, function(err, results) {5. ...6. });更新
1. Person.find({ surname: "Doe" }, function (err, people) {2. // SQL: "SELECT * FROM person WHERE surname = 'Doe'"3. 4. console.log("People found: %d", people.length);5. console.log("First person: %s, age %d", people[0].fullName(), people[0].age);6. 7. people[0].age = 16;8. people[0].save(function (err) {9. // err.msg = "under-age";10. });11. });相关文章推荐
- 【leetcode】Populating Next Right Pointers in Each Node II
- PM2 nodejs 学习(一)
- node 异步回调解决方法之yield
- hadoop入门之设置datanode的心跳时间的方法
- node.js中exports与module.exports
- NodeMCU实现远程控制LED灯
- #19 Remove Nth Node From End of List
- Windows 安装 node.js
- LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- nodejs连接mysql查询 第一次不报错 之后一直报错Cannot enqueue quit after invoking quit.
- Hadoop集群出现no data node to stop的解决方案
- setTimeout函数在浏览器中和Node.js中的区别
- Nodejs使用mailer发送邮件
- 当我设计游戏服务器时,我在想些什么?(4)
- 当我设计游戏服务器时,我在想些什么?(3)
- 当我设计游戏服务器时,我在想些什么?(2)
- 当我设计游戏服务器时,我在想些什么?(1)
- Node实践之一
- Node:最小堆
- leetcode116 Populating Next Right Pointers in Each Node