blitz generate
Alias: blitz g
Use this command to scaffold all the boring code into your project.
Can generate pages, queries, mutations, and Prisma models. Support for custom templates based on the built-in templates is coming soon, so you can customize the generator to your app's needs.
blitz generate [type] [model]
Argument | Required | Description |
---|---|---|
type | Yes | Type of files to generate. Options: all , resource , crud , queries , mutations , and pages . |
model | Yes | The model name to generate files for |
Here's the matrix of which files are generated by which command:
Type | Model | Queries | Mutations | Pages |
---|---|---|---|---|
all | Yes | Yes | Yes | Yes |
resource | Yes | Yes | Yes | |
crud | Yes | Yes | ||
queries | Yes | |||
mutations | Yes | |||
pages | Yes |
Example Output
blitz generate all project
will generate the following files:app/projects/pages/projects/[projectId]/edit.tsxapp/projects/pages/projects/[projectId].tsxapp/projects/pages/projects/index.tsxapp/projects/pages/projects/new.tsxapp/projects/components/ProjectForm.tsxapp/projects/queries/getProject.tsapp/projects/queries/getProjects.tsapp/projects/mutations/createProject.tsapp/projects/mutations/deleteProject.tsapp/projects/mutations/updateProject.ts
For the above example, you can view the generated project index page at
localhost:3000/projectsOptions
context/model
For organization of files within your project, you can specify a nested folder path in which to generate the files.
blitz generate all admin/products// Will generate files in `app/admin/products` instead of `app/products`
Alternatively, you can provide the folder path via the
--context
or -c
options--parent
Shorthand:
-p
Used to specify that you want to generate files for a model which is a child of a parent model.
For example, say you have
Project
and Task
models. A Task
belongs to a Project
and Project
has many Tasks
. You would run this command:blitz generate all task --parent project
which would generate the following files:
app/tasks/pages/projects/[projectId]/tasks/[taskId]/edit.tsxapp/tasks/pages/projects/[projectId]/tasks/[taskId].tsxapp/tasks/pages/projects/[projectId]/tasks/index.tsxapp/tasks/pages/projects/[projectId]/tasks/new.tsxapp/tasks/components/TaskForm.tsxapp/tasks/queries/getTask.tsapp/tasks/queries/getTasks.tsapp/tasks/mutations/createTask.tsapp/tasks/mutations/deleteTask.tsapp/tasks/mutations/updateTask.ts
--dry-run
Shorthand:
-d
Displays what files would be generated but does not write the files to disk.
Basic Examples
blitz generate all project
blitz generate mutations project
blitz generate crud admin/topsecret/files
blitz generate pages tasks --parent=projects
Model Generation
All of the following commands will generate a model in your prisma schema file:
blitz generate all
blitz generate resource
blitz generate model
Model Fields
Model fields can be added like this:
blitz generate model [fieldName]:[type]:[attribute]
fieldName
is the name of your database column and can be anythingtype
can be anything supported by Prisma including scalar types and relation types- Optional - defaults to
string
if not specified - Add
?
to make the type optional like this:string?
- Optional - defaults to
attribute
is for adding a prisma field attribute- Optional
- Supported:
default
,unique
- If the attribute takes an argument, you can include it in brackets like this:
default[false]
That will set the default value tofalse
Example 1
blitz g model project name tasks:Task[]
will generate this in
prisma.schema
:model Project {id Int @default(autoincrement()) @idcreatedAt DateTime @default(now())updatedAt DateTime @updatedAtname Stringtasks Task[]}
Example 2
blitz generate model task \name \completed:boolean:default[false] \belongsTo:project?
will generate this in
prisma.schema
:model Task {id Int @default(autoincrement()) @idcreatedAt DateTime @default(now())updatedAt DateTime @updatedAtname Stringcompleted Boolean @default(false)project Project? @relation(fields: [projectId], references: [id])projectId Int?}