0.0.7 • Published 5 years ago

knex-factory v0.0.7

Weekly downloads
27
License
ISC
Repository
github
Last release
5 years ago

knex-factory

Install

npm i -D knex-factory

Initialize

Require the package with knex

const knex = require('knex');
knex({ ... });

require('knex-factory')(knex);

factory.define(factoryName, tableName, validParams);

When you define factories, you can use functions to generate dynamic data or return a promise;

Define a user factory:

const factory = require('knex-factory');

factory.define('user', 'users', {
  level: 'member',
  username: 'testUser',
  dynamicField() {
    return new Date();
  },
});

Define an avatar factory with user:

const factory = require('knex-factory');

factory.define('avatar', 'avatars', {
  async userId() {
    return await factory.create('user');
  },
  fileName: 'file.jpg',
  path: 'file.jpg',
});

...

factory.build(factoryName, customData)

Build method will not save record's itself but associations.

const avatar = await build('avatar', { fileName: 'no_avatar.jpg' });

expect(avatar.id).to.be(null);
expect(avatar.userId).not.to.be(null);

factory.create(factoryName, customData)

Works like build method expect it will also create a persisted record of the factory instance.

const user = await create('user', { level: 'admin' });

expect(user.id).not.to.eq(null);
expect(user.level).to.eq('admin');
0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago