{"version":3,"file":"db.cjs","names":[],"sources":["../src/db.ts"],"sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\nexport interface MigrationStorage {\n  schema: string;\n  table: string;\n  slug: string;\n}\n\nconst DEFAULT_MIGRATION_JOURNAL = {\n  schema: \"drizzle\",\n  table: \"__drizzle_migrations\",\n} as const;\n\nconst PER_PLUGIN_ISOLATION = false;\n\nfunction normalizeSlug(name: string): string {\n  const basename = name.split(\"/\").pop() ?? name;\n  return basename\n    .replace(/^@/, \"\")\n    .replace(/[^a-zA-Z0-9_-]/g, \"_\")\n    .replace(/-plugin$/i, \"\")\n    .replace(/-/g, \"_\")\n    .toLowerCase();\n}\n\nexport function getMigrationSlug(dir?: string): string {\n  if (!dir) return normalizeSlug(process.env.npm_package_name ?? \"unknown\");\n  let current = dir;\n  for (let i = 0; i < 10; i++) {\n    const pkgPath = join(current, \"package.json\");\n    if (existsSync(pkgPath)) {\n      try {\n        const pkg = JSON.parse(readFileSync(pkgPath, \"utf-8\")) as { name?: string };\n        return normalizeSlug(pkg.name ?? current);\n      } catch {\n        return normalizeSlug(current);\n      }\n    }\n    const parent = dirname(current);\n    if (parent === current) break;\n    current = parent;\n  }\n  return normalizeSlug(dir);\n}\n\nexport function getMigrationStorage(\n  slug?: string,\n  options?: { isolated?: boolean },\n): MigrationStorage {\n  const s = normalizeSlug(slug ?? getMigrationSlug());\n  const isolated = options?.isolated ?? PER_PLUGIN_ISOLATION;\n  if (isolated) {\n    return {\n      schema: DEFAULT_MIGRATION_JOURNAL.schema,\n      table: `__drizzle_migrations_${s}`,\n      slug: s,\n    };\n  }\n  return {\n    schema: DEFAULT_MIGRATION_JOURNAL.schema,\n    table: DEFAULT_MIGRATION_JOURNAL.table,\n    slug: s,\n  };\n}\n\n/**\n * Format a JavaScript string array as a PostgreSQL text array literal for use\n * with Drizzle's `sql` tag. Example return:\n *   `'{\"h1\",\"h2\"}'::text[]`\n *\n * Usage: sql`WHERE col = ANY(${toSqlArray(values)})`\n *\n * Drizzle's default parameter binding does not handle array types correctly\n * with the pg driver (it emits `ANY(($1))` with a single string, which\n * Postgres rejects as a malformed array literal).\n */\nexport function toSqlArray(arr: string[]): string {\n  if (arr.length === 0) return `'{}'::text[]`;\n  const escaped = arr.map((v) => v.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, '\\\\\"'));\n  return `'{${escaped.map((v) => `\"${v}\"`).join(\",\")}}'::text[]`;\n}\n\nexport function pluginMigrationSlug(key: string): string {\n  return normalizeSlug(key);\n}\n\nexport function getDatabaseUrlSecretName(slug: string): string {\n  return `${slug.toUpperCase().replace(/-/g, \"_\")}_DATABASE_URL`;\n}\n\nexport function extractExpectedTables(migrations: { sql: string[] }[]): string[] {\n  const tables = new Set<string>();\n  const re = /CREATE\\s+TABLE\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?(?:\"([^\"]+)\"\\.)?\"([^\"]+)\"/gi;\n  for (const migration of migrations) {\n    for (const stmt of migration.sql) {\n      for (const match of stmt.matchAll(re)) {\n        const tableName = match[2];\n        if (tableName) {\n          tables.add(tableName);\n        }\n      }\n    }\n  }\n  return [...tables];\n}\n"],"mappings":";;;;;;AASA,MAAM,4BAA4B;CAChC,QAAQ;CACR,OAAO;CACR;AAED,MAAM,uBAAuB;AAE7B,SAAS,cAAc,MAAsB;AAE3C,SADiB,KAAK,MAAM,IAAI,CAAC,KAAK,IAAI,MAEvC,QAAQ,MAAM,GAAG,CACjB,QAAQ,mBAAmB,IAAI,CAC/B,QAAQ,aAAa,GAAG,CACxB,QAAQ,MAAM,IAAI,CAClB,aAAa;;AAGlB,SAAgB,iBAAiB,KAAsB;AACrD,KAAI,CAAC,IAAK,QAAO,cAAc,QAAQ,IAAI,oBAAoB,UAAU;CACzE,IAAI,UAAU;AACd,MAAK,IAAI,IAAI,GAAG,IAAI,IAAI,KAAK;EAC3B,MAAM,8BAAe,SAAS,eAAe;AAC7C,8BAAe,QAAQ,CACrB,KAAI;A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