import { Resource } from "../resource.js";
import { secret } from "../secret.js";
import { createPrismaApi } from "./api.js";
/**
 * Creates and manages Prisma Postgres database connections.
 *
 * Prisma Postgres database connections provide secure credentials
 * and connection details for accessing your databases. Connections
 * belong to a Prisma Postgres database.
 *
 * @example
 * // Create a database connection for a database
 * import { Database, Connection } from "alchemy/prisma-postgres";
 * const project = await Project("my-project");
 * const database = await Database("my-database", { project });
 * const connection = await Connection("my-connection", {
 *   database: database,
 * });
 * console.log(`Connection String: ${connection.connectionString.unencrypted}`);
 *
 * @example
 * // Create a database connection and connect it to Cloudflare Hyperdrive
 * import { Hyperdrive } from "alchemy/cloudflare";
 * import { Database, Connection } from "alchemy/prisma-postgres";
 * const project = await Project("my-project");
 * const database = await Database("my-database", { project });
 * const connection = await Connection("my-connection", {
 *   database: database,
 * });
 * const db = await Hyperdrive("prisma-postgres", {
 *   origin: connection.connectionString.unencrypted,
 * });
 */
export const Connection = Resource("prisma::Connection", async function (id, props) {
    const api = createPrismaApi({
        serviceToken: props.serviceToken,
    });
    const name = props.name ?? this.output?.name ?? this.scope.createPhysicalName(id);
    const database = typeof props.database === "string" ? props.database : props.database.id;
    if (this.phase === "delete") {
        await api.deleteDatabaseConnectionString({
            path: {
                id: this.output.id,
            },
        });
        return this.destroy();
    }
    if (this.phase === "update") {
        return this.replace();
    }
    const connection = await api
        .createDatabaseConnectionString({
        path: {
            databaseId: database,
        },
        body: {
            name,
        },
    })
        .then((response) => response.data.data);
    return {
        id: connection.id,
        name: connection.name,
        createdAt: connection.createdAt,
        prismaConnectionString: secret(connection.connectionString),
        connectionString: secret(`postgres://${connection.user}:${connection.pass}@${connection.host}/${connection.database.name}`),
        database: database,
        host: connection.host,
        password: connection.pass ? secret(connection.pass) : null,
        user: connection.user,
    };
});
//# sourceMappingURL=connection.js.map