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

Commit 172272c8 by Mick Hansen

Merge pull request #5170 from sushantdhiman/fix-docs-2

Few more documents updates
2 parents e67a9b3f 21065c37
...@@ -74,7 +74,7 @@ As this article is for beginners, we will skip migrations for now and take a clo ...@@ -74,7 +74,7 @@ As this article is for beginners, we will skip migrations for now and take a clo
In order to let Sequelize create schemas in the database, you need to describe what kind of data you want to store. This can be done with `sequelize.define`: In order to let Sequelize create schemas in the database, you need to describe what kind of data you want to store. This can be done with `sequelize.define`:
```js ```js
var User = sequelize.define('User', { var User = sequelize.define('user', {
username: Sequelize.STRING, username: Sequelize.STRING,
password: Sequelize.STRING password: Sequelize.STRING
}); });
...@@ -111,7 +111,7 @@ Please note, that `{ force: true }` will drop the `Users` table and re-create it ...@@ -111,7 +111,7 @@ Please note, that `{ force: true }` will drop the `Users` table and re-create it
You might not need the timestamps or you might not want the plural of the model's name as table name, right? Luckily there are configuration possibilities for that: You might not need the timestamps or you might not want the plural of the model's name as table name, right? Luckily there are configuration possibilities for that:
```js ```js
var User = sequelize.define('User', { var User = sequelize.define('user', {
username: Sequelize.STRING, username: Sequelize.STRING,
password: Sequelize.STRING password: Sequelize.STRING
}, { }, {
...@@ -123,7 +123,7 @@ var User = sequelize.define('User', { ...@@ -123,7 +123,7 @@ var User = sequelize.define('User', {
And just in case you want to customize the timestamp field names, you can do it like this: And just in case you want to customize the timestamp field names, you can do it like this:
```js ```js
var User = sequelize.define('User', { var User = sequelize.define('user', {
username: Sequelize.STRING, username: Sequelize.STRING,
password: Sequelize.STRING password: Sequelize.STRING
}, { }, {
...@@ -135,7 +135,7 @@ var User = sequelize.define('User', { ...@@ -135,7 +135,7 @@ var User = sequelize.define('User', {
Furthermore you can introduce a `deletedAt` timestamp so models are not actually deleted when you call `destroy`. Adding a `deletedAt` timestamp is called making the model 'paranoid': Furthermore you can introduce a `deletedAt` timestamp so models are not actually deleted when you call `destroy`. Adding a `deletedAt` timestamp is called making the model 'paranoid':
```js ```js
var User = sequelize.define('User', { var User = sequelize.define('user', {
username: Sequelize.STRING, username: Sequelize.STRING,
password: Sequelize.STRING password: Sequelize.STRING
}, { }, {
...@@ -201,8 +201,8 @@ An association between one source and one target is called "one to one" or 1:1 a ...@@ -201,8 +201,8 @@ An association between one source and one target is called "one to one" or 1:1 a
Sequelize expects a foreign key in the target's schema. That means that there has to be an attribute respectively a column in the target's table. Sequelize expects a foreign key in the target's schema. That means that there has to be an attribute respectively a column in the target's table.
```js ```js
var Source = sequelize.define('Source', {}) var Source = sequelize.define('source', {})
, Target = sequelize.define('Target', {}) , Target = sequelize.define('target', {})
Source.hasOne(Target) Source.hasOne(Target)
Target.belongsTo(Source) Target.belongsTo(Source)
...@@ -222,8 +222,8 @@ An association between one source and many target is called "one to many" or 1:N ...@@ -222,8 +222,8 @@ An association between one source and many target is called "one to many" or 1:N
Sequelize expects a foreign key in the target's schema. That means that there has to be an attribute respectively a column in the target's table. Sequelize expects a foreign key in the target's schema. That means that there has to be an attribute respectively a column in the target's table.
```js ```js
var Source = sequelize.define('Source', {}) var Source = sequelize.define('source', {})
, Target = sequelize.define('Target', {}) , Target = sequelize.define('target', {})
Source.hasMany(Target) Source.hasMany(Target)
Target.belongsTo(Source) Target.belongsTo(Source)
...@@ -243,8 +243,8 @@ An association between many sources and many targets is called "many to many" or ...@@ -243,8 +243,8 @@ An association between many sources and many targets is called "many to many" or
Sequelize expects a junction table which contains a foreign key to the source table and a foreign key to the target table. A row in the table connects a source with a target. Sequelize expects a junction table which contains a foreign key to the source table and a foreign key to the target table. A row in the table connects a source with a target.
```js ```js
var Source = sequelize.define('Source', {}) var Source = sequelize.define('source', {})
, Target = sequelize.define('Target', {}) , Target = sequelize.define('target', {})
Source.hasMany(Target) Source.hasMany(Target)
Target.hasMany(Source) Target.hasMany(Source)
...@@ -262,8 +262,8 @@ sequelize ...@@ -262,8 +262,8 @@ sequelize
Defining associations is nice, but won't give you any advantage if you cannot read or set associations. Of course Sequelize will add respective functions to your models. Depending on the type of association you will find different methods: Defining associations is nice, but won't give you any advantage if you cannot read or set associations. Of course Sequelize will add respective functions to your models. Depending on the type of association you will find different methods:
```js ```js
var Source = sequelize.define('Source', {}) var Source = sequelize.define('source', {})
, Target = sequelize.define('Target', {}); , Target = sequelize.define('target', {});
Source.hasOne(Target); Source.hasOne(Target);
Target.belongsTo(Source); Target.belongsTo(Source);
...@@ -309,8 +309,8 @@ source.setTarget(null).then(function() { ...@@ -309,8 +309,8 @@ source.setTarget(null).then(function() {
For 1:N and N:M associations it makes sense to not only set the associations, but also to add or remove associations. Furthermore checking for an association can be handy. For 1:N and N:M associations it makes sense to not only set the associations, but also to add or remove associations. Furthermore checking for an association can be handy.
```js ```js
var Source = sequelize.define('Source', {}) var Source = sequelize.define('source', {})
, Target = sequelize.define('Target', {}); , Target = sequelize.define('target', {});
Source.hasMany(Target); Source.hasMany(Target);
Target.belongsTo(Source); Target.belongsTo(Source);
...@@ -360,7 +360,7 @@ Now that you know the basics of Sequelize, you might want to see everything in a ...@@ -360,7 +360,7 @@ Now that you know the basics of Sequelize, you might want to see everything in a
```js ```js
var Sequelize = require('sequelize') var Sequelize = require('sequelize')
, sequelize = new Sequelize('database_name', 'username', 'password') , sequelize = new Sequelize('database_name', 'username', 'password')
, User = sequelize.define('User', { , User = sequelize.define('user', {
username: Sequelize.STRING, username: Sequelize.STRING,
password: Sequelize.STRING password: Sequelize.STRING
}); });
......
...@@ -40,7 +40,7 @@ There are currently three ways to programmatically add hooks: ...@@ -40,7 +40,7 @@ There are currently three ways to programmatically add hooks:
```js ```js
// Method 1 via the .define() method // Method 1 via the .define() method
var User = sequelize.define('User', { var User = sequelize.define('user', {
username: DataTypes.STRING, username: DataTypes.STRING,
mood: { mood: {
type: DataTypes.ENUM, type: DataTypes.ENUM,
...@@ -83,7 +83,7 @@ User.afterValidate('myHookAfter', function(user, options, fn) { ...@@ -83,7 +83,7 @@ User.afterValidate('myHookAfter', function(user, options, fn) {
Only a hook with name param can be removed. Only a hook with name param can be removed.
```js ```js
var Book = sequelize.define('Book', { var Book = sequelize.define('book', {
title: DataTypes.STRING title: DataTypes.STRING
}) })
...@@ -243,11 +243,11 @@ For the most part hooks will work the same for instances when being associated e ...@@ -243,11 +243,11 @@ For the most part hooks will work the same for instances when being associated e
2. The only way to call beforeDestroy/afterDestroy hooks are on associations with `onDelete: 'cascade'` and the option `hooks: true`. For instance: 2. The only way to call beforeDestroy/afterDestroy hooks are on associations with `onDelete: 'cascade'` and the option `hooks: true`. For instance:
```js ```js
var Projects = sequelize.define('Projects', { var Projects = sequelize.define('projects', {
title: DataTypes.STRING title: DataTypes.STRING
}) })
var Tasks = sequelize.define('Tasks', { var Tasks = sequelize.define('tasks', {
title: DataTypes.STRING title: DataTypes.STRING
}) })
......
...@@ -19,7 +19,7 @@ Built instances will automatically get default values when they were defined&col ...@@ -19,7 +19,7 @@ Built instances will automatically get default values when they were defined&col
```js ```js
// first define the model // first define the model
var Task = sequelize.define('Task', { var Task = sequelize.define('task', {
title: Sequelize.STRING, title: Sequelize.STRING,
rating: { type: Sequelize.STRING, defaultValue: 3 } rating: { type: Sequelize.STRING, defaultValue: 3 }
}) })
...@@ -205,7 +205,7 @@ User.bulkCreate([ ...@@ -205,7 +205,7 @@ User.bulkCreate([
`bulkCreate` was originally made to be a mainstream/fast way of inserting records, however, sometimes you want the luxury of being able to insert multiple rows at once without sacrificing model validations even when you explicitly tell Sequelize which columns to sift through. You can do by adding a `validate: true` property to the options object. `bulkCreate` was originally made to be a mainstream/fast way of inserting records, however, sometimes you want the luxury of being able to insert multiple rows at once without sacrificing model validations even when you explicitly tell Sequelize which columns to sift through. You can do by adding a `validate: true` property to the options object.
```js ```js
var Tasks = sequelize.define('Task', { var Tasks = sequelize.define('task', {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
validate: { validate: {
......
...@@ -2,7 +2,7 @@ While out of the box Sequelize will seem a bit opinionated it's trivial to both ...@@ -2,7 +2,7 @@ While out of the box Sequelize will seem a bit opinionated it's trivial to both
## Tables ## Tables
```js ```js
sequelize.define('User', { sequelize.define('user', {
}, { }, {
tableName: 'users' tableName: 'users'
...@@ -11,7 +11,7 @@ sequelize.define('User', { ...@@ -11,7 +11,7 @@ sequelize.define('User', {
## Fields ## Fields
```js ```js
sequelize.define('ModelName', { sequelize.define('modelName', {
userId: { userId: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
field: 'user_id' field: 'user_id'
......
...@@ -4,12 +4,12 @@ To define mappings between a model and a table, use the `define` method. Sequeli ...@@ -4,12 +4,12 @@ To define mappings between a model and a table, use the `define` method. Sequeli
```js ```js
var Project = sequelize.define('Project', { var Project = sequelize.define('project', {
title: Sequelize.STRING, title: Sequelize.STRING,
description: Sequelize.TEXT description: Sequelize.TEXT
}) })
var Task = sequelize.define('Task', { var Task = sequelize.define('task', {
title: Sequelize.STRING, title: Sequelize.STRING,
description: Sequelize.TEXT, description: Sequelize.TEXT,
deadline: Sequelize.DATE deadline: Sequelize.DATE
...@@ -19,7 +19,7 @@ var Task = sequelize.define('Task', { ...@@ -19,7 +19,7 @@ var Task = sequelize.define('Task', {
You can also set some options on each column: You can also set some options on each column:
```js ```js
var Foo = sequelize.define('Foo', { var Foo = sequelize.define('foo', {
// instantiating will automatically set the flag to true if not set // instantiating will automatically set the flag to true if not set
flag: { type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true}, flag: { type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true},
...@@ -197,7 +197,7 @@ Getters and Setters can be defined in 2 ways (you can mix and match these 2 appr ...@@ -197,7 +197,7 @@ Getters and Setters can be defined in 2 ways (you can mix and match these 2 appr
### Defining as part of a property ### Defining as part of a property
```js ```js
var Employee = sequelize.define('Employee', { var Employee = sequelize.define('employee', {
name: { name: {
type : Sequelize.STRING, type : Sequelize.STRING,
allowNull: false, allowNull: false,
...@@ -231,7 +231,7 @@ Below is an example of defining the getters and setters in the model options. Th ...@@ -231,7 +231,7 @@ Below is an example of defining the getters and setters in the model options. Th
Note that the `this.firstname` and `this.lastname` references in the `fullName` getter function will trigger a call to the respective getter functions. If you do not want that then use the `getDataValue()` method to access the raw value (see below). Note that the `this.firstname` and `this.lastname` references in the `fullName` getter function will trigger a call to the respective getter functions. If you do not want that then use the `getDataValue()` method to access the raw value (see below).
```js ```js
var Foo = sequelize.define('Foo', { var Foo = sequelize.define('foo', {
firstname: Sequelize.STRING, firstname: Sequelize.STRING,
lastname: Sequelize.STRING lastname: Sequelize.STRING
}, { }, {
...@@ -281,7 +281,7 @@ Validations are automatically run on `create`, `update` and `save`. You can also ...@@ -281,7 +281,7 @@ Validations are automatically run on `create`, `update` and `save`. You can also
The validations are implemented by [validator.js][3]. The validations are implemented by [validator.js][3].
```js ```js
var ValidateMe = sequelize.define('Foo', { var ValidateMe = sequelize.define('foo', {
foo: { foo: {
type: Sequelize.STRING, type: Sequelize.STRING,
validate: { validate: {
...@@ -372,7 +372,7 @@ Any error messages collected are put in the validation result object alongside t ...@@ -372,7 +372,7 @@ Any error messages collected are put in the validation result object alongside t
An example: An example:
```js ```js
var Pub = Sequelize.define('Pub', { var Pub = Sequelize.define('pub', {
name: { type: Sequelize.STRING }, name: { type: Sequelize.STRING },
address: { type: Sequelize.STRING }, address: { type: Sequelize.STRING },
latitude: { latitude: {
...@@ -412,7 +412,7 @@ In this simple case an object fails validation if either latitude or longitude i ...@@ -412,7 +412,7 @@ In this simple case an object fails validation if either latitude or longitude i
You can also influence the way Sequelize handles your column names: You can also influence the way Sequelize handles your column names:
```js ```js
var Bar = sequelize.define('Bar', { /* bla */ }, { var Bar = sequelize.define('bar', { /* bla */ }, {
// don't add the timestamp attributes (updatedAt, createdAt) // don't add the timestamp attributes (updatedAt, createdAt)
timestamps: false, timestamps: false,
...@@ -438,7 +438,7 @@ var Bar = sequelize.define('Bar', { /* bla */ }, { ...@@ -438,7 +438,7 @@ var Bar = sequelize.define('Bar', { /* bla */ }, {
If you want sequelize to handle timestamps, but only want some of them, or want your timestamps to be called something else, you can override each column individually: If you want sequelize to handle timestamps, but only want some of them, or want your timestamps to be called something else, you can override each column individually:
```js ```js
var Foo = sequelize.define('Foo', { /* bla */ }, { var Foo = sequelize.define('foo', { /* bla */ }, {
// don't forget to enable timestamps! // don't forget to enable timestamps!
timestamps: true, timestamps: true,
...@@ -457,7 +457,7 @@ var Foo = sequelize.define('Foo', { /* bla */ }, { ...@@ -457,7 +457,7 @@ var Foo = sequelize.define('Foo', { /* bla */ }, {
You can also change the database engine, e.g. to MyISAM. InnoDB is the default. You can also change the database engine, e.g. to MyISAM. InnoDB is the default.
```js ```js
var Person = sequelize.define('Person', { /* attributes */ }, { var Person = sequelize.define('person', { /* attributes */ }, {
engine: 'MYISAM' engine: 'MYISAM'
}) })
...@@ -470,7 +470,7 @@ var sequelize = new Sequelize(db, user, pw, { ...@@ -470,7 +470,7 @@ var sequelize = new Sequelize(db, user, pw, {
Finally you can specify a comment for the table in MySQL and PG Finally you can specify a comment for the table in MySQL and PG
```js ```js
var Person = sequelize.define('Person', { /* attributes */ }, { var Person = sequelize.define('person', { /* attributes */ }, {
comment: "I'm a table comment!" comment: "I'm a table comment!"
}) })
``` ```
...@@ -486,7 +486,7 @@ var Project = sequelize.import(__dirname + "/path/to/models/project") ...@@ -486,7 +486,7 @@ var Project = sequelize.import(__dirname + "/path/to/models/project")
// The model definition is done in /path/to/models/project.js // The model definition is done in /path/to/models/project.js
// As you might notice, the DataTypes are the very same as explained above // As you might notice, the DataTypes are the very same as explained above
module.exports = function(sequelize, DataTypes) { module.exports = function(sequelize, DataTypes) {
return sequelize.define("Project", { return sequelize.define("project", {
name: DataTypes.STRING, name: DataTypes.STRING,
description: DataTypes.TEXT description: DataTypes.TEXT
}) })
...@@ -496,8 +496,8 @@ module.exports = function(sequelize, DataTypes) { ...@@ -496,8 +496,8 @@ module.exports = function(sequelize, DataTypes) {
The `import` method can also 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) {
return sequelize.define("Project", { return sequelize.define("project", {
name: DataTypes.STRING, name: DataTypes.STRING,
description: DataTypes.TEXT description: DataTypes.TEXT
}) })
...@@ -562,7 +562,7 @@ sequelize.sync({ force: true, match: /_test$/ }); ...@@ -562,7 +562,7 @@ sequelize.sync({ force: true, match: /_test$/ });
Sequelize allows you to pass custom methods to a model and its instances. Just do the following: Sequelize allows you to pass custom methods to a model and its instances. Just do the following:
```js ```js
var Foo = sequelize.define('Foo', { /* attributes */}, { var Foo = sequelize.define('foo', { /* attributes */}, {
classMethods: { classMethods: {
method1: function(){ return 'smth' } method1: function(){ return 'smth' }
}, },
...@@ -579,7 +579,7 @@ Foo.build().method2() ...@@ -579,7 +579,7 @@ Foo.build().method2()
Of course you can also access the instance's data and generate virtual getters: Of course you can also access the instance's data and generate virtual getters:
```js ```js
var User = sequelize.define('User', { firstname: Sequelize.STRING, lastname: Sequelize.STRING }, { var User = sequelize.define('user', { firstname: Sequelize.STRING, lastname: Sequelize.STRING }, {
instanceMethods: { instanceMethods: {
getFullname: function() { getFullname: function() {
return [this.firstname, this.lastname].join(' ') return [this.firstname, this.lastname].join(' ')
...@@ -608,7 +608,7 @@ var sequelize = new Sequelize('database', 'username', 'password', { ...@@ -608,7 +608,7 @@ var sequelize = new Sequelize('database', 'username', 'password', {
}) })
// Example: // Example:
var Foo = sequelize.define('Foo', { /* attributes */}); var Foo = sequelize.define('foo', { /* attributes */});
Foo.method1() Foo.method1()
Foo.method2() Foo.method2()
Foo.build().method3() Foo.build().method3()
...@@ -618,7 +618,7 @@ Foo.build().method3() ...@@ -618,7 +618,7 @@ Foo.build().method3()
Sequelize supports adding indexes to the model definition which will be created during `Model.sync()` or `sequelize.sync`. Sequelize supports adding indexes to the model definition which will be created during `Model.sync()` or `sequelize.sync`.
```js ```js
sequelize.define('User', {}, { sequelize.define('user', {}, {
indexes: [ indexes: [
// Create a unique index on email // Create a unique index on email
{ {
......
...@@ -411,9 +411,9 @@ Project.sum('age', { where: { age: { $gt: 5 } } }).then(function(sum) { ...@@ -411,9 +411,9 @@ Project.sum('age', { where: { age: { $gt: 5 } } }).then(function(sum) {
When you are retrieving data from the database there is a fair chance that you also want to get associations with the same query - this is called eager loading. The basic idea behind that, is the use of the attribute `include` when you are calling `find` or `findAll`. Lets assume the following setup: When you are retrieving data from the database there is a fair chance that you also want to get associations with the same query - this is called eager loading. The basic idea behind that, is the use of the attribute `include` when you are calling `find` or `findAll`. Lets assume the following setup:
```js ```js
var User = sequelize.define('User', { name: Sequelize.STRING }) var User = sequelize.define('user', { name: Sequelize.STRING })
, Task = sequelize.define('Task', { name: Sequelize.STRING }) , Task = sequelize.define('task', { name: Sequelize.STRING })
, Tool = sequelize.define('Tool', { name: Sequelize.STRING }) , Tool = sequelize.define('tool', { name: Sequelize.STRING })
Task.belongsTo(User) Task.belongsTo(User)
User.hasMany(Task) User.hasMany(Task)
...@@ -436,8 +436,8 @@ Task.findAll({ include: [ User ] }).then(function(tasks) { ...@@ -436,8 +436,8 @@ Task.findAll({ include: [ User ] }).then(function(tasks) {
"id": 1, "id": 1,
"createdAt": "2013-03-20T20:31:40.000Z", "createdAt": "2013-03-20T20:31:40.000Z",
"updatedAt": "2013-03-20T20:31:40.000Z", "updatedAt": "2013-03-20T20:31:40.000Z",
"UserId": 1, "userId": 1,
"User": { "user": {
"name": "John Doe", "name": "John Doe",
"id": 1, "id": 1,
"createdAt": "2013-03-20T20:31:45.000Z", "createdAt": "2013-03-20T20:31:45.000Z",
...@@ -462,12 +462,12 @@ User.findAll({ include: [ Task ] }).then(function(users) { ...@@ -462,12 +462,12 @@ User.findAll({ include: [ Task ] }).then(function(users) {
"id": 1, "id": 1,
"createdAt": "2013-03-20T20:31:45.000Z", "createdAt": "2013-03-20T20:31:45.000Z",
"updatedAt": "2013-03-20T20:31:45.000Z", "updatedAt": "2013-03-20T20:31:45.000Z",
"Tasks": [{ "tasks": [{
"name": "A Task", "name": "A Task",
"id": 1, "id": 1,
"createdAt": "2013-03-20T20:31:40.000Z", "createdAt": "2013-03-20T20:31:40.000Z",
"updatedAt": "2013-03-20T20:31:40.000Z", "updatedAt": "2013-03-20T20:31:40.000Z",
"UserId": 1 "userId": 1
}] }]
}] }]
*/ */
...@@ -494,7 +494,7 @@ User.findAll({ include: [{ model: Tool, as: 'Instruments' }] }).then(function(us ...@@ -494,7 +494,7 @@ User.findAll({ include: [{ model: Tool, as: 'Instruments' }] }).then(function(us
"id": 1, "id": 1,
"createdAt": null, "createdAt": null,
"updatedAt": null, "updatedAt": null,
"UserId": 1 "userId": 1
}] }]
}] }]
*/ */
...@@ -524,7 +524,7 @@ User.findAll({ ...@@ -524,7 +524,7 @@ User.findAll({
"id": 1, "id": 1,
"createdAt": null, "createdAt": null,
"updatedAt": null, "updatedAt": null,
"UserId": 1 "userId": 1
}] }]
}], }],
...@@ -538,7 +538,7 @@ User.findAll({ ...@@ -538,7 +538,7 @@ User.findAll({
"id": 1, "id": 1,
"createdAt": null, "createdAt": null,
"updatedAt": null, "updatedAt": null,
"UserId": 1 "userId": 1
}] }]
}], }],
*/ */
...@@ -606,7 +606,7 @@ User.findAll({ ...@@ -606,7 +606,7 @@ User.findAll({
"id": 1, "id": 1,
"createdAt": null, "createdAt": null,
"updatedAt": null, "updatedAt": null,
"UserId": 1, "userId": 1,
"Teacher": { // 1:1 association "Teacher": { // 1:1 association
"name": "Jimi Hendrix" "name": "Jimi Hendrix"
} }
......
...@@ -15,7 +15,7 @@ more. ...@@ -15,7 +15,7 @@ more.
var Sequelize = require('sequelize'); var Sequelize = require('sequelize');
var sequelize = new Sequelize('database', 'username', 'password'); var sequelize = new Sequelize('database', 'username', 'password');
var User = sequelize.define('User', { var User = sequelize.define('user', {
username: Sequelize.STRING, username: Sequelize.STRING,
birthday: Sequelize.DATE birthday: Sequelize.DATE
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!