1.0.1 • Published 7 years ago

seed-generator v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
7 years ago

Seed Generator

Seed Generator is a NPM Package that uses faker to generate a json schema for database seeding.

Methods

  • build: Returns seed data as a json schema.
  • relation: Returns an array of sequential numbers that represent indexes for many-to-many relations.

Build

  • name: Match to name of table row.
  • seed: Returns faker generated data.
    • namespace: The namespace of the faker method, ex. faker.NAMESPACE.method(vars).
    • method: The faker method, ex. faker.namespace.METHOD(vars).
    • vars: The faker variables, ex. faker.namespace.method(VARS).
  • unique: Appends a random word to the seed in an effort to maintain a unique string.

Available faker methods can be found here.

Build Input

const buildInput = [
  {
    name: 'firstName',
    seed: { namespace: 'name', method: 'firstName' }
  },
  {
    name: 'uniqueValue',
    seed: { namespace: 'company', method: 'companyName' },
    unique: true
  },
  {
    name: 'valueWithVars',
    seed: { namespace: 'random', method: 'number', vars: { min: 1, max: 5 } }
  }
];

const numberOfResults = 2;

Build Execute

  const buildOutput = seeder.build(buildInput, numberOfResults);

Build Output

[
  {
    firstName: 'Casandra',
    uniqueValue: 'Carroll Inc Gloves',
    valueWithVars: '4',
  },
  {
    firstName: 'Ruben',
    uniqueValue: 'Keebler and Sons Gold',
    valueWithVars: '3',
  }
]

Relation

  • start: The starting number (relation index).
  • limit: The length of each relation array.
  • max: The maximum index number, index moves to start when max is hit.

Relation Execute

  const numberOfResults = 2;
  const relationOutput = seeder.relation({ start: 1, limit: 5, max: 7 }, numberOfResults);

Relation Output

  [
    [ 1, 2, 3, 4, 5 ],
    [ 6, 7, 1, 2, 3 ]
  ]

Example

This example creats posts and post-tag relations using sequelize, an ORM for SQL servers. Sequelize v.4 does not allow bulk create for relations, so looping was required.

  const posts = seeder.build(seed.post.data, seed.post.count);
  const postTags = seeder.relation({ start: 1, limit: 2, max: seed.tag.count }, seed.post.count);
  let promises = [];

  for (let i=0; i<posts.length; i++) {
    promises[i] = db.Post.create(posts[i])
      .then((post) => {
        return post.addTags(postTags[i]);
      });
  }
  return Promise.all(promises);
1.0.1

7 years ago

1.0.0

7 years ago