🚀Announcing Flightcontrol - Easily Deploy Blitz.js and Next.js to AWS 🚀
Back to Documentation Menu

blitz generate

Topics

Jump to a Topic

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]
ArgumentRequiredDescription
typeYesType of files to generate. Options are listed below.
modelYesThe model name to generate files for *

* model can't be any of the following reserved words or their plurals: page, api, query or mutation.

Here's the matrix of which files are generated by which command:

TypeModelQueriesMutationsPages
allYesYesYesYes
resourceYesYesYes
modelYes
crudYesYes
queriesYes
queryYes
mutationsYes
mutationYes
pagesYes
Example Output

blitz generate all project will generate the following files:

app/pages/projects/[projectId]/edit.tsx
app/pages/projects/[projectId].tsx
app/pages/projects/index.tsx
app/pages/projects/new.tsx
app/projects/components/ProjectForm.tsx
app/projects/queries/getProject.ts
app/projects/queries/getProjects.ts
app/projects/mutations/createProject.ts
app/projects/mutations/deleteProject.ts
app/projects/mutations/updateProject.ts

For the above example, you can view the generated project index page at localhost:3000/projects

Options

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/pages/projects/[projectId]/tasks/[taskId]/edit.tsx
app/pages/projects/[projectId]/tasks/[taskId].tsx
app/pages/projects/[projectId]/tasks/index.tsx
app/pages/projects/[projectId]/tasks/new.tsx
app/tasks/components/TaskForm.tsx
app/tasks/queries/getTask.ts
app/tasks/queries/getTasks.ts
app/tasks/mutations/createTask.ts
app/tasks/mutations/deleteTask.ts
app/tasks/mutations/updateTask.ts

Note that this will not generate the relationships between the models, only the queries, mutations and pages.

--dry-run

Shorthand: -d

Displays what files would be generated but does not write the files to disk.

--env

Shorthand: -e

Allows you to set app environment name. Read more.

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 anything

    • Use belongsTo to add a model relationship, ex: belongsTo:user
  • type

    • Optional - defaults to string if not specified

    • Values: string, boolean, int, float, dateTime, json, uuid, or a model name

    • foo:uuid is a shorthand syntax for foo:string:default=uuid

    • Add ? to make the type optional like this: string?

      Note: If you use zsh in your terminal, you need to wrap a field in quotes ("") to be correctly interpreted. For example: "name:string?"

    • Add [] to make the type a list like: task[]

  • attribute is for adding a prisma field attribute

    • Optional
    • Supported: default, unique
    • If the attribute takes an argument, you can include it with = like: default=false. That will set the default value to false

For more details, see the docs on Prisma scalar types or the docs on Prisma relations.

Scalar Fields
> blitz generate model puppy isCute:boolean
> blitz generate model rocket launchedAt:dateTime
> blitz generate model task completed:boolean:default=false
Has One Relation
blitz g model project task:Task

will generate this:

model Project {
  ...
  task     Task
}
Has Many Relation
blitz g model project tasks:Task[]

will generate this:

model Project {
  ...
  tasks     Task[]
}
Belongs To Relation
blitz g model task belongsTo:project

will generate this:

model Task {
  ...
  project   Project? @relation(fields: [projectId], references: [id])
  projectId Int?
}
Full Example
blitz generate model task \
       name \
       completed:boolean:default=false \
       belongsTo:project?

will generate this:

model Task {
  id        Int      @default(autoincrement()) @id
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  name      String
  completed Boolean  @default(false)
  project   Project? @relation(fields: [projectId], references: [id])
  projectId Int?
}

Updating a model

Running blitz generate model subsequent times will add fields to the existing model. For example, the below command will add the subheading field to the Task model.

blitz generate model task subheading:string

Custom templates

If you want to use custom templates with blitz generate instead of the default ones (e.g. with different styles). You can specify it in your src/blitz-server file by exporting the cliConfig object

import type { BlitzCliConfig } from "blitz"
...
export const cliConfig: BlitzCliConfig = {
  customTemplates: "src/templates",
}

Generate custom templates

blitz generate custom-templates

The above command will generate the default templates used by blitz to a directory of your choosing in order to provide a starting point to use this feature. It will also update the cliConfig configuration, in the src/blitz-server file, with the latest data to allow immediate usage.


Idea for improving this page? Edit it on GitHub.