1
0
mirror of https://github.com/journey-ad/Moe-Counter.git synced 2024-11-30 18:24:28 +01:00
Moe-Counter/db/sqlite.js
Jad 3bd1ae58e4 feat: support mongoDB
feat: support dynamic image size
docs: add Readme.md
2020-08-12 10:03:35 +00:00

60 lines
1.3 KiB
JavaScript

'use strict'
const path = require('path')
const sqlite3 = require('sqlite3')
const db = new sqlite3.Database(path.resolve(__dirname, '../count.db'))
db.run(`CREATE TABLE IF NOT EXISTS tb_count (
id INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL
UNIQUE,
name VARCHAR (32) NOT NULL
UNIQUE,
num BIGINT NOT NULL
DEFAULT (0)
);`)
function getNum(name) {
return new Promise((resolve, reject) => {
db.get('SELECT `name`, `num` from tb_count WHERE `name` = ?', name, (err, row) => {
if (err) reject(err)
resolve(row || { name, num: 0 })
})
})
}
function getAll(name) {
return new Promise((resolve, reject) => {
db.get('SELECT * from tb_count', (err, row) => {
if (err) reject(err)
resolve(row)
})
})
}
function setNum(name, num) {
return new Promise((resolve, reject) => {
db.run(`INSERT INTO tb_count(\`name\`, \`num\`)
VALUES($name, $num)
ON CONFLICT(name) DO
UPDATE SET \`num\` = $num;`
, {
$name: name,
$num: num
}
, (err, row) => {
if (err) reject(err)
resolve(row)
})
})
}
module.exports = {
getNum,
getAll,
setNum
}