UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

78 lines (76 loc) • 3.21 kB
const require_core = require('./core.cjs'); const require_identity = require('./identity.cjs'); let node_path = require("node:path"); let effect = require("effect"); //#region src/db/bindings.ts var DatabaseBindingError = class extends effect.Data.TaggedError("DatabaseBindingError") {}; var DatabaseBindings = class extends effect.Context.Service()("everything-dev/DatabaseBindings") {}; function resolvePluginBinding(pluginKey, runtimeConfig, projectDir, env) { if (!runtimeConfig) throw new Error("Runtime config not loaded — cannot resolve plugin database binding"); let source; let section; let secrets; let localPath; let key; if (pluginKey === "api" && runtimeConfig.api) { source = runtimeConfig.api.source; section = "app.api"; secrets = runtimeConfig.api.secrets; localPath = runtimeConfig.api.localPath; key = "api"; } else if (pluginKey === "auth" && runtimeConfig.auth) { source = runtimeConfig.auth.source; section = "app.auth"; secrets = runtimeConfig.auth.secrets; localPath = runtimeConfig.auth.localPath; key = "auth"; } else if (runtimeConfig.plugins?.[pluginKey]) { const plugin = runtimeConfig.plugins[pluginKey]; source = plugin.source; section = "plugins"; secrets = plugin.secrets; localPath = plugin.localPath; key = pluginKey; } else { const available = [ "api", ...runtimeConfig.auth ? ["auth"] : [], ...Object.keys(runtimeConfig.plugins ?? {}) ].join(", "); throw new Error(`Plugin "${pluginKey}" not found in app.api, app.auth, or plugins. Available: ${available}`); } const identity = localPath ? require_identity.workspaceIdentityFromWorkspaceDir((0, node_path.resolve)(projectDir, localPath)) : { slug: require_core.pluginMigrationSlug(key), secretName: require_core.getDatabaseUrlSecretName(require_core.pluginMigrationSlug(key)), journal: require_core.getMigrationStorage(require_core.pluginMigrationSlug(key)), workspaceDir: void 0 }; const url = env[identity.secretName]; if (!url) { const declaredDatabaseSecrets = (secrets ?? []).filter((s) => s.endsWith("_DATABASE_URL")); const mismatch = declaredDatabaseSecrets.length > 0 && !declaredDatabaseSecrets.includes(identity.secretName) ? ` Config declares ${declaredDatabaseSecrets.join(", ")} — the workspace package name and the declared secret disagree.` : ""; throw new Error(`.env missing ${identity.secretName} for plugin "${key}".` + mismatch + ` Add it to your .env file (see .env.example).`); } return { key, source: source ?? "remote", section, identity, url }; } const makeDatabaseBindings = (options) => effect.Layer.succeed(DatabaseBindings, { forPluginKey: (pluginKey) => effect.Effect.tryPromise({ try: async () => { await options.loadEnv?.(); return resolvePluginBinding(pluginKey, await options.loadRuntimeConfig(), options.projectDir, options.env ?? process.env); }, catch: (error) => new DatabaseBindingError({ pluginKey, message: error instanceof Error ? error.message : String(error) }) }) }); //#endregion exports.DatabaseBindingError = DatabaseBindingError; exports.DatabaseBindings = DatabaseBindings; exports.makeDatabaseBindings = makeDatabaseBindings; //# sourceMappingURL=bindings.cjs.map