All jobs

Add Database Seeders to an AdonisJS API

Yes. An agent writes idempotent Lucid seeders for your AdonisJS API, runs them in development and in the test suite with a login test, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the schema and the model before inventing rows. In an API generated from the api kit of adonisjs/starter-kits, database/migrations/ creates users with full_name, a unique email of 254 characters and password, and auth_access_tokens with tokenable_id, type, hash, abilities and expires_at. database/schema.ts is generated from those migrations, since config/database.ts turns on schemaGeneration, and app/models/user.ts composes that UserSchema with withAuthFinder(hash) and DbAccessTokensProvider.forModel(User), so creating a user through the model hashes the password and the token provider can issue a token for it. The default connection is SQLite through better-sqlite3 at tmp/db.sqlite3, there is no seeders entry in the connection config, and database/ has no seeders/ directory.

It then reads what already validates and uses a user. app/validators/user.ts requires an email that is unique in users and a password of 8 to 32 characters, app/controllers/new_account_controller.ts creates the user and returns a token, and start/routes.ts mounts POST /api/v1/auth/signup, POST /api/v1/auth/login and the auth-guarded GET /api/v1/account/profile. adonisrc.ts registers @adonisjs/lucid/commands, which is where make:seeder and db:seed come from, and lists a functional test suite that tests/bootstrap.ts starts an HTTP server for, with the dbAssertions, apiClient and authApiClient Japa plugins already wired.

It writes database/seeders/main/index_seeder.ts and database/seeders/user_seeder.ts. The user seeder extends BaseSeeder from @adonisjs/lucid/seeders, sets static environment = ['development', 'test'] so it never runs in production, and calls User.updateOrCreateMany('email', [...]) with a handful of accounts whose passwords satisfy the validator, so a second node ace db:seed updates the same rows rather than duplicating them. The index seeder imports the others and runs them in order, checking each one's environment against app.nodeEnvironment as the Lucid guide describes, and config/database.ts gains seeders.paths pointing at database/seeders/main so db:seed runs only the index. In tests/bootstrap.ts the runnerHooks.setup list gets testUtils.db().migrate() and testUtils.db().seed(), and a new tests/functional/auth/login.spec.ts logs in as a seeded account and reads /api/v1/account/profile with the token.

In its sandbox the agent runs npm install, copies .env.example to .env, runs node ace migration:run and node ace db:seed twice, and counts users after each to show the second run added nothing. It then runs node ace test, npm run typecheck and npm run lint, and keeps the output of each. It does not run migration:fresh, since the SQLite file under tmp/ is the developer's.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add database seeders to this AdonisJS API. Read adonisrc.ts, config/database.ts, database/migrations, database/schema.ts, app/models/user.ts, app/validators/user.ts, app/controllers/new_account_controller.ts, start/routes.ts, tests/bootstrap.ts, .env.example and package.json first. Create a user seeder that extends BaseSeeder from @adonisjs/lucid/seeders, restrict it to the development and test environments, and make it idempotent with updateOrCreateMany keyed on email, with passwords that satisfy the signup validator. Add a main index seeder that runs the others in order and checks each one's environment, point the connection's seeders paths at it, and run migrations and seeders in the test runner's setup hooks. Add a functional test that logs in as a seeded user and reads the profile route with the token. Run the migrations, run db:seed twice and show the users count after each, then run the test suite, the typecheck and the lint. Open a pull request that pastes those outputs. Do not change the migrations or the User model.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

The pull request adds the two seeder files, the seeders.paths line in config/database.ts, the setup hooks in tests/bootstrap.ts and the login test, and nothing else. Its description pastes the db:seed output from both runs with the users count after each, showing the same number twice, followed by the node ace test summary with the new functional test, and the typecheck and lint results.

A short paragraph names each seeded account and the environments the seeder accepts, and explains why the seeder calls the model rather than the query builder: withAuthFinder hashes the password on create, so a seeded user can log in through the real login route, which is what the test proves.

Before merging, a reviewer decides whether the seeded accounts should come from environment variables instead of the file, whether the test suite should seed in setup or per test with a transaction, and whether a migration:fresh --seed note belongs in the README.

Run it on every new Lucid model

In the console, an automation on the Project with a github trigger on pull_request.opened and pull_request.synchronize and the prompt "if this pull request adds a migration or a model, add a seeder for it to database/seeders, register it in the index seeder, run db:seed twice and the tests, and comment on the pull request with the seeder and the output" starts one run per pull request; its runs read the repository and comment, but do not push. The trigger adds the pull request after the prompt, only work by an owner, member or collaborator fires it, and the automation's max_runs_per_hour bounds a busy afternoon.

From your own server the same work is one POST /o/{org}/sessions per repository with the repository in resources. A webhook_url on the key posts run.completed or run.failed with the session_url when the turn finishes, so the pull request links and the test output collect on your side without polling.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

A seeder is only right when it runs twice and the table does not grow, and when the rows it writes can be used by the app's own routes; both are things to run, not to read. The agent's sandbox has Node, the SQLite driver the kit ships and the Japa runner, so it migrates, seeds, counts and logs in with the real HTTP server, and keeps every command with its output.

The choices come from this repository: the generated UserSchema, the withAuthFinder mixin that hashes on create, the validator's password length and the test plugins already in tests/bootstrap.ts. One run produces the seeders, the config, the test hooks and the proof in one pull request, and the same prompt runs against several AdonisJS services at once.

Frequently asked questions

Will running the seeder twice duplicate users?

No. The seeder uses updateOrCreateMany keyed on email, which looks up each row by that column, updates the ones that exist and inserts the missing ones, so the count stays the same. The description shows the users count after each of the two runs.

Can it run in production by accident?

The user seeder declares static environment = ['development', 'test'], so db:seed skips it when NODE_ENV is production. Because Lucid checks that property only on the top-level seeder, the index seeder checks each child's environment itself before running it.

Why seed through the model instead of raw inserts?

Because User composes withAuthFinder(hash), which hashes the password when a user is created, so a seeded account can log in through POST /api/v1/auth/login and receive a token. A raw insert would store the plain string and the login test would fail.

Does the test suite depend on the seeded data?

Only the new login test does; it reads one seeded email and password. The setup hooks run testUtils.db().migrate() and testUtils.db().seed() before the suites, and the existing functional tests keep creating their own users through signup.

More AdonisJS jobs

The same job on other stacks

Keep reading