Database

Larascript Framework is in Beta

This project is currently in beta stage and under active development.
It is not recommended for use in production environments at this time.
We suggest using it only for small-scale applications and experimental projects while we work on stabilizing the features and APIs.

Seeders

Larascript's database seeding system allows you to populate your database with test or initial data. Like migrations, seeders work with both PostgreSQL and MongoDB adapters, providing a consistent way to seed data across different database systems.

Creating Seeders

To create a new seeder, use the following command in your terminal:

yarn dev make:seeder --name=user-seeder

This command will create a new seeder file in the @src/app/seeders directory.

Seeder Structure

Each seeder file exports a class that extends BaseSeeder with two methods: up() anddown(). The up() method defines the data to be seeded, while the down() method defines how to remove that data.

import User from '@src/app/models/auth/User';
import BaseSeeder from '@src/core/domains/migrations/base/BaseSeeder';

export class UserSeeder extends BaseSeeder {
    async up(): Promise<void> {

        await User.query().insert({
            email: "admin@example.com",
            hashedPassword: "hashed_password_here",
            groups: ["admin"],
            roles: ["super_admin"],
            firstName: "Admin",
            lastName: "User"
        });

        // Seed multiple records
        await User.query().insertMany([
            {
                email: "user1@example.com",
                hashedPassword: "hashed_password_here",
                groups: ["users"],
                roles: ["user"],
                firstName: "User",
                lastName: "One"
            },
            {
                email: "user2@example.com",
                hashedPassword: "hashed_password_here",
                groups: ["users"],
                roles: ["user"],
                firstName: "User",
                lastName: "Two"
            }
        ]);
    }

    async down(): Promise<void> {
        // Remove seeded data
        await User.query()
            .whereIn("email", ["admin@example.com", "user1@example.com", "user2@example.com"])
            .delete();
    }
}

Seeder Options

The BaseSeeder class provides similar configuration options to migrations for controlling seeder execution.

databaseAdapter

type: TClassConstructor<IDatabaseAdapter>
description: Specifies which database system this seeder is for. If undefined, runs on default provider.
This seeder will only run on PostgreSQL
databaseAdapter: TClassConstructor<IDatabaseAdapter> = PostgresAdapter

group

type: string
description: Optional group name to filter seeders
This seeder can be filtered by group using --group=<group-name>
group: string = 'user'

shouldUp()

type: boolean
description: Determines if seeder should execute based on database adapter configuration

Running Seeders

To run all seeders:

yarn dev db:seed

To run a specific group of seeders:

yarn dev db:seed --group=user

To reverse all seeders:

yarn dev db:seed:down

You can also run seeders after fresh migrations:

yarn dev migrate:fresh --seed