Framework

Seed Nest.js + TypeORM + Postgres with MockHero

Script

// src/scripts/seed.ts
import { DataSource } from "typeorm";
import { User, Product } from "../entities";

const ds = new DataSource({
  type: "postgres",
  url: process.env.DATABASE_URL,
  entities: [User, Product],
});
await ds.initialize();

const data = await fetch("https://api.mockhero.dev/api/v1/generate", {
  method: "POST",
  headers: { "x-api-key": process.env.MOCKHERO_API_KEY!, "Content-Type": "application/json" },
  body: JSON.stringify({
    tables: [
      { name: "users", count: 30, fields: [
        { name: "id", type: "uuid" },
        { name: "email", type: "email" }
      ]},
      { name: "products", count: 150, fields: [
        { name: "id", type: "uuid" },
        { name: "seller_id", type: "ref", ref: "users.id" },
        { name: "name", type: "product_name" },
        { name: "price", type: "price" }
      ]}
    ]
  })
}).then(r => r.json());

await ds.getRepository(User).insert(data.users);
await ds.getRepository(Product).insert(data.products);

Get Started

Free API key.

M

MockHero Team

Guides and tutorials for generating realistic test data with the MockHero API.

Start generating test data for free

1,000 rows/month on the free tier. No credit card required.

Get Your API Key

Related Articles