Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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.0.1 to 0.1.0

lib/loader.js

146

index.js

@@ -1,109 +0,53 @@

var glob = require('glob'),
path = require('path'),
yaml = require('js-yaml'),
fs = require('fs');
var Loader = require('./lib/loader'),
Reader = require('./lib/reader');
var PARSERS = exports.PARSERS = {
'.json': JSON.parse,
'.yml': yaml.safeLoad,
'.yaml': yaml.safeLoad
};
function initopts(options){
options = options || {};
options.encoding = options.encoding || 'utf8';
options.log = options.log || console.log;
return options;
}
function loadFixtures (fixtures, models, cb) {
var i = 0;
function load(){
loadFixture(fixtures[i], models, function(err){
if(err) cb(err);
else {
if(fixtures.length > ++i){
load();
} else {
cb();
}
function wrap(fn){
return function(){
if(arguments.length < 2){
throw new error('Insufficient arguments');
} else {
var fixtures = arguments[0], models = arguments[1], options, cb, i;
for(i=2;i<arguments.length;i++){
if(typeof arguments[i] == 'object') options = arguments[i];
else if (typeof arguments[i] == 'function') cb = arguments[i];
}
});
}
if(fixtures.length) load();
else cb();
};
return fn(fixtures, models, initopts(options), cb);
}
};
}
function loadFixture (fixture, models, cb) {
if( !(typeof fixture == 'object')) cb('expected fixture to be object, is ' + (typeof fixture));
else if(!fixture.model) cb('model for a fixture is undefined');
else if(!fixture.data) cb('data undefined for fixture');
var Model = models[fixture.model];
if(!Model) {
cb('Model not found: '+fixture.model);
} else {
Model.findOrCreate(fixture.data, fixture.data).success(function(){
cb();
}).error(function(err){
cb(err);
});
}
};
function readFiles(globpath, options, cb) {
glob(globpath, function(err, filenames){
if(err) cb(err);
else {
var queue = [], data = [], err;
err = filenames.some(function(filename){
var ext = path.extname(filename).toLowerCase();
if(!PARSERS[ext]) {
cb('unknown file type: ', ext);
return true;
}
queue.push({
filename: filename,
parser: PARSERS[ext]
});
});
if(!err){
function readFile () {
var args = queue.shift();
fs.readFile(args.filename, options.encoding || 'utf8', function(err, d){
if(err) cb(err);
else {
d = args.parser(d);
if(d.fixtures) d = d.fixtures;
d.forEach(function(x){
data.push(x);
});
if(queue.length){
readFile();
} else {
cb(null, data);
}
}
});
}
if(queue.length) {
readFile();
} else {
cb(null, []);
}
}
}
});
};
exports.loadFixture = wrap(function (fixture, models, options, cb) {
var loader = new Loader(options);
loader.loadFixture(fixture, models, cb);
return loader;
});
exports.load = function(fixtures, models, options, cb) {
if(!cb) {
cb = options;
options = {};
}
exports.loadFixtures = wrap(function (fixtures, models, options, cb) {
var loader = new Loader(options);
loader.loadFixtures(fixtures, models, cb);
return loader;
});
cb = cb || function(){};
exports.loadFile = wrap(function (filename, models, options, cb) {
var loader = new Loader(options), reader = new Reader(options);
reader.readFileGlob(filename, function(fixtures){
loader.loadFixtures(fixtures, models, cb);
});
return loader;
});
if(Array.isArray(fixtures)){
loadFixtures(fixtures, models, cb);
} else if (typeof fixtures == 'string') {
readFiles(fixtures, options, function(err, fixtures){
if(err) cb(err);
else {
loadFixtures(fixtures, models, cb);
}
});
}
};
exports.loadFiles = wrap(function (filenames, models, options, cb) {
var loader = new Loader(options), reader = new Reader(options);
reader.readFiles(filenames, function(fixtures){
loader.loadFixtures(fixtures, models, cb);
});
return loader;
});
{
"name": "sequelize-fixtures",
"version": "0.0.1",
"version": "0.1.0",
"description": "sequelize fixture loader",

@@ -17,7 +17,8 @@ "main": "index.js",

"dependencies": {
"js-yaml": "~2.1.0",
"glob": "~3.2.1"
"js-yaml": "~2.1.0",
"glob": "~3.2.1",
"q": "~0.9.6"
},
"devDependencies": {
"sequelize-sqlite" :"*",
"sequelize-sqlite": "*",
"mocha": "*",

@@ -24,0 +25,0 @@ "should": "*"

@@ -10,11 +10,11 @@ Sequelize fixtures

# Install
### Install
npm install sequelize-fixtures
# Test
### Test
npm test
# Usage
### Usage

@@ -28,11 +28,21 @@ ```javascript

//from file
sequelize_fixtures.load('fixtures/test_data.json', models, function(err){
sequelize_fixtures.loadFile('fixtures/test_data.json', models, function(){
doStuffAfterLoad();
});
//can use glob syntax to load multiple files
sequelize_fixtures.load('fixtures/*.json', models, function(err){
//can use glob syntax to select multiple files
sequelize_fixtures.loadFile('fixtures/*.json', models, function(){
doStuffAfterLoad();
});
//array of files
sequelize_fixtures.loadFiles(['fixtures/users.json', 'fixtures/data*.json'], models, function(){
doStuffAfterLoad();
};
//specify file encoding (default utf8)
sequelize_fixtures.loadFile('fixtures/*.json', models, { encoding: 'windows-1257'}, function(){
doStuffAfterLoad();
});
//from array

@@ -55,3 +65,3 @@ var fixtures = [

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

@@ -61,5 +71,5 @@ });

# File formats
### File formats
## json
#### json

@@ -85,3 +95,3 @@ ```json

## yaml
#### yaml

@@ -100,2 +110,31 @@ ```yaml

### Natural Keys
To not have to specify id field when describing associated records, you can use 'natural keys'. Or in the context of sequelize, essentially a 'where' clause to be used to retrieve the association via AssociatedModel.find :)
Only BelongsTo is supported for the moment.
Assuming `Bar.belongsTo(Foo)`:
```json
[
{
model: 'Foo',
data: {
uniqueProp: 'FOO1',
uniqueProp2: 1,
propA: 'baz'
}
},
{
model: 'Bar',
data: {
propA: 'something',
foos: {
uniqueProp: 'FOO1',
uniqueProp2: 1
}
}
}
]
```
# grunt task

@@ -113,9 +152,12 @@

test_data2: {
files: 'fixtures/data*.json', //glob path
file: 'fixtures/data*.json', //one file
models: '../models' //string will be require()'d when task is run
},
test_data3: {
files: 'fixtures/*',
file: 'fixtures/*',
models: function () { //function will be evaluated for models object
return require('./models');
},
options: { //specify encoding
encoding: 'windows-1257'
}

@@ -131,3 +173,2 @@ }

Dump data into fixtures
Natural keys for associations
Utility for dumpiong data into fixtures

@@ -8,23 +8,20 @@ var sf = require('../index');

done = this.async(),
options = data.options || {};
options = data.options || {},
loader;
if(typeof models == 'string') models = require(models);
else if(models.call) models = models();
if(Array.isArray(data.files)){
var ff = files.slice(0);
function proc(){
if(!ff.length) done();
else {
sf.load(ff.shift(), options, function(err){
if(err) throw err;
else proc();
});
}
}
var callback = function () {
console.log(loader.saved+' fixtures loaded.');
done();
};
if(Array.isArray(data.from)){
loader = sf.loadFiles(data.from, models, options, callback);
} else if (data.from) {
loader = sf.loadFile(data.from, models, options, callback);
} else {
sf.load(data.files, models, options, function (err) {
if(err) throw err;
else done();
});
throw new error('missing "from" argument');
}
});
};

@@ -14,2 +14,6 @@ var Sequelize = require('sequelize-sqlite').sequelize,

exports.all.push(mod);
});
});
(function (m) {
m.Foo.belongsTo(m.Bar);
})(exports);

@@ -22,5 +22,4 @@ var sf = require('../index'),

describe('fixtures', function(){
it('should save fixture without id', function(done){
sf.load([FOO_FIXTURE], models, function (err){
should.not.exist(err);
it('should load fixture without id', function(done){
sf.loadFixture(FOO_FIXTURE, models, function (){
models.Foo.find({

@@ -40,4 +39,4 @@ where: {

it('should save fixture with id', function(done){
sf.load([{
it('should load fixture with id', function(done){
sf.loadFixture({
model: 'Foo',

@@ -49,3 +48,3 @@ data: {

}
}], models, function (err){
}, models, function (err){
should.not.exist(err);

@@ -62,5 +61,4 @@ models.Foo.find(3).success(function(foo){

it('should not duplicate fixtures', function (done){
sf.load([FOO_FIXTURE], models, function (err){
should.not.exist(err);
sf.load([FOO_FIXTURE], models, function (err){
sf.loadFixture(FOO_FIXTURE, models, function (){
sf.loadFixture(FOO_FIXTURE, models, function (){
models.Foo.count({

@@ -78,4 +76,4 @@ where: {

it('should save multiple fixtures', function(done) {
sf.load([FOO_FIXTURE, {
it('should load multiple fixtures', function(done) {
sf.loadFixtures([FOO_FIXTURE, {
model: 'Foo',

@@ -95,5 +93,4 @@ data: {

it('should save fixtures from json', function(done){
sf.load('tests/fixtures/fixture1.json', models, function(err){
should.not.exist(err);
it('should load fixtures from json', function(done){
sf.loadFile('tests/fixtures/fixture1.json', models, function(){
models.Foo.count().success(function(c){

@@ -109,5 +106,5 @@ c.should.equal(2);

it('should save fixtures from multiple files via glob', function(done){
sf.load('tests/fixtures/*.json', models, function(err){
should.not.exist(err);
it('should load fixtures from multiple files via glob', function(done){
sf.loadFile('tests/fixtures/fixture*.json', models, function(){
should.not.exist();
models.Foo.count().success(function(c){

@@ -123,5 +120,17 @@ c.should.equal(3);

it('should load fixtures from multiple files', function(done){
sf.loadFiles(['tests/fixtures/fixture1.json', 'tests/fixtures/fixture2.json'], models, function(){
should.not.exist();
models.Foo.count().success(function(c){
c.should.equal(3);
models.Bar.count().success(function(c){
c.should.equal(1);
done();
});
});
});
});
it('should load yaml fixtures', function(done){
sf.load('tests/fixtures/fixture3.yaml', models, function(err){
should.not.exist(err);
sf.loadFile('tests/fixtures/fixture3.yaml', models, function(){
models.Foo.count().success(function(c){

@@ -136,2 +145,15 @@ c.should.equal(1);

});
it('should load assosication with. natural keys', function(done){
sf.loadFile('tests/fixtures/natkeys.yaml', models, function(){
models.Foo.findAll().success(function(foos){
foos.length.should.equal(1);
foos[0].getBar().success(function(bar){
bar.propA.should.equal('baz');
bar.propB.should.equal(1);
done();
});
});
});
});
});
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