@hot-updater/server 0.30.12 → 0.31.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_rolldown/runtime.cjs +25 -0
- package/dist/_virtual/_rolldown/runtime.mjs +6 -0
- package/dist/adapters/drizzle.cjs +6 -9
- package/dist/adapters/drizzle.d.cts +8 -1
- package/dist/adapters/drizzle.d.mts +8 -1
- package/dist/adapters/drizzle.mjs +5 -2
- package/dist/adapters/kysely.cjs +7 -9
- package/dist/adapters/kysely.d.cts +14 -1
- package/dist/adapters/kysely.d.mts +14 -1
- package/dist/adapters/kysely.mjs +6 -2
- package/dist/adapters/mongodb.cjs +7 -9
- package/dist/adapters/mongodb.d.cts +9 -1
- package/dist/adapters/mongodb.d.mts +9 -1
- package/dist/adapters/mongodb.mjs +5 -2
- package/dist/adapters/prisma.cjs +6 -9
- package/dist/adapters/prisma.d.cts +8 -1
- package/dist/adapters/prisma.d.mts +8 -1
- package/dist/adapters/prisma.mjs +5 -2
- package/dist/db/createBundleDiff.cjs +166 -0
- package/dist/db/createBundleDiff.d.cts +20 -0
- package/dist/db/createBundleDiff.d.mts +20 -0
- package/dist/db/createBundleDiff.mjs +161 -0
- package/dist/db/index.cjs +15 -16
- package/dist/db/index.d.cts +5 -4
- package/dist/db/index.d.mts +5 -4
- package/dist/db/index.mjs +14 -16
- package/dist/db/ormCore.cjs +173 -65
- package/dist/db/ormCore.d.cts +100 -34
- package/dist/db/ormCore.d.mts +100 -34
- package/dist/db/ormCore.mjs +171 -64
- package/dist/db/pluginCore.cjs +37 -3
- package/dist/db/pluginCore.mjs +36 -3
- package/dist/db/schemaEnhancements.cjs +171 -0
- package/dist/db/schemaEnhancements.mjs +167 -0
- package/dist/db/types.cjs +6 -0
- package/dist/db/types.d.cts +19 -7
- package/dist/db/types.d.mts +22 -10
- package/dist/db/types.mjs +6 -1
- package/dist/db/updateArtifacts.cjs +127 -0
- package/dist/db/updateArtifacts.mjs +125 -0
- package/dist/handler.cjs +61 -5
- package/dist/handler.d.cts +2 -2
- package/dist/handler.d.mts +5 -5
- package/dist/handler.mjs +59 -5
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +3 -2
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +2 -1
- package/dist/node.d.cts +0 -1
- package/dist/node.d.mts +0 -1
- package/dist/node_modules/.pnpm/@noble_hashes@1.8.0/node_modules/@noble/hashes/_u64.cjs +112 -0
- package/dist/node_modules/.pnpm/@noble_hashes@1.8.0/node_modules/@noble/hashes/_u64.mjs +108 -0
- package/dist/node_modules/.pnpm/@noble_hashes@1.8.0/node_modules/@noble/hashes/cryptoNode.cjs +22 -0
- package/dist/node_modules/.pnpm/@noble_hashes@1.8.0/node_modules/@noble/hashes/cryptoNode.mjs +18 -0
- package/dist/node_modules/.pnpm/@noble_hashes@1.8.0/node_modules/@noble/hashes/sha3.cjs +219 -0
- package/dist/node_modules/.pnpm/@noble_hashes@1.8.0/node_modules/@noble/hashes/sha3.mjs +214 -0
- package/dist/node_modules/.pnpm/@noble_hashes@1.8.0/node_modules/@noble/hashes/utils.cjs +275 -0
- package/dist/node_modules/.pnpm/@noble_hashes@1.8.0/node_modules/@noble/hashes/utils.mjs +270 -0
- package/dist/node_modules/.pnpm/@paralleldrive_cuid2@2.3.1/node_modules/@paralleldrive/cuid2/index.cjs +17 -0
- package/dist/node_modules/.pnpm/@paralleldrive_cuid2@2.3.1/node_modules/@paralleldrive/cuid2/index.mjs +13 -0
- package/dist/node_modules/.pnpm/@paralleldrive_cuid2@2.3.1/node_modules/@paralleldrive/cuid2/src/index.cjs +69 -0
- package/dist/node_modules/.pnpm/@paralleldrive_cuid2@2.3.1/node_modules/@paralleldrive/cuid2/src/index.mjs +65 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/column.cjs +52 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/column.mjs +52 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/entity.cjs +16 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/entity.mjs +15 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/pg-core/columns/enum.cjs +7 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/pg-core/columns/enum.mjs +7 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/sql/expressions/conditions.cjs +92 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/sql/expressions/conditions.mjs +78 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/sql/expressions/select.cjs +11 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/sql/expressions/select.mjs +10 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/sql/sql.cjs +383 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/sql/sql.mjs +366 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/subquery.cjs +17 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/subquery.mjs +17 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/table.cjs +60 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/table.mjs +59 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/table.utils.cjs +4 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/table.utils.mjs +4 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/tracing.cjs +6 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/tracing.mjs +6 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/view-common.cjs +4 -0
- package/dist/node_modules/.pnpm/drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pglite@0.2.17_@_31f44b782f9321d71f3ce9d35aa1edf7/node_modules/drizzle-orm/view-common.mjs +4 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/drizzle/index.cjs +383 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/drizzle/index.d.cts +12 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/drizzle/index.d.mts +12 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/drizzle/index.mjs +383 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/kysely/index.cjs +4 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/kysely/index.mjs +5 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/prisma/index.cjs +339 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/prisma/index.d.cts +70 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/prisma/index.d.mts +70 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/adapters/prisma/index.mjs +339 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-7PZK4ONR.cjs +57 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-7PZK4ONR.mjs +56 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-C6OTUURW.cjs +330 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-C6OTUURW.mjs +326 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-CHTIKPQU.cjs +166 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-CHTIKPQU.mjs +163 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-GUE4GMNC.cjs +14 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-GUE4GMNC.mjs +13 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-LHHP6UVP.cjs +24 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-LHHP6UVP.mjs +24 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-LVCPMTAT.cjs +1190 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-LVCPMTAT.mjs +1189 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-PK2W2SQ7.cjs +197 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-PK2W2SQ7.mjs +197 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-ZEQMAIFI.cjs +410 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-ZEQMAIFI.mjs +400 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-ZOCGSAWS.cjs +213 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-ZOCGSAWS.mjs +212 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/create-tg0451Y_.d.cts +285 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/create-tg0451Y_.d.mts +285 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/index-CMqePMTF.d.cts +45 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/index-CMqePMTF.d.mts +45 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/index.cjs +69 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/index.d.cts +49 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/index.d.mts +49 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/index.mjs +67 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/query/index.d.cts +156 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/query/index.d.mts +156 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/schema/index.cjs +1 -0
- package/dist/node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare_workers-types@4.20260313.1_@electric-sql_pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/schema/index.mjs +2 -0
- package/dist/{package.cjs → packages/server/package.cjs} +1 -1
- package/dist/{package.mjs → packages/server/package.mjs} +1 -1
- package/dist/runtime.cjs +13 -13
- package/dist/runtime.d.cts +4 -4
- package/dist/runtime.d.mts +4 -4
- package/dist/runtime.mjs +12 -13
- package/dist/schema/v0_21_0.cjs +16 -15
- package/dist/schema/v0_21_0.mjs +3 -2
- package/dist/schema/v0_29_0.cjs +18 -17
- package/dist/schema/v0_29_0.mjs +3 -2
- package/dist/schema/v0_31_0.cjs +48 -0
- package/dist/schema/v0_31_0.mjs +48 -0
- package/dist/storageAccess.cjs +44 -0
- package/dist/storageAccess.mjs +44 -0
- package/dist/version.cjs +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +15 -7
- package/src/adapters/drizzle.ts +15 -1
- package/src/adapters/kysely.ts +24 -1
- package/src/adapters/mongodb.ts +19 -1
- package/src/adapters/prisma.ts +15 -1
- package/src/db/createBundleDiff.spec.ts +402 -0
- package/src/db/createBundleDiff.ts +375 -0
- package/src/db/index.spec.ts +528 -27
- package/src/db/index.ts +22 -36
- package/src/db/ormCore.ts +308 -75
- package/src/db/pluginCore.spec.ts +385 -0
- package/src/db/pluginCore.ts +45 -4
- package/src/db/schemaEnhancements.ts +460 -0
- package/src/db/types.ts +38 -7
- package/src/db/updateArtifacts.ts +388 -0
- package/src/handler-standalone.integration.spec.ts +1 -0
- package/src/handler.spec.ts +121 -0
- package/src/handler.ts +117 -5
- package/src/runtime.spec.ts +287 -55
- package/src/runtime.ts +21 -37
- package/src/schema/v0_21_0.ts +1 -1
- package/src/schema/v0_29_0.ts +1 -1
- package/src/schema/v0_31_0.ts +58 -0
- package/src/storageAccess.spec.ts +57 -0
- package/src/storageAccess.ts +90 -0
|
@@ -0,0 +1,1190 @@
|
|
|
1
|
+
require("../../../../../../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
const require_chunk_7PZK4ONR = require("./chunk-7PZK4ONR.cjs");
|
|
3
|
+
const require_chunk_ZEQMAIFI = require("./chunk-ZEQMAIFI.cjs");
|
|
4
|
+
const require_chunk_CHTIKPQU = require("./chunk-CHTIKPQU.cjs");
|
|
5
|
+
const require_chunk_C6OTUURW = require("./chunk-C6OTUURW.cjs");
|
|
6
|
+
const require_chunk_ZOCGSAWS = require("./chunk-ZOCGSAWS.cjs");
|
|
7
|
+
const require_chunk_PK2W2SQ7 = require("./chunk-PK2W2SQ7.cjs");
|
|
8
|
+
let kysely = require("kysely");
|
|
9
|
+
//#region ../../node_modules/.pnpm/fumadb@0.2.2_drizzle-orm@0.44.7_@cloudflare+workers-types@4.20260313.1_@electric-sql+pg_c72c8c754becd21f6d6662e8fbd28e7f/node_modules/fumadb/dist/chunk-LVCPMTAT.js
|
|
10
|
+
var CockroachIntrospector = class {
|
|
11
|
+
#db;
|
|
12
|
+
constructor(db) {
|
|
13
|
+
this.#db = db;
|
|
14
|
+
}
|
|
15
|
+
async getSchemas() {
|
|
16
|
+
return (await this.#db.selectFrom("pg_catalog.pg_namespace").select("nspname").$castTo().execute()).map((it) => ({ name: it.nspname }));
|
|
17
|
+
}
|
|
18
|
+
async getTables(options = { withInternalKyselyTables: false }) {
|
|
19
|
+
let query = this.#db.selectFrom("pg_catalog.pg_attribute as a").innerJoin("pg_catalog.pg_class as c", "a.attrelid", "c.oid").innerJoin("pg_catalog.pg_namespace as ns", "c.relnamespace", "ns.oid").innerJoin("pg_catalog.pg_type as typ", "a.atttypid", "typ.oid").innerJoin("pg_catalog.pg_namespace as dtns", "typ.typnamespace", "dtns.oid").select([
|
|
20
|
+
"a.attname as column",
|
|
21
|
+
"a.attnotnull as not_null",
|
|
22
|
+
"a.atthasdef as has_default",
|
|
23
|
+
"c.relname as table",
|
|
24
|
+
"c.relkind as table_type",
|
|
25
|
+
"ns.nspname as schema",
|
|
26
|
+
"typ.typname as type",
|
|
27
|
+
"dtns.nspname as type_schema",
|
|
28
|
+
kysely.sql`col_description(a.attrelid, a.attnum)`.as("column_description"),
|
|
29
|
+
kysely.sql`pg_get_serial_sequence(quote_ident(ns.nspname) || '.' || quote_ident(c.relname), a.attname)`.as("auto_incrementing")
|
|
30
|
+
]).where("c.relkind", "in", [
|
|
31
|
+
"r",
|
|
32
|
+
"v",
|
|
33
|
+
"p"
|
|
34
|
+
]).where("ns.nspname", "!~", "^pg_").where("ns.nspname", "!=", "information_schema").where("ns.nspname", "!=", "crdb_internal").where("a.attnum", ">=", 0).where("a.attisdropped", "!=", true).orderBy("ns.nspname").orderBy("c.relname").orderBy("a.attnum").$castTo();
|
|
35
|
+
if (!options.withInternalKyselyTables) query = query.where("c.relname", "!=", kysely.DEFAULT_MIGRATION_TABLE).where("c.relname", "!=", kysely.DEFAULT_MIGRATION_LOCK_TABLE);
|
|
36
|
+
const rawColumns = await query.execute();
|
|
37
|
+
return this.#parseTableMetadata(rawColumns);
|
|
38
|
+
}
|
|
39
|
+
async getMetadata(options) {
|
|
40
|
+
return { tables: await this.getTables(options) };
|
|
41
|
+
}
|
|
42
|
+
#parseTableMetadata(columns) {
|
|
43
|
+
return columns.reduce((tables, it) => {
|
|
44
|
+
let table2 = tables.find((tbl) => tbl.name === it.table && tbl.schema === it.schema);
|
|
45
|
+
if (!table2) {
|
|
46
|
+
table2 = {
|
|
47
|
+
name: it.table,
|
|
48
|
+
isView: it.table_type === "v",
|
|
49
|
+
schema: it.schema,
|
|
50
|
+
columns: []
|
|
51
|
+
};
|
|
52
|
+
tables.push(table2);
|
|
53
|
+
}
|
|
54
|
+
table2.columns.push({
|
|
55
|
+
name: it.column,
|
|
56
|
+
dataType: it.type,
|
|
57
|
+
dataTypeSchema: it.type_schema,
|
|
58
|
+
isNullable: !it.not_null,
|
|
59
|
+
isAutoIncrementing: it.auto_incrementing !== null,
|
|
60
|
+
hasDefaultValue: it.has_default,
|
|
61
|
+
comment: it.column_description ?? void 0
|
|
62
|
+
});
|
|
63
|
+
return tables;
|
|
64
|
+
}, []);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
async function getUserTables(db, internalTables, provider) {
|
|
68
|
+
const allTables = provider === "cockroachdb" ? await new CockroachIntrospector(db).getTables() : await db.introspection.getTables();
|
|
69
|
+
const excludedSchemas = [
|
|
70
|
+
"mysql",
|
|
71
|
+
"information_schema",
|
|
72
|
+
"performance_schema",
|
|
73
|
+
"sys",
|
|
74
|
+
"pg_catalog",
|
|
75
|
+
"pg_toast",
|
|
76
|
+
"sqlite_master",
|
|
77
|
+
"sqlite_temp_master"
|
|
78
|
+
];
|
|
79
|
+
return allTables.filter((table2) => !table2.isView && (!table2.schema || !excludedSchemas.includes(table2.schema)) && !internalTables.includes(table2.name));
|
|
80
|
+
}
|
|
81
|
+
async function introspectSchema(options) {
|
|
82
|
+
const { db, provider, version = "1.0.0", internalTables = [], tableNameMapping = (t) => t, columnNameMapping = (_, c) => c, columnTypeMapping = (type, options2) => require_chunk_CHTIKPQU.dbToSchemaType(type, provider, options2.metadata)[0], includeRelations = true } = options;
|
|
83
|
+
const dbTables = await getUserTables(db, internalTables, provider);
|
|
84
|
+
const tables = {};
|
|
85
|
+
const relations = {};
|
|
86
|
+
async function buildColumn(dbTable, dbColumn, isPrimaryKey) {
|
|
87
|
+
const metadata = await getColumnMetadata(db, provider, dbTable.name, dbColumn.name);
|
|
88
|
+
const columnType = columnTypeMapping(dbColumn.dataType, {
|
|
89
|
+
isPrimaryKey,
|
|
90
|
+
metadata: {
|
|
91
|
+
...dbColumn,
|
|
92
|
+
...metadata
|
|
93
|
+
},
|
|
94
|
+
tableMetadata: dbTable
|
|
95
|
+
});
|
|
96
|
+
if (!columnType) throw new Error(`Failed to detect data type of ${dbColumn.dataType}, note that FumaDB doesn't support advanced data types in schema.`);
|
|
97
|
+
let rawDefault;
|
|
98
|
+
try {
|
|
99
|
+
rawDefault = await getColumnDefaultValue(db, provider, dbTable.name, dbColumn.name);
|
|
100
|
+
} catch {}
|
|
101
|
+
let col;
|
|
102
|
+
if (isPrimaryKey) {
|
|
103
|
+
if (!columnType.startsWith("varchar") && columnType !== "uuid") throw new Error(`ID column only supports varchar and uuid at the moment, found ${columnType}.`);
|
|
104
|
+
if (columnType === "uuid") col = require_chunk_ZEQMAIFI.idColumn(dbColumn.name, "uuid");
|
|
105
|
+
else col = require_chunk_ZEQMAIFI.idColumn(dbColumn.name, columnType);
|
|
106
|
+
} else col = require_chunk_ZEQMAIFI.column(dbColumn.name, columnType).nullable(dbColumn.isNullable);
|
|
107
|
+
const addDefault = normalizeColumnDefault(rawDefault, columnType);
|
|
108
|
+
if (addDefault) col = addDefault(col);
|
|
109
|
+
return col;
|
|
110
|
+
}
|
|
111
|
+
async function buildRelation(table2) {
|
|
112
|
+
const foreignKeys = await introspectTableForeignKeys(db, provider, table2.names.sql);
|
|
113
|
+
return (b) => {
|
|
114
|
+
const output = {};
|
|
115
|
+
for (const key of foreignKeys) {
|
|
116
|
+
let relationName = key.name;
|
|
117
|
+
if (relationName.endsWith("_fk")) relationName = relationName.slice(0, -3);
|
|
118
|
+
output[relationName] = buildRelationDefinition(b, table2, key, (name) => Object.values(tables).find((t) => t.names.sql === name));
|
|
119
|
+
}
|
|
120
|
+
return output;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
for (const dbTable of dbTables) {
|
|
124
|
+
const columns = {};
|
|
125
|
+
const primaryKeys = await introspectPrimaryKeys(db, dbTable.name, provider);
|
|
126
|
+
const uniqueConsts = await introspectUniqueConstraints(db, dbTable.name, provider);
|
|
127
|
+
for (const index of await introspectUniqueIndexes(db, dbTable.name, provider)) {
|
|
128
|
+
if (uniqueConsts.some((con) => con.name === index.name)) continue;
|
|
129
|
+
uniqueConsts.push(index);
|
|
130
|
+
}
|
|
131
|
+
if (primaryKeys.length !== 1) throw new Error(`FumaDB only supports 1 primary key (ID column), received: ${primaryKeys.length}.`);
|
|
132
|
+
for (const dbColumn of dbTable.columns) {
|
|
133
|
+
const isPrimaryKey = primaryKeys.includes(dbColumn.name);
|
|
134
|
+
columns[columnNameMapping(dbTable.name, dbColumn.name)] = await buildColumn(dbTable, dbColumn, isPrimaryKey);
|
|
135
|
+
}
|
|
136
|
+
const t = require_chunk_ZEQMAIFI.table(dbTable.name, columns);
|
|
137
|
+
for (const con of uniqueConsts) t.unique(con.name, con.columns.map((col) => columnNameMapping(dbTable.name, col)));
|
|
138
|
+
tables[tableNameMapping(dbTable.name)] = t;
|
|
139
|
+
}
|
|
140
|
+
if (includeRelations) for (const k in tables) {
|
|
141
|
+
const table2 = tables[k];
|
|
142
|
+
relations[k] = await buildRelation(table2);
|
|
143
|
+
}
|
|
144
|
+
return { schema: require_chunk_ZEQMAIFI.schema({
|
|
145
|
+
version,
|
|
146
|
+
tables,
|
|
147
|
+
relations
|
|
148
|
+
}) };
|
|
149
|
+
}
|
|
150
|
+
async function getColumnMetadata(db, provider, tableName, columnName) {
|
|
151
|
+
function num(v) {
|
|
152
|
+
if (v == null) return;
|
|
153
|
+
const converted = Number(v);
|
|
154
|
+
if (Number.isNaN(converted) || converted === -1) return;
|
|
155
|
+
return converted;
|
|
156
|
+
}
|
|
157
|
+
switch (provider) {
|
|
158
|
+
case "cockroachdb":
|
|
159
|
+
case "postgresql": {
|
|
160
|
+
const result = await db.selectFrom("information_schema.columns").select([
|
|
161
|
+
"character_maximum_length as length",
|
|
162
|
+
"numeric_precision as precision",
|
|
163
|
+
"numeric_scale as scale"
|
|
164
|
+
]).where("table_name", "=", tableName).where("column_name", "=", columnName).executeTakeFirst();
|
|
165
|
+
return {
|
|
166
|
+
length: num(result?.length),
|
|
167
|
+
precision: num(result?.precision),
|
|
168
|
+
scale: num(result?.scale)
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
case "mysql": {
|
|
172
|
+
const result = await db.selectFrom("information_schema.columns").select([
|
|
173
|
+
"CHARACTER_MAXIMUM_LENGTH as length",
|
|
174
|
+
"NUMERIC_PRECISION as precision",
|
|
175
|
+
"NUMERIC_SCALE as scale"
|
|
176
|
+
]).where("table_name", "=", tableName).where("column_name", "=", columnName).executeTakeFirst();
|
|
177
|
+
return {
|
|
178
|
+
length: num(result?.length),
|
|
179
|
+
precision: num(result?.precision),
|
|
180
|
+
scale: num(result?.scale)
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
case "sqlite": return {};
|
|
184
|
+
case "mssql": {
|
|
185
|
+
const result = await db.selectFrom("sys.columns as c").innerJoin("sys.tables as t", "c.object_id", "t.object_id").select([
|
|
186
|
+
"c.max_length as length",
|
|
187
|
+
"c.precision as precision",
|
|
188
|
+
"c.scale as scale"
|
|
189
|
+
]).where("t.name", "=", tableName).where("c.name", "=", columnName).executeTakeFirst();
|
|
190
|
+
return {
|
|
191
|
+
length: num(result?.length),
|
|
192
|
+
precision: num(result?.precision),
|
|
193
|
+
scale: num(result?.scale)
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async function getColumnDefaultValue(db, provider, tableName, columnName) {
|
|
199
|
+
switch (provider) {
|
|
200
|
+
case "cockroachdb":
|
|
201
|
+
case "postgresql": return await db.selectFrom("information_schema.columns").select("column_default").where("table_name", "=", tableName).where("column_name", "=", columnName).executeTakeFirst().then((result) => result?.column_default ?? null);
|
|
202
|
+
case "mysql": return (await db.selectFrom("information_schema.columns").select("COLUMN_DEFAULT as column_default").where("table_name", "=", tableName).where("column_name", "=", columnName).executeTakeFirst())?.column_default ?? null;
|
|
203
|
+
case "sqlite": {
|
|
204
|
+
const { sql: sql6 } = await import("kysely");
|
|
205
|
+
const pragmaRows = await sql6.raw(`PRAGMA table_info(${tableName})`).execute(db);
|
|
206
|
+
return (Array.isArray(pragmaRows) ? pragmaRows.find((r) => r.name === columnName) : void 0)?.dflt_value ?? null;
|
|
207
|
+
}
|
|
208
|
+
case "mssql": return (await db.selectFrom("sys.columns as c").innerJoin("sys.tables as t", "c.object_id", "t.object_id").leftJoin("sys.default_constraints as d", (join) => join.on("c.default_object_id", "=", "d.object_id")).select("d.definition as column_default").where("t.name", "=", tableName).where("c.name", "=", columnName).executeTakeFirst())?.column_default ?? null;
|
|
209
|
+
default: throw new Error(`Provider ${provider} not supported for default value introspection`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function normalizeColumnDefault(raw, type) {
|
|
213
|
+
if (raw == null) return;
|
|
214
|
+
let str = String(raw).trim();
|
|
215
|
+
if (/^(CURRENT_TIMESTAMP|now\(\)|datetime\('now'\)|getdate\(\))/i.test(str) && (type === "date" || type === "timestamp")) return (col) => col.defaultTo$("now");
|
|
216
|
+
str = str.replace(/::[\w\s[\]."]+$/, "");
|
|
217
|
+
if (str.startsWith("E'") || str.startsWith("N'")) str = str.slice(2, -1);
|
|
218
|
+
else if (str.startsWith("'") && str.endsWith("'") || str.startsWith("\"") && str.endsWith("\"")) str = str.slice(1, -1);
|
|
219
|
+
if (type === "bool") {
|
|
220
|
+
if (str === "true" || str === "1") return (col) => col.defaultTo(true);
|
|
221
|
+
if (str === "false" || str === "0") return (col) => col.defaultTo(false);
|
|
222
|
+
}
|
|
223
|
+
if ((type === "integer" || type === "decimal") && str.length > 0) {
|
|
224
|
+
const parsed = Number(str);
|
|
225
|
+
if (Number.isNaN(parsed)) throw new Error(`Failed to parse number from database default column value: ${str}`);
|
|
226
|
+
return (col) => col.defaultTo(parsed);
|
|
227
|
+
}
|
|
228
|
+
if (type === "json") return (col) => col.defaultTo(JSON.parse(str));
|
|
229
|
+
if (type === "bigint" && str.length > 0) return (col) => col.defaultTo(BigInt(str));
|
|
230
|
+
if (type === "timestamp" || type === "date") return (col) => col.defaultTo(new Date(str));
|
|
231
|
+
if (str.toLowerCase() === "null") return;
|
|
232
|
+
if (type === "string" || type.startsWith("varchar")) return (col) => col.defaultTo(str);
|
|
233
|
+
}
|
|
234
|
+
function buildRelationDefinition(builder, table2, fk, dbNameToTable) {
|
|
235
|
+
const targetTable = dbNameToTable(fk.referencedTable);
|
|
236
|
+
if (!targetTable) throw new Error(`Failed to resolve referenced table in a foreign key: ${fk.referencedTable}`);
|
|
237
|
+
const on = [];
|
|
238
|
+
for (let i = 0; i < fk.columns.length; i++) {
|
|
239
|
+
const col = fk.columns[i];
|
|
240
|
+
const refCol = fk.referencedColumns[i];
|
|
241
|
+
on.push([table2.getColumnByName(col).ormName, targetTable.getColumnByName(refCol).ormName]);
|
|
242
|
+
}
|
|
243
|
+
return builder.one(targetTable.ormName, ...on).foreignKey({
|
|
244
|
+
name: fk.name,
|
|
245
|
+
onDelete: fk.onDelete,
|
|
246
|
+
onUpdate: fk.onUpdate
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
async function introspectPrimaryKeys(db, tableName, provider) {
|
|
250
|
+
if (provider === "sqlite") return (await db.selectFrom(kysely.sql.raw(`pragma_table_info('${tableName}')`).as("t")).select(["name", "pk"]).execute()).filter((col) => col.pk).map((col) => col.name);
|
|
251
|
+
if (provider === "postgresql" || provider === "cockroachdb") {
|
|
252
|
+
const pkRows = await db.selectFrom("pg_constraint").innerJoin("pg_class", "pg_constraint.conrelid", "pg_class.oid").innerJoin("pg_namespace", "pg_class.relnamespace", "pg_namespace.oid").where("pg_class.relname", "=", tableName).where("pg_constraint.contype", "=", "p").select(["pg_constraint.conname", "pg_constraint.conkey"]).execute();
|
|
253
|
+
const attnumToName = await postgresqlIntrospectAttnumToName(db, tableName);
|
|
254
|
+
const primaryKeys = [];
|
|
255
|
+
for (const pk of pkRows) {
|
|
256
|
+
const attnums = postgresqlParseConName(pk.conkey);
|
|
257
|
+
for (const attnum of attnums) {
|
|
258
|
+
const colName = attnumToName.get(attnum);
|
|
259
|
+
if (colName !== void 0) primaryKeys.push(colName);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return primaryKeys;
|
|
263
|
+
}
|
|
264
|
+
if (provider === "mysql") {
|
|
265
|
+
const keyRows = await db.selectFrom("information_schema.KEY_COLUMN_USAGE").where("TABLE_NAME", "=", tableName).select(["CONSTRAINT_NAME", "COLUMN_NAME"]).execute();
|
|
266
|
+
const constraints = {};
|
|
267
|
+
for (const row of keyRows) if (row.CONSTRAINT_NAME && row.COLUMN_NAME) {
|
|
268
|
+
constraints[row.CONSTRAINT_NAME] ??= [];
|
|
269
|
+
constraints[row.CONSTRAINT_NAME]?.push(row.COLUMN_NAME);
|
|
270
|
+
}
|
|
271
|
+
return constraints[(await db.selectFrom("information_schema.TABLE_CONSTRAINTS").where("TABLE_NAME", "=", tableName).where("CONSTRAINT_TYPE", "=", "PRIMARY KEY").select(["CONSTRAINT_NAME"]).executeTakeFirstOrThrow()).CONSTRAINT_NAME] ?? [];
|
|
272
|
+
}
|
|
273
|
+
if (provider === "mssql") return (await db.selectFrom("sys.key_constraints as kc").select("c.name as column_name").innerJoin("sys.index_columns as ic", (v) => v.onRef("kc.parent_object_id", "=", "ic.object_id").onRef("kc.unique_index_id", "=", "ic.index_id")).innerJoin("sys.columns as c", (v) => v.onRef("ic.object_id", "=", "c.object_id").onRef("ic.column_id", "=", "c.column_id")).innerJoin("sys.tables as t", "kc.parent_object_id", "t.object_id").innerJoin("sys.schemas as s", "t.schema_id", "s.schema_id").where("kc.type", "=", "PK").where("s.name", "=", "dbo").where("t.name", "=", tableName).orderBy("ic.key_ordinal").execute()).map((row) => row.column_name);
|
|
274
|
+
return [];
|
|
275
|
+
}
|
|
276
|
+
async function postgresqlIntrospectAttnumToName(db, tableName) {
|
|
277
|
+
const colRows = await db.selectFrom("pg_attribute").innerJoin("pg_class", "pg_attribute.attrelid", "pg_class.oid").where("pg_class.relname", "=", tableName).where("pg_attribute.attnum", ">", 0).select(["pg_attribute.attnum", "pg_attribute.attname"]).execute();
|
|
278
|
+
const attnumToName = /* @__PURE__ */ new Map();
|
|
279
|
+
for (const row of colRows) attnumToName.set(Number(row.attnum), row.attname);
|
|
280
|
+
return attnumToName;
|
|
281
|
+
}
|
|
282
|
+
function postgresqlParseConName(conName) {
|
|
283
|
+
if (Array.isArray(conName)) return conName.map(Number);
|
|
284
|
+
if (typeof conName === "string") return conName.substring(1, conName.length - 1).split(",").map(Number);
|
|
285
|
+
return [];
|
|
286
|
+
}
|
|
287
|
+
function mapToUniqueConstraints(from) {
|
|
288
|
+
const map = /* @__PURE__ */ new Map();
|
|
289
|
+
for (const item of from) {
|
|
290
|
+
const value = map.get(item.constraint_name) ?? {
|
|
291
|
+
name: item.constraint_name,
|
|
292
|
+
columns: []
|
|
293
|
+
};
|
|
294
|
+
value.columns.push(item.column_name);
|
|
295
|
+
map.set(item.constraint_name, value);
|
|
296
|
+
}
|
|
297
|
+
return Array.from(map.values());
|
|
298
|
+
}
|
|
299
|
+
async function introspectUniqueIndexes(db, tableName, provider) {
|
|
300
|
+
if (provider === "mssql") return mapToUniqueConstraints(await db.selectFrom("sys.indexes as i").innerJoin("sys.index_columns as ic", (join) => join.onRef("i.object_id", "=", "ic.object_id").onRef("i.index_id", "=", "ic.index_id")).innerJoin("sys.columns as c", (join) => join.onRef("ic.object_id", "=", "c.object_id").onRef("ic.column_id", "=", "c.column_id")).innerJoin("sys.tables as t", "i.object_id", "t.object_id").where("i.is_unique", "=", 1).where("i.index_id", "not in", db.selectFrom("sys.key_constraints").select("unique_index_id").whereRef("parent_object_id", "=", "t.object_id")).where("t.name", "=", tableName).select([
|
|
301
|
+
"i.name as constraint_name",
|
|
302
|
+
"c.name as column_name",
|
|
303
|
+
"ic.key_ordinal"
|
|
304
|
+
]).orderBy("constraint_name").orderBy("ic.key_ordinal").execute());
|
|
305
|
+
if (provider === "sqlite") {
|
|
306
|
+
const indexes = await db.selectFrom(kysely.sql.raw(`pragma_index_list('${tableName}')`).as("i")).select(["name", "unique"]).execute();
|
|
307
|
+
const uniqueConstraints = [];
|
|
308
|
+
for (const idx of indexes) {
|
|
309
|
+
if (!idx.unique) continue;
|
|
310
|
+
const idxCols = await db.selectFrom(kysely.sql.raw(`pragma_index_info('${idx.name}')`).as("ii")).select(["name"]).execute();
|
|
311
|
+
uniqueConstraints.push({
|
|
312
|
+
name: idx.name,
|
|
313
|
+
columns: idxCols.map((c) => c.name)
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
return uniqueConstraints;
|
|
317
|
+
}
|
|
318
|
+
return [];
|
|
319
|
+
}
|
|
320
|
+
async function introspectUniqueConstraints(db, tableName, provider) {
|
|
321
|
+
if (provider === "postgresql" || provider === "cockroachdb") {
|
|
322
|
+
const uniqueRows = await db.selectFrom("pg_constraint").innerJoin("pg_class", "pg_constraint.conrelid", "pg_class.oid").innerJoin("pg_namespace", "pg_class.relnamespace", "pg_namespace.oid").where("pg_class.relname", "=", tableName).where("pg_constraint.contype", "=", "u").select(["pg_constraint.conname", "pg_constraint.conkey"]).execute();
|
|
323
|
+
const attnumToName = await postgresqlIntrospectAttnumToName(db, tableName);
|
|
324
|
+
const uniqueConstraints = [];
|
|
325
|
+
for (const uq of uniqueRows) {
|
|
326
|
+
const attnums = postgresqlParseConName(uq.conkey);
|
|
327
|
+
uniqueConstraints.push({
|
|
328
|
+
name: uq.conname,
|
|
329
|
+
columns: attnums.flatMap((a) => attnumToName.get(a) ?? [])
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
return uniqueConstraints;
|
|
333
|
+
}
|
|
334
|
+
if (provider === "mysql") {
|
|
335
|
+
const keyRows = await db.selectFrom("information_schema.KEY_COLUMN_USAGE").where("TABLE_NAME", "=", tableName).select(["CONSTRAINT_NAME", "COLUMN_NAME"]).execute();
|
|
336
|
+
const constraints = {};
|
|
337
|
+
for (const row of keyRows) if (row.CONSTRAINT_NAME && row.COLUMN_NAME) {
|
|
338
|
+
constraints[row.CONSTRAINT_NAME] ??= [];
|
|
339
|
+
constraints[row.CONSTRAINT_NAME]?.push(row.COLUMN_NAME);
|
|
340
|
+
}
|
|
341
|
+
const uniqueRows = await db.selectFrom("information_schema.TABLE_CONSTRAINTS").where("TABLE_NAME", "=", tableName).where("CONSTRAINT_TYPE", "=", "UNIQUE").select(["CONSTRAINT_NAME"]).execute();
|
|
342
|
+
const uniqueConstraints = [];
|
|
343
|
+
for (const uq of uniqueRows) uniqueConstraints.push({
|
|
344
|
+
name: uq.CONSTRAINT_NAME,
|
|
345
|
+
columns: constraints[uq.CONSTRAINT_NAME] ?? []
|
|
346
|
+
});
|
|
347
|
+
return uniqueConstraints;
|
|
348
|
+
}
|
|
349
|
+
if (provider === "mssql") return mapToUniqueConstraints(await db.selectFrom("sys.key_constraints as kc").innerJoin("sys.index_columns as ic", (join) => join.onRef("kc.parent_object_id", "=", "ic.object_id").onRef("kc.unique_index_id", "=", "ic.index_id")).innerJoin("sys.columns as c", (join) => join.onRef("ic.object_id", "=", "c.object_id").onRef("ic.column_id", "=", "c.column_id")).innerJoin("sys.tables as t", "kc.parent_object_id", "t.object_id").where("kc.type", "=", "UQ").where("t.name", "=", tableName).select([
|
|
350
|
+
"kc.name as constraint_name",
|
|
351
|
+
"c.name as column_name",
|
|
352
|
+
"ic.key_ordinal"
|
|
353
|
+
]).orderBy("constraint_name").orderBy("ic.key_ordinal").execute());
|
|
354
|
+
return [];
|
|
355
|
+
}
|
|
356
|
+
async function introspectTableForeignKeys(db, provider, tableName) {
|
|
357
|
+
if (provider === "postgresql" || provider === "cockroachdb") {
|
|
358
|
+
const constraints = await db.selectFrom("information_schema.table_constraints as tc").innerJoin("information_schema.key_column_usage as kcu", (join) => join.onRef("tc.constraint_name", "=", "kcu.constraint_name").onRef("tc.table_name", "=", "kcu.table_name")).innerJoin("information_schema.referential_constraints as rc", (join) => join.onRef("tc.constraint_name", "=", "rc.constraint_name")).innerJoin("information_schema.table_constraints as tc_ref", (join) => join.onRef("rc.unique_constraint_name", "=", "tc_ref.constraint_name").onRef("rc.unique_constraint_schema", "=", "tc_ref.constraint_schema")).select([
|
|
359
|
+
"tc.constraint_name as name",
|
|
360
|
+
"kcu.column_name as column_name",
|
|
361
|
+
"kcu.ordinal_position as ordinal_position",
|
|
362
|
+
"tc_ref.table_name as referenced_table",
|
|
363
|
+
"rc.unique_constraint_name as referenced_constraint_name",
|
|
364
|
+
"rc.update_rule as on_update",
|
|
365
|
+
"rc.delete_rule as on_delete"
|
|
366
|
+
]).where("tc.table_name", "=", tableName).where("tc.constraint_type", "=", "FOREIGN KEY").orderBy("name", "asc").orderBy("ordinal_position", "asc").execute();
|
|
367
|
+
const map = /* @__PURE__ */ new Map();
|
|
368
|
+
for (const row of constraints) {
|
|
369
|
+
let fk = map.get(row.name);
|
|
370
|
+
if (!fk) {
|
|
371
|
+
fk = {
|
|
372
|
+
name: row.name,
|
|
373
|
+
columns: [],
|
|
374
|
+
referencedTable: row.referenced_table,
|
|
375
|
+
referencedColumns: [],
|
|
376
|
+
onUpdate: mapAction(row.on_update),
|
|
377
|
+
onDelete: mapAction(row.on_delete),
|
|
378
|
+
referencedConstraintName: row.referenced_constraint_name
|
|
379
|
+
};
|
|
380
|
+
map.set(row.name, fk);
|
|
381
|
+
}
|
|
382
|
+
fk.columns.push(row.column_name);
|
|
383
|
+
}
|
|
384
|
+
for (const fk of map.values()) {
|
|
385
|
+
fk.referencedColumns = (await db.selectFrom("information_schema.key_column_usage").select(["column_name"]).where("constraint_name", "=", fk.referencedConstraintName).where("table_name", "=", fk.referencedTable).orderBy("ordinal_position", "asc").execute()).map((r) => r.column_name);
|
|
386
|
+
delete fk.referencedConstraintName;
|
|
387
|
+
}
|
|
388
|
+
return Array.from(map.values());
|
|
389
|
+
}
|
|
390
|
+
if (provider === "mysql") {
|
|
391
|
+
const constraints = await db.selectFrom("information_schema.key_column_usage as kcu").innerJoin("information_schema.referential_constraints as rc", (join) => join.onRef("kcu.constraint_name", "=", "rc.constraint_name").onRef("kcu.table_name", "=", "rc.table_name")).select([
|
|
392
|
+
"kcu.constraint_name as name",
|
|
393
|
+
"kcu.column_name as column_name",
|
|
394
|
+
"kcu.ordinal_position as ordinal_position",
|
|
395
|
+
"kcu.referenced_table_name as referenced_table",
|
|
396
|
+
"kcu.referenced_column_name as referenced_column",
|
|
397
|
+
"rc.update_rule as on_update",
|
|
398
|
+
"rc.delete_rule as on_delete"
|
|
399
|
+
]).where("kcu.table_name", "=", tableName).where("kcu.referenced_table_name", "is not", null).orderBy("name", "asc").orderBy("ordinal_position", "asc").execute();
|
|
400
|
+
const map = /* @__PURE__ */ new Map();
|
|
401
|
+
for (const row of constraints) {
|
|
402
|
+
let fk = map.get(row.name);
|
|
403
|
+
if (!fk) {
|
|
404
|
+
fk = {
|
|
405
|
+
name: row.name,
|
|
406
|
+
columns: [],
|
|
407
|
+
referencedTable: row.referenced_table,
|
|
408
|
+
referencedColumns: [],
|
|
409
|
+
onUpdate: mapAction(row.on_update),
|
|
410
|
+
onDelete: mapAction(row.on_delete)
|
|
411
|
+
};
|
|
412
|
+
map.set(row.name, fk);
|
|
413
|
+
}
|
|
414
|
+
fk.columns.push(row.column_name);
|
|
415
|
+
fk.referencedColumns.push(row.referenced_column);
|
|
416
|
+
}
|
|
417
|
+
return Array.from(map.values());
|
|
418
|
+
}
|
|
419
|
+
if (provider === "sqlite") {
|
|
420
|
+
const pragmaRows = await kysely.sql.raw(`PRAGMA foreign_key_list(${tableName})`).execute(db);
|
|
421
|
+
const map = /* @__PURE__ */ new Map();
|
|
422
|
+
for (const row of pragmaRows.rows) {
|
|
423
|
+
let fk = map.get(row.id);
|
|
424
|
+
if (!fk) {
|
|
425
|
+
fk = {
|
|
426
|
+
name: `fk_${tableName}_${row.id}`,
|
|
427
|
+
columns: [],
|
|
428
|
+
referencedTable: row.table,
|
|
429
|
+
referencedColumns: [],
|
|
430
|
+
onUpdate: mapAction(row.on_update),
|
|
431
|
+
onDelete: mapAction(row.on_delete)
|
|
432
|
+
};
|
|
433
|
+
map.set(row.id, fk);
|
|
434
|
+
}
|
|
435
|
+
fk.columns.push(row.from);
|
|
436
|
+
fk.referencedColumns.push(row.to);
|
|
437
|
+
}
|
|
438
|
+
return Array.from(map.values());
|
|
439
|
+
}
|
|
440
|
+
if (provider === "mssql") {
|
|
441
|
+
const constraints = await db.selectFrom("sys.foreign_keys as fk").innerJoin("sys.foreign_key_columns as fkc", "fk.object_id", "fkc.constraint_object_id").innerJoin("sys.tables as t", "fk.parent_object_id", "t.object_id").innerJoin("sys.columns as c", (join) => join.onRef("fkc.parent_object_id", "=", "c.object_id").onRef("fkc.parent_column_id", "=", "c.column_id")).innerJoin("sys.tables as rt", "fk.referenced_object_id", "rt.object_id").innerJoin("sys.columns as rc", (join) => join.onRef("fkc.referenced_object_id", "=", "rc.object_id").onRef("fkc.referenced_column_id", "=", "rc.column_id")).select([
|
|
442
|
+
"fk.name as name",
|
|
443
|
+
"c.name as column_name",
|
|
444
|
+
"rc.name as referenced_column",
|
|
445
|
+
"rt.name as referenced_table",
|
|
446
|
+
"fkc.constraint_column_id as ordinal_position",
|
|
447
|
+
"fk.delete_referential_action_desc as on_delete",
|
|
448
|
+
"fk.update_referential_action_desc as on_update"
|
|
449
|
+
]).where("t.name", "=", tableName).orderBy("name", "asc").orderBy("ordinal_position", "asc").execute();
|
|
450
|
+
const map = /* @__PURE__ */ new Map();
|
|
451
|
+
for (const row of constraints) {
|
|
452
|
+
let fk = map.get(row.name);
|
|
453
|
+
if (!fk) {
|
|
454
|
+
fk = {
|
|
455
|
+
name: row.name,
|
|
456
|
+
columns: [],
|
|
457
|
+
referencedTable: row.referenced_table,
|
|
458
|
+
referencedColumns: [],
|
|
459
|
+
onUpdate: mapAction(row.on_update),
|
|
460
|
+
onDelete: mapAction(row.on_delete)
|
|
461
|
+
};
|
|
462
|
+
map.set(row.name, fk);
|
|
463
|
+
}
|
|
464
|
+
fk.columns.push(row.column_name);
|
|
465
|
+
fk.referencedColumns.push(row.referenced_column);
|
|
466
|
+
}
|
|
467
|
+
return Array.from(map.values());
|
|
468
|
+
}
|
|
469
|
+
throw new Error(`Provider ${provider} not supported for foreign key introspection`);
|
|
470
|
+
}
|
|
471
|
+
function mapAction(action) {
|
|
472
|
+
switch (action?.toUpperCase()) {
|
|
473
|
+
case "CASCADE": return "CASCADE";
|
|
474
|
+
case "SET NULL": return "SET NULL";
|
|
475
|
+
case "RESTRICT":
|
|
476
|
+
case "NO ACTION":
|
|
477
|
+
case "NONE": return "RESTRICT";
|
|
478
|
+
default: return "RESTRICT";
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
async function generateMigration(schema2, config, options) {
|
|
482
|
+
const { db, provider } = config;
|
|
483
|
+
const { dropUnusedColumns = true, internalTables, nameVariants } = options;
|
|
484
|
+
const schemaWithVariant = nameVariants ? require_chunk_7PZK4ONR.applyNameVariants(schema2, nameVariants) : schema2;
|
|
485
|
+
const tables = Object.values(schemaWithVariant.tables);
|
|
486
|
+
const tableNameMapping = /* @__PURE__ */ new Map();
|
|
487
|
+
for (const t of tables) tableNameMapping.set(t.names.sql, t.ormName);
|
|
488
|
+
return require_chunk_C6OTUURW.generateMigrationFromSchema((await introspectSchema({
|
|
489
|
+
db,
|
|
490
|
+
provider,
|
|
491
|
+
columnNameMapping(tableName, columnName) {
|
|
492
|
+
const name = tableNameMapping.get(tableName);
|
|
493
|
+
if (!name) return columnName;
|
|
494
|
+
const col = schemaWithVariant.tables[name].getColumnByName(columnName);
|
|
495
|
+
if (!col) return columnName;
|
|
496
|
+
return col.ormName;
|
|
497
|
+
},
|
|
498
|
+
columnTypeMapping(dataType, options2) {
|
|
499
|
+
const predicted = require_chunk_CHTIKPQU.dbToSchemaType(dataType, provider, options2.metadata);
|
|
500
|
+
function fallback() {
|
|
501
|
+
for (let item of predicted) {
|
|
502
|
+
if (item === "varchar(n)") item = "varchar(255)";
|
|
503
|
+
if (!options2.isPrimaryKey) return item;
|
|
504
|
+
if (item.startsWith("varchar")) return item;
|
|
505
|
+
}
|
|
506
|
+
throw new Error("failed to predict");
|
|
507
|
+
}
|
|
508
|
+
const col = schemaWithVariant.tables[tableNameMapping.get(options2.tableMetadata.name) ?? options2.tableMetadata.name]?.getColumnByName(options2.metadata.name);
|
|
509
|
+
if (!col) return fallback();
|
|
510
|
+
for (const item of predicted) {
|
|
511
|
+
if (item === col.type) return item;
|
|
512
|
+
if (item === "varchar(n)" && col.type.startsWith("varchar")) return col.type;
|
|
513
|
+
}
|
|
514
|
+
return fallback();
|
|
515
|
+
},
|
|
516
|
+
tableNameMapping(tableName) {
|
|
517
|
+
return tableNameMapping.get(tableName) ?? tableName;
|
|
518
|
+
},
|
|
519
|
+
internalTables
|
|
520
|
+
})).schema, schema2, {
|
|
521
|
+
...config,
|
|
522
|
+
dropUnusedColumns,
|
|
523
|
+
dropUnusedTables: false
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
function getColumnBuilderCallback(col, provider) {
|
|
527
|
+
return (build) => {
|
|
528
|
+
if (!col.isNullable) build = build.notNull();
|
|
529
|
+
if (col instanceof require_chunk_ZEQMAIFI.IdColumn) build = build.primaryKey();
|
|
530
|
+
const defaultValue = defaultValueToDB(col, provider);
|
|
531
|
+
if (defaultValue) build = build.defaultTo(defaultValue);
|
|
532
|
+
return build;
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
var errors = {
|
|
536
|
+
IdColumnUpdate: "ID columns must not be updated, not every database supports updating primary keys and often requires workarounds.",
|
|
537
|
+
SQLiteUpdateForeignKeys: "In SQLite, you cannot modify foreign keys directly, use `recreate-table` instead."
|
|
538
|
+
};
|
|
539
|
+
function createUniqueIndex(db, name, tableName, cols, provider) {
|
|
540
|
+
const query = db.schema.createIndex(name).on(tableName).columns(cols).unique();
|
|
541
|
+
if (provider === "mssql") return query.where((b) => {
|
|
542
|
+
return b.and(cols.map((col) => b(col, "is not", null)));
|
|
543
|
+
});
|
|
544
|
+
return query;
|
|
545
|
+
}
|
|
546
|
+
function createUniqueIndexOrConstraint(db, name, tableName, cols, provider) {
|
|
547
|
+
if (provider === "sqlite" || provider === "mssql") return createUniqueIndex(db, name, tableName, cols, provider);
|
|
548
|
+
return db.schema.alterTable(tableName).addUniqueConstraint(name, cols);
|
|
549
|
+
}
|
|
550
|
+
function dropUniqueIndexOrConstraint(db, name, tableName, provider) {
|
|
551
|
+
if (provider === "cockroachdb" || provider === "sqlite" || provider === "mssql") {
|
|
552
|
+
let query = db.schema.dropIndex(name).ifExists();
|
|
553
|
+
if (provider === "cockroachdb") query = query.cascade();
|
|
554
|
+
if (provider === "mssql") query = query.on(tableName);
|
|
555
|
+
return query;
|
|
556
|
+
}
|
|
557
|
+
return db.schema.alterTable(tableName).dropConstraint(name);
|
|
558
|
+
}
|
|
559
|
+
function executeColumn(tableName, operation, config) {
|
|
560
|
+
const { db, provider } = config;
|
|
561
|
+
const next = () => db.schema.alterTable(tableName);
|
|
562
|
+
const results = [];
|
|
563
|
+
switch (operation.type) {
|
|
564
|
+
case "rename-column":
|
|
565
|
+
results.push(next().renameColumn(operation.from, operation.to));
|
|
566
|
+
return results;
|
|
567
|
+
case "drop-column":
|
|
568
|
+
results.push(next().dropColumn(operation.name));
|
|
569
|
+
return results;
|
|
570
|
+
case "create-column": {
|
|
571
|
+
const col = operation.value;
|
|
572
|
+
results.push(next().addColumn(col.names.sql, kysely.sql.raw(require_chunk_CHTIKPQU.schemaToDBType(col, provider)), getColumnBuilderCallback(col, provider)));
|
|
573
|
+
return results;
|
|
574
|
+
}
|
|
575
|
+
case "update-column": {
|
|
576
|
+
const col = operation.value;
|
|
577
|
+
if (col instanceof require_chunk_ZEQMAIFI.IdColumn) throw new Error(errors.IdColumnUpdate);
|
|
578
|
+
if (provider === "sqlite") throw new Error("SQLite doesn't support updating column, recreate the table instead.");
|
|
579
|
+
if (!require_chunk_C6OTUURW.isUpdated(operation)) return results;
|
|
580
|
+
if (provider === "mysql") {
|
|
581
|
+
results.push(next().modifyColumn(operation.name, kysely.sql.raw(require_chunk_CHTIKPQU.schemaToDBType(col, provider)), getColumnBuilderCallback(col, provider)));
|
|
582
|
+
return results;
|
|
583
|
+
}
|
|
584
|
+
const mssqlRecreateDefaultConstraint = operation.updateDataType || operation.updateDefault;
|
|
585
|
+
if (provider === "mssql" && mssqlRecreateDefaultConstraint) results.push(rawToNode(db, mssqlDropDefaultConstraint(tableName, col.names.sql)));
|
|
586
|
+
if (operation.updateDataType) {
|
|
587
|
+
const dbType = kysely.sql.raw(require_chunk_CHTIKPQU.schemaToDBType(col, provider));
|
|
588
|
+
results.push(provider === "postgresql" || provider === "cockroachdb" ? rawToNode(db, kysely.sql`ALTER TABLE ${kysely.sql.ref(tableName)} ALTER COLUMN ${kysely.sql.ref(operation.name)} TYPE ${dbType} USING (${kysely.sql.ref(operation.name)}::${dbType})`) : next().alterColumn(operation.name, (b) => b.setDataType(dbType)));
|
|
589
|
+
}
|
|
590
|
+
if (operation.updateNullable) results.push(next().alterColumn(operation.name, (build) => col.isNullable ? build.dropNotNull() : build.setNotNull()));
|
|
591
|
+
if (provider === "mssql" && mssqlRecreateDefaultConstraint) {
|
|
592
|
+
const defaultValue = defaultValueToDB(col, provider);
|
|
593
|
+
if (defaultValue) {
|
|
594
|
+
const name = `DF_${tableName}_${col.names.sql}`;
|
|
595
|
+
results.push(rawToNode(db, kysely.sql`ALTER TABLE ${kysely.sql.ref(tableName)} ADD CONSTRAINT ${kysely.sql.ref(name)} DEFAULT ${defaultValue} FOR ${kysely.sql.ref(col.names.sql)}`));
|
|
596
|
+
}
|
|
597
|
+
} else if (provider !== "mssql" && operation.updateDefault) {
|
|
598
|
+
const defaultValue = defaultValueToDB(col, provider);
|
|
599
|
+
results.push(next().alterColumn(operation.name, (build) => {
|
|
600
|
+
if (!defaultValue) return build.dropDefault();
|
|
601
|
+
return build.setDefault(defaultValue);
|
|
602
|
+
}));
|
|
603
|
+
}
|
|
604
|
+
return results;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
function execute(operation, config, onCustomNode) {
|
|
609
|
+
const { db, provider, relationMode = provider === "mssql" ? "fumadb" : "foreign-keys" } = config;
|
|
610
|
+
function createTable(table2, tableName = table2.names.sql, sqliteDeferChecks = false) {
|
|
611
|
+
const results = [];
|
|
612
|
+
let builder = db.schema.createTable(tableName);
|
|
613
|
+
for (const col of Object.values(table2.columns)) builder = builder.addColumn(col.names.sql, kysely.sql.raw(require_chunk_CHTIKPQU.schemaToDBType(col, provider)), getColumnBuilderCallback(col, provider));
|
|
614
|
+
for (const foreignKey of table2.foreignKeys) {
|
|
615
|
+
if (relationMode === "fumadb") break;
|
|
616
|
+
const compiled = require_chunk_ZEQMAIFI.compileForeignKey(foreignKey, "sql");
|
|
617
|
+
builder = builder.addForeignKeyConstraint(compiled.name, compiled.columns, compiled.referencedTable, compiled.referencedColumns, (b) => {
|
|
618
|
+
const builder2 = b.onUpdate(mapForeignKeyAction(compiled.onUpdate, provider)).onDelete(mapForeignKeyAction(compiled.onDelete, provider));
|
|
619
|
+
if (sqliteDeferChecks) return builder2.deferrable().initiallyDeferred();
|
|
620
|
+
return builder2;
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
for (const con of table2.getUniqueConstraints()) results.push(createUniqueIndexOrConstraint(db, con.name, table2.names.sql, con.columns.map((col) => col.names.sql), provider));
|
|
624
|
+
results.unshift(builder);
|
|
625
|
+
return results;
|
|
626
|
+
}
|
|
627
|
+
switch (operation.type) {
|
|
628
|
+
case "create-table": return createTable(operation.value);
|
|
629
|
+
case "rename-table":
|
|
630
|
+
if (provider === "mssql") return rawToNode(db, kysely.sql.raw(`EXEC sp_rename ${operation.from}, ${operation.to}`));
|
|
631
|
+
return db.schema.alterTable(operation.from).renameTo(operation.to);
|
|
632
|
+
case "update-table": {
|
|
633
|
+
const results = [];
|
|
634
|
+
for (const op of operation.value) results.push(...executeColumn(operation.name, op, config));
|
|
635
|
+
return results;
|
|
636
|
+
}
|
|
637
|
+
case "drop-table": return db.schema.dropTable(operation.name);
|
|
638
|
+
case "custom": return onCustomNode(operation);
|
|
639
|
+
case "add-foreign-key": {
|
|
640
|
+
if (provider === "sqlite") throw new Error(errors.SQLiteUpdateForeignKeys);
|
|
641
|
+
const { table: table2, value } = operation;
|
|
642
|
+
return db.schema.alterTable(table2).addForeignKeyConstraint(value.name, value.columns, value.referencedTable, value.referencedColumns, (b) => b.onUpdate(mapForeignKeyAction(value.onUpdate, provider)).onDelete(mapForeignKeyAction(value.onDelete, provider)));
|
|
643
|
+
}
|
|
644
|
+
case "drop-foreign-key": {
|
|
645
|
+
if (provider === "sqlite") throw new Error(errors.SQLiteUpdateForeignKeys);
|
|
646
|
+
const { table: table2, name } = operation;
|
|
647
|
+
let query = db.schema.alterTable(table2).dropConstraint(name);
|
|
648
|
+
if (provider !== "mysql") query = query.ifExists();
|
|
649
|
+
return query;
|
|
650
|
+
}
|
|
651
|
+
case "add-unique-constraint": return createUniqueIndexOrConstraint(db, operation.name, operation.table, operation.columns, provider);
|
|
652
|
+
case "drop-unique-constraint": return dropUniqueIndexOrConstraint(db, operation.name, operation.table, provider);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
function mapForeignKeyAction(action, provider) {
|
|
656
|
+
switch (action) {
|
|
657
|
+
case "CASCADE": return "cascade";
|
|
658
|
+
case "RESTRICT": return provider === "mssql" ? "no action" : "restrict";
|
|
659
|
+
case "SET NULL": return "set null";
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
function rawToNode(db, raw) {
|
|
663
|
+
return {
|
|
664
|
+
compile() {
|
|
665
|
+
return raw.compile(db);
|
|
666
|
+
},
|
|
667
|
+
execute() {
|
|
668
|
+
return raw.execute(db);
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
function mssqlDropDefaultConstraint(tableName, columnName) {
|
|
673
|
+
const alter = kysely.sql.lit(`ALTER TABLE "dbo"."${tableName}" DROP CONSTRAINT `);
|
|
674
|
+
return kysely.sql`DECLARE @ConstraintName NVARCHAR(200);
|
|
675
|
+
|
|
676
|
+
SELECT @ConstraintName = dc.name
|
|
677
|
+
FROM sys.default_constraints dc
|
|
678
|
+
JOIN sys.columns c ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
|
|
679
|
+
JOIN sys.tables t ON t.object_id = c.object_id
|
|
680
|
+
JOIN sys.schemas s ON t.schema_id = s.schema_id
|
|
681
|
+
WHERE s.name = 'dbo' AND t.name = ${kysely.sql.lit(tableName)} AND c.name = ${kysely.sql.lit(columnName)};
|
|
682
|
+
|
|
683
|
+
IF @ConstraintName IS NOT NULL
|
|
684
|
+
BEGIN
|
|
685
|
+
EXEC(${alter} + @ConstraintName);
|
|
686
|
+
END`;
|
|
687
|
+
}
|
|
688
|
+
function defaultValueToDB(column2, provider) {
|
|
689
|
+
const value = column2.default;
|
|
690
|
+
if (!value) return;
|
|
691
|
+
if (provider === "mysql" && column2.type === "string") return;
|
|
692
|
+
if ("runtime" in value && value.runtime === "now") return kysely.sql`CURRENT_TIMESTAMP`;
|
|
693
|
+
if ("value" in value) return kysely.sql.lit(value.value);
|
|
694
|
+
}
|
|
695
|
+
var SupportedColumnOperations = ["create-column", "rename-column"];
|
|
696
|
+
var transformerSQLite = { afterAuto(operations, { prev, next }) {
|
|
697
|
+
const operationTables = [];
|
|
698
|
+
const nameToTable = /* @__PURE__ */ new Map();
|
|
699
|
+
const recreate = /* @__PURE__ */ new Set();
|
|
700
|
+
for (const table2 of Object.values(prev.tables)) nameToTable.set(table2.names.sql, table2);
|
|
701
|
+
for (const op of operations) {
|
|
702
|
+
let table2;
|
|
703
|
+
switch (op.type) {
|
|
704
|
+
case "create-table":
|
|
705
|
+
table2 = op.value;
|
|
706
|
+
nameToTable.set(op.value.names.sql, table2);
|
|
707
|
+
break;
|
|
708
|
+
case "rename-table":
|
|
709
|
+
table2 = nameToTable.get(op.from);
|
|
710
|
+
if (!table2) break;
|
|
711
|
+
nameToTable.set(op.to, table2);
|
|
712
|
+
nameToTable.delete(op.from);
|
|
713
|
+
break;
|
|
714
|
+
case "add-unique-constraint":
|
|
715
|
+
case "drop-unique-constraint":
|
|
716
|
+
table2 = nameToTable.get(op.table);
|
|
717
|
+
break;
|
|
718
|
+
case "add-foreign-key":
|
|
719
|
+
case "drop-foreign-key":
|
|
720
|
+
table2 = nameToTable.get(op.table);
|
|
721
|
+
if (!table2) break;
|
|
722
|
+
recreate.add(table2);
|
|
723
|
+
break;
|
|
724
|
+
case "update-table":
|
|
725
|
+
table2 = nameToTable.get(op.name);
|
|
726
|
+
if (!table2 || op.value.every((action) => SupportedColumnOperations.includes(action.type))) break;
|
|
727
|
+
recreate.add(table2);
|
|
728
|
+
break;
|
|
729
|
+
case "drop-table":
|
|
730
|
+
table2 = nameToTable.get(op.name);
|
|
731
|
+
if (!table2) break;
|
|
732
|
+
nameToTable.delete(op.name);
|
|
733
|
+
recreate.delete(table2);
|
|
734
|
+
}
|
|
735
|
+
operationTables.push(table2 ?? null);
|
|
736
|
+
}
|
|
737
|
+
operations = operations.filter((_, i) => {
|
|
738
|
+
const table2 = operationTables[i];
|
|
739
|
+
return !table2 || !recreate.has(table2);
|
|
740
|
+
});
|
|
741
|
+
const post = [];
|
|
742
|
+
for (const prevTable of recreate) {
|
|
743
|
+
const nextTable = next.tables[prevTable.ormName];
|
|
744
|
+
if (!nextTable) continue;
|
|
745
|
+
for (const con of prevTable.getUniqueConstraints()) operations.push({
|
|
746
|
+
type: "drop-unique-constraint",
|
|
747
|
+
table: prevTable.names.sql,
|
|
748
|
+
name: con.name
|
|
749
|
+
});
|
|
750
|
+
const tempTable = nextTable.names.sql === prevTable.names.sql ? {
|
|
751
|
+
...nextTable,
|
|
752
|
+
names: {
|
|
753
|
+
...nextTable.names,
|
|
754
|
+
sql: `_temp_${nextTable.names.sql}`
|
|
755
|
+
}
|
|
756
|
+
} : nextTable;
|
|
757
|
+
operations.push({
|
|
758
|
+
type: "create-table",
|
|
759
|
+
value: tempTable
|
|
760
|
+
});
|
|
761
|
+
post.push(() => {
|
|
762
|
+
operations.push(...transferTable(prevTable, tempTable));
|
|
763
|
+
if (tempTable !== nextTable) operations.push({
|
|
764
|
+
type: "rename-table",
|
|
765
|
+
from: tempTable.names.sql,
|
|
766
|
+
to: nextTable.names.sql
|
|
767
|
+
});
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
for (const item of post) item();
|
|
771
|
+
return operations;
|
|
772
|
+
} };
|
|
773
|
+
function transferTable(from, to) {
|
|
774
|
+
const tempName = to.names.sql === from.names.sql ? `_temp_${to.names.sql}` : to.names.sql;
|
|
775
|
+
const colNames = [];
|
|
776
|
+
const values = [];
|
|
777
|
+
for (const prevCol of Object.values(from.columns)) {
|
|
778
|
+
const nextCol = to.columns[prevCol.ormName];
|
|
779
|
+
if (!nextCol) continue;
|
|
780
|
+
colNames.push(`"${nextCol.names.sql}"`);
|
|
781
|
+
values.push(`"${prevCol.names.sql}" as "${nextCol.names.sql}"`);
|
|
782
|
+
}
|
|
783
|
+
return [{
|
|
784
|
+
type: "custom",
|
|
785
|
+
sql: `INSERT INTO "${tempName}" (${colNames.join(", ")}) SELECT ${values.join(", ")} FROM "${from.names.sql}"`
|
|
786
|
+
}, {
|
|
787
|
+
type: "drop-table",
|
|
788
|
+
name: from.names.sql
|
|
789
|
+
}];
|
|
790
|
+
}
|
|
791
|
+
function fullSQLName(column2) {
|
|
792
|
+
return `${column2.table.names.sql}.${column2.names.sql}`;
|
|
793
|
+
}
|
|
794
|
+
function buildWhere(condition, eb, provider) {
|
|
795
|
+
if (condition.type === 2) {
|
|
796
|
+
const left = condition.a;
|
|
797
|
+
const op = condition.operator;
|
|
798
|
+
let val = condition.b;
|
|
799
|
+
if (!(val instanceof require_chunk_ZEQMAIFI.Column)) val = require_chunk_CHTIKPQU.serialize(val, left, provider);
|
|
800
|
+
let v;
|
|
801
|
+
let rhs;
|
|
802
|
+
switch (op) {
|
|
803
|
+
case "contains": v = "like";
|
|
804
|
+
case "not contains":
|
|
805
|
+
v ??= "not like";
|
|
806
|
+
rhs = val instanceof require_chunk_ZEQMAIFI.Column ? kysely.sql`concat('%', ${eb.ref(fullSQLName(val))}, '%')` : `%${val}%`;
|
|
807
|
+
break;
|
|
808
|
+
case "starts with": v = "like";
|
|
809
|
+
case "not starts with":
|
|
810
|
+
v ??= "not like";
|
|
811
|
+
rhs = val instanceof require_chunk_ZEQMAIFI.Column ? kysely.sql`concat(${eb.ref(fullSQLName(val))}, '%')` : `${val}%`;
|
|
812
|
+
break;
|
|
813
|
+
case "ends with": v = "like";
|
|
814
|
+
case "not ends with":
|
|
815
|
+
v ??= "not like";
|
|
816
|
+
rhs = val instanceof require_chunk_ZEQMAIFI.Column ? kysely.sql`concat('%', ${eb.ref(fullSQLName(val))})` : `%${val}`;
|
|
817
|
+
break;
|
|
818
|
+
default:
|
|
819
|
+
v = op;
|
|
820
|
+
rhs = val instanceof require_chunk_ZEQMAIFI.Column ? eb.ref(fullSQLName(val)) : val;
|
|
821
|
+
}
|
|
822
|
+
return eb(fullSQLName(left), v, rhs);
|
|
823
|
+
}
|
|
824
|
+
if (condition.type === 0) return eb.and(condition.items.map((v) => buildWhere(v, eb, provider)));
|
|
825
|
+
if (condition.type === 3) return eb.not(buildWhere(condition.item, eb, provider));
|
|
826
|
+
return eb.or(condition.items.map((v) => buildWhere(v, eb, provider)));
|
|
827
|
+
}
|
|
828
|
+
function mapSelect(select, table2, options = {}) {
|
|
829
|
+
const { relation, tableName = table2.names.sql } = options;
|
|
830
|
+
const out = [];
|
|
831
|
+
const keys = Array.isArray(select) ? select : Object.keys(table2.columns);
|
|
832
|
+
for (const key of keys) {
|
|
833
|
+
const name = relation ? `${relation}:${key}` : key;
|
|
834
|
+
out.push(`${tableName}.${table2.columns[key].names.sql} as ${name}`);
|
|
835
|
+
}
|
|
836
|
+
return out;
|
|
837
|
+
}
|
|
838
|
+
function extendSelect(original) {
|
|
839
|
+
const select = Array.isArray(original) ? new Set(original) : true;
|
|
840
|
+
const extendedKeys = [];
|
|
841
|
+
return {
|
|
842
|
+
extend(key) {
|
|
843
|
+
if (select === true || select.has(key)) return;
|
|
844
|
+
select.add(key);
|
|
845
|
+
extendedKeys.push(key);
|
|
846
|
+
},
|
|
847
|
+
compile() {
|
|
848
|
+
return {
|
|
849
|
+
result: select instanceof Set ? Array.from(select) : true,
|
|
850
|
+
extendedKeys,
|
|
851
|
+
removeExtendedKeys(record) {
|
|
852
|
+
for (const key of extendedKeys) delete record[key];
|
|
853
|
+
return record;
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
function fromKysely(schema2, config) {
|
|
860
|
+
const { db: kysely$1, provider, relationMode = provider === "mssql" ? "fumadb" : "foreign-keys" } = config;
|
|
861
|
+
function encodeValues(values, table2, generateDefault) {
|
|
862
|
+
const result = {};
|
|
863
|
+
for (const k in table2.columns) {
|
|
864
|
+
const col = table2.columns[k];
|
|
865
|
+
let value = values[k];
|
|
866
|
+
if (generateDefault && value === void 0) value = col.generateDefaultValue();
|
|
867
|
+
if (value !== void 0) result[col.names.sql] = require_chunk_CHTIKPQU.serialize(value, col, provider);
|
|
868
|
+
}
|
|
869
|
+
return result;
|
|
870
|
+
}
|
|
871
|
+
function decodeResult(result, table2) {
|
|
872
|
+
const output = {};
|
|
873
|
+
for (const k in result) {
|
|
874
|
+
const segs = k.split(":", 2);
|
|
875
|
+
const value = result[k];
|
|
876
|
+
if (segs.length === 1) output[k] = require_chunk_CHTIKPQU.deserialize(value, table2.columns[k], provider);
|
|
877
|
+
if (segs.length === 2) {
|
|
878
|
+
const [relationName, colName] = segs;
|
|
879
|
+
const relation = table2.relations[relationName];
|
|
880
|
+
if (relation === void 0) continue;
|
|
881
|
+
const col = relation.table.columns[colName];
|
|
882
|
+
if (col === void 0) continue;
|
|
883
|
+
output[relationName] ??= {};
|
|
884
|
+
const obj = output[relationName];
|
|
885
|
+
obj[colName] = require_chunk_CHTIKPQU.deserialize(value, col, provider);
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
return output;
|
|
889
|
+
}
|
|
890
|
+
async function runSubQueryJoin(records, join) {
|
|
891
|
+
const { relation, options: joinOptions } = join;
|
|
892
|
+
if (joinOptions === false) return;
|
|
893
|
+
const selectBuilder = extendSelect(joinOptions.select);
|
|
894
|
+
const root = {
|
|
895
|
+
type: 1,
|
|
896
|
+
items: []
|
|
897
|
+
};
|
|
898
|
+
for (const record of records) {
|
|
899
|
+
const condition = {
|
|
900
|
+
type: 0,
|
|
901
|
+
items: []
|
|
902
|
+
};
|
|
903
|
+
for (const [left, right] of relation.on) {
|
|
904
|
+
selectBuilder.extend(right);
|
|
905
|
+
condition.items.push({
|
|
906
|
+
type: 2,
|
|
907
|
+
a: relation.table.columns[right],
|
|
908
|
+
operator: "=",
|
|
909
|
+
b: record[left]
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
root.items.push(condition);
|
|
913
|
+
}
|
|
914
|
+
const compiledSelect = selectBuilder.compile();
|
|
915
|
+
const subRecords = await findMany(relation.table, {
|
|
916
|
+
...joinOptions,
|
|
917
|
+
select: compiledSelect.result,
|
|
918
|
+
where: joinOptions.where ? {
|
|
919
|
+
type: 0,
|
|
920
|
+
items: [root, joinOptions.where]
|
|
921
|
+
} : root
|
|
922
|
+
});
|
|
923
|
+
for (const record of records) {
|
|
924
|
+
const filtered = subRecords.filter((subRecord) => {
|
|
925
|
+
for (const [left, right] of relation.on) if (record[left] !== subRecord[right]) return false;
|
|
926
|
+
compiledSelect.removeExtendedKeys(subRecord);
|
|
927
|
+
return true;
|
|
928
|
+
});
|
|
929
|
+
record[relation.name] = relation.type === "one" ? filtered[0] ?? null : filtered;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
async function findMany(table2, v) {
|
|
933
|
+
let query = kysely$1.selectFrom(table2.names.sql);
|
|
934
|
+
const where = v.where;
|
|
935
|
+
if (where) query = query.where((eb) => buildWhere(where, eb, provider));
|
|
936
|
+
if (v.offset !== void 0) query = query.offset(v.offset);
|
|
937
|
+
if (v.limit !== void 0) query = provider === "mssql" ? query.top(v.limit) : query.limit(v.limit);
|
|
938
|
+
if (v.orderBy) for (const [col, mode] of v.orderBy) query = query.orderBy(fullSQLName(col), mode);
|
|
939
|
+
const selectBuilder = extendSelect(v.select);
|
|
940
|
+
const mappedSelect = [];
|
|
941
|
+
const subqueryJoins = [];
|
|
942
|
+
for (const join of v.join ?? []) {
|
|
943
|
+
const { options: joinOptions, relation } = join;
|
|
944
|
+
if (joinOptions === false) continue;
|
|
945
|
+
if (relation.type === "many" || joinOptions.join) {
|
|
946
|
+
subqueryJoins.push(join);
|
|
947
|
+
for (const [left] of relation.on) selectBuilder.extend(left);
|
|
948
|
+
continue;
|
|
949
|
+
}
|
|
950
|
+
const targetTable = relation.table;
|
|
951
|
+
const joinName = relation.name;
|
|
952
|
+
mappedSelect.push(...mapSelect(joinOptions.select, targetTable, {
|
|
953
|
+
relation: relation.name,
|
|
954
|
+
tableName: joinName
|
|
955
|
+
}));
|
|
956
|
+
query = query.leftJoin(`${targetTable.names.sql} as ${joinName}`, (b) => b.on((eb) => {
|
|
957
|
+
const conditions = [];
|
|
958
|
+
for (const [left, right] of relation.on) conditions.push(eb(`${table2.names.sql}.${table2.columns[left].names.sql}`, "=", eb.ref(`${joinName}.${targetTable.columns[right].names.sql}`)));
|
|
959
|
+
if (joinOptions.where) conditions.push(buildWhere(joinOptions.where, eb, provider));
|
|
960
|
+
return eb.and(conditions);
|
|
961
|
+
}));
|
|
962
|
+
}
|
|
963
|
+
const compiledSelect = selectBuilder.compile();
|
|
964
|
+
mappedSelect.push(...mapSelect(compiledSelect.result, table2));
|
|
965
|
+
const records = (await query.select(mappedSelect).execute()).map((v2) => decodeResult(v2, table2));
|
|
966
|
+
await Promise.all(subqueryJoins.map((join) => runSubQueryJoin(records, join)));
|
|
967
|
+
for (const record of records) compiledSelect.removeExtendedKeys(record);
|
|
968
|
+
return records;
|
|
969
|
+
}
|
|
970
|
+
let adapter = {
|
|
971
|
+
tables: schema2.tables,
|
|
972
|
+
async count(table2, { where }) {
|
|
973
|
+
let query = await kysely$1.selectFrom(table2.names.sql).select(kysely$1.fn.countAll().as("count"));
|
|
974
|
+
if (where) query = query.where((b) => buildWhere(where, b, provider));
|
|
975
|
+
const result = await query.executeTakeFirstOrThrow();
|
|
976
|
+
const count = Number(result.count);
|
|
977
|
+
if (Number.isNaN(count)) throw new Error(`Unexpected result for count, received: ${count}`);
|
|
978
|
+
return count;
|
|
979
|
+
},
|
|
980
|
+
async create(table2, values) {
|
|
981
|
+
const rawTable = table2;
|
|
982
|
+
const insertValues = encodeValues(values, rawTable, true);
|
|
983
|
+
const insert = kysely$1.insertInto(rawTable.names.sql).values(insertValues);
|
|
984
|
+
if (provider === "mssql") return decodeResult(await insert.output(mapSelect(true, rawTable, { tableName: "inserted" })).executeTakeFirstOrThrow(), rawTable);
|
|
985
|
+
if (provider === "postgresql" || provider === "sqlite") return decodeResult(await insert.returning(mapSelect(true, rawTable)).executeTakeFirstOrThrow(), rawTable);
|
|
986
|
+
const idColumn2 = rawTable.getIdColumn();
|
|
987
|
+
const idValue = insertValues[idColumn2.names.sql];
|
|
988
|
+
if (idValue == null) throw new Error("cannot find value of id column, which is required for `create()`.");
|
|
989
|
+
await insert.execute();
|
|
990
|
+
return decodeResult(await kysely$1.selectFrom(rawTable.names.sql).select(mapSelect(true, rawTable)).where(idColumn2.names.sql, "=", idValue).limit(1).executeTakeFirstOrThrow(), rawTable);
|
|
991
|
+
},
|
|
992
|
+
async findFirst(table2, v) {
|
|
993
|
+
const records = await this.findMany(table2, {
|
|
994
|
+
...v,
|
|
995
|
+
limit: 1
|
|
996
|
+
});
|
|
997
|
+
if (records.length === 0) return null;
|
|
998
|
+
return records[0];
|
|
999
|
+
},
|
|
1000
|
+
async findMany(table2, v) {
|
|
1001
|
+
return findMany(table2, v);
|
|
1002
|
+
},
|
|
1003
|
+
async updateMany(table2, v) {
|
|
1004
|
+
let query = kysely$1.updateTable(table2.names.sql).set(encodeValues(v.set, table2, false));
|
|
1005
|
+
if (v.where) query = query.where((eb) => buildWhere(v.where, eb, provider));
|
|
1006
|
+
await query.execute();
|
|
1007
|
+
},
|
|
1008
|
+
async upsert(table2, { where, update, create }) {
|
|
1009
|
+
if (provider === "mssql") {
|
|
1010
|
+
let query2 = kysely$1.updateTable(table2.names.sql).top(1).set(encodeValues(update, table2, false));
|
|
1011
|
+
if (where) query2 = query2.where((b) => buildWhere(where, b, provider));
|
|
1012
|
+
if ((await query2.executeTakeFirstOrThrow()).numUpdatedRows === 0n) await this.createMany(table2, [create]);
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
const idColumn2 = table2.getIdColumn();
|
|
1016
|
+
let query = kysely$1.selectFrom(table2.names.sql).select([`${idColumn2.names.sql} as id`]);
|
|
1017
|
+
if (where) query = query.where((b) => buildWhere(where, b, provider));
|
|
1018
|
+
const result = await query.limit(1).executeTakeFirst();
|
|
1019
|
+
if (result) await kysely$1.updateTable(table2.names.sql).set(encodeValues(update, table2, false)).where(idColumn2.names.sql, "=", result.id).execute();
|
|
1020
|
+
else await this.createMany(table2, [create]);
|
|
1021
|
+
},
|
|
1022
|
+
async createMany(table2, values) {
|
|
1023
|
+
const encodedValues = values.map((v) => encodeValues(v, table2, true));
|
|
1024
|
+
await kysely$1.insertInto(table2.names.sql).values(encodedValues).execute();
|
|
1025
|
+
return encodedValues.map((value) => ({ _id: value[table2.getIdColumn().names.sql] }));
|
|
1026
|
+
},
|
|
1027
|
+
async deleteMany(table2, { where }) {
|
|
1028
|
+
let query = kysely$1.deleteFrom(table2.names.sql);
|
|
1029
|
+
if (where) query = query.where((eb) => buildWhere(where, eb, provider));
|
|
1030
|
+
await query.execute();
|
|
1031
|
+
},
|
|
1032
|
+
transaction(run) {
|
|
1033
|
+
return kysely$1.transaction().execute((ctx) => {
|
|
1034
|
+
return run(fromKysely(schema2, {
|
|
1035
|
+
...config,
|
|
1036
|
+
db: ctx
|
|
1037
|
+
}));
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
if (relationMode === "fumadb") adapter = require_chunk_ZOCGSAWS.createSoftForeignKey(schema2, {
|
|
1042
|
+
...adapter,
|
|
1043
|
+
generateInsertValuesDefault(table2, values) {
|
|
1044
|
+
const result = {};
|
|
1045
|
+
for (const k in table2.columns) {
|
|
1046
|
+
const col = table2.columns[k];
|
|
1047
|
+
if (values[k] === void 0) result[k] = col.generateDefaultValue();
|
|
1048
|
+
else result[k] = values[k];
|
|
1049
|
+
}
|
|
1050
|
+
return result;
|
|
1051
|
+
}
|
|
1052
|
+
});
|
|
1053
|
+
return require_chunk_PK2W2SQ7.toORM(adapter);
|
|
1054
|
+
}
|
|
1055
|
+
function kyselyAdapter(config) {
|
|
1056
|
+
return {
|
|
1057
|
+
name: "kysely",
|
|
1058
|
+
createORM(schema2) {
|
|
1059
|
+
return fromKysely(schema2, config);
|
|
1060
|
+
},
|
|
1061
|
+
getSchemaVersion() {
|
|
1062
|
+
return createSettingsManager(config.db, config.provider, { settings: `private_${this.namespace}_settings` }).get("version");
|
|
1063
|
+
},
|
|
1064
|
+
createMigrationEngine() {
|
|
1065
|
+
return createSQLMigrator(this, config, { settings: `private_${this.namespace}_settings` });
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
function createSQLMigrator(lib, config, modelNames) {
|
|
1070
|
+
const manager = createSettingsManager(config.db, config.provider, modelNames);
|
|
1071
|
+
function onCustomNode(node, db) {
|
|
1072
|
+
const statement = kysely.sql.raw(node.sql);
|
|
1073
|
+
return {
|
|
1074
|
+
compile() {
|
|
1075
|
+
return statement.compile(db);
|
|
1076
|
+
},
|
|
1077
|
+
execute() {
|
|
1078
|
+
return statement.execute(db);
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
async function getNameVariants() {
|
|
1083
|
+
const currentVariants = await manager.get("name-variants");
|
|
1084
|
+
if (!currentVariants) return;
|
|
1085
|
+
try {
|
|
1086
|
+
return JSON.parse(currentVariants);
|
|
1087
|
+
} catch (e) {
|
|
1088
|
+
console.warn("failed to parse stored name variants, skipping for now", e);
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
function preprocess(operations, db) {
|
|
1092
|
+
if (config.provider === "mysql") {
|
|
1093
|
+
operations.unshift({
|
|
1094
|
+
type: "custom",
|
|
1095
|
+
sql: "SET FOREIGN_KEY_CHECKS = 0"
|
|
1096
|
+
});
|
|
1097
|
+
operations.push({
|
|
1098
|
+
type: "custom",
|
|
1099
|
+
sql: "SET FOREIGN_KEY_CHECKS = 1"
|
|
1100
|
+
});
|
|
1101
|
+
} else if (config.provider === "sqlite") operations.unshift({
|
|
1102
|
+
type: "custom",
|
|
1103
|
+
sql: "PRAGMA defer_foreign_keys = ON"
|
|
1104
|
+
});
|
|
1105
|
+
const tsConfig = {
|
|
1106
|
+
...config,
|
|
1107
|
+
db
|
|
1108
|
+
};
|
|
1109
|
+
return operations.flatMap((op) => execute(op, tsConfig, (node) => onCustomNode(node, db)));
|
|
1110
|
+
}
|
|
1111
|
+
return require_chunk_C6OTUURW.createMigrator({
|
|
1112
|
+
libConfig: lib,
|
|
1113
|
+
userConfig: config,
|
|
1114
|
+
async generateMigrationFromDatabase(options) {
|
|
1115
|
+
return generateMigration(options.target, config, {
|
|
1116
|
+
nameVariants: await getNameVariants(),
|
|
1117
|
+
internalTables: Object.values(modelNames),
|
|
1118
|
+
dropUnusedColumns: options.dropUnusedColumns
|
|
1119
|
+
});
|
|
1120
|
+
},
|
|
1121
|
+
async executor(operations) {
|
|
1122
|
+
await config.db.transaction().execute(async (tx) => {
|
|
1123
|
+
for (const node of preprocess(operations, tx)) try {
|
|
1124
|
+
await node.execute();
|
|
1125
|
+
} catch (e) {
|
|
1126
|
+
console.error("failed at", node.compile(), e);
|
|
1127
|
+
throw e;
|
|
1128
|
+
}
|
|
1129
|
+
});
|
|
1130
|
+
},
|
|
1131
|
+
settings: {
|
|
1132
|
+
getVersion: () => manager.get("version"),
|
|
1133
|
+
getNameVariants,
|
|
1134
|
+
async updateSettingsInMigration(schema2) {
|
|
1135
|
+
const settings = {
|
|
1136
|
+
version: schema2.version,
|
|
1137
|
+
"name-variants": JSON.stringify(require_chunk_C6OTUURW.exportNameVariants(schema2))
|
|
1138
|
+
};
|
|
1139
|
+
const init = await manager.initIfNeeded();
|
|
1140
|
+
const statements = [];
|
|
1141
|
+
if (init) statements.push(init);
|
|
1142
|
+
for (const [k, v] of Object.entries(settings)) {
|
|
1143
|
+
if (init || !await manager.get(k)) {
|
|
1144
|
+
statements.push(manager.insert(k, v));
|
|
1145
|
+
continue;
|
|
1146
|
+
}
|
|
1147
|
+
statements.push(manager.update(k, v));
|
|
1148
|
+
}
|
|
1149
|
+
return statements.map((statement) => ({
|
|
1150
|
+
type: "custom",
|
|
1151
|
+
sql: statement
|
|
1152
|
+
}));
|
|
1153
|
+
}
|
|
1154
|
+
},
|
|
1155
|
+
sql: { toSql(operations) {
|
|
1156
|
+
return preprocess(operations, config.db).map((m) => `${m.compile().sql};`).join("\n\n");
|
|
1157
|
+
} },
|
|
1158
|
+
transformers: config.provider === "sqlite" ? [transformerSQLite] : []
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
function createSettingsManager(db, provider, modelNames) {
|
|
1162
|
+
const { settings } = modelNames;
|
|
1163
|
+
function initTable() {
|
|
1164
|
+
return db.schema.createTable(settings).addColumn("key", provider === "sqlite" ? "text" : "varchar(255)", (col) => col.primaryKey()).addColumn("value", kysely.sql.raw(require_chunk_CHTIKPQU.schemaToDBType({ type: "string" }, provider)), (col) => col.notNull());
|
|
1165
|
+
}
|
|
1166
|
+
return {
|
|
1167
|
+
async get(key) {
|
|
1168
|
+
try {
|
|
1169
|
+
return (await db.selectFrom(settings).where("key", "=", key).select(["value"]).executeTakeFirstOrThrow()).value;
|
|
1170
|
+
} catch {
|
|
1171
|
+
return;
|
|
1172
|
+
}
|
|
1173
|
+
},
|
|
1174
|
+
async initIfNeeded() {
|
|
1175
|
+
if ((await db.introspection.getTables()).some((table2) => table2.name === settings)) return;
|
|
1176
|
+
return initTable().compile().sql;
|
|
1177
|
+
},
|
|
1178
|
+
insert(key, value) {
|
|
1179
|
+
return db.insertInto(settings).values({
|
|
1180
|
+
key: kysely.sql.lit(key),
|
|
1181
|
+
value: kysely.sql.lit(value)
|
|
1182
|
+
}).compile().sql;
|
|
1183
|
+
},
|
|
1184
|
+
update(key, value) {
|
|
1185
|
+
return db.updateTable(settings).set({ value: kysely.sql.lit(value) }).where("key", "=", kysely.sql.lit(key)).compile().sql;
|
|
1186
|
+
}
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
//#endregion
|
|
1190
|
+
exports.kyselyAdapter = kyselyAdapter;
|