From e28e5eb7008f6f2e41443e21f218e8c95c0e3770 Mon Sep 17 00:00:00 2001 From: BohuTANG Date: Wed, 6 Apr 2022 09:34:57 +0800 Subject: [PATCH] feat(doc): add node.js to develop --- docs/doc/20-develop/00-golang.md | 2 +- docs/doc/20-develop/02-nodejs.md | 71 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 docs/doc/20-develop/02-nodejs.md diff --git a/docs/doc/20-develop/00-golang.md b/docs/doc/20-develop/00-golang.md index 68fede4daa3f..a54f2249ef08 100644 --- a/docs/doc/20-develop/00-golang.md +++ b/docs/doc/20-develop/00-golang.md @@ -88,7 +88,7 @@ func main() { log.Println("Insert 1 row") // Select. - res, err := db.Query("SELECT * FROM books") + res, err := db.Query("select * from books") if err != nil { log.Fatal(err) } diff --git a/docs/doc/20-develop/02-nodejs.md b/docs/doc/20-develop/02-nodejs.md new file mode 100644 index 000000000000..c706e565a230 --- /dev/null +++ b/docs/doc/20-develop/02-nodejs.md @@ -0,0 +1,71 @@ +--- +title: How to Work with Databend in Node.js +sidebar_label: node.js +description: + How to Work with Databend in Node.js +--- + +### Before You Begin + +* **Databend :** Make sure Databend is running and accessible, see [How to deploy Databend](/doc/deploy). +* Install the mysql node module using the NPM: npm install --save mysql + +### Node.js + +This guideline show how to connect and query to Databend using Node.js. + +We will be creating a table named `books` and insert a row, then query it. + +```js +const mysql = require('mysql'); +const con = mysql.createConnection({ + host: 'localhost', + port: 3307, + user: 'root', + password: '', +}); + +con.connect((err) => { + if (err) throw err; + console.log('Connected to Databend Server!'); + + var sql = "create database if not exists book_db"; + con.query(sql, function (err, result) { + if (err) throw err; + console.log("Dataabse created"); + }); + + var sql = "use book_db"; + con.query(sql, function (err, result) { + if (err) throw err; + }); + + + var sql = "create table if not exists books(title varchar(255), author varchar(255), date varchar(255))"; + con.query(sql, function (err, result) { + if (err) throw err; + console.log("Table created"); + }); + + var sql = "insert into books values('mybook', 'author', '2022')"; + con.query(sql, function (err, result) { + if (err) throw err; + console.log("1 record inserted"); + }); + + con.query("select * from books", function (err, result, fields) { + if (err) throw err; + console.log(result); + }); + +}); +``` + +The output: +```shell +Connected to MySQL Server! +Dataabse created +Table created +1 record inserted +[ RowDataPacket { title: 'mybook', author: 'author', date: '2022' } ] +```