Database Management

How to reset and seed your database.

Database Reset & Seeding

When developing with nuxt-auto-crud and Drizzle ORM, you may often need to reset your database and seed it with initial data (like default roles and permissions).

Resetting the Database

If you are using NuxtHub (Cloudflare D1) or a local SQLite file:

  1. Delete the data:
    • For local development, this usually means deleting the .data directory or your specific SQLite file.
  2. Regenerate the database:
    bun db:generate
    # or
    npx drizzle-kit generate
    

Seeding Data

Seeding is crucial for setting up your initial RBAC structure (Roles, Resources, Permissions).

Automatic Seeding

In the starter template, the database is automatically seeded when you attempt to log in as admin@example.com for the first time. This ensures that your production or development environment is always ready with the necessary roles and permissions.

Customizing the Seed Data

The seeding logic is located in server/tasks/seed.ts. You can modify this file to:

  • Change default roles.
  • Add custom permissions.
  • Create different initial users.

Manual Seeding (DevTools)

If you need to manually re-seed the database (e.g., after a reset), you can use Nuxt DevTools:

  1. Open Nuxt DevTools (usually Shift + Alt + D).
  2. Go to the Server Tasks tab.
  3. Find db:seed and click Run.

This task typically:

  1. Creates default roles (Admin, Manager, Public, etc.).
  2. Creates resources based on your schema.
  3. Creates default permissions (create, read, update, delete, list).
  4. Assigns permissions to roles.
  5. Creates default users.

Default Users (Starter Template)

If you are using the default seed script from the starter, the following users are created with the password $1Password:

  • Admin: admin@example.com (Full Access)
  • Manager: manager@example.com
  • Moderator: moderator@example.com
  • Customer: customer@example.com

Security Note: Always change default passwords and delete default admin users before deploying to production.