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
npx @orphnet/d1-eloquent make:migration <name>

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

Example:

bash
npx @orphnet/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',

  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
npx @orphnet/d1-eloquent make:model <Name>

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

Example:

bash
npx @orphnet/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
npx @orphnet/d1-eloquent make:seeder <Name>

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

Example:

bash
npx @orphnet/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',
  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
npx @orphnet/d1-eloquent make:factory <Name>

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

Example:

bash
npx @orphnet/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
npx @orphnet/d1-eloquent make:resource <Name>

Example:

bash
npx @orphnet/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
npx @orphnet/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
npx @orphnet/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.

Released under the MIT License.