Skip to content

Commit

Permalink
feat(doc): add node.js to develop
Browse files Browse the repository at this point in the history
  • Loading branch information
BohuTANG committed Apr 6, 2022
1 parent 4ef6f03 commit c575101
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/doc/20-develop/00-golang.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
62 changes: 62 additions & 0 deletions docs/doc/20-develop/02-nodejs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
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);
});

});
```

0 comments on commit c575101

Please sign in to comment.