Created
October 15, 2013 07:28
-
-
Save rubydoc/6987853 to your computer and use it in GitHub Desktop.
node_orm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var orm = require('orm'), | |
async = require('async'), | |
_ = require('underscore'); | |
var seed = false; | |
var show = true; | |
var _delete = false; | |
orm.settings.set("connection.debug", true); | |
orm.connect("mysql://root@localhost:3306/node_orm", function (err, db) { | |
if (err) throw err; | |
var User = db.define("users", { | |
username : String, | |
password : String, | |
age : Number, | |
gender : ["male", "female"], // ENUM | |
data : Object // JSON encoded | |
}, { | |
methods: { | |
hello: function () { | |
console.log('hello world'); | |
} | |
}, | |
validations: { | |
age: orm.enforce.ranges.number(18, undefined, "under-age") | |
} | |
}); | |
var Group = db.define('groups', { | |
confirm : Boolean | |
},{ | |
}); | |
var Post = db.define("posts", { | |
title : String, | |
desc : String | |
}, {}); | |
var Comment = db.define("comments", { | |
desc : String | |
}, {}); | |
var Tag = db.define("tags", { | |
name : String | |
}, {}); | |
Group.hasOne('teacher', User, { reverse: 'studentGroups', mergeAssocId: 'teacher_id'}); | |
Group.hasOne('student', User, { reverse: 'teacherGroups', mergeAssocId: 'student_id'}); | |
Post.hasOne('user', User, {reverse: 'posts'}); | |
Comment.hasOne('post', Post, {reverse: 'comments'}); | |
Comment.hasOne('user', User, {reverse: 'comments'}); | |
Post.hasMany('tags', Tag, { }, { | |
merge_table: 'posts_tags', | |
reverse: 'tags', | |
mergeId: 'post_id', | |
mergeAssocId: 'tag_id' } | |
); | |
if (seed) { | |
console.log('seeding..'); | |
async.series([ | |
function(cb) { | |
// method 1 | |
var user1 = new User; | |
user1.username = "KimSeongJun";// + Math.floor(Math.random()*11); | |
user1.password = "helloworld"; | |
user1.age = 27; | |
user1.gender = 'male'; | |
user1.data = { a: '1', b: '2'}; | |
user1.save(function(err){ | |
if (err) { | |
console.log(err); | |
}else{ | |
console.log(user1.id); | |
} | |
cb(); | |
}); | |
}, | |
function(cb) { | |
// method 2 | |
User.create([{ | |
username: 'ChoSeungYun', | |
password: 'helloworld', | |
age: 29, | |
gender: 'male', | |
data: {a: 'aa', b: 'bb'} | |
}], function(err, users){ | |
if (!err) { | |
Post.create([{ | |
user: users[0], | |
title: 'node', | |
desc: 'js' | |
}] ,function (err2, posts){ | |
if (!err2) { | |
cb(); | |
}; | |
}); | |
}else{ | |
console.log(err); | |
cb(); | |
} | |
}); | |
}, | |
function(cb) { | |
// method 3 | |
Post.get(1, function(err, post){ | |
if (!err) { | |
User.get(1, function(err, user){ | |
if (!err){ | |
var comment = new Comment; | |
comment.user = user; | |
comment.post = post; | |
comment.desc = "good post"; | |
comment.save(function(err){ | |
if (err) console.log(err); | |
}); | |
} | |
}); | |
}else{ | |
console.log(err); | |
} | |
}); | |
}, | |
function(cb) { | |
// method 4 | |
Post.get(1, function(err, post){ | |
if (!err) { | |
Tag.create([ | |
{ name: 'hello'}, | |
{ name: 'world'} | |
], function(err2, tags){ | |
post.addTags(tags, function(err3){ | |
if (err3) console.log(err3); | |
}); | |
}); | |
}else{ | |
console.log(err); | |
} | |
}); | |
}, | |
function(cb) { | |
// method 5 | |
var post = new Post({user_id: 1, title: 'helloworld', desc: 'create by new'}); | |
post.save(function(err){ | |
if (err) console.log(err); | |
}); | |
}, | |
function(cb) { | |
// method 6 | |
var post = new Post({user_id: 1, title: 'helloworld', desc: 'create by new'}); | |
Tag.create([ | |
{ name: 'hello'}, | |
{ name: 'world'} | |
], function(err2, tags){ | |
post.addTags(tags, function(err3){ | |
if (err3) console.log(err3); | |
}); | |
}); | |
post.save(function(err){ | |
if (err) console.log(err); | |
}); | |
}, | |
function(cb) { | |
// method 7 | |
var post = new Post({user_id: 1, title: 'helloworld', desc: 'create by new'}); | |
post.tags = [new Tag({ name: 'ff'}), new Tag({ name: 'gg'})]; | |
post.save(function(err){ | |
if (err) console.log(err); | |
}); | |
} | |
]); | |
} | |
if (_delete) { | |
console.log('delete..'); | |
} | |
if (show) { | |
async.series([ | |
function(cb){ | |
Group.get(1, function(err, group){ | |
group.getTeacher(function(err, teacher){ | |
console.log(teacher.username); | |
}); | |
}); | |
}, | |
function(cb){ | |
User.get(1, function(err, teacher){ | |
teacher.getStudentGroups(function(err, groups){ | |
groups.forEach(function(group){ | |
group.getStudent(function(err, student){ | |
if (err) { | |
console.log(err); | |
}else{ | |
console.log(student.username); | |
} | |
}); | |
}); | |
}); | |
}); | |
}, | |
function(cb){ | |
User.get(22, function(err, student){ | |
student.getTeacherGroups(function(err, groups){ | |
groups.forEach(function(group){ | |
group.getTeacher(function(err, teacher){ | |
if (err) { | |
console.log(err); | |
}else{ | |
console.log(teacher.username); | |
} | |
}); | |
}); | |
}); | |
}); | |
}, | |
// function(cb){ | |
// User.get(1, function(err, user) { | |
// console.log(user.data); | |
// }); | |
// }, | |
// function(cb){ | |
// Post.get(1, function(err, post) { | |
// console.log(post.title); | |
// console.log(post.desc); | |
// post.getUser(function(err, user){ | |
// console.log(user.id); | |
// }); | |
// post.getTags(function(err, tags){ | |
// console.log("Tags"); | |
// tags.forEach(function(tag){ | |
// console.log('#' + tag.id + ' =>' + tag.name); | |
// }); | |
// }); | |
// }); | |
// }, | |
// function(cb){ | |
// Post.get(1, function(err, post) { | |
// console.log(post.title); | |
// console.log(post.desc); | |
// post.getUser(function(err, user){ | |
// console.log(user.id); | |
// }); | |
// post.getTags().order('id').run(function(err, tags){ | |
// if (err) console.log(err); | |
// console.log("Tags"); | |
// tags.forEach(function(tag){ | |
// console.log('#' + tag.id + ' =>' + tag.name); | |
// }); | |
// }); | |
// }); | |
// }, | |
// function(cb){ | |
// Post.get(1, function(err, post) { | |
// console.log(post.title); | |
// console.log(post.desc); | |
// post.getUser(function(err, user){ | |
// console.log(user.id); | |
// }); | |
// post.getTags().orderRaw("?? ASC", ['id']).run(function(err, tags){ | |
// if (err) console.log(err); | |
// console.log("Tags"); | |
// tags.forEach(function(tag){ | |
// console.log('#' + tag.id + ' =>' + tag.name); | |
// }); | |
// }); | |
// }); | |
// }, | |
// function(cb){ | |
// Post.findByUser({id: 16},function(err, posts){ | |
// console.log(posts[0].title); | |
// }); | |
// }, | |
// function(cb){ | |
// User.find({}, function(err, users) { | |
// users.forEach(function(user, i, arr) { | |
// console.log( 'User #' + user.id + ' => ' + user.username ); | |
// }); | |
// if (users.length == 0) console.log('users length is 0.'); | |
// cb(); | |
// }); | |
// }, | |
// function(cb){ | |
// Post.find({}, function(err, posts) { | |
// posts.forEach(function(post, i, arr) { | |
// console.log( 'Post #' + post.id + ' => ' + post.title ); | |
// }); | |
// if (posts.length == 0) console.log('posts length is 0.'); | |
// cb(); | |
// }); | |
// }, | |
// function(cb){ | |
// Comment.find({}, function(err, comments) { | |
// comments.forEach(function(comment, i, arr) { | |
// console.log( 'Comment #' + comment.id + ' => ' + comment.desc ); | |
// }); | |
// if (comments.length == 0) console.log('comments length is 0.'); | |
// cb(); | |
// }); | |
// }, | |
// function(cb){ | |
// Tag.find({}, function(err, tags) { | |
// tags.forEach(function(tag, i, arr) { | |
// console.log( 'Tag #' + tag.id + ' => ' + tag.name ); | |
// }); | |
// if (tags.length == 0) console.log('tags length is 0.'); | |
// cb(); | |
// }); | |
// } | |
]); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment