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.
npx @orphnet/d1-eloquent make:migration <name>Output: src/database/migrations/<timestamp>_<name>.ts
Example:
npx @orphnet/d1-eloquent make:migration create_posts_tableGenerated file (src/database/migrations/20240115_000000_create_posts_table.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 migrationmake:model
Generate a model class that extends BaseModel with a typed attributes interface.
npx @orphnet/d1-eloquent make:model <Name>Output: src/app/models/<Name>.ts
Example:
npx @orphnet/d1-eloquent make:model PostGenerated file (src/app/models/Post.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.
npx @orphnet/d1-eloquent make:seeder <Name>Output: src/database/seeders/<Name>.ts
Example:
npx @orphnet/d1-eloquent make:seeder PostSeederGenerated file (src/database/seeders/PostSeeder.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 seederFor 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.
npx @orphnet/d1-eloquent make:factory <Name>Output: src/database/factories/<Name>.ts
Example:
npx @orphnet/d1-eloquent make:factory PostFactoryGenerated file (src/database/factories/PostFactory.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.
npx @orphnet/d1-eloquent make:resource <Name>Example:
npx @orphnet/d1-eloquent make:resource PostExample 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.tsThis 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.
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:
npx @orphnet/d1-eloquent make:pivot user_rolesGenerated file (src/database/migrations/20240115_000000_user_roles.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 migrationFor usage patterns after generating, see the Seeders & Factories guide.