Registry / testing / orchid-orm-test-factory

orchid-orm-test-factory

JSON →
library1.0.130jsnpmunverified

Orchid ORM Test Factory is a companion library for Orchid ORM that simplifies creating mock data for tests. Version 1.0.130 is the current stable release, updated regularly alongside Orchid ORM. It uses @faker-js/faker for generating realistic fake data and provides a declarative API to define factories for database models. Unlike generic factories, it integrates directly with Orchid ORM's model layers, supporting associations, custom column types, and lifecycle hooks. The factory is type-safe, fully typed with TypeScript, and designed for seamless use in integration or unit tests.

npm install orchid-orm-test-factory
INSTALL
IMPORT
SIG · ORCHID-ORM-TEST-FA
O
orchid-orm-test-factory
testingjavascriptv1.0.130
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

factory
import { factory } from 'orchid-orm-test-factory'
import factory from 'orchid-orm-test-factory'
Named export, not default. TypeScript types are included.
defineFactory
import { defineFactory } from 'orchid-orm-test-factory'
import { DefineFactory } from 'orchid-orm-test-factory'
Case-sensitive: 'defineFactory' is lowercase d. Used to define a factory for a model.
FactoryOrCallback
import { FactoryOrCallback } from 'orchid-orm-test-factory'
Type import for passing factory or callback. Also import 'factory' type for the return type.

Shows how to define a factory for a User model, build an object without persisting, create a single record, create multiple records, and override attributes.

import { factory, defineFactory } from 'orchid-orm-test-factory'; import { db } from './db'; // your Orchid ORM db instance import { faker } from '@faker-js/faker'; // Define a factory for a User model const userFactory = defineFactory(db.user, (f) => ({ name: faker.person.fullName(), email: faker.internet.email(), age: faker.number.int({ min: 18, max: 99 }), })); // Build a user object without persisting const userData = await userFactory.build(); console.log(userData); // Create a user in the database const user = await userFactory.create(); console.log(user); // Create multiple users const users = await userFactory.createList(5); console.log(users); // Override attributes const customUser = await userFactory.create({ name: 'Alice' }); console.log(customUser);
Debug
Known issues
gotchaFactory functions must return a plain object, not an async function or promise. Async callbacks cause undefined attribute values.
fix
Ensure the factory callback returns a synchronous object. If async data is needed, precompute it outside the factory.
affects: >=1.0.0
gotchaThe 'defineFactory' function must be called with a model from your Orchid ORM db instance, not with a table name string or raw query builder.
fix
Use 'db.user' (where 'db' is your Orchid ORM instance) instead of a plain 'user' object.
affects: >=1.0.0
deprecatedUsing 'faker' directly inside factory callbacks may conflict with future versions if faker is removed as a dependency. The library wraps faker behind the scenes.
fix
Prefer using the 'f' parameter passed to the callback (currently undocumented).
affects: >=1.0.0
breakingIn version 0.x, factory was a default export. In 1.0.0, it changed to a named export.
fix
Change 'import orchidFactory from 'orchid-orm-test-factory'' to 'import { factory } from 'orchid-orm-test-factory''.
affects: 1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'orchid-orm-test-factory'
Package not installed or missing peer dependency '@faker-js/faker'.
fix
Run 'npm install orchid-orm-test-factory @faker-js/faker'.
TypeError: db.user is not a function
Passing a non-model object; 'db.user' must be an Orchid ORM model, not a raw table or query builder.
fix
Ensure 'db' is your Orchid ORM database instance and 'user' is a defined model.
Upgrade
Version history
1.0.130latest on npm
Audit
Dependencies
@faker-js/fakerrequiredGenerates fake data for factory attributes.
Agent activity
9 hits · last 30 days
node
8
Resources
orchid-orm-test-factory — npm install orchid-orm-test-factory · libregistry