-
Notifications
You must be signed in to change notification settings - Fork 22
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DATABASE_URL="mysql://root:@127.0.0.1:2881/test?prefer_socket=false&a=.psdb.cloud" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Prisma 连接 OceanBase 指南 | ||
|
||
[English](README.md) | 简体中文 | ||
|
||
本文介绍如何通过 [Prisma](https://www.prisma.io) 连接 [OceanBase](https://www.oceanbase.com) 数据库。 | ||
|
||
## 准备工作 | ||
|
||
确保 Nodejs 和 npm 已经安装。 | ||
|
||
## 项目配置 | ||
|
||
拉取项目并进入相应目录: | ||
|
||
```bash | ||
git clone [email protected]:oceanbase/ob-samples.git | ||
cd javascript/prisma | ||
``` | ||
|
||
全局配置环境变量 (原因详见 https://open.oceanbase.com/blog/15137753618): | ||
|
||
```bash | ||
export PRISMA_ENGINES_MIRROR=https://oceanbase-prisma-builds.s3.ap-southeast-1.amazonaws.com | ||
export BINARY_DOWNLOAD_VERSION=96fa66f2f130d66795d9f79dd431c678a9c7104e | ||
``` | ||
|
||
安装依赖 (注意 `prisma` 和 `@prisma/client` 版本在 `^5.20.0` 及以上): | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
修改 `.env` 中的数据库连接串,格式如下。注意需要设置 `prefer_socket=false`,以避免和 OceanBase 建立连接时报错。 | ||
|
||
```bash | ||
DATABASE_URL="mysql://root:@127.0.0.1:2881/test?prefer_socket=false&a=.psdb.cloud" | ||
``` | ||
|
||
执行以下命令,将 `prisma/schema.prisma` 中定义的 `User`、`Post` 和 `Profile` 模型同步到数据库中: | ||
|
||
```bash | ||
npx prisma migrate dev --name init | ||
``` | ||
|
||
```sql | ||
mysql> show tables; | ||
+--------------------+ | ||
| Tables_in_test | | ||
+--------------------+ | ||
| _prisma_migrations | | ||
| posts | | ||
| profiles | | ||
| users | | ||
+--------------------+ | ||
4 rows in set (0.02 sec) | ||
``` | ||
|
||
执行 `index.ts`: | ||
|
||
```bash | ||
npx ts-node index.ts | ||
``` | ||
|
||
输出以下内容,说明执行成功: | ||
|
||
```bash | ||
[ | ||
{ | ||
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 } | ||
} | ||
] | ||
``` | ||
|
||
查看对应的 `users`、`posts` 和 `profiles` 表,数据已正常插入: | ||
|
||
```bash | ||
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Connect to OceanBase with Prisma | ||
|
||
English | [简体中文](README-CN.md) | ||
|
||
This document describes how to connect to [OceanBase](https://www.oceanbase.com) with [Prisma](https://www.prisma.io). | ||
|
||
## Preparation | ||
|
||
Make sure `Node.js` and `npm` are installed. | ||
|
||
## Configuration | ||
|
||
Clone the project and navigate to the appropriate directory: | ||
|
||
```bash | ||
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): | ||
|
||
```bash | ||
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): | ||
|
||
```bash | ||
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. | ||
|
||
```bash | ||
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: | ||
|
||
```bash | ||
npx prisma migrate dev --name init | ||
``` | ||
|
||
```sql | ||
mysql> show tables; | ||
+--------------------+ | ||
| Tables_in_test | | ||
+--------------------+ | ||
| _prisma_migrations | | ||
| posts | | ||
| profiles | | ||
| users | | ||
+--------------------+ | ||
4 rows in set (0.02 sec) | ||
``` | ||
|
||
Execute `index.ts`: | ||
|
||
```bash | ||
npx ts-node index.ts | ||
``` | ||
|
||
The output should be as follows, indicating successful execution: | ||
|
||
```bash | ||
[ | ||
{ | ||
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: | ||
|
||
```bash | ||
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
async function main() { | ||
await prisma.user.create({ | ||
data: { | ||
name: "Alice", | ||
email: "[email protected]", | ||
posts: { | ||
create: { title: "Hello World" }, | ||
}, | ||
profile: { | ||
create: { bio: "I like turtles" }, | ||
}, | ||
}, | ||
}); | ||
|
||
const allUsers = await prisma.user.findMany({ | ||
include: { | ||
posts: true, | ||
profile: true, | ||
}, | ||
}); | ||
console.dir(allUsers, { depth: null }); | ||
} | ||
|
||
main() | ||
.then(async () => { | ||
await prisma.$disconnect(); | ||
}) | ||
.catch(async (e) => { | ||
console.error(e); | ||
await prisma.$disconnect(); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "prisma", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"devDependencies": { | ||
"@types/node": "^22.8.4", | ||
"prisma": "^5.21.1", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.6.3" | ||
}, | ||
"dependencies": { | ||
"@prisma/client": "^5.21.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// This is your Prisma schema file, | ||
// learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "mysql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model Post { | ||
id Int @id @default(autoincrement()) | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
title String @db.VarChar(255) | ||
content String? | ||
published Boolean @default(false) | ||
author User @relation(fields: [authorId], references: [id]) | ||
authorId Int | ||
|
||
@@map("posts") | ||
} | ||
|
||
model Profile { | ||
id Int @id @default(autoincrement()) | ||
bio String? | ||
user User @relation(fields: [userId], references: [id]) | ||
userId Int @unique | ||
|
||
@@map("profiles") | ||
} | ||
|
||
model User { | ||
id Int @id @default(autoincrement()) | ||
email String @unique | ||
name String? | ||
posts Post[] | ||
profile Profile? | ||
|
||
@@map("users") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export PRISMA_ENGINES_MIRROR=https://oceanbase-prisma-builds.s3.ap-southeast-1.amazonaws.com | ||
export BINARY_DOWNLOAD_VERSION=96fa66f2f130d66795d9f79dd431c678a9c7104e | ||
npm install | ||
npx prisma migrate dev --name init | ||
npx ts-node index.ts |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
supportsutf8mb4_unicode_ci
collation and that is required for Prisma.