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:
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
):
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 :
- 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
.
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.