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.
Features
- Generate plain javascript objects on demand
- Generate objects with nested relations
- Typeorm Support - Generate Entities and Persist Entities and nested relations
Installation
npm install --save @entity-factory/core
Example
Entity blueprints can also be defined within classes to enable better code separation.
import { EntityFactory, ObjectBlueprint } from '@entity-factory/core';
class UserBlueprint extends ObjectBlueprint {
constructor() {
super();
this.type('user');
this.define(async faker => {
return {
username: faker.internet.userName(),
email: faker.internet.email(),
active: false,
};
});
this.state('active', async faker => {
return {
active: true,
};
});
this.afterMaking(async (user, { faker, factory, adapter }) => {
});
this.afterMakingState(
'active',
async (user, { faker, factory, adapter }) => {
},
);
this.afterCreating(async (user, { faker, factory, adapter }) => {
});
this.afterCreatingState(
'active',
async (user, { faker, factory, adapter }) => {
},
);
}
}
export const entityFactory = new EntityFactory({
blueprints: [UserBlueprint],
});
entityFactory
.for('user')
.state('active')
.create(3)
.then(users => console.log(users));
Alternate Inline Blueprint Declaration
import { EntityFactory, ObjectBlueprint } from 'entity-factory';
interface User {
id: number;
username: string;
email: string;
active: boolean;
}
export const entityFactory = new EntityFactory();
entityFactory.register((profile: ObjectBlueprint<User>) => {
profile.type('user');
profile.define(async faker => {
return {
username: faker.internet.userName(),
email: faker.internet.email(),
active: faker.random.boolean(),
};
});
});
entityFactory
.for<User>('user')
.create(3)
.then(users => console.log(users));
Additional Samples
TODO