1import * as Effect from "effect/Effect";
2import type { Input } from "../Input.ts";
3import * as Output from "../Output.ts";
4import type { App } from "./App.ts";
5import type { Bucket } from "./Bucket.ts";
6import type { Database } from "./Database.ts";
7import type { Project } from "./Project.ts";
8
9export type InputObject<T extends object> = {
10 [Key in keyof T]: Input<T[Key]>;
11};
12
13export const isInputObject = <T extends object>(
14 value: Input<T>,
15): value is InputObject<T> & Input<T> =>
16 typeof value === "object" &&
17 value !== null &&
18 !Output.isOutput(value) &&
19 !Effect.isEffect(value);
20
21export const isPrismaDevId = (value: unknown): value is string =>
22 typeof value === "string" && value.startsWith("dev:");
23
24export const concreteIdOf = (value: unknown): string | undefined =>
25 typeof value === "string" && !isPrismaDevId(value) ? value : undefined;
26
27export const concreteIdsChanged = (
28 oldId: string | undefined,
29 newId: string | undefined,
30) => oldId !== undefined && newId !== undefined && newId !== oldId;
31
32const resolveId = (label: string, value: unknown) =>
33 typeof value === "string"
34 ? Effect.succeed(value)
35 : Effect.fail(new Error(`Unable to resolve Prisma ${label}.`));
36
37export const unresolvedProjectIdOf = (project: string | Project | undefined) =>
38 concreteIdOf(
39 typeof project === "string" ? project : (project?.projectId as unknown),
40 );
41
42export const resolveProjectId = (project: string | Project) =>
43 resolveId(
44 "project id",
45 typeof project === "string" ? project : (project.projectId as unknown),
46 );
47
48export const unresolvedDatabaseIdOf = (
49 database: string | Database | undefined,
50) =>
51 concreteIdOf(
52 typeof database === "string" ? database : (database?.databaseId as unknown),
53 );
54
55export const resolveDatabaseId = (database: string | Database) =>
56 resolveId(
57 "database id",
58 typeof database === "string" ? database : (database.databaseId as unknown),
59 );
60
61export const unresolvedBucketIdOf = (bucket: string | Bucket | undefined) =>
62 concreteIdOf(
63 typeof bucket === "string" ? bucket : (bucket?.bucketId as unknown),
64 );
65
66export const resolveBucketId = (bucket: string | Bucket) =>
67 resolveId(
68 "bucket id",
69 typeof bucket === "string" ? bucket : (bucket.bucketId as unknown),
70 );
71
72export const unresolvedAppIdOf = (app: string | App | undefined) =>
73 concreteIdOf(typeof app === "string" ? app : (app?.appId as unknown));
74
75export const resolveAppId = (app: string | App) =>
76 resolveId("app id", typeof app === "string" ? app : (app.appId as unknown));