Skip to content

Commit 8076a44

Browse files
committed
Added run repository feature flag, allowing passing a default when getting a flag
1 parent 5800cb4 commit 8076a44

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

apps/webapp/app/v3/featureFlags.server.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
import { z } from "zod";
2-
import { prisma, PrismaClientOrTransaction } from "~/db.server";
2+
import { prisma, type PrismaClientOrTransaction } from "~/db.server";
33

44
export const FEATURE_FLAG = {
55
defaultWorkerInstanceGroupId: "defaultWorkerInstanceGroupId",
6+
runsListRepository: "runsListRepository",
67
} as const;
78

89
const FeatureFlagCatalog = {
910
[FEATURE_FLAG.defaultWorkerInstanceGroupId]: z.string(),
11+
[FEATURE_FLAG.runsListRepository]: z.enum(["clickhouse", "postgres"]),
1012
};
1113

1214
type FeatureFlagKey = keyof typeof FeatureFlagCatalog;
1315

14-
export type FlagsOptions = {
15-
key: FeatureFlagKey;
16+
export type FlagsOptions<T extends FeatureFlagKey> = {
17+
key: T;
18+
defaultValue?: z.infer<(typeof FeatureFlagCatalog)[T]>;
1619
};
1720

1821
export function makeFlags(_prisma: PrismaClientOrTransaction = prisma) {
19-
return async function flags<T extends FeatureFlagKey>(
20-
opts: FlagsOptions
22+
function flags<T extends FeatureFlagKey>(
23+
opts: FlagsOptions<T> & { defaultValue: z.infer<(typeof FeatureFlagCatalog)[T]> }
24+
): Promise<z.infer<(typeof FeatureFlagCatalog)[T]>>;
25+
function flags<T extends FeatureFlagKey>(
26+
opts: FlagsOptions<T>
27+
): Promise<z.infer<(typeof FeatureFlagCatalog)[T]> | undefined>;
28+
async function flags<T extends FeatureFlagKey>(
29+
opts: FlagsOptions<T>
2130
): Promise<z.infer<(typeof FeatureFlagCatalog)[T]> | undefined> {
2231
const value = await _prisma.featureFlag.findUnique({
2332
where: {
@@ -28,16 +37,18 @@ export function makeFlags(_prisma: PrismaClientOrTransaction = prisma) {
2837
const parsed = FeatureFlagCatalog[opts.key].safeParse(value?.value);
2938

3039
if (!parsed.success) {
31-
return;
40+
return opts.defaultValue;
3241
}
3342

3443
return parsed.data;
35-
};
44+
}
45+
46+
return flags;
3647
}
3748

3849
export function makeSetFlags(_prisma: PrismaClientOrTransaction = prisma) {
3950
return async function setFlags<T extends FeatureFlagKey>(
40-
opts: FlagsOptions & { value: z.infer<(typeof FeatureFlagCatalog)[T]> }
51+
opts: FlagsOptions<T> & { value: z.infer<(typeof FeatureFlagCatalog)[T]> }
4152
): Promise<void> {
4253
await _prisma.featureFlag.upsert({
4354
where: {

0 commit comments

Comments
 (0)