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

Commit 35f9f304 by Sascha Depold

documentation and further tests

1 parent 4e154206
With Sequelize you can also specify associations between multiple classes. Doing so will help you to easily With Sequelize you can also specify associations between multiple classes. Doing so will help you to easily
access and set those associated objects. The library therefore provides for each defined class the method access and set those associated objects. The library therefore provides for each defined class the following methods:<br/><br/>
the following methods:
<pre><%- koala(".js", partial("code/associations/associations-1.ejs")) %></pre> NOTE: Associations with models that use custom primaryKeys (so not the field 'id') are currently unsupported.
<pre><%- koala(".js", partial("code/associations/has-one.ejs")) %></pre>
<pre><%- koala(".js", partial("code/associations/belongs-to.ejs")) %></pre>
<pre><%- koala(".js", partial("code/associations/has-many.ejs")) %></pre>
Because Sequelize is doing a lot of magic, you have to call Sequelize#sync after setting the associations! Because Sequelize is doing a lot of magic, you have to call Sequelize#sync after setting the associations!
Doing so will allow you the following: Doing so will allow you the following:
......
// many to many association:
Class.hasMany('association name', AssociationClass, 'back association name')
// example
Project.hasMany('tasks', Task, 'projects')
// many to many association on the same class
Class.hasMany('association name')
// example
Person.hasMany('friends')
// one-to-one association
Class.hasOneAndBelongsTo('association name', AssociationClass)
// example
Person.hasOneAndBelongsTo('house', House, 'owner')
// one-to-many association
Class.hasManyAndBelongsTo('association name', AssociationClass, 'back association name')
// example
Person.hasManyAndBelongsTo('cars', Car, 'owner')
// one way associations
Class.hasMany('association name', AssociationClass)
Class.hasOne('association name', AssociationClass)
\ No newline at end of file
var User = sequelize.define('User', {/* ... */})
var Project = sequelize.define('Project', {/* ... */})
// One-way back associations
Project.belongsTo('initiator', User)
/*
In this example belongsTo will add an attribute UserId to the Project model!
That's the only difference to hasMany.
*/
\ No newline at end of file
var User = sequelize.define('User', {/* ... */})
var Project = sequelize.define('Project', {/* ... */})
// OK. Now things get more complicated (not really visible to the user :)).
// First let's define a hasMany association
Project.hasMany('Workers', User, {as: 'Workers'})
/*
This will add the attribute ProjectId or project_id to User.
Instances of Project will get the accessors getWorkers and setWorkers.
We could just leave it the way it is and let it be a one-way association.
But we want more! Let's define the other way around:
*/
User.hasMany('Projects', Project)
/*
This will remove the attribute ProjectId (or project_id) from User and create
a new model called ProjectsUsers with the equivalent foreign keys ProjectId
(or project_id) and UserId (or user_id). If the attributes are camelcase or
not depends on the Model it represents.
Now you can use Project#getWorkers, Project#setWorkers, User#getTasks and
User#setTasks.
*/
\ No newline at end of file
var User = sequelize.define('User', {/* ... */})
var Project = sequelize.define('Project', {/* ... */})
// One-way associations
Project.hasOne('initiator', User)
/*
In this example hasOne will add an attribute ProjectId to the User model!
Furthermore, Project.prototype will gain the methods getUser and setUser according
to the first parameter passed to define. If you have underscore style
enabled, the added attribute will be project_id instead of ProjectId.
You can also define the foreign key, e.g. if you already have an existing
database and want to work on it:
*/
Project.hasOne('inititator', User, { foreignKey: 'initiator_id' })
/*
Because Sequelize will use the model's name (first parameter of define) for
the accessor methods, it is also possible to pass a special option to hasOne:
*/
Project.hasOne('initiator', User, { as: 'Initiator' })
// Now you will get Project#getInitiator and Project#setInitiator
\ No newline at end of file
...@@ -9,6 +9,12 @@ var Task = sequelize.define('Task', { ...@@ -9,6 +9,12 @@ var Task = sequelize.define('Task', {
deadline: Sequelize.DATE deadline: Sequelize.DATE
}) })
/*
Sequelize will automatically add the attributes createdAt and updatedAt to the
Model. So you will be able to know when the database entry went into the db and
when it was updated the last time.
*/
// You can also set some options: // You can also set some options:
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
...@@ -21,4 +27,21 @@ var Foo = sequelize.define('Foo', { ...@@ -21,4 +27,21 @@ var Foo = sequelize.define('Foo', {
identifier: { type: Sequelize.String, primaryKey: true}, identifier: { type: Sequelize.String, primaryKey: true},
// autoIncrement can be used to create auto_incrementing integer columns // autoIncrement can be used to create auto_incrementing integer columns
incrementMe: { type: Sequelize.INTEGER, autoIncrement: true } incrementMe: { type: Sequelize.INTEGER, autoIncrement: true }
})
// And you can take influence of the way, Sequelize handles your column names:
var Bar = sequelize.define('Bar', { /* bla */ }, {
// don't add the timestamp attributes (updatedAt, createdAt)
timestamps: false,
// don't delete database entries but set the newly added attribute deletedAt
// to the current date (when deletion was done). paranoid will only work if
// timestamps are not disabled
paranoid: true,
// don't use camelcase for automatically added attributes but underscore style
// so updatedAt will be updated_at
underscore: true,
// disable the modification of tablenames; By default, sequelize will automatically
// transform all passed model names (first parameter of define) into plural.
// if you don't want that, set the following
freezeTableName: true
}) })
\ No newline at end of file
...@@ -25,6 +25,8 @@ module.exports = { ...@@ -25,6 +25,8 @@ module.exports = {
sequelize.modelManager.models.forEach(function(model) { sequelize.modelManager.models.forEach(function(model) {
if(model.tableName == (Task.tableName + User.tableName)) { if(model.tableName == (Task.tableName + User.tableName)) {
assert.isDefined(model.attributes['User'+num+'Id'])
assert.isDefined(model.attributes['Task'+num+'Id'])
exit(function(){}) exit(function(){})
} }
}) })
...@@ -40,7 +42,7 @@ module.exports = { ...@@ -40,7 +42,7 @@ module.exports = {
'it should correctly add the foreign id with underscore - bidirectional': function() { 'it should correctly add the foreign id with underscore - bidirectional': function() {
var num = parseInt(Math.random() * 99999999) var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING }, {underscored: true}) var User = sequelize.define('User' + num, { username: Sequelize.STRING }, {underscored: true})
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) var Task = sequelize.define('Task' + num, { title: Sequelize.STRING })
Task.hasMany(User) Task.hasMany(User)
User.hasMany(Task) User.hasMany(Task)
...@@ -51,6 +53,7 @@ module.exports = { ...@@ -51,6 +53,7 @@ module.exports = {
sequelize.modelManager.models.forEach(function(model) { sequelize.modelManager.models.forEach(function(model) {
if(model.tableName == (Task.tableName + User.tableName)) { if(model.tableName == (Task.tableName + User.tableName)) {
assert.isDefined(model.attributes['user'+ num +'_id']) assert.isDefined(model.attributes['user'+ num +'_id'])
assert.isDefined(model.attributes['Task'+ num +'Id'])
} }
}) })
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!