Feat: Config repository

This commit is contained in:
2026-03-19 11:37:36 +09:00
parent 50657066a6
commit 47556e5fec
2 changed files with 36 additions and 2 deletions
@@ -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;
}
}