function BookshelfAdapter() { _classCallCheck(this, BookshelfAdapter); return _possibleConstructorReturn(this, _Object$getPrototypeOf(BookshelfAdapter).apply(this, arguments)); }
n/a
function DefaultAdapter() { _classCallCheck(this, DefaultAdapter); }
n/a
function MongooseAdapter() { _classCallCheck(this, MongooseAdapter); return _possibleConstructorReturn(this, _Object$getPrototypeOf(MongooseAdapter).apply(this, arguments)); }
...
## Adapters
Adapters provide support for different databases and ORMs. Adapters can be registered for
specific models, or as the 'default adapter', which is used for any models for which an
adapter has not been specified. See the adapter docs for usage, but typical usage is:
```javascript
const adapter = new factory.MongooseAdapter();
// use the mongoose adapter as the default adapter
factory.setAdapter(adapter);
// Or use it only for one model-factory
factory.setAdapter(adapter, 'factory-name');
```
...
function ObjectAdapter() { _classCallCheck(this, ObjectAdapter); return _possibleConstructorReturn(this, _Object$getPrototypeOf(ObjectAdapter).apply(this, arguments)); }
...
### ObjectAdapter
`ObjectAdapter` is a simple adapter that uses `const model = new MyModel()`,
`model.save()` and `model.destroy()`.
```js
factory.setAdapter(new factory.ObjectAdapter());
class MyModel {
save() {
// save the model
},
destroy() {
// destroy the model
}
...
function ReduxORMAdapter(session) { _classCallCheck(this, ReduxORMAdapter); var _this = _possibleConstructorReturn(this, _Object$getPrototypeOf(ReduxORMAdapter).call(this)); _this.session = session; return _this; }
n/a
function SequelizeAdapter() { _classCallCheck(this, SequelizeAdapter); return _possibleConstructorReturn(this, _Object$getPrototypeOf(SequelizeAdapter).apply(this, arguments)); }
n/a
function BookshelfAdapter() { (0, _classCallCheck3.default)(this, BookshelfAdapter); return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(BookshelfAdapter).apply(this, arguments)); }
n/a
function DefaultAdapter() { (0, _classCallCheck3.default)(this, DefaultAdapter); }
n/a
function MongooseAdapter() { (0, _classCallCheck3.default)(this, MongooseAdapter); return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(MongooseAdapter).apply(this, arguments)); }
...
## Adapters
Adapters provide support for different databases and ORMs. Adapters can be registered for
specific models, or as the 'default adapter', which is used for any models for which an
adapter has not been specified. See the adapter docs for usage, but typical usage is:
```javascript
const adapter = new factory.MongooseAdapter();
// use the mongoose adapter as the default adapter
factory.setAdapter(adapter);
// Or use it only for one model-factory
factory.setAdapter(adapter, 'factory-name');
```
...
function ObjectAdapter() { (0, _classCallCheck3.default)(this, ObjectAdapter); return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(ObjectAdapter).apply(this, arguments)); }
...
### ObjectAdapter
`ObjectAdapter` is a simple adapter that uses `const model = new MyModel()`,
`model.save()` and `model.destroy()`.
```js
factory.setAdapter(new factory.ObjectAdapter());
class MyModel {
save() {
// save the model
},
destroy() {
// destroy the model
}
...
function ReduxORMAdapter(session) { (0, _classCallCheck3.default)(this, ReduxORMAdapter); var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(ReduxORMAdapter).call(this)); _this.session = session; return _this; }
n/a
function SequelizeAdapter() { (0, _classCallCheck3.default)(this, SequelizeAdapter); return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(SequelizeAdapter).apply(this, arguments)); }
n/a
function FactoryGirl() { var _this = this; var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, FactoryGirl); this.factories = {}; this.options = {}; this.adapters = {}; this.created = new _Set(); this.assoc = generatorThunk(this, Assoc); this.assocMany = generatorThunk(this, AssocMany); this.assocBuild = deprecate('assocBuild', 'assocAttrs'); this.assocBuildMany = deprecate('assocBuildMany', 'assocAttrsMany'); this.assocAttrs = generatorThunk(this, AssocAttrs); this.assocAttrsMany = generatorThunk(this, AssocAttrsMany); this.seq = this.sequence = function () { return generatorThunk(_this, Sequence).apply(undefined, arguments); }; this.resetSeq = this.resetSequence = function (id) { Sequence.reset(id); }; this.chance = generatorThunk(this, ChanceGenerator); this.oneOf = generatorThunk(this, OneOf); this.defaultAdapter = new DefaultAdapter(); this.options = options; }
...
```
## Creating new Factories
You can create multiple factories which have different settings:
```javascript
let anotherFactory = new factory.FactoryGirl();
anotherFactory.setAdapter(new MongooseAdapter()); // use the Mongoose adapter
```
## History
This module started out as a fork of
[factory-lady](https://github.com/petejkim/factory-lady), but the fork deviated quite a
...
assoc = function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return function () { return generator.generate.apply(generator, args); }; }
...
// seq is an alias for sequence
email: factory.seq('User.email', (n) => `user${n}@ymail.com`),
// use the chance(http://chancejs.com/) library to generate real-life like data
about: factory.chance('sentence'),
// use assoc to associate with other models
profileImage: factory.assoc('profile_image', '_id'),
// or assocMany to associate multiple models
addresses: factory.assocMany('address', 2, '_id'),
// use assocAttrs to embed models that are not persisted
creditCardNumber: factory.assocAttrs('credit_card', 'number', {type: 'masterCard'}),
...
assocAttrs = function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return function () { return generator.generate.apply(generator, args); }; }
...
// use assoc to associate with other models
profileImage: factory.assoc('profile_image', '_id'),
// or assocMany to associate multiple models
addresses: factory.assocMany('address', 2, '_id'),
// use assocAttrs to embed models that are not persisted
creditCardNumber: factory.assocAttrs('credit_card', 'number', {
type: 'masterCard'}),
// use assocAttrs or assocAttrsMany to embed plain json objects
twitterDetails: factory.assocAttrs('twitter_details'),
});
```
```javascript
...
assocAttrsMany = function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return function () { return generator.generate.apply(generator, args); }; }
n/a
assocBuild = function () { throw new Error('The ' + method + ' method has been deprecated, use ' + see + ' instead'); }
n/a
assocBuildMany = function () { throw new Error('The ' + method + ' method has been deprecated, use ' + see + ' instead'); }
n/a
assocMany = function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return function () { return generator.generate.apply(generator, args); }; }
...
// use the chance(http://chancejs.com/) library to generate real-life like data
about: factory.chance('sentence'),
// use assoc to associate with other models
profileImage: factory.assoc('profile_image', '_id'),
// or assocMany to associate multiple models
addresses: factory.assocMany('address', 2, '_id'),
// use assocAttrs to embed models that are not persisted
creditCardNumber: factory.assocAttrs('credit_card', 'number', {type: 'masterCard'}),
// use assocAttrs or assocAttrsMany to embed plain json objects
twitterDetails: factory.assocAttrs('twitter_details'),
});
...
chance = function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return function () { return generator.generate.apply(generator, args); }; }
...
asyncData: () => fetch('some/resource'),
});
factory.define('user', User, {
// seq is an alias for sequence
email: factory.seq('User.email', (n) => `user${n}@ymail.com`),
// use the chance(http://chancejs.com/) library to generate real-life like data
about: factory.chance('sentence'),
// use assoc to associate with other models
profileImage: factory.assoc('profile_image', '_id'),
// or assocMany to associate multiple models
addresses: factory.assocMany('address', 2, '_id'),
...
oneOf = function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return function () { return generator.generate.apply(generator, args); }; }
n/a
resetSeq = function (id) { Sequence.reset(id); }
...
var adapter = _ref7[0];
var model = _ref7[1];
return prev.then(function () {
return adapter.destroy(model, model.constructor);
});
}, _Promise.resolve());
this.created.clear();
this.resetSeq();
return promise;
}
}, {
key: 'setAdapter',
value: function setAdapter(adapter) {
var _this6 = this;
...
resetSequence = function (id) { Sequence.reset(id); }
n/a
seq = function () { return generatorThunk(_this, Sequence).apply(undefined, arguments); }
...
// use functions to compute some complex value
launchDate: () => new Date(),
// return a promise to populate data asynchronously
asyncData: () => fetch('some/resource'),
});
factory.define('user', User, {
// seq is an alias for sequence
email: factory.seq('User.email', (n) => `user${n}@ymail.com`),
// use the chance(http://chancejs.com/) library to generate real-life like data
about: factory.chance('sentence'),
// use assoc to associate with other models
profileImage: factory.assoc('profile_image', '_id'),
...
sequence = function () { return generatorThunk(_this, Sequence).apply(undefined, arguments); }
...
For example:
```javascript
// Using objects as initializer
factory.define('product', Product, {
// use sequences to generate values sequentially
id: factory.sequence('Product.id', (n) => `product_${n}`),
// use functions to compute some complex value
launchDate: () => new Date(),
// return a promise to populate data asynchronously
asyncData: () => fetch('some/resource'),
});
factory.define('user', User, {
// seq is an alias for sequence
...