2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

prisma-lint を使用し schema ファイルの記述を統一する

Last updated at Posted at 2024-12-08

はじめに

Prisma の schema ファイル。Prisma を触るうえでは切っても切り離せない重要なファイルです。
そんな Prisma の schema file の linter の紹介です。

prisma-lint を導入することで schema file の記述が統一され、ファイルに追記する私はもちろんレビュアーの負荷も下げることができました。(おそらく)

prisma-lint のインストールと設定

インストール

npm を使っている場合は下記コマンドを実行してください。
yarn, pnpm 等利用されている方は読み替えてください。

npm install --save-dev prisma-lint

設定ファイルの作成

prisma-lint の設定ファイルは .prismalintrc.json です。
README.md に記述例がありますが eslint (eslintrc format) に似ているため eslintrc のルールを記述したことがある方ならスムーズに用意できると思います。

{
  "rules": {
    "field-name-mapping-snake-case": [
      "error",
      {
        "compoundWords": ["S3"]
      }
    ]
  }
}

prisma-lint に設定できるルールは、 RULES.md に記載されています。

ルール例

prisma-lint にはいくつかルールが存在します。
よく使うものをピックアップして記述します。

フィールドの記述順を揃える、必須フィールドを定義場合

"field-order を定義しルールを "order": ["id", "..."] のように設定することで id, id 以外のカラム の順になるように制限することができます。
加えて require-field-type で必ず用意するカラム制限も追加することが可能です。

{
  "rules": {
    "field-order": ["error", { "order": ["id", "..."] }],
    "require-field-type": [
      "error",
      {
        "require": [{ "ifName": "id", "type": "String" }]
      }
    ]
  }
}

その他 Rule

prisma-lint に設定できるルールは、 RULES.md に記載されているので適宜確認してください。

Ref

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?