Skip to content

Generators

Generator commands scaffold boilerplate files so you can focus on implementation rather than file structure. All generators are prefixed with make:.

make:migration

Generate a new migration file with up() and down() stubs.

bash
bunx d1-eloquent make:migration <name>

Output: src/database/migrations/<timestamp>_<name>.ts

Example:

bash
bunx d1-eloquent make:migration create_posts_table

Generated file (src/database/migrations/20240115_000000_create_posts_table.ts):

ts
import type { TMigration } from '@orphnet/d1-eloquent/cli'
import { Schema } from '@orphnet/d1-eloquent/cli'

const migration: TMigration = {
  name: '20240115_000000_create_posts_table',
  // description: 'Purpose of this migration',

  up: (schema: Schema) => {
    // schema.createTable('posts', (t) => {
    //   t.id()
    //   t.timestamps()
    // })
  },

  down: (schema: Schema) => {
    // schema.dropTable('posts')
  },
}

export default migration

make:model

Generate a model class that extends BaseModel with a typed attributes interface.

bash
bunx d1-eloquent make:model <Name>

Output: src/app/models/<Name>.ts

Example:

bash
bunx d1-eloquent make:model Post

Generated file (src/app/models/Post.ts):

ts
import { BaseModel } from '@orphnet/d1-eloquent'

export type TPostAttrs = {
  id: string
  // add fields
  created_at: string
  updated_at: string
}

export class Post extends BaseModel<TPostAttrs> {
  public static table = 'posts'
  public static primaryKey = 'id'
  public static softDeletes = false
  // public static revisions = { enabled: true, mode: 'diff+after', includeRequestId: true }
}

make:seeder

Generate a seeder class with a run(db) method stub.

bash
bunx d1-eloquent make:seeder <Name>

Output: src/database/seeders/<Name>.ts

Example:

bash
bunx d1-eloquent make:seeder PostSeeder

Generated file (src/database/seeders/PostSeeder.ts):

ts
import type { TSeeder, TSeederOpts } from '@orphnet/d1-eloquent/cli'
// import { PostFactory } from '../factories/PostFactory'

const seeder: TSeeder = {
  name: 'PostSeeder',
  // description: 'Purpose of this seeder',
  run: async (opts: TSeederOpts): Promise<void> => {
    // const factory = new PostFactory()
    // await factory.createMany(opts, 10)
  },
}

export default seeder

For usage patterns — including combining seeders with factories — see Seeders & Factories.


make:factory

Generate a factory class with a definition() method that returns partial model attributes.

bash
bunx d1-eloquent make:factory <Name>

Output: src/database/factories/<Name>.ts

Example:

bash
bunx d1-eloquent make:factory PostFactory

Generated file (src/database/factories/PostFactory.ts):

ts
import { Factory } from '@orphnet/d1-eloquent'

export type TPostAttrs = {
  id: string
  // add fields
  created_at: string
  updated_at: string
}

export class PostFactory extends Factory<TPostAttrs> {
  public readonly table = 'posts'

  public definition(): TPostAttrs {
    const ts = new Date().toISOString()
    return {
      id: crypto.randomUUID(),
      // define defaults
      created_at: ts,
      updated_at: ts,
    }
  }
}

For usage patterns — including overrides and bulk creation — see Seeders & Factories.


make:resource

Generate a model, migration, factory, and seeder in a single command.

bash
bunx d1-eloquent make:resource <Name>

Example:

bash
bunx d1-eloquent make:resource Post

Example output:

  Created  src/database/migrations/20240115_000000_create_posts_table.ts
  Created  src/app/models/Post.ts
  Created  src/database/factories/PostFactory.ts
  Created  src/database/seeders/PostSeeder.ts

This is equivalent to running make:migration create_posts_table, make:model Post, make:factory PostFactory, and make:seeder PostSeeder separately.


make:pivot

Generate a pivot table migration for a many-to-many relationship. No model or seeder is created — pivot tables are typically managed directly through the join table.

bash
bunx d1-eloquent make:pivot <pivot_table>

Output: src/database/migrations/<timestamp>_<pivot_table>.ts

The table name is used to derive foreign key column names: each underscore-separated segment is singularized to produce the FK name. For example, user_roles produces user_id and role_id.

Example:

bash
bunx d1-eloquent make:pivot user_roles

Generated file (src/database/migrations/20240115_000000_user_roles.ts):

ts
import type { TMigration } from '@orphnet/d1-eloquent/cli'
import { Schema } from '@orphnet/d1-eloquent/cli'

const migration: TMigration = {
  name: '20240115_000000_create_user_roles',

  up: (schema: Schema) => {
    schema.createTable('user_roles', (t) => {
      t.text('user_id', { nullable: false })
      t.text('role_id', { nullable: false })
      t.primary('user_id, role_id')
      t.index('user_id')
      t.index('role_id')
    })
  },

  down: (schema: Schema) => {
    schema.dropTable('user_roles')
  },
}

export default migration

For usage patterns after generating, see the Seeders & Factories guide.


make:dto

Generate a typed attributes interface for a single model. Reads the model source and its corresponding migration to produce TModelAttrs (all fields) and TModelCreateAttrs (omitting auto-generated fields like id, created_at, updated_at, deleted_at).

bash
bunx d1-eloquent make:dto <model> [--out-dir=path] [--force]

Output: src/types/generated/<Model>Attrs.ts (default, override with --out-dir)

OptionDescription
--out-dir=pathOutput directory (default: src/types/generated)
--forceOverwrite without prompting

Example:

bash
bunx d1-eloquent make:dto User

Generated file (src/types/generated/UserAttrs.ts):

ts
// Auto-generated by d1-eloquent make:dto
// Source model: User

export type TUserAttrs = {
  id: string;
  name: string;
  email: string;
  created_at: string;
  updated_at: string;
};

export type TUserCreateAttrs = {
  name: string;
  email: string;
};

If the file already exists, the CLI will prompt for confirmation before overwriting. Pass --force to skip the prompt.


make:types

Generate typed attributes for all models in the project, plus a barrel index.ts that re-exports everything.

bash
bunx d1-eloquent make:types [--out-dir=path] [--force] [--json]

Output: Individual <Model>Attrs.ts files + index.ts barrel in src/types/generated/ (default)

OptionDescription
--out-dir=pathOutput directory (default: src/types/generated)
--forceOverwrite all without prompting
--jsonMachine-readable output

Example:

bash
bunx d1-eloquent make:types

Example output:

Generating types for 3 model(s)...

Created DTO: src/types/generated/UserAttrs.ts
Created DTO: src/types/generated/PostAttrs.ts
Created DTO: src/types/generated/CommentAttrs.ts
Created barrel: src/types/generated/index.ts

Done: 3 generated, 0 skipped

Models that cannot be parsed are skipped with a warning. Use --json for CI integration.

Released under the MIT License.