sequelize-fixtures
Advanced tools
Comparing version 0.10.0 to 1.0.0
@@ -9,2 +9,3 @@ var Promise = require('./Promise'); | ||
this.skipped = 0; | ||
this.fixtureModels = {}; | ||
}; | ||
@@ -16,3 +17,3 @@ | ||
}.bind(this)).then(function() { | ||
return Promise.resolve(this.saved); | ||
return Promise.resolve({count:this.saved, models:this.fixtureModels}); | ||
}.bind(this)); | ||
@@ -24,2 +25,3 @@ }; | ||
saveOptions = fixture.saveOptions, | ||
ignoreSet = fixture.ignoreSet, | ||
onError = function(err) { | ||
@@ -81,8 +83,10 @@ if (err instanceof Error) { | ||
if (fieldType === 'JSONB') { | ||
where[k] = {}; | ||
where[k][Op.contains] = data[k]; | ||
where[k] = {}; | ||
where[k][Op.contains] = data[k]; | ||
} else if (fieldType === 'JSON') { | ||
where[k] = JSON.stringify(data[k]); | ||
} else if (Model.rawAttributes[k].hasOwnProperty('set')) { | ||
} else if (Model.rawAttributes[k].hasOwnProperty('set') && !ignoreSet) { | ||
var val = null; | ||
// create a simulated setDataValue method, which is commonly | ||
// called within setter to update the value after formatting | ||
Model.setDataValue = function(name, value) { | ||
@@ -93,6 +97,11 @@ val = value; | ||
// (e.g. serializing a JSON blob.) | ||
Model.rawAttributes[k].set.call(Model, data[k]); | ||
// don't include VIRTUAL column in filter condition | ||
where[k] = val; | ||
// this will fail, however, if OTHER instance methods are called within | ||
// the setter, other than setDataValue as defined above | ||
try { | ||
Model.rawAttributes[k].set.call(Model, data[k]); | ||
// don't include VIRTUAL column in filter condition | ||
where[k] = val; | ||
} catch (err) { | ||
throw new Error('Error using Model.set method for model ' + fixture.model + ' property ' + k + ' (possibly due to the use of instance methods). You should use option ignoreSet: true'); | ||
} | ||
} else { | ||
@@ -112,2 +121,6 @@ where[k] = data[k]; | ||
self.skipped++; | ||
if (!self.fixtureModels[Model.name]) { | ||
self.fixtureModels[Model.name] = []; | ||
} | ||
self.fixtureModels[Model.name].push(instance); | ||
return setManyToMany(instance); | ||
@@ -128,2 +141,7 @@ } | ||
self.saved++; | ||
if (!self.fixtureModels[Model.name]) { | ||
self.fixtureModels[Model.name] = []; | ||
} | ||
self.fixtureModels[Model.name].push(instance); | ||
return setManyToMany(instance); | ||
@@ -137,2 +155,3 @@ } | ||
Loader.prototype.prepFixtureData = function(data, Model) { | ||
var self = this, | ||
@@ -174,3 +193,2 @@ result = {}, | ||
options.transaction = self.options.transaction; | ||
promises.push( | ||
@@ -177,0 +195,0 @@ assoc.target.findOne(options) |
{ | ||
"name": "sequelize-fixtures", | ||
"version": "0.10.0", | ||
"version": "1.0.0", | ||
"description": "sequelize fixture loader", | ||
"main": "index.js", | ||
"scripts": { | ||
"release": "release-it", | ||
"test": "./node_modules/.bin/mocha tests" | ||
@@ -19,3 +20,3 @@ }, | ||
"glob": "~7.0.5", | ||
"js-yaml": "^3.13.0", | ||
"js-yaml": "^3.13.1", | ||
"object-assign": "^4.0.1" | ||
@@ -25,2 +26,3 @@ }, | ||
"mocha": "*", | ||
"release-it": "^12.3.0", | ||
"sequelize": "^5.0.0", | ||
@@ -40,3 +42,16 @@ "should": "*", | ||
"license": "BSD", | ||
"readmeFilename": "README.md" | ||
"readmeFilename": "README.md", | ||
"release-it": { | ||
"git": { | ||
"commit": true, | ||
"tag": true, | ||
"push": true | ||
}, | ||
"github": { | ||
"release": true | ||
}, | ||
"npm": { | ||
"publish": true | ||
} | ||
} | ||
} |
[![Build Status](https://travis-ci.org/domasx2/sequelize-fixtures.svg?branch=master)](https://travis-ci.org/domasx2/sequelize-fixtures) | ||
**I'm no longer using Sequelize or this lib, and have very little motivation to maintain it. If someone wants to take over, let me know!** | ||
Sequelize fixtures | ||
@@ -54,2 +52,10 @@ ========================================== | ||
//specify logging function (default console.log) | ||
function myLogging(defaultLog) { | ||
console.log('Fixtures: processing ...') | ||
} | ||
sequelize_fixtures.loadFile('fixtures/*.json', models, { log: myLogging}).then(function(){ | ||
doStuffAfterLoad(); | ||
}); | ||
//load fixtures inside a transaction | ||
@@ -385,2 +391,48 @@ sequelize.transaction(function(tx) { | ||
#### Ignore setters (`ignoreSet`) | ||
By default, this library attempts to run values through any defined property setters to coerce the value correctly. | ||
If you use instance methods (other than `setDataValue`, which a mock is created for), then this will raise an error. | ||
For example: | ||
```javascript | ||
const User = sequelize.define('User', | ||
email: { | ||
type: DataTypes.STRING, | ||
unique: true, | ||
validate: { | ||
isEmail: true, | ||
}, | ||
set: function set(val) { | ||
if (this.previous('email')) { // <--- this line will raise an error | ||
// check some thing | ||
} | ||
this.setDataValue('email', val); | ||
} | ||
} | ||
}); | ||
``` | ||
You can turn off this behavior by setting `ignoreSet` to true. | ||
```json | ||
{ | ||
"model": "User", | ||
"ignoreSet": true, | ||
"saveOptions": { | ||
"fields": ["title", "body"] | ||
}, | ||
"data": { | ||
"title": "Any title", | ||
"slug": "My Invalid Slug" | ||
} | ||
} | ||
``` | ||
This ignores any defined setters for this model and instead just set the value | ||
as the same data literal specified in the fixture. | ||
# grunt task | ||
@@ -387,0 +439,0 @@ |
var sf = require('../index'); | ||
module.exports = function(grunt) { | ||
grunt.task.registerMultiTask('fixtures', 'Load fixtures', function () { | ||
var data = this.data, | ||
grunt.task.registerMultiTask('fixtures', 'Load fixtures', function() { | ||
var taskInstance = this, | ||
data = this.data, | ||
models = data.models, | ||
done = this.async(), | ||
options = data.options || {}, | ||
loader; | ||
options = data.options || {}, | ||
loadFiles = function(models) { | ||
var sources = []; | ||
taskInstance.files.forEach(function(f) { | ||
[].push.apply(sources, f.src); | ||
}); | ||
if (sources.length) { | ||
sf.loadFiles(sources, models, options) | ||
.then(function(numSaved) { | ||
grunt.log.ok(numSaved + ' fixtures loaded.'); | ||
done(); | ||
}); | ||
} else { | ||
throw new Error('no sources provided'); | ||
} | ||
}; | ||
// link grunt logging to internal, if not specified | ||
options.log = options.log || grunt.verbose.writeln; | ||
if (typeof models == 'string') { | ||
grunt.verbose.writeln('Loading models using file path: ' + models); | ||
models = require(models); | ||
} else if (models.call) { | ||
models = models(); | ||
grunt.verbose.writeln('Loading models using function callback...'); | ||
models = models.call(taskInstance, options); | ||
if (models.then) { | ||
models.then(function(loadedModels) { | ||
return loadFiles(loadedModels); | ||
}); | ||
} else { | ||
loadFiles(models); | ||
} | ||
} | ||
var sources = []; | ||
this.files.forEach(function(f){ | ||
[].push.apply(sources, f.src); | ||
}); | ||
if (sources.length) { | ||
sf.loadFiles(sources, models, options).then(function(saved){ | ||
console.log(saved+' fixtures loaded.'); | ||
done(); | ||
}); | ||
} else { | ||
throw new Error('no sources provided'); | ||
} | ||
}); | ||
}; | ||
}; |
@@ -153,2 +153,11 @@ var sf = require('../index'), | ||
it('should return model count and models', function() { | ||
return sf.loadFile('tests/fixtures/fixture1.json', models) | ||
.then(function(retVal) { | ||
retVal.count.should.equal(3); | ||
retVal.models.foo.length.should.equal(2); | ||
retVal.models.bar.length.should.equal(1); | ||
}); | ||
}); | ||
it('should load fixtures from js (implied relative)', function() { | ||
@@ -155,0 +164,0 @@ return sf.loadFile('tests/fixtures/fixture1.js', models) |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
59058
1253
1
458
6
1
Updatedjs-yaml@^3.13.1