sequelize-fixtures
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -95,3 +95,10 @@ var Promise = require('bluebird'); | ||
Object.keys(data).forEach(function(key) { | ||
var assoc = Model.associations[key], | ||
var foundInThroughName = null; | ||
// For Sequelize < 3.0.0 compatibility (using for instance "actorsmovies" instead of "actors" or "movies") | ||
Object.keys(Model.associations).forEach(function(assoc_name) { | ||
if(Model.associations[assoc_name].options && (Model.associations[assoc_name].options.through == key || Model.associations[assoc_name].options.as == key)) { | ||
return foundInThroughName = assoc_name; | ||
} | ||
}); | ||
var assoc = Model.associations[foundInThroughName || key], | ||
val = data[key]; | ||
@@ -103,3 +110,3 @@ if (assoc) { | ||
where: val | ||
} : val).then(function(obj) { | ||
} : {where: {id: val}}).then(function(obj) { | ||
result[assoc.identifier] = obj.id; | ||
@@ -115,3 +122,3 @@ }) | ||
where: v | ||
} : v).then(function(obj) { | ||
} : {where: {id: v}}).then(function(obj) { | ||
many2many[assoc.associationAccessor].push(obj); | ||
@@ -118,0 +125,0 @@ }) |
{ | ||
"name": "sequelize-fixtures", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "sequelize fixture loader", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,4 +8,6 @@ Sequelize fixtures | ||
Duplicate records are not insertd. | ||
API returns bluebird promises, but callbacks can also be used as the last argument. | ||
API returns bluebird promises, but callbacks can also be used as the last argument. | ||
Tested with latest Sequelize (3.0.0), should work on 2.x. | ||
### Install | ||
@@ -146,7 +148,11 @@ | ||
#### belongsTo | ||
#### One to many | ||
Assuming `Car.belongsTo(Owner)`: | ||
Assuming | ||
```javascript | ||
Car.belongsTo(Owner); | ||
Owner.hasMany(Car); | ||
``` | ||
Associated entity can be mapped by providing either an id: | ||
```json | ||
@@ -173,3 +179,3 @@ [ | ||
OR | ||
Or a property-value map (like a 'where' object) to find the entity with: | ||
@@ -189,3 +195,3 @@ ```json | ||
"make": "Ford", | ||
"owner": { //make sure it's unique across all owners | ||
"owner": { | ||
"name": "John Doe" | ||
@@ -198,3 +204,3 @@ } | ||
#### hasMany, belongsToMany | ||
#### Many to many | ||
@@ -204,13 +210,8 @@ Assuming | ||
```javascript | ||
Project.hasMany(Person); | ||
Person.hasMany(Project); | ||
Project.belongsToMany(Person, {through: 'peopleprojects'}); | ||
Person.belongsToMany(Project, {through: 'peopleprojects'}); | ||
``` | ||
or | ||
Associated entities can be mapped using the association 'as' name ('people' for Project and 'projects' for People) or 'through' table name, and providing an array of ids: | ||
```javascript | ||
Project.belongsToMany(Person); | ||
Person.belongsToMany(Project); | ||
``` | ||
```json | ||
@@ -239,3 +240,3 @@ [ | ||
"name": "The Great Project", | ||
"peopleprojects": [122, 123] | ||
"people": [122, 123] | ||
} | ||
@@ -247,3 +248,3 @@ } | ||
OR | ||
Or an array of property-value mappings to find the entities with: | ||
@@ -271,3 +272,3 @@ | ||
"name": "The Great Project", | ||
"peopleprojects": [ | ||
"people": [ | ||
{ | ||
@@ -274,0 +275,0 @@ "name": "Jack" |
@@ -18,6 +18,7 @@ var Sequelize = require('sequelize'), | ||
m.Foo.belongsTo(m.Bar); | ||
m.Project.hasMany(m.Person); | ||
m.Person.hasMany(m.Project); | ||
m.Actor.belongsToMany(m.Movie); | ||
m.Movie.belongsToMany(m.Actor); | ||
m.Bar.hasMany(m.Foo); | ||
m.Project.belongsToMany(m.Person, {through: 'peopleprojects'}); | ||
m.Person.belongsToMany(m.Project, {through: 'peopleprojects'}); | ||
m.Actor.belongsToMany(m.Movie, {through: 'actorsmovies'}); | ||
m.Movie.belongsToMany(m.Actor, {through: 'actorsmovies'}); | ||
})(exports); |
@@ -46,3 +46,3 @@ var sf = require('../index'), | ||
should.not.exist(err); | ||
models.Foo.find(3).then(function(foo){ | ||
models.Foo.find({where: {id: 3}}).then(function(foo){ | ||
should.exist(foo); | ||
@@ -398,3 +398,3 @@ foo.propA.should.equal('bar'); | ||
name: 'Bad Project', | ||
peopleprojects: [ | ||
people: [ | ||
{ | ||
@@ -401,0 +401,0 @@ name: 'John' |
@@ -45,3 +45,3 @@ var sf = require('../index'), | ||
}, models).then(function() { | ||
return models.Foo.find(3); | ||
return models.Foo.find({where: {id: 3}}); | ||
}).then(function(foo){ | ||
@@ -392,3 +392,3 @@ should.exist(foo); | ||
name: 'Bad Project', | ||
peopleprojects: [ | ||
people: [ | ||
{ | ||
@@ -395,0 +395,0 @@ name: 'John' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50683
1146
347