{"version":3,"file":"index.mjs","names":[],"sources":["../../src/db/adapters.ts"],"sourcesContent":["/**\n * Database Adapter Functions\n *\n * These run at config time (astro.config.mjs) and return serializable descriptors.\n * The actual dialect is created at runtime by loading the entrypoint.\n *\n * @example\n * ```ts\n * // astro.config.mjs\n * import emdash from \"@premium-cms/emdash/astro\";\n * import { sqlite } from \"@premium-cms/emdash/db\";\n *\n * export default defineConfig({\n *   integrations: [\n *     emdash({\n *       database: sqlite({ url: \"file:./data.db\" }),\n *     }),\n *   ],\n * });\n * ```\n */\n\n/**\n * Dialect family identifier.\n * Used at runtime to select dialect-specific SQL fragments.\n */\nexport type DatabaseDialectType = \"sqlite\" | \"postgres\";\n\nexport type CollectionDeletionGuardInput =\n\t| {\n\t\t\taction: \"fence\";\n\t\t\tcollectionId: string;\n\t\t\tcollectionSlug: string;\n\t\t\tleaseToken: string;\n\t\t\tforceDelete: boolean;\n\t  }\n\t| {\n\t\t\taction: \"drop\";\n\t\t\tcollectionId: string;\n\t\t\tcollectionSlug: string;\n\t\t\tleaseToken: string;\n\t  };\n\nexport type CollectionDeletionGuardResult =\n\t| { outcome: \"fenced\" }\n\t| { outcome: \"has_content\" }\n\t| { outcome: \"stale\" }\n\t| { outcome: \"dropped\" };\n\nexport type ExecuteCollectionDeletionGuard = (\n\tconfig: unknown,\n\tinput: CollectionDeletionGuardInput,\n) => Promise<CollectionDeletionGuardResult>;\n\nconst ENVIRONMENT_VARIABLE_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\nfunction migrationEnvironmentVariable(\n\tvalue: string | undefined,\n\tfallback: string,\n\toptionName: string,\n): string {\n\tconst name = value ?? fallback;\n\tif (!ENVIRONMENT_VARIABLE_PATTERN.test(name)) {\n\t\tthrow new Error(`${optionName} must be a valid environment variable name.`);\n\t}\n\treturn name;\n}\n\n/**\n * Database descriptor - serializable config for virtual modules\n */\nexport interface DatabaseDescriptor {\n\tentrypoint: string;\n\tconfig: unknown;\n\ttype: DatabaseDialectType;\n\t/** Deployment migration capability with configuration safe for a build artifact. */\n\tmigrations?: {\n\t\tentrypoint: string;\n\t\tmanifestConfig: unknown;\n\t};\n\t/**\n\t * When true, the adapter's runtime entrypoint MUST export a named\n\t * `createRequestScopedDb` function matching the signature declared in\n\t * `virtual:emdash/dialect`. The virtual-module generator re-exports it\n\t * by name, so a missing export becomes a build-time bundler error.\n\t *\n\t * The function is called once per request and decides — based on its own\n\t * runtime config (e.g. whether the user opted into D1 sessions) — whether\n\t * to return a per-request Kysely or null. Use this for features like D1\n\t * read-replica sessions, bookmark cookies, or any per-request DB handle.\n\t *\n\t * When false or absent, the generator emits a stub that returns null and\n\t * the middleware takes its default (singleton) path.\n\t */\n\tsupportsRequestScope?: boolean;\n\t/**\n\t * When true, request middleware resolves the last content-namespace\n\t * invalidation timestamp and passes it to `createRequestScopedDb`.\n\t *\n\t * Keep this unset unless request routing depends on that timestamp: reading\n\t * it may require an object-cache backend round trip.\n\t */\n\tneedsLastContentWriteAt?: boolean;\n\t/**\n\t * When true, the adapter's runtime entrypoint MUST export a named\n\t * `createCoalescingDialect` function. The runtime uses this fresh dialect\n\t * only for its cold-start read batch.\n\t *\n\t * When false or absent, the virtual module exports `undefined` without\n\t * inspecting an optional entrypoint export.\n\t */\n\tsupportsCoalescing?: boolean;\n\t/** The runtime entrypoint exports the deletion-specific atomic guard. */\n\tsupportsCollectionDeletionGuard?: boolean;\n}\n\nexport interface SqliteConfig {\n\t/**\n\t * Database URL (e.g., \"file:./data.db\")\n\t */\n\turl: string;\n}\n\nexport interface LibsqlConfig {\n\t/**\n\t * Database URL (e.g., \"file:./data.db\" or \"libsql://...\")\n\t */\n\turl: string;\n\t/**\n\t * Auth token for remote libSQL\n\t */\n\tauthToken?: string;\n\tmigrationAuthTokenEnv?: string;\n}\n\nexport interface SnapshotLiveConfig {\n\t/**\n\t * Backend origin to pull the content snapshot from\n\t * (e.g. \"https://beta.saastemly.com\")\n\t */\n\turl: string;\n\t/**\n\t * API token with content:read + schema:read + `GET /snapshot` (the frontend\n\t * service account's). Falls back to process.env.EMDASH_API_TOKEN at runtime.\n\t */\n\ttoken?: string;\n\t/** Ask the backend for draft content too (?drafts=true). */\n\tincludeDrafts?: boolean;\n\t/** Directory holding git-backed collection entries (default \"content\"). */\n\tcontentDir?: string;\n\t/**\n\t * Re-fetch the snapshot when it is older than this many ms\n\t * (default 2000; <= 0 disables refresh).\n\t */\n\trefreshMs?: number;\n}\n\n/**\n * SQLite database adapter (better-sqlite3)\n *\n * For local development and Node.js deployments.\n *\n * @example\n * ```ts\n * database: sqlite({ url: \"file:./data.db\" })\n * ```\n */\nexport function sqlite(config: SqliteConfig): DatabaseDescriptor {\n\treturn {\n\t\tentrypoint: \"@premium-cms/emdash/db/sqlite\",\n\t\tconfig,\n\t\ttype: \"sqlite\",\n\t\tmigrations: {\n\t\t\tentrypoint: \"@premium-cms/emdash/db/sqlite-migrations\",\n\t\t\tmanifestConfig: { url: config.url },\n\t\t},\n\t};\n}\n\n/**\n * Live-snapshot adapter — an in-memory SQLite database continuously refreshed\n * from a live backend's `/_emdash/api/snapshot`. For `astro dev` (and one-shot\n * builds) against a deployed instance: no local backend, no snapshot file —\n * the same data path the platform's builds and previews use, kept live.\n *\n * @example\n * ```ts\n * database: snapshotLive({\n *   url: \"https://example.com\",\n *   token: process.env.EMDASH_API_TOKEN,\n * })\n * ```\n */\nexport function snapshotLive(config: SnapshotLiveConfig): DatabaseDescriptor {\n\treturn {\n\t\tentrypoint: \"@premium-cms/emdash/db/snapshot-live\",\n\t\tconfig,\n\t\ttype: \"sqlite\",\n\t};\n}\n\n/**\n * libSQL database adapter (Turso)\n *\n * For Turso hosted databases or local libSQL.\n *\n * @example\n * ```ts\n * database: libsql({\n *   url: \"libsql://my-db.turso.io\",\n *   authToken: process.env.TURSO_AUTH_TOKEN,\n * })\n * ```\n */\nexport function libsql(config: LibsqlConfig): DatabaseDescriptor {\n\tconst { migrationAuthTokenEnv, ...runtimeConfig } = config;\n\treturn {\n\t\tentrypoint: \"@premium-cms/emdash/db/libsql\",\n\t\tconfig: runtimeConfig,\n\t\ttype: \"sqlite\",\n\t\tmigrations: {\n\t\t\tentrypoint: \"@premium-cms/emdash/db/libsql-migrations\",\n\t\t\tmanifestConfig: {\n\t\t\t\turl: config.url,\n\t\t\t\tauthTokenEnv: migrationEnvironmentVariable(\n\t\t\t\t\tmigrationAuthTokenEnv,\n\t\t\t\t\t\"TURSO_AUTH_TOKEN\",\n\t\t\t\t\t\"migrationAuthTokenEnv\",\n\t\t\t\t),\n\t\t\t},\n\t\t},\n\t};\n}\n\n/**\n * PostgreSQL connection configuration\n */\nexport interface PostgresConfig {\n\tconnectionString?: string;\n\thost?: string;\n\tport?: number;\n\tdatabase?: string;\n\tuser?: string;\n\tpassword?: string;\n\tssl?: boolean;\n\tpool?: { min?: number; max?: number };\n\tmigrationConnectionStringEnv?: string;\n}\n\n/**\n * PostgreSQL database adapter\n *\n * For PostgreSQL deployments with connection pooling.\n *\n * @example\n * ```ts\n * database: postgres({ connectionString: process.env.DATABASE_URL })\n * ```\n */\nexport function postgres(config: PostgresConfig): DatabaseDescriptor {\n\tconst { migrationConnectionStringEnv, ...runtimeConfig } = config;\n\treturn {\n\t\tentrypoint: \"@premium-cms/emdash/db/postgres\",\n\t\tconfig: runtimeConfig,\n\t\ttype: \"postgres\",\n\t\tmigrations: {\n\t\t\tentrypoint: \"@premium-cms/emdash/db/postgres-migrations\",\n\t\t\tmanifestConfig: {\n\t\t\t\tconnectionStringEnv: migrationEnvironmentVariable(\n\t\t\t\t\tmigrationConnectionStringEnv,\n\t\t\t\t\t\"DATABASE_URL\",\n\t\t\t\t\t\"migrationConnectionStringEnv\",\n\t\t\t\t),\n\t\t\t},\n\t\t},\n\t};\n}\n"],"mappings":";;;;;AAsDA,MAAM,+BAA+B;AAErC,SAAS,6BACR,OACA,UACA,YACS;CACT,MAAM,OAAO,SAAS;AACtB,KAAI,CAAC,6BAA6B,KAAK,KAAK,CAC3C,OAAM,IAAI,MAAM,GAAG,WAAW,6CAA6C;AAE5E,QAAO;;;;;;;;;;;;AAsGR,SAAgB,OAAO,QAA0C;AAChE,QAAO;EACN,YAAY;EACZ;EACA,MAAM;EACN,YAAY;GACX,YAAY;GACZ,gBAAgB,EAAE,KAAK,OAAO,KAAK;GACnC;EACD;;;;;;;;;;;;;;;;AAiBF,SAAgB,aAAa,QAAgD;AAC5E,QAAO;EACN,YAAY;EACZ;EACA,MAAM;EACN;;;;;;;;;;;;;;;AAgBF,SAAgB,OAAO,QAA0C;CAChE,MAAM,EAAE,uBAAuB,GAAG,kBAAkB;AACpD,QAAO;EACN,YAAY;EACZ,QAAQ;EACR,MAAM;EACN,YAAY;GACX,YAAY;GACZ,gBAAgB;IACf,KAAK,OAAO;IACZ,cAAc,6BACb,uBACA,oBACA,wBACA;IACD;GACD;EACD;;;;;;;;;;;;AA4BF,SAAgB,SAAS,QAA4C;CACpE,MAAM,EAAE,8BAA8B,GAAG,kBAAkB;AAC3D,QAAO;EACN,YAAY;EACZ,QAAQ;EACR,MAAM;EACN,YAAY;GACX,YAAY;GACZ,gBAAgB,EACf,qBAAqB,6BACpB,8BACA,gBACA,+BACA,EACD;GACD;EACD"}