Skip to content
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

add mysql db #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ If you use mongodb, you need to specify the environment variable `DB_URL`
export DB_URL=mongodb+srv://account:passwd@***.***.***.mongodb.net/db_count
```

If you use mysql, you need to specify the environment variable `DB_URL`

```shell
# eg:
export DB_URL=mysql://user:password@localhost:3306/database
```

replit can use Secrets, [documentation](https://docs.replit.com/programming-ide/storing-sensitive-information-environment-variables)

```
Expand Down
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ app:
port: 3000

db:
type: sqlite # sqlite or mongodb
type: sqlite # sqlite or mongodb or mysql
3 changes: 3 additions & 0 deletions db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ switch(config.db.type){
case 'mongodb':
db = require('./mongodb')
break;
case 'mysql':
db = require('./mysql')
break;
case 'sqlite':
default:
db = require('./sqlite')
Expand Down
70 changes: 70 additions & 0 deletions db/mysql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict'

const mysql = require('mysql2')

const mysqlURL = process.env.DB_URL || 'mysql://user:password@localhost:3306/database'

let hosts = mysqlURL.split("://")[1].split("/")[0];
let mysql_database = mysqlURL.split("://")[1].split("/")[1].split("?")[0];
let mysql_user = hosts.split("@")[0].split(":")[0];
let mysql_password = hosts.split("@")[0].split(":")[1];
let mysql_host = hosts.split("@")[1].split(":")[0];
let mysql_port = hosts.split("@")[1].split(":")[1];

const db = mysql.createPool({
host: mysql_host,
user: mysql_user,
password: mysql_password,
database: mysql_database,
port: mysql_port
})

db.query(`CREATE TABLE IF NOT EXISTS tb_count (
id INTEGER PRIMARY KEY AUTO_INCREMENT
NOT NULL
UNIQUE,
name VARCHAR (32) NOT NULL
UNIQUE,
num BIGINT NOT NULL
DEFAULT (0)
);`)

function getNum(name) {
return new Promise((resolve, reject) => {
db.query('SELECT `name`, `num` from tb_count WHERE `name` = ?', name, (err, rows) => {
if (err) reject(err)

resolve(rows[0] || { name, num: 0 })
})
})
}

function getAll(name) {
return new Promise((resolve, reject) => {
db.query('SELECT * from tb_count', (err, rows) => {
if (err) reject(err)

resolve(rows)
})
})
}

function setNum(name, num) {
return new Promise((resolve, reject) => {
db.query(`INSERT INTO tb_count(\`name\`, \`num\`)
VALUES(?, ?)
ON DUPLICATE KEY UPDATE \`num\` = ?;`
, [name, num, num]
, (err, result) => {
if (err) reject(err)

resolve(result)
})
})
}

module.exports = {
getNum,
getAll,
setNum
}
Loading