Skip to content

Database Seeding

This document explains how to generate and apply seed data for your database.

phantom-seed generate

This command triggers the backend to generate a TypeScript file with random, coherent data based on your resource meta-definitions (phantom-api-backend/meta/*.json). This is incredibly useful for quickly populating your development database with sample data for testing and development purposes.

Usage Example:

npx phantom-seed generate

Expected Behavior:

When you execute this command, the phantom-api CLI sends a request to your running backend server. The backend then performs the following actions: 1. It reads all your defined resource meta-definitions from the phantom-api-backend/meta/ directory. 2. It intelligently generates sample data for each resource, respecting the field types (e.g., string, number, boolean) and basic relationships you've defined. 3. Finally, it creates a new TypeScript file in the phantom-api-backend/seeds/ directory. This file contains a seed function that, when executed, will insert the generated data into your database.

Example of a Generated Seed File (phantom-api-backend/seeds/YYYYMMDDHHmmss_generated_seed.ts):

// This file is automatically generated by the backend.
// Do not modify it manually unless you know what you are doing.

export const seed = async (tableManager: any) => {
  console.log('Applying generated seed data...');

  // Example: Seeding User records
  for (const record of [
    { id: "user_1", name: "Alice Smith", email: "alice.smith@example.com" },
    { id: "user_2", name: "Bob Johnson", email: "bob.johnson@example.com" },
    // ... more generated user data
  ]) {
    try {
      await tableManager.create(`User`, record);
      console.log(`  Created User with ID: ${record.id}`);
    } catch (error) {
      console.error(`  Failed to create User record ${record.id}: ${(error as Error).message}`);
    }
  }

  // Example: Seeding Post records (assuming a relation to User)
  for (const record of [
    { id: "post_1", title: "Hello World", content: "First post content.", author: "user_1" },
    { id: "post_2", title: "Another Post", content: "Second post content.", author: "user_2" },
    // ... more generated post data
  ]) {
    try {
      await tableManager.create(`Post`, record);
      console.log(`  Created Post with ID: ${record.id}`);
    } catch (error) {
      console.error(`  Failed to create Post record ${record.id}: ${(error as Error).message}`);
    }
  }

  console.log('Generated seed data applied.');
};

Appliquer des données de test

Cette commande envoie une requête au backend pour appliquer un fichier de "seed" spécifique à votre base de données.

Exemple d’utilisation :

# Remplacer <fileName> par le nom du fichier de seed
node phantom-api/src/seed-cli.ts apply <fileName>
  • Replace <fileName> with the exact name of the seed file you wish to apply (e.g., 20231027103000_generated_seed.ts).

Concrete Example:

Let's assume you have a generated seed file named 20231027103000_generated_seed.ts.

npx phantom-seed apply 20231027103000_generated_seed.ts

Important Notes: - Backend Server: Your backend server must be running and accessible for these commands to work, as the CLI communicates with it. - Data Duplication: Running phantom-seed apply multiple times with the same seed file will insert new records each time. This can lead to duplicate data unless your database schema has unique constraints defined (e.g., unique email addresses). - Fresh Dataset: If you need a fresh dataset, it's often recommended to clear your database or specific tables before applying seed data. You can achieve this by dropping and recreating your database or by implementing a custom cleanup script.