objection-foundry

A model factory library for Objection.js

Usage no npm install needed!

<script type="module">
  import objectionFoundry from 'https://cdn.skypack.dev/objection-foundry';
</script>

README

objection-foundry

npm version

A model factory library for Objection.js.

Installation

Install the npm package:

npm i objection-foundry

Quick Start

This library provides a Factory class mixin which you must use to extend Objection.js' Model class, adding support to model factories.

const { Model } = require('objection');
const Factory = require('objection-foundry');

class User extends Factory(Model) {
    // ...

You must also define the factory schema which is used as blueprint when generating fake data. The factory schema is stored in the factorySchema static attribute.

The attribute is a plain old JavaScript object. Each key represents a column, and each value is either a Faker.js method or a literal value (e.g. 2 or "john.doe@example.com").

class User extends Factory(Model) {
    static get factorySchema() {
        return {
            first_name: this.faker.name.firstName,
            last_name: this.faker.name.lastName,
            email: this.faker.internet.email,
            created_at: this.faker.datatype.datetime,
            updated_at: this.faker.datatype.datetime,
        };
    }

    // ...

You can now start generating fake data.

const user = await User.create();
const users = await User.count(5);

The create method returns a plain old JavaScript object, while the count method returns a collection of objects. Both methods allow you to override attributes.

const user = await User.create({ firstName: 'John' });
const users = await User.count(5, {
    $transform: (record, idx) => {
        record.firstName = idx % 2 ? 'John' : 'Ivy';
        return record;
    },
});

You can also write data to the database by binding a Knex client to the model. If you write to the database, the create and count methods will return instances of the model (e.g. User) instead of plain old JavaScript objects.

User.knex(db);
const user = await User.create(); // User { id: 1, ... }

You can use the destroy instance method to remove a record.

const user = await User.create();
await user.destroy(); //=> true

Relations

This library only supports belongs to and has many relations. Suppose you have two models: User and Post, which you linked together through the following relation mappings:

Model Relation name From To Type
Post user posts.user_id users.id Belongs to
User posts users.id posts.user_id Has many

Then you can build a relation by passing a special attribute—which starts with either $has or $for—to the create and count methods. These special attributes build has many and belongs to relations, respectively.

Attributes starting with $has accept an integer which represents the amount of records to create. Attributes starting with $for accept literal values (e.g. 2) or model instances.

const user = await User.create({
    $hasPosts: 3,
});

const post = await Post.create({ $forUser: 3 }); // Post { userId: 3, ... }
const posts = await Post.count(3, { $forUser: user });

License

objection-foundry is released under the MIT License.