@@ -61,7 +61,7 @@ Post.create(cb);
61
61
// all posts
62
62
Post .all (cb)
63
63
// all posts by user
64
- Post .all ({userId: user .id });
64
+ Post .all ({where : { userId: user .id } });
65
65
// the same as prev
66
66
user .posts (cb)
67
67
// same as new Post({userId: user.id});
@@ -83,9 +83,64 @@ User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too shor
83
83
User .validatesInclusionOf (' gender' , {in: [' male' , ' female' ]});
84
84
User .validatesExclusionOf (' domain' , {in: [' www' , ' billing' , ' admin' ]});
85
85
User .validatesNumericalityOf (' age' , {int: true });
86
+ User .validatesUniquenessOf (' email' , {message: ' email is not unique' });
86
87
87
- user .isValid () // false
88
- user .errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...}
88
+ user .isValid (function (valid ) {
89
+ if (! valid) {
90
+ user .errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...}
91
+ }
92
+ })
93
+
94
+ ```
95
+
96
+ ## Callbacks
97
+
98
+ The following callbacks supported:
99
+
100
+ - afterInitialize
101
+ - beforeCreate
102
+ - afterCreate
103
+ - beforeSave
104
+ - afterSave
105
+ - beforeUpdate
106
+ - afterUpdate
107
+ - beforeDestroy
108
+ - afterDestroy
109
+ - beforeValidation
110
+ - afterValidation
111
+
112
+ Each callback is class method of the model, it should accept single argument: ` next ` , this is callback which
113
+ should be called after end of the hook. Except ` afterInitialize ` because this method is syncronous (called after ` new Model ` ).
114
+
115
+ ## Object lifecycle:
116
+
117
+ ``` javascript
118
+ var user = new User ;
119
+ // afterInitialize
120
+ user .save (callback);
121
+ // beforeValidation
122
+ // afterValidation
123
+ // beforeSave
124
+ // beforeCreate
125
+ // afterCreate
126
+ // afterSave
127
+ // callback
128
+ user .
updateAttribute (
' email' ,
' [email protected] ' , callback);
129
+ // beforeValidation
130
+ // afterValidation
131
+ // beforeUpdate
132
+ // afterUpdate
133
+ // callback
134
+ user .destroy (callback);
135
+ // beforeDestroy
136
+ // afterDestroy
137
+ // callback
138
+ User .create (data, callback);
139
+ // beforeValidate
140
+ // afterValidate
141
+ // beforeCreate
142
+ // afterCreate
143
+ // callback
89
144
```
90
145
91
146
Read the tests for usage examples: ./test/common_test.js
0 commit comments