0
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?

More than 1 year has passed since last update.

Node.jsのPrismaからIBMCloudDatabasesForEnterpriseDBに接続する

Posted at

やりたいこと

IBM Cloud Databases for EnterpriseDB(以下EDB)上で作られたDBがある。
Node.jsのORM Prisma を使って、このDBにアクセスしたい。

前提

  • EDBのインスタンスが立ち上がっていて、gold_kiwi (※)というDBがある。
    • DBには、すでにいくつかテーブルが作られている。
  • 利用環境は下記の通り。
    • Node.js: 20.3.1
    • Prisma: 5.2.0
  • こちらのチュートリアルに沿って、Prismaのインストールとinitまで終わっているものとする。

※ お寝坊さんのチームメンバーが「良い睡眠が取れますように」という願いを込めて、この名前になりました。。

ゴール

  • npx prisma db pullでPrisma schemaが更新されれば、接続成功とする。
    (テーブル作りが先行していたので、今回はmigrationを使わないことにしました。)

実施した内容

EDB

サービスクレデンシャルを準備

  1. IBM Cloudの管理画面で、EDBのインスタンスを開きます。
  2. Service credentialsNew credential をクリックします。
    image.png
  3. ダイアログが開くので、 Name に任意の名前を入力して、 Add をクリックします。
    image.png
  4. クレデンシャルが追加されました。
    image.png
  5. 追加したクレデンシャルをコピーします。
    image.png
  6. 適当なテキストエディタを開いて、コピーした内容を貼り付けます。
    中身はJsonになっています。
    要素 postgres にユーザー名とパスワードが入っているので、控えておいてください。
    "postgres": {
      "authentication": {
        "method": "direct",
        "password": "XXXX", ← パスワード
        "username": "YYYY" ← ユーザー名
      },

接続情報を調べる

  1. IBM Cloudの管理画面で、EDBのインスタンスを開きます。
  2. Overview > EndpointsPostgreSQL タブを開きます。
    image.png
  3. Endpointをコピーします。
    image.png
  4. テキストエディタを開いて、コピーした内容を貼り付けます。
    おそらく中身はこんな感じになっているので・・・
postgres://$USERNAME:$PASSWORD@[Hostnameが自動転記されている]:[Portが自動転記されている]/[Databaseが自動転記されている]?sslmode=verify-full

クレデンシャルとDB名を書き換えます。
(HostnameやPortがセットされていなかったら、これらもEndpointsの情報を転記しておきましょう。)

postgres://XXXX:YYYY@[Hostnameが自動転記されている]:[Portが自動転記されている]/gold_kiwi?sslmode=verify-full

Prisma(Node.js)

npx prisma init したフォルダの直下に .envファイルがあるので、DATABASE_URLを先ほど作った接続情報で置き換えます。

DATABASE_URL="postgres://XXXX:YYYY@[Hostnameが自動転記されている]:[Portが自動転記されている]/gold_kiwi?sslmode=verify-full"

.envを保存して閉じたら、npx prisma db pullを実行!
こんな感じのメッセージが出れば、接続成功です。

Prisma schema loaded from prisma/schema.prisma
Environment variables loaded from .env
Datasource "db": PostgreSQL database "gold_kiwi", schema "public" at "~~~~"

✔ Introspected 8 models and wrote them into prisma/schema.prisma in 2.39s

*** WARNING ***

The following models were ignored as they do not have a valid unique identifier or id. This is currently not supported by Prisma Client:
  - "comment"
  - "discuss"
  - "project"
  - "user_info"

Run npx prisma generate to generate Prisma Client.

prisma/schema.prismaを開きます。
Prismaスキーマが作られていたので、DBから情報を取得できたことを確認できました。

/// The underlying table does not contain a valid unique identifier and can therefore currently not be handled by Prisma Client.
model comment {
  comment_id       Int      @default(autoincrement())
  discuss_id       Int
  project_id       Int
  content          String   @db.VarChar(200)
  is_like          Boolean?
  create_user_id   Int
  create_timestamp DateTime @default(now()) @db.Timestamp(6)
  update_timestamp DateTime @default(now()) @db.Timestamp(6)

  @@ignore
}

引っ掛かりポイント

  • ユーザー名とパスワードの取得方法でしょうか?
0
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
0
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?