Registry / database / typeorm-seeding

typeorm-seeding

JSON →
library1.6.1jsnpmunverified

TypeORM Seeding is a utility library designed to simplify the process of populating databases with test or sample data within TypeORM projects. It allows developers to define entity factories, inspired by Laravel's Eloquent factories, and create modular seed scripts for programmatically generating and inserting data. The current stable version is 1.6.2, released in September 2021. The package has a slow release cadence, primarily supporting TypeORM 0.2.x. Key differentiators include a clear separation of factory definitions from seeding logic, support for nested seeding to create related entities, and a command-line interface (CLI) for executing seeders, making it convenient for development, testing, and staging environment setups. It streamlines the creation of complex relational datasets required for application development and testing workflows.

npm install typeorm-seeding
INSTALL
IMPORT
SIG · TYPEORM-SEEDING
T
typeorm-seeding
databasejavascriptv1.6.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

define
import { define } from 'typeorm-seeding';
import define from 'typeorm-seeding'; // Not a default export const { define } = require('typeorm-seeding'); // CommonJS in ESM project
Used to define entity factories for specific TypeORM entities.
Seeder
import { Seeder } from 'typeorm-seeding';
import Seeder from 'typeorm-seeding'; // Not a default export
An interface that seed classes must implement, defining the `run` method.
runSeeder
import { runSeeder } from 'typeorm-seeding';
import runSeeder from 'typeorm-seeding'; // Not a default export const { runSeeder } = require('typeorm-seeding'); // CommonJS in ESM project
Programmatic utility to execute a single seeder class. `runSeeders` is available for multiple.

This quickstart demonstrates how to define a TypeORM entity, create an associated factory using `typeorm-seeding` and `faker`, implement a `Seeder` class, and then programmatically run the seeder to populate an in-memory SQLite database with sample user data.

import { createConnection, Connection, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; import { define, runSeeder, Factory, Seeder } from 'typeorm-seeding'; import * as Faker from 'faker'; // Using faker-js/faker in modern context // 1. Define Entity @Entity() export class User { @PrimaryGeneratedColumn('uuid') id: string = ''; @Column({ nullable: true }) name: string = ''; @Column() email: string = ''; } // 2. Define Factory for the User entity define(User, (faker: typeof Faker) => { const user = new User(); user.name = faker.name.findName(); user.email = faker.internet.email(); return user; }); // 3. Define a Seeder class class UserCreateSeeder implements Seeder { public async run(factory: Factory, connection: Connection): Promise<any> { console.log('Running UserCreateSeeder...'); await factory(User)().createMany(5); // Create 5 users using the factory console.log('Created 5 users.'); } } // 4. Programmatic execution of the seeder async function seedDatabase() { let connection: Connection | undefined; try { // Establish a TypeORM connection (using in-memory SQLite for quick demo) connection = await createConnection({ type: 'sqlite', database: ':memory:', entities: [User], synchronize: true, // Auto-create schema for demo purposes logging: false, }); await connection.synchronize(true); // Ensure schema is created console.log('Database connection established. Starting seeding...'); await runSeeder(UserCreateSeeder); // Execute the defined seeder console.log('Seeding complete.'); // Verify seeded data const userRepository = connection.getRepository(User); const users = await userRepository.find(); console.log('Users in DB:', users.map(u => u.name)); } catch (error) { console.error('Seeding failed:', error); } finally { if (connection && connection.isConnected) { await connection.close(); console.log('Database connection closed.'); } } } seedDatabase();
ts-node --version
Debug
Known issues
breakingIn version 1.5.0, the `seed` method on factory instances was renamed to `create` to better reflect its function.
fix
Replace calls like `factory(Entity)().seed()` with `factory(Entity)().create()`.
affects: >=1.5.0
breakingThis package is explicitly designed for TypeORM versions 0.2.x. It is not compatible with TypeORM 0.3.x, 0.4.x, or newer versions due to significant breaking API changes in TypeORM's connection management, repository interactions, and decorator usage.
fix
Ensure your project uses TypeORM 0.2.x. If you need to use TypeORM 0.3.x or later, consider alternative seeding solutions or migration paths as `typeorm-seeding` is not actively maintained for newer TypeORM versions.
affects: *
gotchaVersion 1.6.2 updated its internal Faker.js dependency. This might cause issues if your factory definitions rely on specific Faker.js API behavior that changed between versions, especially when moving from older Faker.js versions (pre-5.0) to newer ones, or if your project has a pinned Faker.js dependency that conflicts.
fix
Review your factory definitions for any deprecated or changed Faker.js API calls and update them according to the Faker.js documentation for the version bundled with `typeorm-seeding` (which implicitly means a relatively old version of Faker.js itself, as the package has not been updated since 2021).
affects: >=1.6.2
Errors
Common errors & fixes
Error: Cannot find module 'typeorm-seeding'
The `typeorm-seeding` package or its peer dependency `typeorm` is not installed, or the import path is incorrect.
fix
Ensure the package is installed: `npm install typeorm-seeding typeorm faker` or `yarn add typeorm-seeding typeorm faker`. Verify your `tsconfig.json` paths and module resolution if using TypeScript.
TypeError: factory(...).seed is not a function
You are attempting to use the deprecated `seed()` method on a factory instance. This method was renamed in version 1.5.0.
fix
Update your factory usage to call `factory(Entity)().create()` instead of `factory(Entity)().seed()`.
TypeError: Cannot read properties of undefined (reading 'findName')
This typically indicates an issue with the Faker.js instance passed to your factory definition or a breaking change in the Faker.js API that your factory is trying to use.
fix
Check that the `faker` parameter in your `define` callback is correctly typed and imported. If migrating, review Faker.js documentation for API changes between versions, particularly concerning `faker` (deprecated) vs. `@faker-js/faker` (community maintained fork) and their respective major versions.
Upgrade
Version history
1.6.1latest on npm
Audit
Dependencies
typeormrequiredRuntime peer dependency for ORM operations and entity definitions.
Agent activity
22 hits · last 30 days
node
18
Meta
2
OpenAI (training)
1
Resources
typeorm-seeding — npm install typeorm-seeding · libregistry