Registry / testing / mikro-orm-entity-factory

mikro-orm-entity-factory

JSON →
library1.0.0jsnpmunverified

A library for bulk-creating MikroORM entities with pseudo-random data and relational support, primarily for E2E testing and seeding. Current version 1.0.0 is stable with a low release cadence. Key differentiators: built on Faker.js for realistic data, supports override of specific fields, and handles cascading saves of related entities via a factory container pattern. Requires @mikro-orm/core >=5.1.2.

npm install mikro-orm-entity-factory
INSTALL
IMPORT
SIG · MIKRO-ORM-ENTITY-F
M
mikro-orm-entity-factory
testingjavascriptv1.0.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.

FactoryContainer
import { FactoryContainer } from 'mikro-orm-entity-factory'
const { FactoryContainer } = require('mikro-orm-entity-factory')
Package is ESM-only, no CommonJS support.
Factory
import { Factory } from 'mikro-orm-entity-factory'
Factory is an abstract class used as base for your entity factories.
defineFactory
import { defineFactory } from 'mikro-orm-entity-factory'
const { defineFactory } = require('mikro-orm-entity-factory')
Alternative factory definition using a functional approach.

Shows how to define a MikroORM entity, create a factory class, initialize FactoryContainer, and save single/multiple entities with random data.

import { MikroORM, Entity, PrimaryKey, Property } from '@mikro-orm/core'; import { FactoryContainer, Factory } from 'mikro-orm-entity-factory'; @Entity() class User { @PrimaryKey() id!: number; @Property() name!: string; @Property() email!: string; } class UserFactory extends Factory<User> { model = User; definition() { return { name: 'John Doe', email: 'john@example.com', }; } } async function main() { const orm = await MikroORM.init({ entities: [User], dbName: ':memory:', type: 'sqlite', }); await orm.schema.createSchema(); const container = await FactoryContainer.init({ em: orm.em, factories: [UserFactory], }); const userFactory = container.getFactory(User); const user = await userFactory.saveOne(); console.log(user); const users = await userFactory.saveMany(5); console.log(users); await orm.close(); } main().catch(console.error);
Debug
Known issues
gotchaFactoryContainer.init() must be called with a valid MikroORM EntityManager; otherwise, save operations will fail with 'EntityManager not found'.
fix
Ensure you pass the EntityManager (orm.em) from a properly initialized MikroORM instance.
affects: >=1.0.0
gotchaFactoryContainer.init() is async and should be awaited; forgetting await leads to a pending Promise and subsequent factories being undefined.
fix
Always await FactoryContainer.init().
affects: >=1.0.0
gotchaThe definition() method must return an object with fields that match the entity's properties; otherwise, MikroORM may throw a validation error.
fix
Ensure the returned object keys and types correspond to the entity's decorated properties.
affects: >=1.0.0
breakingVersion 0.x had a different API using FactoryContainer.create(); removed in 1.0.0.
fix
Use FactoryContainer.init() instead.
affects: >=1.0.0
Errors
Common errors & fixes
Error: EntityManager not found. Did you forget to pass `em` to FactoryContainer.init?
FactoryContainer.init was called without a valid EntityManager or the entity manager is from a closed MikroORM instance.
fix
Pass `em: orm.em` where `orm` is a MikroORM instance that has been initialized and not closed.
TypeError: container.getFactory is not a function
FactoryContainer.init was not awaited or returned undefined due to missing factories array.
fix
Ensure you await FactoryContainer.init and pass a non-empty array of factory classes.
ValidationError: Entity User not found in collection
The entity class passed to getFactory is not registered in the factories array or MikroORM entities.
fix
Add the corresponding factory to the factories array and ensure the entity is listed in MikroORM config entities.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
@mikro-orm/corerequiredPeer dependency for MikroORM entity manager and entity metadata
Agent activity
6 hits · last 30 days
node
6
Resources
mikro-orm-entity-factory — npm install mikro-orm-entity-factory · libregistry