项目作者: jcloutz

项目描述 :
Generate entities on demand for the browser or server.
高级语言: TypeScript
项目地址: git://github.com/jcloutz/entity-factory.git
创建时间: 2019-02-23T14:12:57Z
项目社区:https://github.com/jcloutz/entity-factory

开源协议:MIT License

下载


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.

Documentation

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

  1. npm install --save @entity-factory/core

Quick Guide

Try it on Runkit

  1. const { EntityFactory, ObjectBlueprint } = require('@entity-factory/core');
  2. class UserBlueprint extends ObjectBlueprint {
  3. constructor() {
  4. super();
  5. this.type('user');
  6. this.define(async ({ faker, factory }) => ({
  7. username: faker.internet.userName(),
  8. email: faker.internet.email(),
  9. active: faker.random.boolean(),
  10. }));
  11. // define a state transform to return a user with embedded posts
  12. this.state('with-posts', async ({ faker, factory }) => ({
  13. posts: await factory.for('post').create(2),
  14. }));
  15. }
  16. }
  17. class PostBlueprint extends ObjectBlueprint {
  18. constructor() {
  19. super();
  20. this.type('post');
  21. this.define(async ({ faker, factory }) => ({
  22. title: faker.company.bsBuzz(),
  23. body: faker.lorem.sentences(2),
  24. }));
  25. }
  26. }
  27. export const entityFactory = new EntityFactory({
  28. blueprints: [UserBlueprint, PostBlueprint],
  29. });
  30. // Generate entities
  31. entityFactory
  32. .for('user') // get builder instance for 'user'
  33. .state('with-posts') // get users with posts
  34. .create(3) // generate 3 users with incrementing id's
  35. .then(users => console.log(users));

It is also possible to generate random data using the gen method available on
EntityFactory

  1. await entityFactory.gen(3, async ({ faker, factory }) => {
  2. return {
  3. name: faker.company.bsBuzz(),
  4. active: faker.random.boolean(),
  5. };
  6. });

Additional Samples

TODO

  • imporove docs
  • resolve nested objects
  • resolve nested array
  • allow guid id’s for object adapter
  • add method to generate random objects on the fly without a blueprint
    1. definition