#grow #ron #libsql #fake #seeder #write

app grow-rs

🌱 Write sedders in RON

3 stable releases

new 2.0.0 Feb 7, 2025
1.1.0 Jan 9, 2025
1.0.0 Jan 7, 2025

#138 in Database interfaces

Download history 254/week @ 2025-01-06 11/week @ 2025-01-13 111/week @ 2025-02-03

134 downloads per month

MIT license

40KB
737 lines

Grow Seeder CLI is a command line tool written in Rust to manage database seeders. It allows to generate, list and run seeders defined in RON format for LibSQL, PostgreSQL, MySQL and SQLite compatible databases. Automatically detects the database type through the DATABASE_URL environment variable.

Requirements

  • Environment variables:
    • DATABASE_URL: Database connection URL.
    • TURSO_AUTH_TOKEN (only for LibSQL/Turso databases).

Installation

cargo install grow-rs
# or
cargo install --git https://github.com/Wilovy09/Grow-rs

Commands

Commands Functions
grow init Creates a seeders/ folder in the current directory to store seeders.
grow new <NAME> Creates a new .ron file inside the seeders/ folder. The file name will be <NAME>.ron.
grow list Displays a list of all available seeders in the seeders/ folder.
grow run [NAME.ron] Run the seeder. If no file is specified, it will run all seeders in alphabetical order.

Cargo features

Feature Description
default Install libsql, sqlx databases and fake support.
fake Enable fake support
libsql Install only libsql support.
sqlx Install only sqlx databases support.

Seeder Example

A seeder file in .ron format could have the following content:

[!NOTE] If the ID is generated automatically by the db, it is not necessary to enter it in the seeder.

{

    // Static data
    // TABLE_NAME: DATA[],
    User: [
        {
            "role": "Admin",
            "email": "admin@example.com",
            "password": "hashed_password_admin",
            "created_at": "2024-12-22 12:00:00",
            "updated_at": "2024-12-22 12:00:00"
        },
    ]

    // Repeated data
    // TABLE_NAME(REPEATED_TIMES): DATA,
    User(4): [
        {
            "role": "client",
            "email": "{fake(EMAIL_PT_BR)}",
            // Templating have `i` to know the iteration
            "password": "hashed_password_admin{i}",
            "created_at": "2024-12-22 12:00:{mul_u32(i, 10)}",
            "updated_at": "2024-12-22 12:00:{mul_u32(i, 20)}"
        },
    ]
}

.env file

Configuration of DATABASE_URL for Different Databases

  1. PostgresSQL, MySQL, SQLite (SQLx Support):

    DATABASE_URL=sqlite://DB_NAME
    
    • This line configures the database URL for SQLite. The format sqlite://DB_NAME indicates that SQLite is being used, and DB_NAME is the name of the database.
  2. libsql (TURSO)

    DATABASE_URL=libsql://DB_NAME.turso.io
    TURSO_AUTH_TOKEN=eyJhbGciWJhNGRjIn0.igV6yDaKTuDKM7_5J-UWGOftULBg
    
    • Here, DATABASE_URL is configured to use libsql, which is a SQLite-compatible database with additional features like cloud synchronization. DB_NAME.turso.io is the URL of the database on Turso.
    • TURSO_AUTH_TOKEN is an authentication token required to access the database on Turso.
  3. Dry-run (Simulation)

    DATABASE_URL=mock://DB_NAME
    
    • This configuration is used to perform a simulation or "dry-run." Instead of connecting to a real database, a mock database is used to test commands without affecting real data.

Considerations for Windows: Using / Instead of \

On operating systems like Windows, paths (file directories) are traditionally written using the backslash (). However, in the context of software development, especially when working with cross-platform tools and languages, it is common to use the forward slash (/) instead of the backslash.

  • Reason: The forward slash (/) is the standard on Unix systems (like Linux and macOS) and is widely supported on Windows. Using / ensures that the code is compatible across different operating systems.
  • Example:
    • Instead of writing:
    DATABASE_URL=sqlite://C:\Users\User\Documents\DB_NAME
    
    • You should write:
    DATABASE_URL=sqlite://C:/Users/User/Documents/DB_NAME
    

This approach avoids compatibility issues and errors when moving code between different operating systems.

Database Compatibility

Grow Seeder CLI is compatible with:

[!NOTE] Required on all DATABASE_URL in your .env

  • LibSQL: Requires the TURSO_AUTH_TOKEN variable.
  • PostgreSQL
  • MySQL
  • SQLite

The CLI automatically detects the database type via DATABASE_URL and handles the connection appropriately.

Contributions

Read CONTRIBUTING.md

Features

  • Create a library to run seeder in the code and not with CLI
  • Add cargo features to CLI.
  • Add fake in column value to create fake data.

Example for fake feature:

{
    // Generate 20 fake users
    User(20): [
        {
            "role": "CLIENT",
            "email": "{fake(EMAIL)}",
            "password": "{fake(PASSWORD)}",
            // Multi-language support
            "first_name": "{fake(FIRST_NAME_FR_FR)}",
        },
    ]
}

see more

License

This project is licensed under the MIT License.

Dependencies

~7–23MB
~344K SLoC