LoginSignup
1
2

More than 5 years have passed since last update.

Prismaの中で何が起こっているか(2) Introspect

Last updated at Posted at 2018-07-23

prisma init (既存データベースを利用するIntrospect)

通常prisma initコマンドでは新しいデータベース、テーブルを作成しますが、最近追加されたIntrospectでは、既存データベース・スキーマを読み込んでGraphQLのデータモデルを自動で書き起こすことをしてくれます。

さらに、既存データベースに接続するprisma用のdocker-compose.ymlファイルを作ってくれます。

やり方は簡単で、prisma init中に既存データがあるを選べばよいです。

$ prisma init test-load
? Set up a new Prisma server or deploy to an existing server? Use existing database
? What kind of database do you want to deploy to? PostgreSQL
? Does your database contain existing data? Yes ←★ここ
? Enter database host ←★PostgreSQLのアドレスを入れます
? Enter database port ←★ポート番号を入れます
? Enter database user prisma ←★ユーザ名を入れます(ここでは昔Prismaで作ったデータベースを参照します)
? Enter database password prisma
? Enter name of existing database prisma
? Use SSL? No
? Enter name of existing schema default$default ←★(これも昔Prismaで作ったためこの設定)

Introspecting database 90ms
Created datamodel definition based on 2 database tables.

Created 3 new files:

  prisma.yml           Prisma service definition
  datamodel.graphql    GraphQL SDL-based datamodel (foundation for database)
  docker-compose.yml   Docker configuration file

Next steps:

  1. Open folder: cd test-load
  2. Start your Prisma server: docker-compose up -d
  3. Deploy your Prisma service: prisma deploy
  4. Read more about introspection:
     http://bit.ly/prisma-introspection

もとのデータベースには下記のようなテーブルが含まれておりました。

prisma=# \d "User"
              Table "default$default.User"
  Column   |              Type              | Modifiers
-----------+--------------------------------+-----------
 id        | character varying(25)          | not null
 name      | text                           | not null
 updatedAt | timestamp(3) without time zone | not null
 createdAt | timestamp(3) without time zone | not null
Indexes:
    "User_pkey" PRIMARY KEY, btree (id)

そのため、出力されたdatamodel.graphqlには以下のようにGraphQLスキーマ形式で同じ内容が記載されます。

type User @pgTable(name: "User") {
  id: ID! @unique
  createdAt: DateTime!
  name: String!
  updatedAt: DateTime!
}
1
2
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
1
2