@entity-factory/core
v0.2.2
Published
Create entities on the fly for mocking and testing
Downloads
42
Maintainers
Readme
Entity Factory
Entity Factory is a library used for quickly creating fixture data from plain objects or classes using faker. Inspired by laravel's factories for generating test data. Currently the library supports plain JS objects and Typeorm entities.
Table of Contents
Features
- Generate plain javascript objects on demand
- Generate objects with nested relations
- Typeorm Support - Generate and Persist Entities and nested relations
Installation
npm install --save @entity-factory/core
Quick Guide
Try it on Runkit
const { EntityFactory, ObjectBlueprint } = require('@entity-factory/core');
class UserBlueprint extends ObjectBlueprint {
constructor() {
super();
this.type('user');
this.define(async ({ faker, factory }) => ({
username: faker.internet.userName(),
email: faker.internet.email(),
active: faker.random.boolean(),
}));
// define a state transform to return a user with embedded posts
this.state('with-posts', async ({ faker, factory }) => ({
posts: await factory.for('post').create(2),
}));
}
}
class PostBlueprint extends ObjectBlueprint {
constructor() {
super();
this.type('post');
this.define(async ({ faker, factory }) => ({
title: faker.company.bsBuzz(),
body: faker.lorem.sentences(2),
}));
}
}
export const entityFactory = new EntityFactory({
blueprints: [UserBlueprint, PostBlueprint],
});
// Generate entities
entityFactory
.for('user') // get builder instance for 'user'
.state('with-posts') // get users with posts
.create(3) // generate 3 users with incrementing id's
.then(users => console.log(users));
It is also possible to generate random data using the gen
method available on
EntityFactory
await entityFactory.gen(3, async ({ faker, factory }) => {
return {
name: faker.company.bsBuzz(),
active: faker.random.boolean(),
};
});
TODO
- [ ] Add Mongoose adapter
- [ ] Add Sequelize adapter
- [x] improve docs
- [x] resolve nested objects
- [x] resolve nested array
- [x] allow guid id's for object adapter
- [x] add method to generate random objects on the fly without a blueprint definition