Registry / testing / drizzle-fixtures

drizzle-fixtures

JSON →
library1.2.0jsnpmunverified

Type-safe test data factories for Drizzle ORM (v1.2.0). Introspects your schema at runtime to automatically generate sensible fixture values with zero configuration. Supports PostgreSQL, MySQL, SQLite, CockroachDB, and SingleStore. Active development with frequent releases. Key differentiators vs @praha/drizzle-factory: automatic value inference, built-in faker.js integration, defineSeeder for DB seeding, and test framework helpers for Vitest/Jest. Peer dependency on drizzle-orm >=0.29.0. Ships TypeScript types.

npm install drizzle-fixtures
INSTALL
IMPORT
SIG · DRIZZLE-FIXTURES
D
drizzle-fixtures
testingjavascriptv1.2.0
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.

defineFactory
✓ import { defineFactory } from 'drizzle-fixtures'
✗ const { defineFactory } = require('drizzle-fixtures')
ESM-only package. CommonJS require() will fail.
defineFactory
✓ import { defineFactory } from 'drizzle-fixtures'
✗ import { defineFactory } from 'drizzle-fixtures/defineFactory'
All exports are at the package root. Subpath imports are not supported.
defineSeeder
✓ import { defineSeeder } from 'drizzle-fixtures'
Also ESM-only. Used for seeding databases with multiple factories.
createVitestHelpers
✓ import { createVitestHelpers } from 'drizzle-fixtures'
Only available in Vitest environment. Provides beforeAll/afterAll setup.
faker
✓ import { faker } from '@faker-js/faker'
✗ import { faker } from 'drizzle-fixtures'
Faker is an optional peer dependency. Import from '@faker-js/faker' directly. drizzle-fixtures auto-detects its presence.

Shows basic factory setup, build methods, database creation, overrides, multiple records, faker integration, and related record creation using use().

import { defineFactory } from 'drizzle-fixtures'; import { users } from './schema'; import { db } from './db'; // Define a factory from a Drizzle table schema const userFactory = defineFactory(users); // Build a user object without DB (InsertUser type) const user = userFactory.build(); // { id: 1, name: 'name-1', email: 'email-1@example.com', role: 'viewer', ... } // Build with overrides (fully typed) const admin = userFactory.build({ role: 'admin' }); // Create a user in the database (returns SelectUser) const savedUser = await userFactory.create(db); // Create multiple records const manyUsers = await userFactory.createMany(db, 5); // Build with faker.js (if installed) // Values become more realistic: names, emails, etc. const richUser = userFactory.buildWithFaker(); // Related records: automatically creates associated rows const postFactory = defineFactory(posts); const postWithUser = await postFactory.use(userFactory).create(db); // Also inserts a user row linked by foreign key
Debug
Known issues
breakingESM-only package from version 1.0.0. Cannot be used with require().
fix
Use import syntax or switch to dynamic import() if in CommonJS project.
affects: >=1.0.0
gotchaFactory must be associated with a table that has a primary key. Tables without PK will cause runtime errors.
fix
Ensure every Drizzle table used with defineFactory has a primary key column defined in schema.
affects: >=1.0.0
gotchaFaker.js integration is auto-detected. If faker is not installed but you call buildWithFaker(), it will silently fall back to default generation (no error).
fix
Install @faker-js/faker as devDependency if you intend to use buildWithFaker().
affects: >=1.0.0
deprecatedThe `seed` method from v0.x has been removed in v1.0.0. Use `defineSeeder` instead.
fix
Replace seed() calls with defineSeeder({ ... }) and invoke the returned function.
affects: >=1.0.0
gotchacreate() and createMany() mutate the database and do not roll back automatically. Use test framework helpers (createVitestHelpers, createJestHelpers) for transaction-based cleanup.
fix
Wrap test suites with createVitestHelpers(db) to automatically roll back after each test.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
drizzle-fixtures is ESM-only; CommonJS require() or non-module Node.js context.
fix
Add "type": "module" to package.json, or use dynamic import: const { defineFactory } = await import('drizzle-fixtures');
TypeError: defineFactory is not a function
Incorrect import — possibly importing default instead of named export.
fix
Use named import: import { defineFactory } from 'drizzle-fixtures';
Error: Table must have a primary key to use drizzle-fixtures
Factory table schema lacks a primary key column.
fix
Add a primary key (e.g., id integer primary key) to your Drizzle table definition.
Error: No database provided
Calling create() or createMany() without passing a db instance.
fix
Pass your Drizzle database instance as the first argument: userFactory.create(db);
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
drizzle-ormrequiredruntime peer dependency for schema introspection and database operations
@faker-js/fakeroptionaloptional dependency for richer generated values
Agent activity
7 hits · last 30 days
node
6
Resources
drizzle-fixtures — npm install drizzle-fixtures · libregistry