@megaorm/seeder v1.0.0
MegaORM Seeder
This package allows you to seed data into your database tables in MegaORM. Each seeder file corresponds to a specific table, allowing for targeted and efficient seeding and clearing. Let's explore how to create and use seeders!
Table of Contents
- Installation
- Adding Seeder Files
- Seeder File Structure
- Seeding Tables
- Clearing Tables
- Removing Seeder Files
Installation
To install this package, run the following command:
npm install @megaorm/seederYou should be familiar with @megaorm/cli.
Adding Seeder Files
To create a seeder file, use the following command:
node mega add:seeder <table>To create a seeder for the users table, run:
node mega add:seeder usersThis command will output the following:
Seeder added in: ./seeders/01_seed_users_table.js- Similar to @megaorm/gen, each seeder file is prefixed with a number, defining the order in which tables are seeded.
- This numbering is crucial, especially when your tables reference other tables through foreign keys. Always make sure seeder files are in the same order as generator files.
Seeder File Structure
Below is an example of a seeder file for the users table:
const { MegaSeeder } = require('@megaorm/seeder');
class UsersTableSeeder extends MegaSeeder {
constructor() {
super();
this.set.rows(10); // Insert 10 rows
this.set.table('users'); // Target the 'users' table
}
layout() {
const faker = this.get.faker(); // Access a MegaFaker instance
return {
username: faker.username(), // Generate a random username
email: faker.email(), // Generate a random email
password: faker.password(), // Generate a random password
created_at: faker.datetime(), // Random datetime
updated_at: faker.datetime(), // Random datetime
};
}
}
module.exports = new UsersTableSeeder(); // Export a new instanceCode Overview
this.set.rows(number): Defines the number of rows to create (numbermust be > 0).this.set.table(name): Specifies the target table for seeding.this.get.faker(): AccessesMegaFakerinstance to generate realistic, fake data for insertion.- You should be familiar with @megaorm/faker.
How it Works
- The
layoutmethod is executed the specified number of times to generate rows. - Every time the
layoutmethod is called, it should return an object:- Object keys represent column names.
- Object values represent data to insert.
- An
INSERTSQL query is built and executed.
Seeding Tables
To seed all your tables, use:
node mega seedTo seed only one table, use:
node mega seed <table>This command will seed the users table only:
node mega seed usersYou can use this command to seed the same table multiple times!
- The first execution inserts the specified number of rows.
- The next execution inserts the same number of rows, and so on...
Clearing Tables
To clear all tables with associated seeder files, use:
node mega clearTo clear only one table, use the following command:
node mega clear <table>This command will clear all data from the users table only:
node mega clear usersRemoving Seeder Files
To remove a specific seeder file, use the following command:
node mega remove:seeder <table>For example, consider the following seeders folder structure:
- 01_seed_users_table.js
- 02_seed_profiles_table.jsIf you execute:
node mega remove:seeder usersThe users seeder file will be removed, and the numbering will update automatically:
- 01_seed_profiles_table.jsThat's it! Managing your seeder files has never been easier.
11 months ago