mirror of
https://github.com/captbaritone/webamp.git
synced 2026-07-21 00:59:29 +00:00
Use sqlite to store arbitrary JSON for quick and dirty state storage
This commit is contained in:
parent
684d7a972d
commit
367b85448f
3 changed files with 54 additions and 0 deletions
28
packages/skin-database/data/KeyValue.ts
Normal file
28
packages/skin-database/data/KeyValue.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { knex } from "../db";
|
||||
|
||||
export default class KeyValue {
|
||||
static async get(key: string): Promise<any> {
|
||||
const result = await knex("key_value").where({key}).first("value");
|
||||
return JSON.parse(result.value);
|
||||
}
|
||||
|
||||
static async set(key: string, value: any): Promise<void> {
|
||||
const {count} = (await knex("key_value").where({key}).count({count: '*'}).first() as {count: number});
|
||||
if(count) {
|
||||
return await KeyValue.update(key, value);
|
||||
} else {
|
||||
return await KeyValue.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
static async update(key: string, value: any): Promise<void> {
|
||||
const json = JSON.stringify(value);
|
||||
await knex("key_value").where({key}).update({value: json});
|
||||
}
|
||||
|
||||
static async insert(key: string, value: any): Promise<any> {
|
||||
const json = JSON.stringify(value);
|
||||
await knex("key_value").insert({key, value: json});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
import * as Knex from "knex";
|
||||
|
||||
|
||||
export async function up(knex: Knex): Promise<any> {
|
||||
await knex.raw(
|
||||
`CREATE TABLE "key_value" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
||||
"key" TEXT NOT NULL UNIQUE,
|
||||
value TEXT NOT_NULL
|
||||
);`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
export async function down(knex: Knex): Promise<any> {
|
||||
await knex.raw(`DROP TABLE "key_value"`);
|
||||
}
|
||||
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import { getTwitterClient } from "../twitter";
|
||||
import fs from "fs";
|
||||
import DiscordEventHandler from "../api/DiscordEventHandler";
|
||||
import KeyValue from "../data/KeyValue";
|
||||
|
||||
const MAX_CALL_COUNT = 2;
|
||||
|
||||
|
|
@ -51,6 +52,7 @@ function tweetUrl(tweet: { id_str: string }) {
|
|||
}
|
||||
|
||||
const JSON_FILE_NAME = "./popularTweets.json";
|
||||
const KEY = "tweet_milestones";
|
||||
|
||||
// Sort key/value entries by key largest to smallest
|
||||
function sortEntries(a: [string, never], b: [string, never]): number {
|
||||
|
|
@ -65,6 +67,11 @@ export async function popularTweets(handler: DiscordEventHandler) {
|
|||
const currentJSON = fs.readFileSync(JSON_FILE_NAME, "utf8");
|
||||
const current: { [bracket: string]: string[] } = JSON.parse(currentJSON);
|
||||
|
||||
const kvCurrent = await KeyValue.get(KEY);
|
||||
if(JSON.stringify(kvCurrent) === JSON.stringify(current)) {
|
||||
console.warn("KV value does not match!")
|
||||
}
|
||||
|
||||
for (const tweet of tweets) {
|
||||
let notified = false;
|
||||
for (const [_bracket, seen] of Object.entries(current).sort(sortEntries)) {
|
||||
|
|
@ -85,6 +92,7 @@ export async function popularTweets(handler: DiscordEventHandler) {
|
|||
notified = true;
|
||||
}
|
||||
|
||||
await KeyValue.set(KEY, current);
|
||||
fs.writeFileSync(JSON_FILE_NAME, JSON.stringify(current, null, 2));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue