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

sequelize-bakery

v0.0.3

Published

A sequelize test model builder inspired by Django's model-bakery

Readme

sequelize-bakery

test CI

A test model builder inspired by Django's model-bakery. Allows you to focus your test setup by only specifying fields relevant to your test, and letting the bakery build out everything else.

Problem

You have a deep hierarchy of models, and test setups are tedious because of it.

User.belongsTo(Cohort);
Account.belongsTo(User);
Wallet.belongsTo(Account);

test('should be muddied by needless setup', async () => {
	const cohort = await Cohort.create({ ... }); // irrelevant
	const user = await User.create({ ..., cohort }); // irrelevant
	const account = await Account.create({ ..., user }); // irrelevant
	const wallet = await Wallet.create({ ..., account, balance: 500000 }); // we only care about the balance here

	// ... assertions ...
});

With sequelize-bakery, you can jump straight to target model creation.

const { build } = require('sequelize-bakery');

test('should be clear and to the point', async () => {
	const wallet = await build(Wallet);
	// ... assertions ...
}

If you want to specify any of the fields explicitly:

const wallet = await build(Wallet, { balance: 5000000 } });

If you want to go deep with your specifications:

const wallet = await build(Wallet, { Account: { User: { username: 'overriden' } } });

If you want to explicity supply an existing model, for example, to have two generated models belong to the same parent:

const wallet1 = await build(Wallet);
const wallet2 = await build(Wallet, { Account: wallet1.account });

Nullable fields and default values

Nullable fields will be not be generated, unless they have a defaultValue set, or you specify the fillOptional as true in the third parameter to the build call.

const user = build(User, {}, { fillOptional: true });

In case you want to allow bakery to generate some fields, but not others, you can supply an array of allowed fields instead.

const user = build(User, {}, { fillOptional: ['dateOfBirth'] });

Destroying built models in test teardowns

sequelize-bakery tracks all instances it creates. Calling destroyAllBuilt in test teardowns will automatically destroy all instances built by Sequelize.

const { destroyAllBuilt } = require('sequelize-bakery');

afterEach(async () => {
    await destroyAllBuilt();
});

Overriding generators

In case your models require specialized conversion, you can override the underlying generators. The first parameter is is the type of your column in underlying SQL dialect. (e.g. VARCHAR)

const { overrideGenerator } = require('sequlize-bakery');

overrideGenerator('DECIMAL', () => new CustomBigNumber(faker.datatype.number().toString()));

In addition, if your column has sequelize validation (not constraint!), you can use the same override with the first parameter being the sequelize validator.

overrideGenerator('isEmal', () => `${faker.name.firstName()}@yahoo.com`);

If a column has sequelize validation, the specialized generators take precedence. Some are built in, such as isEmail, isIPv4, etc.

Warning: overrideGenerator overrides both SQL typemap and validator typemap. This overlap behavior is not thoroughly tested and might cause some problems! Be suspect of this if you encounter odd issues while using overrideGenerator.

Limitations

Currently only creating BelongsTo relations, hasMany relations are not yet supported.