import { Retry } from "@distilled.cloud/prisma";
import { AuthProviders, AuthProvidersLive } from "alchemy/Auth/AuthProvider";
import { CredentialsStoreLive } from "alchemy/Auth/Credentials";
import { ProfileStore, ProfileStoreLive } from "alchemy/Auth/Profile";
import * as Command from "alchemy/Command";
import * as Provider from "alchemy/Provider";
import { PlatformServices } from "alchemy/Util/PlatformServices";
import { proxyChain } from "alchemy/Util/proxy-chain";
import { Server, ServerProvider } from "alchemy/Website/Server";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as Layer from "effect/Layer";
import { App, AppProvider } from "./App.ts";
import { PrismaAuth } from "./AuthProvider.ts";
import { Branch, BranchProvider } from "./Branch.ts";
import { Bucket, BucketProvider } from "./Bucket.ts";
import { BucketAccessKey, BucketAccessKeyProvider } from "./BucketAccessKey.ts";
import { PrismaClient, PrismaClientLive, type PrismaManagementClient } from "./Client.ts";
import { Compute, ComputeProvider } from "./Compute.ts";
import { Connection, ConnectionProvider } from "./Connection.ts";
import { CustomDomain, CustomDomainProvider } from "./CustomDomain.ts";
import { Database, DatabaseProvider } from "./Database.ts";
import { Deployment, DeploymentProvider } from "./Deployment.ts";
import { EnvironmentVariable, EnvironmentVariableProvider } from "./EnvironmentVariable.ts";
import * as Credentials from "./internal/Credentials.ts";
import { PrismaHttpClientLive, PrismaUploadClientLive } from "./internal/HttpClient.ts";
import { Contract, ContractProvider } from "./ORM/Contract.ts";
import { Migrate, MigrateProvider } from "./ORM/Migrate.ts";
import { fromProfile } from "./PrismaEnvironment.ts";
import { Project, ProjectProvider } from "./Project.ts";
import { SourceRepository, SourceRepositoryProvider } from "./SourceRepository.ts";
import { WebsiteArtifact, WebsiteArtifactProvider } from "./Website/Artifact.ts";
export { PrismaEnvironment } from "./PrismaEnvironment.ts";
export class Providers extends Provider.ProviderCollection<Providers>()("Prisma") {}
export type ProviderRequirements = Layer.Services<ReturnType<typeof providers>>;
/**
* Standalone operation helpers own a private auth registry because they run
* outside a Stack. Credential resolution stays eager here so constructing
* `managementApi()` preserves its existing fail-fast behavior — which also
* lets the distilled `Credentials` service read the already-resolved
* {@link PrismaEnvironment} rather than building its own.
*/
const standaloneManagementApiLayer = () =>
PrismaClientLive.pipe(
Layer.provideMerge(
Layer.mergeAll(Credentials.fromEnvironment(), Layer.succeed(Retry.Retry, Retry.makeDefault)),
),
Layer.provideMerge(fromProfile()),
Layer.provideMerge(PrismaAuth),
Layer.provideMerge(
Layer.mergeAll(
AuthProvidersLive,
// The Prisma-scoped upload client (node transport) rides the
// providers' output so artifact uploads can reach it at op time.
PrismaUploadClientLive,
Layer.provide(ProfileStoreLive, PlatformServices),
Layer.provide(CredentialsStoreLive, PlatformServices),
),
),
// Provide (NOT provideMerge) the node transport privately: it must serve
// only the Prisma management client. Exposing it from `providers()` would
// override the ambient HttpClient for every other provider in the stack —
// Cloudflare Worker script uploads then go out via node:http, which
// streams multipart bodies as `Transfer-Encoding: chunked`, and
// api.cloudflare.com never answers chunked uploads.
Layer.provide(PrismaHttpClientLive),
);
/**
* Stack provider discovery must register auth without requiring credentials.
* The management client is resolved on its first API operation, after
* `alchemy profile edit` has had a chance to configure the registered Prisma
* auth provider. The nested client layer shares the provider layer's lifetime.
*/
const stackManagementApiLayer = () =>
Layer.effect(
PrismaClient,
Effect.gen(function* () {
const scope = yield* Effect.scope;
const authProviders = yield* AuthProviders;
const profileStore = yield* ProfileStore;
const client = Layer.buildWithScope(
PrismaClientLive.pipe(
Layer.provideMerge(
fromProfile().pipe(
Layer.provide(
Layer.mergeAll(
Layer.succeed(AuthProviders, authProviders),
Layer.succeed(ProfileStore, profileStore),
),
),
),
),
Layer.provide(PrismaHttpClientLive),
),
scope,
).pipe(Effect.map((context) => Context.get(context, PrismaClient)));
const cached = yield* Effect.cached(client);
return proxyChain(cached) as PrismaManagementClient;
}),
).pipe(
Layer.provideMerge(
Layer.mergeAll(Credentials.fromAuthProvider(), Layer.succeed(Retry.Retry, Retry.makeDefault)),
),
Layer.provideMerge(PrismaAuth),
Layer.provideMerge(
Layer.mergeAll(
// The Prisma-scoped upload client (node transport) rides the
// providers' output so artifact uploads can reach it at op time.
PrismaUploadClientLive,
Layer.provide(ProfileStoreLive, PlatformServices),
Layer.provide(CredentialsStoreLive, PlatformServices),
),
),
);
/**
* Build a layer for Prisma Management API operation helpers.
*
* Use this when calling helpers like `Prisma.listProjects()` outside an
* Alchemy stack or test. Inside a stack (or a `test.provider` body) deployed
* with `Prisma.providers()`, the management client is already in context, so
* operation helpers work without providing this layer.
*
* @example
* ```typescript
* const projects = yield* Prisma.listProjects().pipe(
* Effect.provide(Prisma.managementApi()),
* );
* ```
*
* This layer covers the operation helpers. The lifecycle helpers
* (`destroyApp`, `destroyDeployment`, `destroyProjectApps`,
* `waitForDeploymentStatus`, `syncComputeEnvironment`) also need an
* `HttpClient` from the caller, because the Prisma-scoped node transport is
* `Layer.provide`d privately here — see the comment on the private provide
* above for why it must not reach the surrounding stack. Add one, e.g.:
*
* ```typescript
* yield* Prisma.destroyApp(appId).pipe(
* Effect.provide(Prisma.managementApi()),
* Effect.provide(FetchHttpClient.layer),
* );
* ```
*/
export const managementApi = () =>
standaloneManagementApiLayer().pipe(Layer.provide(FetchHttpClient.layer), Layer.orDie);
/**
* Build a layer that registers all Prisma resource providers, the Prisma
* auth provider, resolved credentials, and an HTTP client.
*
* Every resource is registered with {@link ../Local/ProviderLayer.ts
* ProviderLayer.dual}: a **live** implementation backed by the Prisma
* Management API and a **local** implementation used by `alchemy dev`
* (fabricated `dev:` identifiers; `Prisma.Database` boots a local
* `@prisma/dev` server). The engine picks the variant per run and per
* resource:
*
* - `alchemy deploy` resolves the live variant; `alchemy dev` resolves the
* local variant automatically.
* - Wrap a resource in `Alchemy.remote(...)` to keep it live even during
* `alchemy dev`.
* - To replace implementations wholesale, construct your own layer instead
* of this one: register the {@link Providers} collection and provide your
* own per-resource provider layers (compose the exported per-resource
* factories like {@link ProjectProvider} with your replacements, and
* {@link managementApi} when live operations are needed).
*
* @example
* ```typescript
* import * as Alchemy from "alchemy";
* import * as Prisma from "@alchemy.run/prisma";
* import * as Effect from "effect/Effect";
*
* export default Alchemy.Stack(
* "MyStack",
* { providers: Prisma.providers(), state: Alchemy.localState() },
* Effect.gen(function* () {
* const project = yield* Prisma.Project("app", {
* name: "app",
* region: "us-east-1",
* });
* return { projectId: project.projectId };
* }),
* );
* ```
*/
export const providers = () =>
Layer.effect(
Providers,
Provider.collection([
Project,
Database,
Connection,
Branch,
Bucket,
BucketAccessKey,
Compute,
App,
Deployment,
CustomDomain,
EnvironmentVariable,
SourceRepository,
Contract,
Migrate,
Server,
WebsiteArtifact,
]),
).pipe(
Layer.provideMerge(
Layer.mergeAll(
ProjectProvider(),
DatabaseProvider(),
ConnectionProvider(),
BranchProvider(),
BucketProvider(),
BucketAccessKeyProvider(),
ComputeProvider(),
AppProvider(),
DeploymentProvider(),
CustomDomainProvider(),
EnvironmentVariableProvider(),
SourceRepositoryProvider(),
ContractProvider(),
MigrateProvider(),
ServerProvider(),
WebsiteArtifactProvider(),
),
),
// The management client layer is shared by every live variant. It is
// built in both modes but stays inert until the first API operation:
// auth registers without resolving credentials, so `alchemy dev` never
// needs a Prisma token.
Layer.provideMerge(stackManagementApiLayer()),
Layer.provideMerge(Command.providers()),
Layer.provide(FetchHttpClient.layer),
Layer.orDie,
);