-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add sequelize sample for javascript
- Loading branch information
1 parent
beeb74f
commit 2187649
Showing
4 changed files
with
204 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# 使用 Sequelize 连接 OceanBase | ||
|
||
[English](README.md) | 简体中文 | ||
|
||
本文介绍如何通过 [Sequelize](https://sequelize.org) 连接 [OceanBase](https://www.oceanbase.com) 数据库。 | ||
|
||
## 准备工作 | ||
|
||
确保 Node.js 和 npm 已经安装。 | ||
|
||
## 项目使用 | ||
|
||
拉取项目并进入相应目录: | ||
|
||
```bash | ||
git clone [email protected]:oceanbase/ob-samples.git | ||
cd javascript/sequelize | ||
``` | ||
|
||
安装依赖: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
修改 `index.js` 中的数据库连接串: | ||
|
||
```javascript | ||
const sequelize = new Sequelize( | ||
"mysql://username:password@localhost:3306/test", | ||
{ | ||
dialect: "mysql", | ||
logging: false, | ||
} | ||
); | ||
``` | ||
|
||
执行 `index.js`: | ||
|
||
```bash | ||
node index.js | ||
``` | ||
|
||
输出以下内容,说明执行成功: | ||
|
||
```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,66 @@ | ||
# Connect to OceanBase with Sequelize | ||
|
||
English | [简体中文](README-CN.md) | ||
|
||
This document describes how to connect to [OceanBase](https://www.oceanbase.com) with [Sequelize](https://sequelize.org). | ||
|
||
## 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/sequelize | ||
``` | ||
|
||
Install dependencies: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
Modify the connection string in the `index.js` file: | ||
|
||
```javascript | ||
const sequelize = new Sequelize( | ||
"mysql://username:password@localhost:3306/test", | ||
{ | ||
dialect: "mysql", | ||
logging: false, | ||
} | ||
); | ||
``` | ||
|
||
Execute `index.js`: | ||
|
||
```bash | ||
node index.js | ||
``` | ||
|
||
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,56 @@ | ||
const { Sequelize, DataTypes } = require("sequelize"); | ||
|
||
const sequelize = new Sequelize( | ||
"mysql://username:password@localhost:3306/test", | ||
{ | ||
dialect: "mysql", | ||
logging: false, | ||
} | ||
); | ||
|
||
const UserModel = sequelize.define( | ||
"users", | ||
{ | ||
id: { | ||
field: "id", | ||
type: DataTypes.BIGINT, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
allowNull: false, | ||
}, | ||
email: { | ||
field: "email", | ||
type: DataTypes.STRING, | ||
unique: true, | ||
allowNull: false, | ||
}, | ||
name: { | ||
field: "name", | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
}, | ||
{ | ||
timestamps: false, | ||
} | ||
); | ||
|
||
async function main() { | ||
await UserModel.sync(); | ||
await UserModel.create({ | ||
name: "Alice", | ||
email: "[email protected]", | ||
}); | ||
const allUsers = await UserModel.findAll(); | ||
console.log(JSON.stringify(allUsers, null, 2)); | ||
} | ||
|
||
main() | ||
.then(async () => { | ||
await sequelize.close(); | ||
}) | ||
.catch(async (e) => { | ||
console.error(e); | ||
await sequelize.close(); | ||
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,16 @@ | ||
{ | ||
"name": "sequelize", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"dependencies": { | ||
"mysql2": "^3.11.3", | ||
"sequelize": "^6.37.5" | ||
} | ||
} |