Feat: Config repository
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
|
||||
import { Entity, EntityRepositoryType, PrimaryKey, Property } from "@mikro-orm/core";
|
||||
import { ConfigRepository } from "../repositories/Config";
|
||||
|
||||
@Entity({ tableName: "config" })
|
||||
@Entity({
|
||||
tableName: "config",
|
||||
repository: () => ConfigRepository,
|
||||
})
|
||||
export class ConfigEntity {
|
||||
[EntityRepositoryType]?: ConfigRepository;
|
||||
|
||||
@PrimaryKey({ type: "string" })
|
||||
name!: string;
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { EntityRepository } from "@mikro-orm/postgresql";
|
||||
import type { ConfigEntity } from "@/modules/entities/Config";
|
||||
|
||||
export class ConfigRepository extends EntityRepository<ConfigEntity> {
|
||||
async set(name: string, value: string) {
|
||||
await this.upsert({
|
||||
name,
|
||||
value,
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
async get(name: string, defaultValue?: string) {
|
||||
const row = await this.findOne({ name });
|
||||
|
||||
if (!row && defaultValue !== undefined) {
|
||||
await this.upsert({
|
||||
name,
|
||||
value: defaultValue,
|
||||
});
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return row?.value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user