Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add prisma sample for javascript #73

Merged
merged 2 commits into from
Dec 18, 2024

Conversation

dengfuping
Copy link
Contributor

@dengfuping dengfuping commented Oct 31, 2024


Connect to OceanBase with Prisma

This document describes how to connect to OceanBase with Prisma.

Preparation

Make sure Node.js and npm are installed.

Configuration

Clone the project and navigate to the appropriate directory:

git clone [email protected]:oceanbase/ob-samples.git
cd javascript/prisma

Set the global environment variables (for reasons see https://open.oceanbase.com/blog/15137753618):

export PRISMA_ENGINES_MIRROR=https://oceanbase-prisma-builds.s3.ap-southeast-1.amazonaws.com
export BINARY_DOWNLOAD_VERSION=96fa66f2f130d66795d9f79dd431c678a9c7104e

Install dependencies (note that the versions of prisma and @prisma/client should be ^5.20.0 or higher):

npm install

Modify the connection string in the .env file, formatted as follows. Note that prefer_socket=false must be set to avoid errors when connecting to OceanBase.

DATABASE_URL="mysql://root:@127.0.0.1:2881/test?prefer_socket=false&a=.psdb.cloud"

Execute the following command to synchronize the User, Post and Profile data models defined in prisma/schema.prisma to the database:

npx prisma migrate dev --name init
mysql> show tables;
+--------------------+
| Tables_in_test     |
+--------------------+
| _prisma_migrations |
| posts              |
| profiles           |
| users              |
+--------------------+
4 rows in set (0.02 sec)

Execute index.ts:

npx ts-node index.ts

The output should be as follows, indicating successful execution:

[
  {
    id: 1,
    email: '[email protected]',
    name: 'Alice',
    posts: [
      {
        id: 1,
        createdAt: 2024-10-31T04:33:45.535Z,
        updatedAt: 2024-10-31T04:33:45.535Z,
        title: 'Hello World',
        content: null,
        published: false,
        authorId: 1
      }
    ],
    profile: { id: 1, bio: 'I like turtles', userId: 1 }
  }
]

Check the corresponding users, posts and profiles tables and the data has been inserted:

mysql> select * from users;
+----+---------------------+-------+
| id | email               | name  |
+----+---------------------+-------+
|  1 | [email protected] | Alice |
+----+---------------------+-------+
1 row in set (0.01 sec)

mysql> select * from posts;
+----+-------------------------+-------------------------+-------------+---------+-----------+----------+
| id | createdAt               | updatedAt               | title       | content | published | authorId |
+----+-------------------------+-------------------------+-------------+---------+-----------+----------+
|  1 | 2024-10-31 04:33:45.535 | 2024-10-31 04:33:45.535 | Hello World | NULL    |         0 |        1 |
+----+-------------------------+-------------------------+-------------+---------+-----------+----------+
1 row in set (0.01 sec)

mysql> select * from profiles;
+----+----------------+--------+
| id | bio            | userId |
+----+----------------+--------+
|  1 | I like turtles |      1 |
+----+----------------+--------+
1 row in set (0.01 sec)

@dengfuping dengfuping force-pushed the prisma-sample branch 2 times, most recently from 474260f to 88808fb Compare October 31, 2024 09:49
@dengfuping dengfuping changed the title feat: Add prisma sample for javascript feat: Add prisma sample for javascript application Dec 2, 2024
@dengfuping dengfuping force-pushed the prisma-sample branch 2 times, most recently from a6439cc to 2e16005 Compare December 2, 2024 12:40
@dengfuping dengfuping changed the title feat: Add prisma sample for javascript application feat: Add prisma sample for javascript Dec 2, 2024
@@ -17,8 +17,12 @@ jobs:
module:
- name: 'mysql2'
with_oceanbase_container: true
- name: 'prisma'
with_oceanbase_container: true
oceanbase_image_tag: '4.2.2'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only oceanbase-ce >= 4.2.2 supports utf8mb4_unicode_ci collation and that is required for Prisma.

@whhe whhe merged commit 26cc63d into oceanbase:master Dec 18, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants