【部分翻译】Prisma CLI 指令参数

原文:Prisma CLI Command Reference | Prisma Docs

部分 Google 机翻,部分手翻,还有一些,似乎用英文原文读着更贴合原意。

This document describes the Prisma CLI commands, arguments, and options.

Synopsis

安装后可以从命令行调用prisma命令。 当不带参数调用时,它将显示其命令用法和帮助文档:

Prisma is a modern DB toolkit to query, migrate and model your database (https://prisma.io)

Usage

  $ prisma [command]

Commands

            init   Setup Prisma for your app
        generate   Generate artifacts (e.g. Prisma Client)
              db   Manage your database schema and lifecycle
         migrate   Migrate your database
          studio   Browse your data with Prisma Studio
          format   Format your schema

Flags

     --preview-feature   Run Preview Prisma commands

Examples

  Setup a new Prisma project
  $ prisma init

  Generate artifacts (e.g. Prisma Client)
  $ prisma generate

  Browse your data
  $ prisma studio

  Create migrations from your Prisma schema, apply them to the database, generate artifacts (e.g. Prisma Client)
  $ prisma migrate dev

  Pull the schema from an existing database, updating the Prisma schema
  $ prisma db pull

  Push the Prisma schema state to the database
  $ prisma db push

您可以通过在命令后添加 --help 标志来获得有关任何 prisma 命令的其他帮助。

version (-v)

version 命令输出有关当前 prisma 版本、平台和引擎二进制文件的信息。

Options

version 命令识别以下选项来修改其行为:

Option Required Description
--json No Outputs version information in JSON format.

Examples

Environment variables loaded from ./prisma/.env
prisma               : 2.21.0-dev.4
@prisma/client       : 2.21.0-dev.4
Current platform     : windows
Query Engine         : query-engine 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\query-engine-windows.exe)
Migration Engine     : migration-engine-cli 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\migration-engine-windows.exe)
Introspection Engine : introspection-core 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at C:\Users\veroh\AppData\Roaming\npm\node_modules\@prisma\cli\introspection-engine-windows.exe)
Format Binary        : prisma-fmt 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : 60ba6551f29b17d7d6ce479e5733c70d9c00860e
Studio               : 0.365.0
$prisma version --json

Hide CLI results

{
  "prisma": "2.21.0-dev.4",
  "@prisma/client": "2.21.0-dev.4",
  "current-platform": "windows",
  "query-engine": "query-engine 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\\@prisma\\engines\\query-engine-windows.exe)",
  "migration-engine": "migration-engine-cli 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\\@prisma\\engines\\migration-engine-windows.exe)",
  "introspection-engine": "introspection-core 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\\@prisma\\engines\\introspection-engine-windows.exe)",
  "format-binary": "prisma-fmt 60ba6551f29b17d7d6ce479e5733c70d9c00860e (at node_modules\\@prisma\\engines\\prisma-fmt-windows.exe)",
  "default-engines-hash": "60ba6551f29b17d7d6ce479e5733c70d9c00860e",
  "studio": "0.365.0"
}

init

在当前目录中引导一个新的 Prisma 项目。

init 命令不解释任何现有文件。 相反,它会创建一个 prisma 目录,其中包含您当前目录中的基本 schema.prisma 文件。

Arguments

Argument Required Description Default
--datasource-provider No Specifies the default value for the provider field in the datasource block - can be sqlite, postgresql, mysql, sqlserver, mongodb (Preview, requires preview flag). postgresql
--url No Define a custom datasource url.

Examples

Run prisma init
$prisma init

Hide CLI results

✔ Your Prisma schema was created at prisma/schema.prisma.
  You can now open it in your favorite editor.

Next steps:
1. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql or sqlite.
2. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started.
3. Run prisma db pull to turn your database schema into a Prisma data model.
4. Run prisma generate to install Prisma Client. You can then start querying your database.

More information in our documentation:
https://pris.ly/d/getting-started
Run prisma init --datasource-provider sqlite
$prisma init --datasource-provider sqlite

The command output contains helpful information on how to use the generated files and begin using Prisma with your project.

Generated Assets

prisma/schema.prisma

An initial schema.prisma file to define your schema in:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

prisma/.env

A file to define environment variables for your project:

# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="file:./dev.db"
Run prisma init --url mysql://user:password@localhost:3306/mydb
$prisma init --url mysql://user:password@localhost:3306/mydb

The command output contains helpful information on how to use the generated files and begin using Prisma with your project.

Generated Assets

prisma/schema.prisma

A minimal schema.prisma file to define your schema in:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

prisma/.env

A file to define environment variables for your project:

# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="mysql://user:password@localhost:3306/mydb"

generate

generate 命令在您的 prisma/schema.prisma 文件中生成 Prisma Client基本内容,如 generator 和 [data model](https ://www.prisma.io/docs/concepts/components/prisma-schema/data-model) 块。

generate 命令最常用于使用 prisma-client-js 生成器生成 Prisma Client。

这做了三件事:

  1. 搜索当前目录和父目录以找到适用的 npm 项目。 如果找不到,它将在当前目录中创建一个 package.json 文件。
  2. @prisma/client 安装到 npm 项目中(如果它不存在)。
  3. 检查当前目录以查找要处理的 Prisma schema 文件。 然后它将为您的项目生成一个自定义的 Prisma Client

Prerequisites(先决条件)

要使用 generate 命令,您必须在 schema.prisma 文件中添加生成器定义。 prisma-client-js 生成器,用于生成 Prisma Client,可以通过在 schema.prisma 中添加。

generator client {
  provider = "prisma-client-js"
}

Options

Option Required Description Default
--watch No The generate command will continue to watch the schema.prisma file and re-generate Prisma Client on file changes.

Arguments

Argument Required Description Default
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma, ./prisma/schema.prisma

Examples

Generate Prisma Client using the default schema.prisma path
$prisma generate

Hide CLI results

✔ Generated Prisma Client to ./node_modules/.prisma/client in 61ms

You can now start using Prisma Client in your code:

import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()

Explore the full API: https://pris.ly/d/client
Generate Prisma Client using a non-default schema.prisma path
$prisma generate --schema=./alternative/schema.prisma
Continue watching the schema.prisma file for changes to automatically re-generate Prisma Client
$prisma generate --watch

Hide CLI results

Watching... /home/prismauser/prisma/prisma-play/prisma/schema.prisma

✔ Generated Prisma Client to ./node_modules/.prisma/client in 45ms

Generated Assets

prisma-cleint-js 生成器会根据你的数据库(环境)创建一个自定义的客户端,并写入 ./node_modules/.prisma/client 目录,您可以 你可以自定义输出目录

introspect

弃用警告

From Prisma 3.0.0 onwards, the prisma introspect command is deprecated and replaced with the prisma db pull command.

这部分跳过不翻译

format

Formats the Prisma schema file, which includes validating, formatting, and persisting the schema.

Arguments

Argument Required Description Default
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma, ./prisma/schema.prisma

Examples

Validate a schema without errors
$npx prisma format

Show CLI results

Formatting a schema with validation errors
$npx prisma format

Show CLI results

db

db pull

Introspection with the db pull command on the MongoDB connector samples the data instead of reading a schema.

MongoDB 连接器 上使用 db pull 命令进行自省对数据进行采样而不是读取模式。

db pull 命令连接到您的数据库并将 Prisma 模型添加到反映当前数据库 schema 的 Prisma schema 中。

警告:该命令将使用新架构覆盖当前的 schema.prisma 文件。 任何手动更改或自定义都将丢失。 如果包含重要修改,请务必在运行 db pull 之前备份当前的 schema.prisma 文件。

Prerequisites

在使用 db pull 命令之前,您必须在 schema.prisma 中定义一个有效的 datasource `文件。

例如,以下 datasource 在当前目录中定义了一个 SQLite 数据库文件:

datasource db {
  provider = "sqlite"
  url      = "file:my-database.db"
}

Options

Option Required Description Default
--force No 强制覆盖对 schema 所做的手动更改。 生成的 schema 将仅基于自省模式。
--print No 将创建的 schema.prisma 打印到屏幕上,而不是将其写入文件系统。

Arguments

Argument Required Description Default
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma, ./prisma/schema.prisma

Examples

分析数据库并将其模式写入 schema.prisma 文件
$prisma db pull
Introspecting based on datasource defined in schema.prisma …

✔ Wrote Prisma data model into schema.prisma in 38ms

Run prisma generate to generate Prisma Client.
Specify an alternative schema.prisma file to read and write to
$prisma db pull --schema=./alternative/schema.prisma
Introspecting based on datasource defined in alternative/schema.prisma …

✔ Wrote Prisma data model into alternative/schema.prisma in 60ms

Run prisma generate to generate Prisma Client.
Display the generated schema.prisma file instead of writing it to the filesystem
$prisma db pull --print
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:./hello-prisma.db"
}

model User {
  email   String    @unique
  name    String?
  user_id Int       @id @default(autoincrement())
  post    Post[]
  profile Profile[]
}

model Post {
  content   String?
  post_id   Int     @id @default(autoincrement())
  title     String
  author    User?   @relation(fields: [author_id], references: [user_id])
  author_id Int?
}

model Profile {
  bio        String?
  profile_id Int     @id @default(autoincrement())
  user       User    @relation(fields: [user_id], references: [user_id])
  user_id    Int
}

db push

db push 命令在不使用迁移的情况下将 Prisma 模式文件的状态推送到数据库。 如果数据库不存在,它会创建数据库。

当您不需要版本模式更改时,例如在原型设计和本地开发期间,此命令是一个不错的选择。

See also:

Prerequisites

在使用 db push 命令之前,您必须在 schema.prisma 文件中定义一个有效的 datasource .

例如,以下 datasource 在当前目录中定义了一个 SQLite 数据库文件:

datasource db {
  provider = "sqlite"
  url      = "file:my-database.db"
}

Options

Options Required Description
--skip-generate No 跳过 Prisma Client 等工件的生成
--force-reset No 重置数据库,然后更新架构 - 如果由于无法执行的迁移而需要从头开始,这很有用。
--accept-data-loss No 忽略数据丢失警告。 如果由于进行架构更改而导致数据丢失,则需要此选项。
--help / --h No Displays the help message

Arguments

Argument Required Description Default
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma ./prisma/schema.prisma

Examples

Push the schema:

$npx prisma db push

Push the schema, accepting data loss:

$npx prisma db push --accept-data-loss

Push the schema with a custom schema location:

$npx prisma db push --schema=/tmp/schema.prisma

db seed

db seed 在 3.0.1 中从 Preview 更改为 General Available (GA)。

See Seeding your database

Options

Options Required Description
--help / --h No Displays the help message

Examples

$npx prisma db seed

db execute

此命令在 3.9.0 及更高版本的预览版中可用。MongoDB 目前不支持此命令。

MongoDB 目前不支持此命令。

此命令将 SQL 脚本应用于数据库,而不与 Prisma 迁移表交互。 该脚本有两个输入:

  • SQL 脚本,可以在标准输入或文件中提供
  • 数据源,可以是数据源的 URL 或 Prisma 模式文件的路径

该命令的输出是特定于连接器的,并不意味着返回数据,而只是报告成功或失败。

See also:

Prerequisites

在使用db execute命令之前,如果你不使用--url选项你必须定义一个有效的 datasource在你的 schema.prisma 文件中。

例如,以下 datasource 在当前目录中定义了一个 SQLite 数据库文件:

datasource db {
  provider = "sqlite"
  url      = "file:my-database.db"
}

Options

One of the following data source inputs is required:

Options Description
--url 要在其上运行命令的数据源的 URL
--schema Prisma 模式文件的路径,使用 datasource 块中的 URL

One of the following script inputs is required:

Options Description
--stdin 使用终端标准输入作为要执行的脚本
--file 文件的路径。 内容将作为要执行的脚本发送

Other options:

Options Required Description
--help No Displays the help message.

Examples

  • 获取位于 ./script.sql 的 SQL 文件的内容,并在 schema.prisma 文件的 datasource 块中的 URL 指定的数据库上执行它:

    $prisma db execute --preview-feature --file ./script.sql --schema schema.prisma
    
  • 从标准输入中获取 SQL 脚本并在 DATABASE_URL 环境变量中给出的数据源 URL 指定的数据库上执行它:

    $$ echo 'TRUNCATE TABLE dev;' | prisma db execute --preview-feature --stdin --url="$DATABASE_URL"
    

Prisma Migrate

Prisma Migrate changed from Preview to Generally Available (GA) in 2.19.0.

MongoDB not supported Prisma Migrate does not currently support the MongoDB connector.

migrate dev

For use in development environments only, requires shadow database

migrate dev 命令在开发过程中使用迁移更新你的数据库,如果数据库不存在则创建它。 也可以看看:

Options

Option Required Description Default
--create-only No 基于 schema 文件的修改,创建一个新的迁移,但不会执行该迁移。执行migrate dev来执行迁移
--skip-seed No Skip triggering seed
--skip-generate No Skip triggering generators (for example, Prisma Client)
--help / --h No Displays the help message

Arguments

Argument Required Description Default
--name No 迁移的名称,如果未指定,命令行会提示你输入。
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma ./prisma/schema.prisma

Examples

Apply all migrations, then create and apply any new migrations:

应用所有迁移,然后创建并应用任何新迁移??:

$prisma migrate dev

Apply all migrations and create a new migration if there are schema changes, but do not apply it:

如果架构发生更改,则应用所有迁移并创建新迁移,但不要应用它??:

$prisma migrate dev --create-only

migrate reset

仅用于开发环境

此命令删除并重新创建数据库,或通过删除所有数据、表、索引和其他工件来执行“软重置”。

Options

Option Required Description Default
--force No Skip the confirmation prompt
--skip-generate No Skip triggering generators (for example, Prisma Client)
--skip-seed No Skip triggering seed
--help / --h No Displays the help message

Arguments

Argument Required Description Default
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma ./prisma/schema.prisma

Examples

$prisma migrate reset

migrate deploy

migrate deploy 命令应用所有挂起的迁移,并在数据库不存在时创建它。 主要用于非开发环境。 这个命令:

  • Does not look for drift in the database or changes in the Prisma schema
  • Does not reset the database or generate artifacts
  • Does not rely on a shadow database

Options

Option Required Description Default
--help / --h No Displays the help message

Arguments

Argument Required Description Default
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma ./prisma/schema.prisma

Examples

$prisma migrate deploy

migrate resolve

migrate resolve 命令允许您通过将迁移标记为已应用(支持 baselining )或回滚来解决生产中的迁移历史问题。

Options

Option Required Description Default
--help / --h No Displays the help message

Arguments

Argument Required Description Default
--applied No* Record a specific migration as applied - for example --applied "20201231000000_add_users_table"
--rolled-back No* Record a specific migration as rolled back - for example --rolled-back "20201231000000_add_users_table" ./schema.prisma ./prisma/schema.prisma
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma ./prisma/schema.prisma

您必须指定 --rolled-back --applied

Examples

$prisma migrate resolve --applied 20201231000000_add_users_table
$prisma migrate resolve --rolled-back 20201231000000_add_users_table

migrate status

prisma migrate status 命令在 /prisma/migrations/* 文件夹中查找迁移以及 _prisma_migrations 表中的条目,并编译有关数据库中迁移状态的信息。 例如:

Status
3 migrations found in prisma/migrations

Your local migration history and the migrations table from your database are different:

The last common migration is: 20201127134938_new_migration

The migration have not yet been applied:
20201208100950_test_migration

The migrations from the database are not found locally in prisma/migrations:
20201208100950_new_migration

Options

Option Required Description Default
--help / --h No Displays the help message

Arguments

Argument Required Description Default
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma ./prisma/schema.prisma

Examples

$prisma migrate status

migrate diff

This command is available in Preview in versions 3.9.0 and later.

This command is only partially supported in MongoDB. See the command options below for details.

此命令比较两个数据库 schema 源并输出将第一个迁移到第二个状态的描述。

输出可以作为可读的摘要(默认)或可执行脚本给出。

The migrate diff command can only compare database features that are supported by Prisma. If two databases differ only in unsupported features, such as views or triggers, then migrate diff will not show any difference between them.

migrate diff 命令只能比较 Prisma 支持 的数据库功能。 如果两个数据库仅在不受支持的功能(例如视图或触发器)上有所不同,则 migrate diff 不会显示它们之间的任何差异。

The format of the command is:

$npx prisma migrate diff --preview-feature --from-... <source1> --to-... <source2>

其中 --from-...--to-... 选项是根据数据库模式源的类型选择的。 支持的来源类型有:

  • live databases
  • migration histories
  • Prisma data models
  • an empty schema

两个模式源必须使用相同的数据库提供程序。 例如,不支持比较 PostgreSQL 数据源与 SQLite 数据源的差异。

See also:

Prerequisites

在使用 migrate diff 命令之前,如果您使用的是 --from-schema-datasource--to-schema-datasource,则必须定义一个有效的 [datasource](https://www. prisma.io/docs/concepts/components/prisma-schema/data-sources)在您的 schema.prisma 文件中。

例如,以下 datasource 在当前目录中定义了一个 SQLite 数据库文件:

datasource db {
  provider = "sqlite"
  url      = "file:my-database.db"
}

Options

One of the following --from-... options is required:

Options Description Notes
--from-url A data source URL
--from-migrations Path to the Prisma Migrate migrations directory Not supported in MongoDB
--from-schema-datamodel Path to a Prisma schema file, uses the data model for the diff
--from-schema-datasource Path to a Prisma schema file, uses the URL in the datasource block for the diff
--from-empty Assume that you the data model you are migrating from is empty

One of the following --to-... options is required:

Options Description Notes
--to-url A data source URL
--to-migrations Path to the Prisma Migrate migrations directory Not supported in MongoDB
--to-schema-datamodel Path to a Prisma schema file, uses the data model for the diff
--to-schema-datasource Path to a Prisma schema file, uses the URL in the datasource block for the diff
--to-empty Assume that you the data model you are migrating to is empty

Other options:

Options Required Description Notes
--shadow-database-url No URL for the shadow database Only required if using --to-migrations or --from-migrations
--script No Outputs a SQL script instead of the default human-readable summary. Not supported in MongoDB
--help No Displays the help message.

Examples

  • 比较由其数据源 URL 指定的两个数据库,并输出默认的人类可读摘要:

    $ prisma migrate diff \
    $  --preview-feature \
    $  --from-url "$DATABASE_URL" \
    $  --to-url "postgresql://login:password@localhost:5432/db2"
    
  • 将带有 $DATABASE_URL 的 URL 的数据库状态与 ./migrations 目录中的迁移定义的模式进行比较,并将差异输出到脚本 script.sql

    $prisma migrate diff \
    $ --preview-feature \
    $ --from-url "$DATABASE_URL" \
    $ --to-migrations ./migrations \
    $ --script > script.sql
    

Studio

studio

studio 命令允许您以交互方式与数据交互和管理数据。 它通过使用配置有项目数据架构和记录的 Web 应用程序启动本地 Web 服务器来实现此目的。

Prerequisites

在使用 studio 命令之前,您必须在 schema.prisma 中定义一个有效的 datasource 文件。

例如,以下 datasource 在当前目录中定义了一个 SQLite 数据库文件:

datasource db {
  provider = "sqlite"
  url      = "file:my-database.db"
}

Options

The studio command recognizes the following options:

Option Required Description Default
-b, --browser No The browser to auto-open Studio in. <your-default-browser>
-h, --help No Show all available options and exit
-p, --port No The port number to start Studio on. 5555

Arguments

Argument Required Description Default
--schema No Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported. ./schema.prisma ./prisma/schema.prisma

Examples

在默认端口上启动 Studio 并打开一个新的浏览器选项卡

$prisma studio

在不同的端口上启动 Studio 并打开一个新的浏览器选项卡

$prisma studio --port 7777

启动 Studio 并打开一个 Firefox 标签页

$prisma studio --browser firefox

在不打开新浏览器选项卡的情况下启动 Studio

$prisma studio --browser none
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章