不要怂,就是干,撸起袖子干!

Commit 9814684e by Mick Hansen

[ci skip] Update models docs

1 parent 99576a86
Showing with 20 additions and 22 deletions
...@@ -220,15 +220,13 @@ function(title) { ...@@ -220,15 +220,13 @@ function(title) {
**N.B.: **It is important to stick to using the `setDataValue()` and `getDataValue()` functions (as opposed to accessing the underlying "data values" property directly) - doing so protects your custom getters and setters from changes in the underlying model implementations (i.e. how and where data values are stored in your model instances) **N.B.: **It is important to stick to using the `setDataValue()` and `getDataValue()` functions (as opposed to accessing the underlying "data values" property directly) - doing so protects your custom getters and setters from changes in the underlying model implementations (i.e. how and where data values are stored in your model instances)
### Setter methods and Object Initialization
!!!TODO: write about how setters affect object initialization (both creating new objects with Model.build and retrieving existing objects from storage) !!!!!
## Validations ## Validations
Model validations, allow you to specify format/content/inheritance validations for each attribute of the model. You can perform the validation by calling the `validate()` method on an instance before saving. The validations are implemented by [validator][3]. Model validations, allow you to specify format/content/inheritance validations for each attribute of the model.
**Note: **In `v1.7.0` validations will now be called when executing the `build()` or `create()` functions. Validations are automatically run on `create`, `update` and `save`. You can also call `validate()` to manually validate an instance.
The validations are implemented by [validator][3].
```js ```js
var ValidateMe = sequelize.define('Foo', { var ValidateMe = sequelize.define('Foo', {
...@@ -309,11 +307,11 @@ See [the node-validator project][4]for more details on the built in validation m ...@@ -309,11 +307,11 @@ See [the node-validator project][4]for more details on the built in validation m
### Validators and`allowNull` ### Validators and`allowNull`
Since `v1.7.0` if a particular field of a model is set to allow null (with `allowNull: true`) and that value has been set to `null` , its validators do not run. This means you can, for instance, have a string field which validates its length to be at least 5 characters, but which also allows`null`. If a particular field of a model is set to allow null (with `allowNull: true`) and that value has been set to `null` , its validators do not run. This means you can, for instance, have a string field which validates its length to be at least 5 characters, but which also allows`null`.
### Model validations ### Model validations
Since `v1.7.0` , validations can also be defined to check the model after the field-specific validators. Using this you could, for example, ensure either neither of `latitude` and `longitude` are set or both, and fail if one but not the other is set. Validations can also be defined to check the model after the field-specific validators. Using this you could, for example, ensure either neither of `latitude` and `longitude` are set or both, and fail if one but not the other is set.
Model validator methods are called with the model object's context and are deemed to fail if they throw an error, otherwise pass. This is just the same as with custom field-specific validators. Model validator methods are called with the model object's context and are deemed to fail if they throw an error, otherwise pass. This is just the same as with custom field-specific validators.
...@@ -443,7 +441,7 @@ module.exports = function(sequelize, DataTypes) { ...@@ -443,7 +441,7 @@ module.exports = function(sequelize, DataTypes) {
} }
``` ```
Since `v1.7.0` the `import` method can now accept a callback as an argument. The `import` method can also accept a callback as an argument.
```js ```js
sequelize.import('Project', function(sequelize, DataTypes) { sequelize.import('Project', function(sequelize, DataTypes) {
...@@ -460,15 +458,15 @@ When starting a new project you won't have a database structure and using Sequel ...@@ -460,15 +458,15 @@ When starting a new project you won't have a database structure and using Sequel
```js ```js
// Create the tables: // Create the tables:
Project.sync() // will emit success or failure event Project.sync()
Task.sync() // will emit success or failure event Task.sync()
   
// Force the creation! // Force the creation!
Project.sync({force: true}) // this will drop the table first and re-create it afterwards Project.sync({force: true}) // this will drop the table first and re-create it afterwards
   
// drop the tables: // drop the tables:
Project.drop() // will emit success or failure event Project.drop()
Task.drop() // will emit success or failure event Task.drop()
   
// event handling: // event handling:
Project.[sync|drop]().then(function() { Project.[sync|drop]().then(function() {
...@@ -481,14 +479,14 @@ Project.[sync|drop]().then(function() { ...@@ -481,14 +479,14 @@ Project.[sync|drop]().then(function() {
Because synchronizing and dropping all of your tables might be a lot of lines to write, you can also let Sequelize do the work for you: Because synchronizing and dropping all of your tables might be a lot of lines to write, you can also let Sequelize do the work for you:
```js ```js
// create all tables... now! // Sync all models that aren't already in the database
sequelize.sync() // will emit success or failure sequelize.sync()
   
// force it! // Force sync all modes
sequelize.sync({force: true}) // emit ... nomnomnom sequelize.sync({force: true})
   
// want to drop 'em all? // Drop all tables
sequelize.drop() // I guess you've got it (emit) sequelize.drop()
   
// emit handling: // emit handling:
sequelize.[sync|drop]().then(function() { sequelize.[sync|drop]().then(function() {
...@@ -572,7 +570,7 @@ Project.find({ where: {title: 'aProject'} }).then(function(project) { ...@@ -572,7 +570,7 @@ Project.find({ where: {title: 'aProject'} }).then(function(project) {
// project will be the first entry of the Projects table with the title 'aProject' || null // project will be the first entry of the Projects table with the title 'aProject' || null
}) })
   
// since v1.3.0: only select some attributes and rename one
Project.find({ Project.find({
where: {title: 'aProject'}, where: {title: 'aProject'},
attributes: ['id', ['name', 'title']] attributes: ['id', ['name', 'title']]
...@@ -872,7 +870,7 @@ Project.min('age').then(function(min) { ...@@ -872,7 +870,7 @@ Project.min('age').then(function(min) {
// this will return 5 // this will return 5
}) })
   
Project.min('age', { where: { age: { gt: 5 } } }).then(function(min) { Project.min('age', { where: { age: { $gt: 5 } } }).then(function(min) {
// will be 10 // will be 10
}) })
``` ```
...@@ -893,7 +891,7 @@ Project.sum('age').then(function(sum) { ...@@ -893,7 +891,7 @@ Project.sum('age').then(function(sum) {
// this will return 55 // this will return 55
}) })
   
Project.sum('age', { where: { age: { gt: 5 } } }).then(function(sum) { Project.sum('age', { where: { age: { $gt: 5 } } }).then(function(sum) {
// wil be 50 // wil be 50
}) })
``` ```
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!