diff --git a/.github/workflows/javascript.yml b/.github/workflows/javascript.yml new file mode 100644 index 0000000..1fd7ab5 --- /dev/null +++ b/.github/workflows/javascript.yml @@ -0,0 +1,24 @@ +name: JavaScript CI + +on: + push: + paths: + - '.github/workflows/javascript.yml' + - 'javascript/**' + pull_request: + paths: + - '.github/workflows/javascript.yml' + - 'javascript/**' + +jobs: + ci: + strategy: + matrix: + module: + - name: 'mysql2' + with_oceanbase_container: true + uses: ./.github/workflows/basic-workflow.yml + with: + language: 'javascript' + module: ${{ matrix.module.name }} + with_oceanbase_container: ${{ matrix.module.with_oceanbase_container }} diff --git a/javascript/mysql2/README-CN.md b/javascript/mysql2/README-CN.md new file mode 100644 index 0000000..12933a4 --- /dev/null +++ b/javascript/mysql2/README-CN.md @@ -0,0 +1,59 @@ +# javascript连接OceanBase 指南 + +[English](README.md) | 简体中文 + +本文介绍如何通过javascript连接 OceanBase 数据库。 + +## 准备工作 + +需要创建一个项目,确认nodejs,npm,mysql2已经安装安装。 + +命令 + +``` +mkdir example +cd example +npm init -y + +npm install mysql2 + +``` + +创建 [index.js](index.js) 文件 + +``` +const mysql = require('mysql2'); + +const connection = mysql.createConnection({ + host: '127.0.0.1', // OceanBase服务器地址 + port: 2881, // OceanBase端口 + user: 'root', // 数据库用户名 + password: '', // 数据库密码 + database: 'test' // 数据库名称 +}); + +// 连接到数据库 +connection.connect(error => { + if (error) { + return console.error('连接到OceanBase数据库失败: ' + error.message); + } + + console.log('成功连接到OceanBase数据库'); + + // 这里可以执行其他数据库操作 + + // 关闭连接 + connection.end(err => { + if (err) { + return console.error('关闭数据库连接失败: ' + err.message); + } + console.log('关闭数据库连接成功'); + }); +}); +``` + +修改代码中的连接信息,之后你就可以直接使用命令行运行示例代码。 + +```bash +sh run.sh +``` diff --git a/javascript/mysql2/README.md b/javascript/mysql2/README.md new file mode 100644 index 0000000..52d0d0c --- /dev/null +++ b/javascript/mysql2/README.md @@ -0,0 +1,59 @@ +# JavaScript Connection OceanBase Guide + +English | [简体中文](README-CN.md) + +This article introduces how to connect to the OceanBase database through JavaScript. + +## prepare + +We need to create a project to confirm nodejs, NPM, mysql2 has been installed and installed. + +command + +``` +mkdir example +cd example +npm init -y + +npm install mysql2 + +``` + +create [index.js](index.js) file + +``` +const mysql = require('mysql2'); + +const connection = mysql.createConnection({ + host: '127.0.0.1', // OceanBase address + port: 2881, // OceanBase port + user: 'root', // username + password: '', // passwd + database: 'test' // database +}); + +// Connection OceanBase server +connection.connect(error => { + if (error) { + return console.error('Connection OceanBase faild: ' + error.message); + } + + console.log('Connection OceanBase Successd'); + + // Other Database Operations + + // Close Connection + connection.end(err => { + if (err) { + return console.error('Close Connection Faild: ' + err.message); + } + console.log('Close Connection Successd'); + }); +}); +``` + +Modify the connection information in the code, and then you can directly run the example code using the command line. + +```bash +sh run.sh +``` diff --git a/javascript/mysql2/index.js b/javascript/mysql2/index.js new file mode 100644 index 0000000..252354b --- /dev/null +++ b/javascript/mysql2/index.js @@ -0,0 +1,28 @@ +const mysql = require('mysql2'); + +const connection = mysql.createConnection({ + host: '127.0.0.1', // OceanBase服务器地址 + port: 2881, // OceanBase端口 + user: 'root', // 数据库用户名 + password: '', // 数据库密码 + database: 'test' // 数据库名称 +}); + +// 连接到数据库 +connection.connect(error => { + if (error) { + return console.error('连接到OceanBase数据库失败: ' + error.message); + } + + console.log('成功连接到OceanBase数据库'); + + // 这里可以执行其他数据库操作 + + // 关闭连接 + connection.end(err => { + if (err) { + return console.error('关闭数据库连接失败: ' + err.message); + } + console.log('关闭数据库连接成功'); + }); +}); diff --git a/javascript/mysql2/run.sh b/javascript/mysql2/run.sh new file mode 100644 index 0000000..bd16d03 --- /dev/null +++ b/javascript/mysql2/run.sh @@ -0,0 +1,8 @@ +mkdir example +cd example +npm init -y + +npm install mysql2 + +cp ../index.js ./ +node index.js