Socket
Socket
Sign inDemoInstall

sequelize-fixtures

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sequelize-fixtures - npm Package Compare versions

Comparing version 0.3.1 to 0.3.2

tests/fixtures/fixture1.js

120

lib/loader.js

@@ -5,3 +5,3 @@ var util = require('util'),

var Loader = module.exports = function(options){
var Loader = module.exports = function(options) {
this.options = options;

@@ -12,8 +12,10 @@ this.saved = 0;

Loader.prototype.loadFixtures = function (fixtures, models, cb) {
var i = 0, self = this;
function load(){
if(fixtures.length > i){
self.loadFixture(fixtures[i], models, function(err){
if(err) throw err;
Loader.prototype.loadFixtures = function(fixtures, models, cb) {
var i = 0,
self = this;
function load() {
if (fixtures.length > i) {
self.loadFixture(fixtures[i], models, function(err) {
if (err) throw err;
else {

@@ -25,3 +27,3 @@ i++;

} else {
if(cb) cb();
if (cb) cb();
}

@@ -32,49 +34,55 @@ }

Loader.prototype.loadFixture = function (fixture, models, cb) {
Loader.prototype.loadFixture = function(fixture, models, cb) {
var buildOptions = fixture.buildOptions,
saveOptions = fixture.saveOptions,
onError = function(err){
throw new Error(JSON.stringify(err));
};
saveOptions = fixture.saveOptions,
onError = function(err) {
if (err instanceof Error) {
throw err;
} else {
throw new Error(JSON.stringify(err));
}
};
if(typeof fixture != 'object') throw new Error('expected fixture to be object, is ' + (typeof fixture));
else if(!fixture.model) throw new Error('model for a fixture is undefined');
else if(!fixture.data) throw new Error('data undefined for fixture');
var Model = models[fixture.model], self = this;
if(!Model) {
throw new Error('Model not found: '+fixture.model);
if (typeof fixture != 'object') throw new Error('expected fixture to be object, is ' + (typeof fixture));
else if (!fixture.model) throw new Error('model for a fixture is undefined');
else if (!fixture.data) throw new Error('data undefined for fixture');
var Model = models[fixture.model],
self = this;
if (!Model) {
throw new Error('Model not found: ' + fixture.model);
} else {
this.prepFixtureData(fixture.data, Model, function(data, many2many){
this.prepFixtureData(fixture.data, Model, function(data, many2many) {
var setManyToMany = function(instance) {
//set many2many assocs if there are any
if(Object.keys(many2many).length){
if (Object.keys(many2many).length) {
var promises = [];
Object.keys(many2many).forEach(function(key){
Object.keys(many2many).forEach(function(key) {
var def = q.defer();
promises.push(def.promise);
var assoc = Model.associations[key];
instance[assoc.accessors.set](many2many[key]).success(function(){
instance[assoc.accessors.set](many2many[key]).success(function() {
def.resolve();
});
});
q.all(promises).then(function(){
if(cb) cb();
q.all(promises).then(function() {
if (cb) cb();
});
} else {
if(cb) cb();
if (cb) cb();
}
}
var where = {};
Object.keys(Model.rawAttributes).forEach(function (k) {
if(data[k]) where[k] = data[k];
Object.keys(Model.rawAttributes).forEach(function(k) {
if (data[k]) where[k] = data[k];
});
Model.find({ where: where }).success(function(instance){
if(instance) {
Model.find({
where: where
}).success(function(instance) {
if (instance) {
self.skipped++;
setManyToMany(instance);
}
else {
Model.build(data, buildOptions).save(undefined, saveOptions).success(function (instance) {
if(instance) {
} else {
Model.build(data, buildOptions).save(undefined, saveOptions).success(function(instance) {
if (instance) {
self.saved++;

@@ -90,30 +98,38 @@ setManyToMany(instance);

Loader.prototype.prepFixtureData = function(data, Model, cb){
var result = {}, promises = [], errors = [], many2many = {};
Loader.prototype.prepFixtureData = function(data, Model, cb) {
var result = {},
promises = [],
errors = [],
many2many = {};
// Allows an external caller to do some transforms to the data
// before it is saved
if(this.options.transformFixtureDataFn) {
if (this.options.transformFixtureDataFn) {
result = this.options.transformFixtureDataFn(data, Model);
}
Object.keys(data).forEach(function(key){
var assoc = Model.associations[key], val = data[key];
if(assoc){
if(assoc.associationType == 'BelongsTo'){
Object.keys(data).forEach(function(key) {
var assoc = Model.associations[key],
val = data[key];
if (assoc) {
if (assoc.associationType == 'BelongsTo') {
var def = q.defer();
promises.push(def.promise);
assoc.target.find(typeof val == 'object' ? {where:val} : val).success(function(obj){
assoc.target.find(typeof val == 'object' ? {
where: val
} : val).success(function(obj) {
result[assoc.identifier] = obj.id;
def.resolve();
}).error(function(err){
}).error(function(err) {
throw err;
});
} else if(assoc.associationType == 'HasMany') {
} else if (assoc.associationType == 'HasMany') {
many2many[assoc.associationAccessor] = [];
if(Array.isArray(val)){
val.forEach(function(v){
if (Array.isArray(val)) {
val.forEach(function(v) {
var def = q.defer();
promises.push(def.promise);
assoc.target.find(typeof v == 'object' ? {where:v} : v).success(function(obj){
assoc.target.find(typeof v == 'object' ? {
where: v
} : v).success(function(obj) {
many2many[assoc.associationAccessor].push(obj);

@@ -129,3 +145,3 @@ def.resolve();

}
} else {

@@ -136,5 +152,5 @@ result[key] = val;

q.all(promises).then(function(){
q.all(promises).then(function() {
cb(result, many2many);
});
};
};

@@ -20,11 +20,16 @@ var glob = require('glob'),

var ext = path.extname(filename).toLowerCase();
if(!PARSERS[ext]) {
throw new Error('unknown file type: ', ext);
if(ext === '.js') {
cb(require(path.resolve(process.cwd(), filename)));
}
fs.readFile(filename, this.options.encoding, function(err, data){
if(err) throw err;
var fixtures = PARSERS[ext](data);
if(fixtures.fixtures) fixtures = fixtures.fixtures;
cb(fixtures);
});
else {
if(!PARSERS[ext]) {
throw new Error('unknown file type: ', ext);
}
fs.readFile(filename, this.options.encoding, function(err, data){
if(err) throw err;
var fixtures = PARSERS[ext](data);
if(fixtures.fixtures) fixtures = fixtures.fixtures;
cb(fixtures);
});
}
};

@@ -65,2 +70,2 @@

read();
};
};
{
"name": "sequelize-fixtures",
"version": "0.3.1",
"version": "0.3.2",
"description": "sequelize fixture loader",

@@ -36,2 +36,2 @@ "main": "index.js",

"readmeFilename": "README.md"
}
}

@@ -45,2 +45,13 @@ Sequelize fixtures

});
//apply transform for each model being loaded
sequelize_fixtures.loadFile('ixtures/*.json', models, {
transformFixtureDataFn: function (data) {
if(data.createdAt
&& data.createdAt < 0) {
data.createdAt = new Date((new Date()).getTime() + parseFloat(data.createdAt) * 1000 * 60);
}
return data;
}
});

@@ -63,3 +74,3 @@ //from array

}
]
];
sequelize_fixtures.loadFixtures(fixtures, models, function(err){

@@ -108,2 +119,24 @@ doStuffAfterLoad();

#### javascript
```javascript
module.exports = [
{
"model": "Foo",
"data": {
"propA": "bar",
"propB": 1
}
},
{
"model": "Foo",
"data": {
"propA": "baz",
"propB": 3
}
}
];
```
### Associations

@@ -110,0 +143,0 @@

@@ -141,2 +141,38 @@ var sf = require('../index'),

it('should load fixtures from js (implied relative)', function(done){
sf.loadFile('tests/fixtures/fixture1.js', models, function(){
models.Foo.count().success(function(c){
c.should.equal(2);
models.Bar.count().success(function(c){
c.should.equal(1);
done();
});
});
});
});
it('should load fixtures from js (explicit relative)', function(done){
sf.loadFile('./tests/fixtures/fixture1.js', models, function(){
models.Foo.count().success(function(c){
c.should.equal(2);
models.Bar.count().success(function(c){
c.should.equal(1);
done();
});
});
});
});
it('should load fixtures from js (absolute)', function(done){
sf.loadFile(process.cwd() + '/tests/fixtures/fixture1.js', models, function(){
models.Foo.count().success(function(c){
c.should.equal(2);
models.Bar.count().success(function(c){
c.should.equal(1);
done();
});
});
});
});
it('should load fixtures from multiple files via glob', function(done){

@@ -316,2 +352,2 @@ sf.loadFile('tests/fixtures/fixture*.json', models, function(){

});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc