This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
Files
scoresaber-reloadedv3/apps/frontend/src/common/database/database.ts

76 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-09-08 22:35:32 +01:00
import Dexie, { EntityTable } from "dexie";
import { setPlayerIdCookie } from "../website-utils";
2024-09-11 23:10:16 +01:00
import BeatSaverMap from "./types/beatsaver-map";
2024-09-08 22:35:32 +01:00
import Settings from "./types/settings";
const SETTINGS_ID = "SSR"; // DO NOT CHANGE
export default class Database extends Dexie {
2024-09-11 20:10:27 +01:00
/**
* The settings for the website.
*/
2024-09-08 22:35:32 +01:00
settings!: EntityTable<Settings, "id">;
2024-09-11 23:10:16 +01:00
/**
2024-10-04 16:43:12 +01:00
* BeatSaver maps
2024-09-11 23:10:16 +01:00
*/
beatSaverMaps!: EntityTable<BeatSaverMap, "hash">;
2024-09-08 22:35:32 +01:00
constructor() {
super("ScoreSaberReloaded");
// Stores
this.version(1).stores({
settings: "id",
2024-09-11 23:10:16 +01:00
beatSaverMaps: "hash",
2024-09-08 22:35:32 +01:00
});
// Mapped tables
this.settings.mapToClass(Settings);
2024-09-11 23:10:16 +01:00
this.beatSaverMaps.mapToClass(BeatSaverMap);
2024-09-08 22:35:32 +01:00
// Populate default settings if the table is empty
this.on("populate", () => this.populateDefaults());
this.on("ready", async () => {
const settings = await this.getSettings();
// If the settings are not found, return
if (settings == undefined || settings.playerId == undefined) {
return;
}
setPlayerIdCookie(settings.playerId);
});
2024-09-08 22:35:32 +01:00
}
2024-09-11 20:10:27 +01:00
/**
* Populates the default settings
*/
2024-09-08 22:35:32 +01:00
async populateDefaults() {
2024-10-04 16:43:12 +01:00
await this.settings.add({
id: SETTINGS_ID, // Fixed ID for the single settings object
backgroundImage: "/assets/background.jpg",
});
2024-09-08 22:35:32 +01:00
}
/**
* Gets the settings from the database
*
* @returns the settings
*/
async getSettings(): Promise<Settings | undefined> {
2024-10-04 16:43:12 +01:00
return await this.settings.get(SETTINGS_ID);
2024-09-08 22:35:32 +01:00
}
/**
* Sets the settings in the database
*
* @param settings the settings to set
* @returns the settings
*/
async setSettings(settings: Settings) {
2024-10-04 16:43:12 +01:00
return await this.settings.update(SETTINGS_ID, settings);
2024-10-01 21:36:46 +01:00
}
2024-09-08 22:35:32 +01:00
}
export const db = new Database();