Skip to content
/ factory Public

A simple factory implementation for any persistence.

License

Notifications You must be signed in to change notification settings

wappla/factory

Repository files navigation

Dashdot Factory

build codecov

A simple factory implementation for any persistence.

Usage

Inside your Node project directory, run the following:

npm i --save @dashdot/factory

Or with Yarn:

yarn add @dashdot/factory

API

import faker from 'faker'
import Factory from '@dashdot/factory'
import { Post, User } from './models'

class MongoFactory extends Factory {
    static async persist(records) {
        const docs = await this.Model.insertMany(records)
        return docs.map((doc) => doc.toObject({
            virtuals: true,
            getters: true
        }))
    }
}

class PostFactory extends MongoFactory {
    static get Model() {
        return Post
    }

    static make() {
        return {
            title: faker.name.title(),
            content: faker.lorem.paragraphs(),
        }
    }
}

class UserFactory extends MongoFactory {
    static get Model() {
        return User
    }

    static make() {
        return {
            name: faker.name.firstName(),
            email: faker.unique(faker.internet.email),
        }
    }

    createPosts(records, states, data) {
        return PostFactory.create(records, states, {
            ...data,
            user: this.id,
        })
    }
}

const [user] = await UserFactory.create(1)
const posts = await user.createPosts(4)