-
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 drizzle sample for javascript #76
Merged
Merged
Changes from all commits
Commits
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" |
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,60 @@ | ||
# 使用 Drizzle 连接 OceanBase | ||
|
||
[English](README.md) | 简体中文 | ||
|
||
本文介绍如何通过 [Drizzle](https://orm.drizzle.team) 连接 [OceanBase](https://www.oceanbase.com) 数据库。 | ||
|
||
## 准备工作 | ||
|
||
确保 Node.js 和 npm 已经安装。 | ||
|
||
## 项目使用 | ||
|
||
拉取项目并进入相应目录: | ||
|
||
```bash | ||
git clone [email protected]:oceanbase/ob-samples.git | ||
cd javascript/drizzle | ||
``` | ||
|
||
安装依赖: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
修改 `.env` 中的数据库连接串: | ||
|
||
```bash | ||
DATABASE_URL="mysql://root:@127.0.0.1:2881/test" | ||
``` | ||
|
||
将 `db/schema.ts` 中定义的 `users` 模型同步到数据库中: | ||
|
||
```bash | ||
npx drizzle-kit push | ||
``` | ||
|
||
执行 `index.ts` 中的示例代码: | ||
|
||
```bash | ||
npx ts-node index.ts | ||
``` | ||
|
||
输出以下内容,说明执行成功: | ||
|
||
```bash | ||
[ { id: 1, email: '[email protected]', name: 'Alice' } ] | ||
``` | ||
|
||
查看对应的 `users` 表,数据已正常插入: | ||
|
||
```bash | ||
mysql> select * from users; | ||
+----+---------------------+-------+ | ||
| id | email | name | | ||
+----+---------------------+-------+ | ||
| 1 | [email protected] | Alice | | ||
+----+---------------------+-------+ | ||
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,60 @@ | ||
# Connect to OceanBase with Drizzle | ||
|
||
English | [简体中文](README-CN.md) | ||
|
||
This document describes how to connect to [OceanBase](https://www.oceanbase.com) with [Drizzle](https://orm.drizzle.team). | ||
|
||
## Preparation | ||
|
||
Make sure `Node.js` and `npm` are installed. | ||
|
||
## Usage | ||
|
||
Clone the project and navigate to the appropriate directory: | ||
|
||
```bash | ||
git clone [email protected]:oceanbase/ob-samples.git | ||
cd javascript/drizzle | ||
``` | ||
|
||
Install dependencies: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
Modify the connection string in the `.env` file: | ||
|
||
```bash | ||
DATABASE_URL="mysql://root:@127.0.0.1:2881/test" | ||
``` | ||
|
||
Synchronize the `users` model defined in `db/schema.ts` to the database: | ||
|
||
```bash | ||
npx drizzle-kit push | ||
``` | ||
|
||
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' } ] | ||
``` | ||
|
||
Check the corresponding `users` table 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) | ||
``` |
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,7 @@ | ||
import { mysqlTable, bigint, varchar } from "drizzle-orm/mysql-core"; | ||
|
||
export const usersTable = mysqlTable("users", { | ||
id: bigint({ mode: "bigint" }).autoincrement().primaryKey(), | ||
email: varchar({ length: 255 }).notNull().unique(), | ||
name: varchar({ length: 255 }).notNull(), | ||
}); |
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,11 @@ | ||
import "dotenv/config"; | ||
import { defineConfig } from "drizzle-kit"; | ||
|
||
export default defineConfig({ | ||
out: "./drizzle", | ||
schema: "./db/schema.ts", | ||
dialect: "mysql", | ||
dbCredentials: { | ||
url: process.env.DATABASE_URL!, | ||
}, | ||
}); |
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,24 @@ | ||
import "dotenv/config"; | ||
import { drizzle } from "drizzle-orm/mysql2"; | ||
import { usersTable } from "./db/schema"; | ||
|
||
const db = drizzle(process.env.DATABASE_URL!); | ||
|
||
async function main() { | ||
const user: typeof usersTable.$inferInsert = { | ||
name: "Alice", | ||
email: "[email protected]", | ||
}; | ||
await db.insert(usersTable).values(user); | ||
|
||
const allUsers = await db.select().from(usersTable); | ||
console.log(allUsers); | ||
} | ||
|
||
main() | ||
.then(() => { | ||
process.exit(0); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
}); |
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,23 @@ | ||
{ | ||
"name": "drizzle", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"dependencies": { | ||
"dotenv": "^16.4.5", | ||
"drizzle-orm": "^0.36.0", | ||
"mysql2": "^3.11.3" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^22.8.5", | ||
"drizzle-kit": "^0.27.0", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.6.3" | ||
} | ||
} |
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,4 @@ | ||
npm install | ||
npx drizzle-kit generate --name=init | ||
npx drizzle-kit migrate | ||
npx ts-node index.ts |
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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2016", | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"module": "commonjs", | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"strictPropertyInitialization": false, | ||
"skipLibCheck": true | ||
} | ||
} |
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.3
supportsserial
data type and that is required for Drizzle.