0.2.2 • Published 1 year ago

@dashdot/factory v0.2.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

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)
0.2.1

1 year ago

0.2.2

1 year ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago