Coder Social home page Coder Social logo

objection-foundry's Introduction

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 "[email protected]").

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.

objection-foundry's People

Contributors

dependabot[bot] avatar domcorvasce avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.