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

factory-girl

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

factory-girl - npm Package Compare versions

Comparing version 2.3.0 to 3.0.0

CHANGELOG.md

45

index.js

@@ -182,8 +182,20 @@ /* global window, define */

builder.build(name, attrs, function(err, doc) {
builder._build(name, attrs, function(err, doc, resultingAttrs) {
if (err) return callback(err);
save(name, doc, callback);
save(name, doc, resultingAttrs, callback);
});
};
// Shrink last parameter from callback
// for more comfortable work with promises
function wrapCallback(callback) {
if (callback) {
var oldCallback = callback;
return function(error, doc, attrs) {
return oldCallback(error, doc);
}
}
}
builder.build = function(name, attrs, callback) {

@@ -194,3 +206,6 @@ if (typeof attrs === 'function') {

}
return this._build(name, attrs, wrapCallback(callback));
};
builder._build = function(name, attrs, callback) {
if (!factories[name]) {

@@ -227,5 +242,5 @@ return callback(new Error("No factory defined for model '" + name + "'"));

factories[name].options.afterBuild.call(
this, doc, builder.options, callback);
builder, doc, attrs, callback);
} else {
callback(null, doc);
callback(null, doc, attrs);
}

@@ -264,6 +279,6 @@ });

callback = args.callback;
args.callback = function(err, docs) {
args.callback = function(err, docs, resultingAttrsArray) {
if (err) return callback(err);
asyncForEach(docs, function(doc, cb, index) {
save(name, doc, function(err) {
save(name, doc, resultingAttrsArray[index], function(err) {
if (!err) results[index] = doc;

@@ -280,10 +295,14 @@ cb(err);

function buildMany(args) {
var results = [];
var results = [],
resultingAttrsArray = [];
asyncForEach(args.attrsArray, function(attrs, cb, index) {
builder.build(args.name, attrs, function(err, doc) {
if (!err) results[index] = doc;
builder._build(args.name, attrs, function(err, doc, resultingAttrs) {
if (!err) {
results[index] = doc;
resultingAttrsArray[index] = resultingAttrs;
}
cb(err);
});
}, function(err) {
args.callback(err, results);
args.callback(err, results, resultingAttrsArray);
});

@@ -319,7 +338,7 @@ }

num: num,
callback: callback
callback: wrapCallback(callback)
};
}
function save(name, doc, callback) {
function save(name, doc, resultingAttrs, callback) {
var model = factories[name].model;

@@ -330,3 +349,3 @@ factory.adapterFor(name).save(doc, model, function (err) {

if (factories[name].options.afterCreate) {
factories[name].options.afterCreate.call(this, doc, builder.options, callback);
factories[name].options.afterCreate.call(builder, doc, resultingAttrs, callback);
}

@@ -333,0 +352,0 @@ else {

@@ -6,3 +6,3 @@ {

"author": "Simon Wade",
"version": "2.3.0",
"version": "3.0.0",
"keywords": [

@@ -9,0 +9,0 @@ "factory",

@@ -75,10 +75,29 @@ # factory-girl

#### `afterBuild: function(instance, options, callback)`
#### `afterBuild: function(instance, attrs, callback)`
Provides a function that is called after the model is built.
#### `afterCreate: function(instance, options, callback)`
#### `afterCreate: function(instance, attrs, callback)`
Provides a function that is called after a new model instance is saved.
```javascript
factory.define('user', User, {
foo: 'bar'
}, {
afterCreate: function(instance, options, callback) {
generateBazBasedOnID(instance.id, function(error, generatedBaz) {
if(error) {
callback(error, null);
} else {
instance.baz = generatedBaz;
callback(null, instance);
}
});
}
});
```
Other builder options can be accessed, inside hooks, using `this.options`.
## Defining Associations

@@ -85,0 +104,0 @@

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