WIP: Add basic code for storing settings in the DB
This commit is contained in:
45
database.ts
45
database.ts
@@ -1,7 +1,28 @@
|
||||
import { Sequelize } from "sequelize";
|
||||
import { Model, Sequelize, STRING } from "sequelize";
|
||||
|
||||
import "node:fs/promises";
|
||||
import { readFile } from "node:fs";
|
||||
|
||||
export const sql = new Sequelize("sqlite://./blitzcrank.sqlite");
|
||||
|
||||
export class GuildSetting extends Model {
|
||||
declare guildId: string;
|
||||
declare key: string;
|
||||
declare value?: string;
|
||||
}
|
||||
|
||||
GuildSetting.init(
|
||||
{
|
||||
guildId: {
|
||||
type: STRING,
|
||||
primaryKey: true,
|
||||
},
|
||||
key: { type: STRING, primaryKey: true },
|
||||
value: { type: STRING },
|
||||
},
|
||||
{ sequelize: sql },
|
||||
);
|
||||
|
||||
export async function initDb() {
|
||||
try {
|
||||
await sql.authenticate();
|
||||
@@ -10,3 +31,25 @@ export async function initDb() {
|
||||
console.error("Unable to connect to the database:", error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function readSettingsFromFile(path: string) {
|
||||
readFile(path, async (err, data) => {
|
||||
try {
|
||||
const json = JSON.parse(data.toString());
|
||||
const toInsert: any[] = [];
|
||||
for (const [guildId, guildSettings] of json) {
|
||||
for (const [key, value] of guildSettings) {
|
||||
toInsert.push({
|
||||
guildId,
|
||||
key,
|
||||
value,
|
||||
});
|
||||
}
|
||||
}
|
||||
GuildSetting.bulkCreate(toInsert, { updateOnDuplicate: ["value"] });
|
||||
} catch (error) {
|
||||
// TODO
|
||||
console.log("Could not read settings:", error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user