【部分翻譯】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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章