UNPKG

everything-dev

Version:

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

82 lines (80 loc) 2.97 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); let node_fs = require("node:fs"); let node_path = require("node:path"); //#region src/db.ts const DEFAULT_MIGRATION_JOURNAL = { schema: "drizzle", table: "__drizzle_migrations" }; const PER_PLUGIN_ISOLATION = false; function normalizeSlug(name) { return (name.split("/").pop() ?? name).replace(/^@/, "").replace(/[^a-zA-Z0-9_-]/g, "_").replace(/-plugin$/i, "").replace(/-/g, "_").toLowerCase(); } function getMigrationSlug(dir) { if (!dir) return normalizeSlug(process.env.npm_package_name ?? "unknown"); let current = dir; for (let i = 0; i < 10; i++) { const pkgPath = (0, node_path.join)(current, "package.json"); if ((0, node_fs.existsSync)(pkgPath)) try { return normalizeSlug(JSON.parse((0, node_fs.readFileSync)(pkgPath, "utf-8")).name ?? current); } catch { return normalizeSlug(current); } const parent = (0, node_path.dirname)(current); if (parent === current) break; current = parent; } return normalizeSlug(dir); } function getMigrationStorage(slug, options) { const s = normalizeSlug(slug ?? getMigrationSlug()); if (options?.isolated ?? PER_PLUGIN_ISOLATION) return { schema: DEFAULT_MIGRATION_JOURNAL.schema, table: `__drizzle_migrations_${s}`, slug: s }; return { schema: DEFAULT_MIGRATION_JOURNAL.schema, table: DEFAULT_MIGRATION_JOURNAL.table, slug: s }; } /** * Format a JavaScript string array as a PostgreSQL text array literal for use * with Drizzle's `sql` tag. Example return: * `'{"h1","h2"}'::text[]` * * Usage: sql`WHERE col = ANY(${toSqlArray(values)})` * * Drizzle's default parameter binding does not handle array types correctly * with the pg driver (it emits `ANY(($1))` with a single string, which * Postgres rejects as a malformed array literal). */ function toSqlArray(arr) { if (arr.length === 0) return `'{}'::text[]`; return `'{${arr.map((v) => v.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")).map((v) => `"${v}"`).join(",")}}'::text[]`; } function pluginMigrationSlug(key) { return normalizeSlug(key); } function getDatabaseUrlSecretName(slug) { return `${slug.toUpperCase().replace(/-/g, "_")}_DATABASE_URL`; } function extractExpectedTables(migrations) { const tables = /* @__PURE__ */ new Set(); const re = /CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:"([^"]+)"\.)?"([^"]+)"/gi; for (const migration of migrations) for (const stmt of migration.sql) for (const match of stmt.matchAll(re)) { const tableName = match[2]; if (tableName) tables.add(tableName); } return [...tables]; } //#endregion exports.extractExpectedTables = extractExpectedTables; exports.getDatabaseUrlSecretName = getDatabaseUrlSecretName; exports.getMigrationSlug = getMigrationSlug; exports.getMigrationStorage = getMigrationStorage; exports.pluginMigrationSlug = pluginMigrationSlug; exports.toSqlArray = toSqlArray; //# sourceMappingURL=db.cjs.map