Uriel Katz씨의 Google Gears ORM 프로젝트가 드디어 모습을 드러냈다. 이번 버전은 JSON 또는 Real foreign key(SQLite는 이 것을 지원하지 않음)를 쉽게 읽고 쓸 수 있으며, trigger도 사용할 수 있는 버전이라고 한다. 아래의 예제를 보자.
var Post = new GearsORM.Model({
name:"Post",
fields: {
title:new GearsORM.Fields.String({maxLength:256}),
body:new GearsORM.Fields.String({maxLength:5000}),
tags:new GearsORM.Fields.ManyToMany({related:"Tag"}),
comments:new GearsORM.Fields.ManyToOne({ related:"Comment"})
}
});
var Comment = new GearsORM.Model({
name:"Comment",
fields: {
name:new GearsORM.Fields.String({maxLength:50}),
email:new GearsORM.Fields.String({maxLength:75}),
body:new GearsORM.Fields.String({maxLength:1000}),
post:new GearsORM.Fields.OneToMany({ related:"Post",onDeleteCascade:true})
}
});
var Tag = new GearsORM.Model({
name:"Tag", fields: {
name:new GearsORM.Fields.String({maxLength:25}),
posts:new GearsORM.Fields.ManyToMany({related:"Post"})
}
});
// now we have three models: Post,Comment and Tag.
Tag.createTable();
Post.createTable();
Comment.createTable();
// This will insure that all the tables exist(but this doesn't insure that the table definition is correct,in case that the table already exist).
// Now that we have everything configured(in terms of models and tables) we can start to insert some data.
var testTag = new Tag();
testTag.name = "TestTag";
testTag.save();
// This will create a new Tag object,set its name and save it to the database(this happen when you call the save method of the Tag instance).
// there is a shorter way to create a instance and set its values
var testTag = new Tag({name:"TestTag"}).save();
// Now for relations
var myTestPost = new Post({
title:"my test post",
body:"my test post body"
}).save();
// myTestPost.tags is a ManyToMany relation field.
myTestPost.tags.add(testTag);
와~ SQLite 디비 생성과정이 지나치게 간단명료하고 저장하는 방식도 쿨하다. 한 라인으로 name 필드 체우는 것 좀 봐. 우~ 모델만 있으면 디비 관계는 정말로 쉽게 맺어지는 구나. 멋지다 Gears ORM!
Comments