npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bluesh55/factory

v1.4.5

Published

Factory is the library for creating data from pre-configured attribute specs.

Downloads

38

Readme

Factory

NPM version

Factory is the library for creating data from pre-configured model definition. It is good to use for testing purposes.

This library is inspired by FactoryBot.

Installation

Node.js:

npm install --save @bluesh55/factory

Getting Started

Create configuration file

First of all, you must run init command for creating a configuration file.

npx factory init

This command will create .factoryrc.json file at the directory where you run the command.

// .factoryrc.json

{
  "modelsDir": "./mytest/factories/",
  "loaderDir": "./tests/helpers"
}
Create loader file

And then, you have to run init:loader command for creating loader file that loads all models at runtime. If you want to writing model using typescript, you can use --typescript option.

# Create loader file (default: Javascript)
npx factory init:loader

# Create loader file (Typescript)
npx factory init:loader --typescript
Create test model

Create your test model at the directory you configured above.

// `./mytest/factories/person.js`
const { Person } = require('../../db/models') // use Sequelize

module.exports = {
  name: 'person',
  specification: {
    id: {
      type: Number,
    },
    name: {
      type: String,
      defaultValue: async seq => {
        return `name{seq}`
      },
    },
    tel: {
      type: String,
      defaultValue: '010-1234-4321',
    },
  },
  creator: attributes => {
    return Person.create(attributes)
  },
}
Use factory at your test file
// example of jest

const { factory } = require('@bluesh55/factory')

describe('Person', () => {
  beforeEach(async done => {
    await factory.create('person')
    await factory.create('person', { name: 'Andrew' })
    /*
       This will create the person in the database like this

       {
          id: 1,
          name: 'name0',
          tel: '010-1234-4321'
       },
       {
          id: 2,
          name: 'Andrew',
          tel: '010-1234-4321'
       }
     */
  })

  it('the test case what you want', done => {
    // ...
  })
})

Configuration

// .factoryrc.json

{
  "modelsDir": "./mytest/factories/",
  "loaderDir": "./tests/helpers"
}
  • modelsDir: The directory path that includes all test models.
  • loaderDir: The directory path that will be created loader file when you run the command npx factory init:loader

Test model

The test model is a definition of what you want to build and how to create to the database(or whatever).
It has some options which be able to create data dynamically from static definition.

name

The test model's name for creating.

// Define mymodel

module.exports = {
  name: 'mymodelname',
  specification: {
    ...
  },
  creator: async (attributes) => {
    ...
  }
}
// Create mymodel

factory.create('mymodelname')

specification

specification is a complicated option that has some sub-options. See below example.

const { encryptPassword } = require('../utils/crypto'); // use custom crypto util to encrypt password

module.exports = {
  name: 'user',
  specification: {
    id: {
      type: Number,
    },
    email: {
      type: String,
      defaultValue: async (seq) => {
        return `email${seq}@gmail.com`
      }
    },
    password: {
      type: String,
      attributeName: 'encrypted_password',
      transform: async (password) => {
        const encryptedPassword = await encryptPassword(password);
        return encryptedPassword;
      },
      defaultValue: 'password',
    },
    username: {
      type: String,
      defaultValue: async (seq) => {
        return `username${seq}`
      }
    },
    tel: {
      type: String,
      defaultValue: '010-1234-4321'
    },
    isAdmin: {
      type: Boolean,
      defaultValue: false,
      attributeName: 'user_type',
      transform: (value) => {
        const newValue = value ? 1: 0;
        return newValue;
      }
    }
  },
  creator: async (attributes) => {
    ...
  }
}
  • type: The type of data. There is nothing using this option yet. Just specification.
  • defaultValue: This is used as a default value if you don't specify attribute when call create method.
    If function or async function is given, defaultValue would be the value of the function returns and get the seq parameter. The seq parameter is sequential number start from 0. This is good to use for uniqueness data.
  • attributeName: This is used if the name of attribute is different between specification key and database column name.
  • transform: In some cases, maybe you want to transform value to something different. In the above example, the password attribute has the transform option to encrypt input value. It can be function or async function that gets old value as parameter. It have to return the transformed data.

creator

The Factory delegates the data creation part to you. You can use Sequelize, Mongoose, whatever to save the data in database.
What you have to do is just defining creator function. If you define creator function, the Factory uses that when you call create method.
The creator function gets an attributes parameter that is derived from specification.

// Define creator function uses sequelize

const { MyModel } = require('../../db/models'); // require Sequelize instance

module.exports = {
  name: 'mymodel',
  specification: {
    ...
  },
  creator: async (attributes) => {
    return await MyModel.create(attributes);
  }
}

And it must return created data if you want to use that in the test.

// And use in the test
describe('...', () => {
  let mymodel;

  beforeEach(async (done) => {
    mymodel = await factory.create('mymodel');
    done();
  });

  ...
})

License

Factory is licensed under the MIT License.