Better docs, no warning

This commit is contained in:
Anatoliy Chakkaev 2012-11-10 09:28:07 +04:00
parent c28b803927
commit cb9f0c3d16
2 changed files with 14 additions and 2 deletions

View File

@ -25,8 +25,9 @@ var Post = schema.define('Post', {
title: { type: String, length: 255 }, title: { type: String, length: 255 },
content: { type: Schema.Text }, content: { type: Schema.Text },
date: { type: Date, default: Date.now }, date: { type: Date, default: Date.now },
published: { type: Boolean, default: false } published: { type: Boolean, default: false, index: true }
}); });
// simplier way to describe model // simplier way to describe model
var User = schema.define('User', { var User = schema.define('User', {
name: String, name: String,
@ -36,6 +37,15 @@ var User = schema.define('User', {
age: Number age: Number
}); });
// define any custom method
User.prototype.getNameAndAge = function () {
return this.name + ', ' + this.age;
};
// models also accessible in schema:
schema.models.User;
schema.models.Post;
// setup relationships // setup relationships
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'}); User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
// creates instance methods: // creates instance methods:
@ -74,6 +84,8 @@ Post.all(cb)
Post.all({where: {userId: user.id}, order: 'id', limit: 10, skip: 20}); Post.all({where: {userId: user.id}, order: 'id', limit: 10, skip: 20});
// the same as prev // the same as prev
user.posts(cb) user.posts(cb)
// get one latest post
Post.findOne({where: {published: true}, order: 'date DESC'}, cb);
// same as new Post({userId: user.id}); // same as new Post({userId: user.id});
user.posts.build user.posts.build
// save as Post.create({userId: user.id}, cb); // save as Post.create({userId: user.id}, cb);

View File

@ -90,7 +90,7 @@ Object.keys(models).forEach(function (model) {
if (displayWarning) { if (displayWarning) {
var $ = railway.utils.stylize.$; var $ = railway.utils.stylize.$;
require('util').puts($('WARNING:').bold.red + ' ' + $('I can see that you\'ve added validation to db/schema.js. However schema.js file is only used to describe database schema. Therefore validations configured in db/schema.js will be ignored.\nFor business logic (incl. validations) please create models as separate .js files here: app/models/*.js').yellow); // require('util').puts($('WARNING:').bold.red + ' ' + $('I can see that you\'ve added validation to db/schema.js. However schema.js file is only used to describe database schema. Therefore validations configured in db/schema.js will be ignored.\nFor business logic (incl. validations) please create models as separate .js files here: app/models/*.js').yellow);
} }
function log(str, startTime) { function log(str, startTime) {