63 lines
No EOL
1.9 KiB
TypeScript
63 lines
No EOL
1.9 KiB
TypeScript
import {Repository} from "./Repository";
|
|
import {Nullable} from "../../types/Nullable";
|
|
import {DatabaseConnection} from "../DatabaseConnection";
|
|
import {ServerConfigurationModel} from "../Models/ServerConfigurationModel";
|
|
import ServerConfiguration, {DBServerConfiguration} from "../tables/ServerConfiguration";
|
|
import {Snowflake} from "discord.js";
|
|
|
|
export class ServerConfigurationRepository extends Repository<ServerConfigurationModel, DBServerConfiguration> {
|
|
|
|
constructor(
|
|
protected readonly database: DatabaseConnection,
|
|
) {
|
|
super(
|
|
database,
|
|
ServerConfiguration
|
|
);
|
|
}
|
|
|
|
public findServerConfigurations(server: Snowflake): ServerConfigurationModel[] {
|
|
return this.database.fetchAll<number, DBServerConfiguration>(
|
|
`SELECT * FROM serverConfiguration WHERE serverid = ?`,
|
|
server
|
|
).map((config) => {
|
|
return this.convertToModelType(config);
|
|
})
|
|
}
|
|
|
|
public findConfigurationByPath(server: Snowflake, path: string): Nullable<ServerConfigurationModel> {
|
|
const result = this.database.fetch<number, DBServerConfiguration>(
|
|
`SELECT * FROM serverConfiguration WHERE serverid = ? AND key = ?`,
|
|
server,
|
|
path
|
|
);
|
|
|
|
if (!result) {
|
|
return null;
|
|
}
|
|
|
|
return this.convertToModelType(result);
|
|
}
|
|
|
|
|
|
protected convertToModelType(intermediateModel: DBServerConfiguration | undefined): ServerConfigurationModel {
|
|
if (!intermediateModel) {
|
|
throw new Error("No intermediate model provided");
|
|
}
|
|
|
|
return {
|
|
id: intermediateModel.id,
|
|
serverid: intermediateModel.serverid,
|
|
key: intermediateModel.key,
|
|
value: intermediateModel.value,
|
|
}
|
|
}
|
|
|
|
protected convertToCreateObject(instance: Partial<ServerConfigurationModel>): object {
|
|
return {
|
|
serverid: instance.serverid ?? undefined,
|
|
key: instance.key ?? undefined,
|
|
value: instance.value ?? undefined,
|
|
}
|
|
}
|
|
} |