add mysql db #54
@ -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
|
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)
|
replit can use Secrets, [documentation](https://docs.replit.com/programming-ide/storing-sensitive-information-environment-variables)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -3,4 +3,4 @@ app:
|
|||||||
port: 3000
|
port: 3000
|
||||||
|
|
||||||
db:
|
db:
|
||||||
type: sqlite # sqlite or mongodb
|
type: sqlite # sqlite or mongodb or mysql
|
@ -8,6 +8,9 @@ switch(config.db.type){
|
|||||||
case 'mongodb':
|
case 'mongodb':
|
||||||
db = require('./mongodb')
|
db = require('./mongodb')
|
||||||
break;
|
break;
|
||||||
|
case 'mysql':
|
||||||
|
db = require('./mysql')
|
||||||
|
break;
|
||||||
case 'sqlite':
|
case 'sqlite':
|
||||||
default:
|
default:
|
||||||
db = require('./sqlite')
|
db = require('./sqlite')
|
||||||
|
70
db/mysql.js
Normal file
70
db/mysql.js
Normal file
@ -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
|
||||||
|
}
|
2091
package-lock.json
generated
2091
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -18,6 +18,8 @@
|
|||||||
"image-size": "^0.8.3",
|
"image-size": "^0.8.3",
|
||||||
"mime-types": "^2.1.27",
|
"mime-types": "^2.1.27",
|
||||||
"mongoose": "^5.9.28",
|
"mongoose": "^5.9.28",
|
||||||
"pug": "^3.0.0"
|
"mysql2": "^3.3.5",
|
||||||
|
"pug": "^3.0.0",
|
||||||
|
"sqlite3": "^5.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user