Feat: Add Replit Database support.

This commit is contained in:
MashiroSA 2023-03-14 21:30:13 +08:00
parent 6b4d7a010a
commit 1edf2b73f1
3 changed files with 35 additions and 2 deletions

View File

@ -2,4 +2,4 @@ app:
port: 3000
db:
type: sqlite # sqlite or mongodb
type: sqlite # sqlite or mongodb or repl(using replit inbuilt database)

View File

@ -4,10 +4,13 @@ const config = require('config-yml')
let db
switch(config.db.type){
switch (config.db.type) {
case 'mongodb':
db = require('./mongodb')
break;
case 'repl':
db = require('./repl')
break;
case 'sqlite':
default:
db = require('./sqlite')

30
db/repl.js Normal file
View File

@ -0,0 +1,30 @@
const Database = require("@replit/database")
const db = new Database()
async function getNum(name) {
const num = await db.get(name)
return { name, num: num || 0 }
}
async function getAll() {
const keys = await db.list()
const rows = await Promise.all(keys.map(async key => ({ name: key, num: await db.get(key) || 0 })))
return rows
}
async function setNum(name, num) {
await db.set(name, num)
}
async function setNumMulti(counters) {
const promises = counters.map(({ name, num }) => db.set(name, num))
await Promise.all(promises)
}
module.exports = {
getNum,
getAll,
setNum,
setNumMulti
}