drizzle-orm 0.39.1 → 0.39.2-1b8a752

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.
Files changed (635) hide show
  1. package/_relations.cjs +328 -0
  2. package/_relations.cjs.map +1 -0
  3. package/_relations.d.cts +215 -0
  4. package/_relations.d.ts +215 -0
  5. package/_relations.js +316 -0
  6. package/_relations.js.map +1 -0
  7. package/alias.cjs.map +1 -1
  8. package/alias.d.cts +3 -3
  9. package/alias.d.ts +3 -3
  10. package/alias.js.map +1 -1
  11. package/aws-data-api/pg/driver.cjs +19 -5
  12. package/aws-data-api/pg/driver.cjs.map +1 -1
  13. package/aws-data-api/pg/driver.d.cts +9 -8
  14. package/aws-data-api/pg/driver.d.ts +9 -8
  15. package/aws-data-api/pg/driver.js +9 -8
  16. package/aws-data-api/pg/driver.js.map +1 -1
  17. package/aws-data-api/pg/migrator.cjs.map +1 -1
  18. package/aws-data-api/pg/migrator.d.cts +2 -1
  19. package/aws-data-api/pg/migrator.d.ts +2 -1
  20. package/aws-data-api/pg/migrator.js.map +1 -1
  21. package/aws-data-api/pg/session.cjs +67 -4
  22. package/aws-data-api/pg/session.cjs.map +1 -1
  23. package/aws-data-api/pg/session.d.cts +13 -8
  24. package/aws-data-api/pg/session.d.ts +13 -8
  25. package/aws-data-api/pg/session.js +67 -4
  26. package/aws-data-api/pg/session.js.map +1 -1
  27. package/better-sqlite3/driver.cjs +12 -5
  28. package/better-sqlite3/driver.cjs.map +1 -1
  29. package/better-sqlite3/driver.d.cts +7 -6
  30. package/better-sqlite3/driver.d.ts +7 -6
  31. package/better-sqlite3/driver.js +12 -8
  32. package/better-sqlite3/driver.js.map +1 -1
  33. package/better-sqlite3/migrator.cjs.map +1 -1
  34. package/better-sqlite3/migrator.d.cts +2 -1
  35. package/better-sqlite3/migrator.d.ts +2 -1
  36. package/better-sqlite3/migrator.js.map +1 -1
  37. package/better-sqlite3/session.cjs +49 -4
  38. package/better-sqlite3/session.cjs.map +1 -1
  39. package/better-sqlite3/session.d.cts +14 -8
  40. package/better-sqlite3/session.d.ts +14 -8
  41. package/better-sqlite3/session.js +49 -4
  42. package/better-sqlite3/session.js.map +1 -1
  43. package/bun-sql/driver.cjs +16 -5
  44. package/bun-sql/driver.cjs.map +1 -1
  45. package/bun-sql/driver.d.cts +7 -6
  46. package/bun-sql/driver.d.ts +7 -6
  47. package/bun-sql/driver.js +6 -8
  48. package/bun-sql/driver.js.map +1 -1
  49. package/bun-sql/migrator.cjs.map +1 -1
  50. package/bun-sql/migrator.d.cts +2 -1
  51. package/bun-sql/migrator.d.ts +2 -1
  52. package/bun-sql/migrator.js.map +1 -1
  53. package/bun-sql/session.cjs +50 -6
  54. package/bun-sql/session.cjs.map +1 -1
  55. package/bun-sql/session.d.cts +14 -9
  56. package/bun-sql/session.d.ts +14 -9
  57. package/bun-sql/session.js +50 -6
  58. package/bun-sql/session.js.map +1 -1
  59. package/bun-sqlite/driver.cjs +22 -5
  60. package/bun-sqlite/driver.cjs.map +1 -1
  61. package/bun-sqlite/driver.d.cts +7 -6
  62. package/bun-sqlite/driver.d.ts +7 -6
  63. package/bun-sqlite/driver.js +12 -8
  64. package/bun-sqlite/driver.js.map +1 -1
  65. package/bun-sqlite/migrator.cjs.map +1 -1
  66. package/bun-sqlite/migrator.d.cts +2 -1
  67. package/bun-sqlite/migrator.d.ts +2 -1
  68. package/bun-sqlite/migrator.js.map +1 -1
  69. package/bun-sqlite/session.cjs +49 -4
  70. package/bun-sqlite/session.cjs.map +1 -1
  71. package/bun-sqlite/session.d.cts +14 -8
  72. package/bun-sqlite/session.d.ts +14 -8
  73. package/bun-sqlite/session.js +49 -4
  74. package/bun-sqlite/session.js.map +1 -1
  75. package/d1/driver.cjs +22 -5
  76. package/d1/driver.cjs.map +1 -1
  77. package/d1/driver.d.cts +3 -2
  78. package/d1/driver.d.ts +3 -2
  79. package/d1/driver.js +12 -8
  80. package/d1/driver.js.map +1 -1
  81. package/d1/migrator.cjs.map +1 -1
  82. package/d1/migrator.d.cts +2 -1
  83. package/d1/migrator.d.ts +2 -1
  84. package/d1/migrator.js.map +1 -1
  85. package/d1/session.cjs +50 -8
  86. package/d1/session.cjs.map +1 -1
  87. package/d1/session.d.cts +15 -8
  88. package/d1/session.d.ts +15 -8
  89. package/d1/session.js +50 -8
  90. package/d1/session.js.map +1 -1
  91. package/durable-sqlite/driver.cjs +16 -5
  92. package/durable-sqlite/driver.cjs.map +1 -1
  93. package/durable-sqlite/driver.d.cts +3 -2
  94. package/durable-sqlite/driver.d.ts +3 -2
  95. package/durable-sqlite/driver.js +6 -8
  96. package/durable-sqlite/driver.js.map +1 -1
  97. package/durable-sqlite/migrator.cjs.map +1 -1
  98. package/durable-sqlite/migrator.d.cts +2 -1
  99. package/durable-sqlite/migrator.d.ts +2 -1
  100. package/durable-sqlite/migrator.js.map +1 -1
  101. package/durable-sqlite/session.cjs +46 -4
  102. package/durable-sqlite/session.cjs.map +1 -1
  103. package/durable-sqlite/session.d.cts +14 -8
  104. package/durable-sqlite/session.d.ts +14 -8
  105. package/durable-sqlite/session.js +46 -4
  106. package/durable-sqlite/session.js.map +1 -1
  107. package/expo-sqlite/driver.cjs +22 -5
  108. package/expo-sqlite/driver.cjs.map +1 -1
  109. package/expo-sqlite/driver.d.cts +3 -2
  110. package/expo-sqlite/driver.d.ts +3 -2
  111. package/expo-sqlite/driver.js +12 -8
  112. package/expo-sqlite/driver.js.map +1 -1
  113. package/expo-sqlite/migrator.cjs.map +1 -1
  114. package/expo-sqlite/migrator.d.cts +3 -2
  115. package/expo-sqlite/migrator.d.ts +3 -2
  116. package/expo-sqlite/migrator.js.map +1 -1
  117. package/expo-sqlite/query.cjs +13 -2
  118. package/expo-sqlite/query.cjs.map +1 -1
  119. package/expo-sqlite/query.d.cts +2 -1
  120. package/expo-sqlite/query.d.ts +2 -1
  121. package/expo-sqlite/query.js +3 -2
  122. package/expo-sqlite/query.js.map +1 -1
  123. package/expo-sqlite/session.cjs +47 -4
  124. package/expo-sqlite/session.cjs.map +1 -1
  125. package/expo-sqlite/session.d.cts +14 -8
  126. package/expo-sqlite/session.d.ts +14 -8
  127. package/expo-sqlite/session.js +47 -4
  128. package/expo-sqlite/session.js.map +1 -1
  129. package/libsql/driver-core.cjs +22 -5
  130. package/libsql/driver-core.cjs.map +1 -1
  131. package/libsql/driver-core.d.cts +2 -1
  132. package/libsql/driver-core.d.ts +2 -1
  133. package/libsql/driver-core.js +12 -8
  134. package/libsql/driver-core.js.map +1 -1
  135. package/libsql/driver.cjs.map +1 -1
  136. package/libsql/driver.d.cts +6 -5
  137. package/libsql/driver.d.ts +6 -5
  138. package/libsql/driver.js.map +1 -1
  139. package/libsql/http/index.cjs.map +1 -1
  140. package/libsql/http/index.d.cts +6 -5
  141. package/libsql/http/index.d.ts +6 -5
  142. package/libsql/http/index.js.map +1 -1
  143. package/libsql/migrator.cjs.map +1 -1
  144. package/libsql/migrator.d.cts +2 -1
  145. package/libsql/migrator.d.ts +2 -1
  146. package/libsql/migrator.js.map +1 -1
  147. package/libsql/node/index.cjs.map +1 -1
  148. package/libsql/node/index.d.cts +6 -5
  149. package/libsql/node/index.d.ts +6 -5
  150. package/libsql/node/index.js.map +1 -1
  151. package/libsql/session.cjs +56 -4
  152. package/libsql/session.cjs.map +1 -1
  153. package/libsql/session.d.cts +15 -9
  154. package/libsql/session.d.ts +15 -9
  155. package/libsql/session.js +56 -4
  156. package/libsql/session.js.map +1 -1
  157. package/libsql/sqlite3/index.cjs.map +1 -1
  158. package/libsql/sqlite3/index.d.cts +6 -5
  159. package/libsql/sqlite3/index.d.ts +6 -5
  160. package/libsql/sqlite3/index.js.map +1 -1
  161. package/libsql/wasm/index.cjs.map +1 -1
  162. package/libsql/wasm/index.d.cts +6 -5
  163. package/libsql/wasm/index.d.ts +6 -5
  164. package/libsql/wasm/index.js.map +1 -1
  165. package/libsql/web/index.cjs.map +1 -1
  166. package/libsql/web/index.d.cts +6 -5
  167. package/libsql/web/index.d.ts +6 -5
  168. package/libsql/web/index.js.map +1 -1
  169. package/libsql/ws/index.cjs.map +1 -1
  170. package/libsql/ws/index.d.cts +6 -5
  171. package/libsql/ws/index.d.ts +6 -5
  172. package/libsql/ws/index.js.map +1 -1
  173. package/mysql-core/db.cjs +30 -6
  174. package/mysql-core/db.cjs.map +1 -1
  175. package/mysql-core/db.d.cts +15 -8
  176. package/mysql-core/db.d.ts +15 -8
  177. package/mysql-core/db.js +29 -5
  178. package/mysql-core/db.js.map +1 -1
  179. package/mysql-core/dialect.cjs +191 -15
  180. package/mysql-core/dialect.cjs.map +1 -1
  181. package/mysql-core/dialect.d.cts +36 -13
  182. package/mysql-core/dialect.d.ts +36 -13
  183. package/mysql-core/dialect.js +189 -22
  184. package/mysql-core/dialect.js.map +1 -1
  185. package/mysql-core/query-builders/_query.cjs +149 -0
  186. package/mysql-core/query-builders/_query.cjs.map +1 -0
  187. package/mysql-core/query-builders/_query.d.cts +44 -0
  188. package/mysql-core/query-builders/_query.d.ts +44 -0
  189. package/mysql-core/query-builders/_query.js +114 -0
  190. package/mysql-core/query-builders/_query.js.map +1 -0
  191. package/mysql-core/query-builders/count.cjs.map +1 -1
  192. package/mysql-core/query-builders/count.d.cts +1 -1
  193. package/mysql-core/query-builders/count.d.ts +1 -1
  194. package/mysql-core/query-builders/count.js.map +1 -1
  195. package/mysql-core/query-builders/query.cjs +18 -31
  196. package/mysql-core/query-builders/query.cjs.map +1 -1
  197. package/mysql-core/query-builders/query.d.cts +8 -10
  198. package/mysql-core/query-builders/query.d.ts +8 -10
  199. package/mysql-core/query-builders/query.js +18 -31
  200. package/mysql-core/query-builders/query.js.map +1 -1
  201. package/mysql-core/session.cjs +3 -2
  202. package/mysql-core/session.cjs.map +1 -1
  203. package/mysql-core/session.d.cts +10 -7
  204. package/mysql-core/session.d.ts +10 -7
  205. package/mysql-core/session.js +3 -2
  206. package/mysql-core/session.js.map +1 -1
  207. package/mysql-proxy/driver.cjs +22 -5
  208. package/mysql-proxy/driver.cjs.map +1 -1
  209. package/mysql-proxy/driver.d.cts +3 -2
  210. package/mysql-proxy/driver.d.ts +3 -2
  211. package/mysql-proxy/driver.js +12 -8
  212. package/mysql-proxy/driver.js.map +1 -1
  213. package/mysql-proxy/migrator.cjs.map +1 -1
  214. package/mysql-proxy/migrator.d.cts +2 -1
  215. package/mysql-proxy/migrator.d.ts +2 -1
  216. package/mysql-proxy/migrator.js.map +1 -1
  217. package/mysql-proxy/session.cjs +27 -2
  218. package/mysql-proxy/session.cjs.map +1 -1
  219. package/mysql-proxy/session.d.cts +13 -8
  220. package/mysql-proxy/session.d.ts +13 -8
  221. package/mysql-proxy/session.js +27 -2
  222. package/mysql-proxy/session.js.map +1 -1
  223. package/mysql2/driver.cjs +24 -7
  224. package/mysql2/driver.cjs.map +1 -1
  225. package/mysql2/driver.d.cts +10 -9
  226. package/mysql2/driver.d.ts +10 -9
  227. package/mysql2/driver.js +14 -10
  228. package/mysql2/driver.js.map +1 -1
  229. package/mysql2/migrator.cjs.map +1 -1
  230. package/mysql2/migrator.d.cts +2 -1
  231. package/mysql2/migrator.d.ts +2 -1
  232. package/mysql2/migrator.js.map +1 -1
  233. package/mysql2/session.cjs +30 -2
  234. package/mysql2/session.cjs.map +1 -1
  235. package/mysql2/session.d.cts +13 -8
  236. package/mysql2/session.d.ts +13 -8
  237. package/mysql2/session.js +30 -2
  238. package/mysql2/session.js.map +1 -1
  239. package/neon/neon-identity.cjs +2 -2
  240. package/neon/neon-identity.cjs.map +1 -1
  241. package/neon/neon-identity.d.cts +3 -3
  242. package/neon/neon-identity.d.ts +3 -3
  243. package/neon/neon-identity.js +2 -2
  244. package/neon/neon-identity.js.map +1 -1
  245. package/neon-http/driver.cjs +21 -7
  246. package/neon-http/driver.cjs.map +1 -1
  247. package/neon-http/driver.d.cts +10 -9
  248. package/neon-http/driver.d.ts +10 -9
  249. package/neon-http/driver.js +11 -7
  250. package/neon-http/driver.js.map +1 -1
  251. package/neon-http/migrator.cjs.map +1 -1
  252. package/neon-http/migrator.d.cts +2 -1
  253. package/neon-http/migrator.d.ts +2 -1
  254. package/neon-http/migrator.js.map +1 -1
  255. package/neon-http/session.cjs +32 -2
  256. package/neon-http/session.cjs.map +1 -1
  257. package/neon-http/session.d.cts +13 -8
  258. package/neon-http/session.d.ts +13 -8
  259. package/neon-http/session.js +32 -2
  260. package/neon-http/session.js.map +1 -1
  261. package/neon-serverless/driver.cjs +18 -7
  262. package/neon-serverless/driver.cjs.map +1 -1
  263. package/neon-serverless/driver.d.cts +9 -8
  264. package/neon-serverless/driver.d.ts +9 -8
  265. package/neon-serverless/driver.js +8 -10
  266. package/neon-serverless/driver.js.map +1 -1
  267. package/neon-serverless/migrator.cjs.map +1 -1
  268. package/neon-serverless/migrator.d.cts +2 -1
  269. package/neon-serverless/migrator.d.ts +2 -1
  270. package/neon-serverless/migrator.js.map +1 -1
  271. package/neon-serverless/session.cjs +40 -5
  272. package/neon-serverless/session.cjs.map +1 -1
  273. package/neon-serverless/session.d.cts +13 -8
  274. package/neon-serverless/session.d.ts +13 -8
  275. package/neon-serverless/session.js +40 -5
  276. package/neon-serverless/session.js.map +1 -1
  277. package/node-postgres/driver.cjs +13 -7
  278. package/node-postgres/driver.cjs.map +1 -1
  279. package/node-postgres/driver.d.cts +9 -8
  280. package/node-postgres/driver.d.ts +9 -8
  281. package/node-postgres/driver.js +13 -10
  282. package/node-postgres/driver.js.map +1 -1
  283. package/node-postgres/migrator.cjs.map +1 -1
  284. package/node-postgres/migrator.d.cts +2 -1
  285. package/node-postgres/migrator.d.ts +2 -1
  286. package/node-postgres/migrator.js.map +1 -1
  287. package/node-postgres/session.cjs +45 -4
  288. package/node-postgres/session.cjs.map +1 -1
  289. package/node-postgres/session.d.cts +13 -8
  290. package/node-postgres/session.d.ts +13 -8
  291. package/node-postgres/session.js +45 -4
  292. package/node-postgres/session.js.map +1 -1
  293. package/op-sqlite/driver.cjs +22 -5
  294. package/op-sqlite/driver.cjs.map +1 -1
  295. package/op-sqlite/driver.d.cts +3 -2
  296. package/op-sqlite/driver.d.ts +3 -2
  297. package/op-sqlite/driver.js +12 -8
  298. package/op-sqlite/driver.js.map +1 -1
  299. package/op-sqlite/migrator.cjs.map +1 -1
  300. package/op-sqlite/migrator.d.cts +3 -2
  301. package/op-sqlite/migrator.d.ts +3 -2
  302. package/op-sqlite/migrator.js.map +1 -1
  303. package/op-sqlite/session.cjs +47 -4
  304. package/op-sqlite/session.cjs.map +1 -1
  305. package/op-sqlite/session.d.cts +14 -8
  306. package/op-sqlite/session.d.ts +14 -8
  307. package/op-sqlite/session.js +47 -4
  308. package/op-sqlite/session.js.map +1 -1
  309. package/package.json +51 -3
  310. package/pg-core/db.cjs +32 -5
  311. package/pg-core/db.cjs.map +1 -1
  312. package/pg-core/db.d.cts +16 -9
  313. package/pg-core/db.d.ts +16 -9
  314. package/pg-core/db.js +31 -4
  315. package/pg-core/db.js.map +1 -1
  316. package/pg-core/dialect.cjs +166 -488
  317. package/pg-core/dialect.cjs.map +1 -1
  318. package/pg-core/dialect.d.cts +27 -8
  319. package/pg-core/dialect.d.ts +27 -8
  320. package/pg-core/dialect.js +164 -495
  321. package/pg-core/dialect.js.map +1 -1
  322. package/pg-core/query-builders/_query.cjs +155 -0
  323. package/pg-core/query-builders/_query.cjs.map +1 -0
  324. package/pg-core/query-builders/_query.d.cts +47 -0
  325. package/pg-core/query-builders/_query.d.ts +47 -0
  326. package/pg-core/query-builders/_query.js +120 -0
  327. package/pg-core/query-builders/_query.js.map +1 -0
  328. package/pg-core/query-builders/count.cjs.map +1 -1
  329. package/pg-core/query-builders/count.d.cts +1 -1
  330. package/pg-core/query-builders/count.d.ts +1 -1
  331. package/pg-core/query-builders/count.js.map +1 -1
  332. package/pg-core/query-builders/query.cjs +15 -18
  333. package/pg-core/query-builders/query.cjs.map +1 -1
  334. package/pg-core/query-builders/query.d.cts +6 -6
  335. package/pg-core/query-builders/query.d.ts +6 -6
  336. package/pg-core/query-builders/query.js +15 -18
  337. package/pg-core/query-builders/query.js.map +1 -1
  338. package/pg-core/session.cjs +3 -2
  339. package/pg-core/session.cjs.map +1 -1
  340. package/pg-core/session.d.cts +9 -6
  341. package/pg-core/session.d.ts +9 -6
  342. package/pg-core/session.js +3 -2
  343. package/pg-core/session.js.map +1 -1
  344. package/pg-proxy/driver.cjs +21 -5
  345. package/pg-proxy/driver.cjs.map +1 -1
  346. package/pg-proxy/driver.d.cts +3 -2
  347. package/pg-proxy/driver.d.ts +3 -2
  348. package/pg-proxy/driver.js +11 -8
  349. package/pg-proxy/driver.js.map +1 -1
  350. package/pg-proxy/migrator.cjs.map +1 -1
  351. package/pg-proxy/migrator.d.cts +2 -1
  352. package/pg-proxy/migrator.d.ts +2 -1
  353. package/pg-proxy/migrator.js.map +1 -1
  354. package/pg-proxy/session.cjs +37 -2
  355. package/pg-proxy/session.cjs.map +1 -1
  356. package/pg-proxy/session.d.cts +13 -8
  357. package/pg-proxy/session.d.ts +13 -8
  358. package/pg-proxy/session.js +37 -2
  359. package/pg-proxy/session.js.map +1 -1
  360. package/pglite/driver.cjs +23 -7
  361. package/pglite/driver.cjs.map +1 -1
  362. package/pglite/driver.d.cts +9 -8
  363. package/pglite/driver.d.ts +9 -8
  364. package/pglite/driver.js +13 -10
  365. package/pglite/driver.js.map +1 -1
  366. package/pglite/migrator.cjs.map +1 -1
  367. package/pglite/migrator.d.cts +2 -1
  368. package/pglite/migrator.d.ts +2 -1
  369. package/pglite/migrator.js.map +1 -1
  370. package/pglite/session.cjs +34 -3
  371. package/pglite/session.cjs.map +1 -1
  372. package/pglite/session.d.cts +13 -8
  373. package/pglite/session.d.ts +13 -8
  374. package/pglite/session.js +34 -3
  375. package/pglite/session.js.map +1 -1
  376. package/planetscale-serverless/driver.cjs +22 -5
  377. package/planetscale-serverless/driver.cjs.map +1 -1
  378. package/planetscale-serverless/driver.d.cts +7 -6
  379. package/planetscale-serverless/driver.d.ts +7 -6
  380. package/planetscale-serverless/driver.js +12 -8
  381. package/planetscale-serverless/driver.js.map +1 -1
  382. package/planetscale-serverless/migrator.cjs.map +1 -1
  383. package/planetscale-serverless/migrator.d.cts +2 -1
  384. package/planetscale-serverless/migrator.d.ts +2 -1
  385. package/planetscale-serverless/migrator.js.map +1 -1
  386. package/planetscale-serverless/session.cjs +45 -5
  387. package/planetscale-serverless/session.cjs.map +1 -1
  388. package/planetscale-serverless/session.d.cts +14 -9
  389. package/planetscale-serverless/session.d.ts +14 -9
  390. package/planetscale-serverless/session.js +45 -5
  391. package/planetscale-serverless/session.js.map +1 -1
  392. package/postgres-js/driver.cjs +6 -5
  393. package/postgres-js/driver.cjs.map +1 -1
  394. package/postgres-js/driver.d.cts +7 -6
  395. package/postgres-js/driver.d.ts +7 -6
  396. package/postgres-js/driver.js +6 -8
  397. package/postgres-js/driver.js.map +1 -1
  398. package/postgres-js/migrator.cjs.map +1 -1
  399. package/postgres-js/migrator.d.cts +2 -1
  400. package/postgres-js/migrator.d.ts +2 -1
  401. package/postgres-js/migrator.js.map +1 -1
  402. package/postgres-js/session.cjs +51 -6
  403. package/postgres-js/session.cjs.map +1 -1
  404. package/postgres-js/session.d.cts +15 -9
  405. package/postgres-js/session.d.ts +15 -9
  406. package/postgres-js/session.js +51 -6
  407. package/postgres-js/session.js.map +1 -1
  408. package/prisma/mysql/driver.cjs +1 -1
  409. package/prisma/mysql/driver.cjs.map +1 -1
  410. package/prisma/mysql/driver.js +1 -1
  411. package/prisma/mysql/driver.js.map +1 -1
  412. package/prisma/mysql/session.cjs +3 -0
  413. package/prisma/mysql/session.cjs.map +1 -1
  414. package/prisma/mysql/session.d.cts +3 -1
  415. package/prisma/mysql/session.d.ts +3 -1
  416. package/prisma/mysql/session.js +3 -0
  417. package/prisma/mysql/session.js.map +1 -1
  418. package/prisma/pg/driver.cjs +1 -1
  419. package/prisma/pg/driver.cjs.map +1 -1
  420. package/prisma/pg/driver.js +1 -1
  421. package/prisma/pg/driver.js.map +1 -1
  422. package/prisma/pg/session.cjs +3 -0
  423. package/prisma/pg/session.cjs.map +1 -1
  424. package/prisma/pg/session.d.cts +3 -1
  425. package/prisma/pg/session.d.ts +3 -1
  426. package/prisma/pg/session.js +3 -0
  427. package/prisma/pg/session.js.map +1 -1
  428. package/prisma/sqlite/driver.cjs +1 -1
  429. package/prisma/sqlite/driver.cjs.map +1 -1
  430. package/prisma/sqlite/driver.js +1 -1
  431. package/prisma/sqlite/driver.js.map +1 -1
  432. package/prisma/sqlite/session.cjs +3 -0
  433. package/prisma/sqlite/session.cjs.map +1 -1
  434. package/prisma/sqlite/session.d.cts +4 -2
  435. package/prisma/sqlite/session.d.ts +4 -2
  436. package/prisma/sqlite/session.js +3 -0
  437. package/prisma/sqlite/session.js.map +1 -1
  438. package/relations.cjs +656 -245
  439. package/relations.cjs.map +1 -1
  440. package/relations.d.cts +310 -140
  441. package/relations.d.ts +310 -140
  442. package/relations.js +649 -240
  443. package/relations.js.map +1 -1
  444. package/singlestore/driver.cjs +1 -1
  445. package/singlestore/driver.cjs.map +1 -1
  446. package/singlestore/driver.d.cts +1 -1
  447. package/singlestore/driver.d.ts +1 -1
  448. package/singlestore/driver.js +3 -3
  449. package/singlestore/driver.js.map +1 -1
  450. package/singlestore/session.cjs.map +1 -1
  451. package/singlestore/session.d.cts +4 -4
  452. package/singlestore/session.d.ts +4 -4
  453. package/singlestore/session.js.map +1 -1
  454. package/singlestore-core/db.cjs.map +1 -1
  455. package/singlestore-core/db.d.cts +3 -3
  456. package/singlestore-core/db.d.ts +3 -3
  457. package/singlestore-core/db.js.map +1 -1
  458. package/singlestore-core/dialect.cjs +16 -6
  459. package/singlestore-core/dialect.cjs.map +1 -1
  460. package/singlestore-core/dialect.d.cts +6 -6
  461. package/singlestore-core/dialect.d.ts +6 -6
  462. package/singlestore-core/dialect.js +6 -12
  463. package/singlestore-core/dialect.js.map +1 -1
  464. package/singlestore-core/query-builders/query.cjs +12 -2
  465. package/singlestore-core/query-builders/query.cjs.map +1 -1
  466. package/singlestore-core/query-builders/query.d.cts +6 -6
  467. package/singlestore-core/query-builders/query.d.ts +6 -6
  468. package/singlestore-core/query-builders/query.js +2 -4
  469. package/singlestore-core/query-builders/query.js.map +1 -1
  470. package/singlestore-core/session.cjs.map +1 -1
  471. package/singlestore-core/session.d.cts +5 -5
  472. package/singlestore-core/session.d.ts +5 -5
  473. package/singlestore-core/session.js.map +1 -1
  474. package/singlestore-proxy/driver.cjs +1 -1
  475. package/singlestore-proxy/driver.cjs.map +1 -1
  476. package/singlestore-proxy/driver.js +3 -3
  477. package/singlestore-proxy/driver.js.map +1 -1
  478. package/singlestore-proxy/session.cjs.map +1 -1
  479. package/singlestore-proxy/session.d.cts +4 -4
  480. package/singlestore-proxy/session.d.ts +4 -4
  481. package/singlestore-proxy/session.js.map +1 -1
  482. package/sql/expressions/conditions.cjs.map +1 -1
  483. package/sql/expressions/conditions.d.cts +4 -4
  484. package/sql/expressions/conditions.d.ts +4 -4
  485. package/sql/expressions/conditions.js.map +1 -1
  486. package/sql/sql.cjs +21 -0
  487. package/sql/sql.cjs.map +1 -1
  488. package/sql/sql.d.cts +8 -8
  489. package/sql/sql.d.ts +8 -8
  490. package/sql/sql.js +22 -1
  491. package/sql/sql.js.map +1 -1
  492. package/sql-js/driver.cjs +16 -5
  493. package/sql-js/driver.cjs.map +1 -1
  494. package/sql-js/driver.d.cts +3 -2
  495. package/sql-js/driver.d.ts +3 -2
  496. package/sql-js/driver.js +6 -8
  497. package/sql-js/driver.js.map +1 -1
  498. package/sql-js/migrator.cjs.map +1 -1
  499. package/sql-js/migrator.d.cts +2 -1
  500. package/sql-js/migrator.d.ts +2 -1
  501. package/sql-js/migrator.js.map +1 -1
  502. package/sql-js/session.cjs +71 -36
  503. package/sql-js/session.cjs.map +1 -1
  504. package/sql-js/session.d.cts +16 -13
  505. package/sql-js/session.d.ts +16 -13
  506. package/sql-js/session.js +71 -36
  507. package/sql-js/session.js.map +1 -1
  508. package/sqlite-core/db.cjs +39 -12
  509. package/sqlite-core/db.cjs.map +1 -1
  510. package/sqlite-core/db.d.cts +16 -8
  511. package/sqlite-core/db.d.ts +16 -8
  512. package/sqlite-core/db.js +38 -11
  513. package/sqlite-core/db.js.map +1 -1
  514. package/sqlite-core/dialect.cjs +180 -7
  515. package/sqlite-core/dialect.cjs.map +1 -1
  516. package/sqlite-core/dialect.d.cts +31 -9
  517. package/sqlite-core/dialect.d.ts +31 -9
  518. package/sqlite-core/dialect.js +178 -14
  519. package/sqlite-core/dialect.js.map +1 -1
  520. package/sqlite-core/query-builders/_query.cjs +187 -0
  521. package/sqlite-core/query-builders/_query.cjs.map +1 -0
  522. package/sqlite-core/query-builders/_query.d.cts +55 -0
  523. package/sqlite-core/query-builders/_query.d.ts +55 -0
  524. package/sqlite-core/query-builders/_query.js +151 -0
  525. package/sqlite-core/query-builders/_query.js.map +1 -0
  526. package/sqlite-core/query-builders/count.cjs.map +1 -1
  527. package/sqlite-core/query-builders/count.d.cts +1 -1
  528. package/sqlite-core/query-builders/count.d.ts +1 -1
  529. package/sqlite-core/query-builders/count.js.map +1 -1
  530. package/sqlite-core/query-builders/delete.cjs.map +1 -1
  531. package/sqlite-core/query-builders/delete.d.cts +1 -1
  532. package/sqlite-core/query-builders/delete.d.ts +1 -1
  533. package/sqlite-core/query-builders/delete.js.map +1 -1
  534. package/sqlite-core/query-builders/insert.cjs.map +1 -1
  535. package/sqlite-core/query-builders/insert.d.cts +3 -3
  536. package/sqlite-core/query-builders/insert.d.ts +3 -3
  537. package/sqlite-core/query-builders/insert.js.map +1 -1
  538. package/sqlite-core/query-builders/query.cjs +56 -35
  539. package/sqlite-core/query-builders/query.cjs.map +1 -1
  540. package/sqlite-core/query-builders/query.d.cts +18 -17
  541. package/sqlite-core/query-builders/query.d.ts +18 -17
  542. package/sqlite-core/query-builders/query.js +56 -35
  543. package/sqlite-core/query-builders/query.js.map +1 -1
  544. package/sqlite-core/query-builders/select.cjs.map +1 -1
  545. package/sqlite-core/query-builders/select.d.cts +3 -3
  546. package/sqlite-core/query-builders/select.d.ts +3 -3
  547. package/sqlite-core/query-builders/select.js.map +1 -1
  548. package/sqlite-core/query-builders/update.cjs.map +1 -1
  549. package/sqlite-core/query-builders/update.d.cts +3 -3
  550. package/sqlite-core/query-builders/update.d.ts +3 -3
  551. package/sqlite-core/query-builders/update.js.map +1 -1
  552. package/sqlite-core/session.cjs +6 -2
  553. package/sqlite-core/session.cjs.map +1 -1
  554. package/sqlite-core/session.d.cts +15 -7
  555. package/sqlite-core/session.d.ts +15 -7
  556. package/sqlite-core/session.js +6 -2
  557. package/sqlite-core/session.js.map +1 -1
  558. package/sqlite-proxy/driver.cjs +23 -5
  559. package/sqlite-proxy/driver.cjs.map +1 -1
  560. package/sqlite-proxy/driver.d.cts +4 -3
  561. package/sqlite-proxy/driver.d.ts +4 -3
  562. package/sqlite-proxy/driver.js +13 -5
  563. package/sqlite-proxy/driver.js.map +1 -1
  564. package/sqlite-proxy/migrator.cjs.map +1 -1
  565. package/sqlite-proxy/migrator.d.cts +2 -1
  566. package/sqlite-proxy/migrator.d.ts +2 -1
  567. package/sqlite-proxy/migrator.js.map +1 -1
  568. package/sqlite-proxy/session.cjs +50 -6
  569. package/sqlite-proxy/session.cjs.map +1 -1
  570. package/sqlite-proxy/session.d.cts +14 -8
  571. package/sqlite-proxy/session.d.ts +14 -8
  572. package/sqlite-proxy/session.js +50 -6
  573. package/sqlite-proxy/session.js.map +1 -1
  574. package/table.cjs.map +1 -1
  575. package/table.d.cts +2 -2
  576. package/table.d.ts +2 -2
  577. package/table.js.map +1 -1
  578. package/tidb-serverless/driver.cjs +22 -5
  579. package/tidb-serverless/driver.cjs.map +1 -1
  580. package/tidb-serverless/driver.d.cts +7 -6
  581. package/tidb-serverless/driver.d.ts +7 -6
  582. package/tidb-serverless/driver.js +12 -8
  583. package/tidb-serverless/driver.js.map +1 -1
  584. package/tidb-serverless/migrator.cjs.map +1 -1
  585. package/tidb-serverless/migrator.d.cts +2 -1
  586. package/tidb-serverless/migrator.d.ts +2 -1
  587. package/tidb-serverless/migrator.js.map +1 -1
  588. package/tidb-serverless/session.cjs +62 -5
  589. package/tidb-serverless/session.cjs.map +1 -1
  590. package/tidb-serverless/session.d.cts +14 -9
  591. package/tidb-serverless/session.d.ts +14 -9
  592. package/tidb-serverless/session.js +62 -5
  593. package/tidb-serverless/session.js.map +1 -1
  594. package/utils.cjs +2 -2
  595. package/utils.cjs.map +1 -1
  596. package/utils.d.cts +3 -1
  597. package/utils.d.ts +3 -1
  598. package/utils.js +2 -2
  599. package/utils.js.map +1 -1
  600. package/vercel-postgres/driver.cjs +23 -7
  601. package/vercel-postgres/driver.cjs.map +1 -1
  602. package/vercel-postgres/driver.d.cts +9 -8
  603. package/vercel-postgres/driver.d.ts +9 -8
  604. package/vercel-postgres/driver.js +13 -10
  605. package/vercel-postgres/driver.js.map +1 -1
  606. package/vercel-postgres/migrator.cjs.map +1 -1
  607. package/vercel-postgres/migrator.d.cts +2 -1
  608. package/vercel-postgres/migrator.d.ts +2 -1
  609. package/vercel-postgres/migrator.js.map +1 -1
  610. package/vercel-postgres/session.cjs +34 -4
  611. package/vercel-postgres/session.cjs.map +1 -1
  612. package/vercel-postgres/session.d.cts +13 -8
  613. package/vercel-postgres/session.d.ts +13 -8
  614. package/vercel-postgres/session.js +34 -4
  615. package/vercel-postgres/session.js.map +1 -1
  616. package/version.cjs +1 -1
  617. package/version.d.cts +1 -1
  618. package/version.d.ts +1 -1
  619. package/version.js +1 -1
  620. package/xata-http/driver.cjs +17 -5
  621. package/xata-http/driver.cjs.map +1 -1
  622. package/xata-http/driver.d.cts +5 -4
  623. package/xata-http/driver.d.ts +5 -4
  624. package/xata-http/driver.js +7 -5
  625. package/xata-http/driver.js.map +1 -1
  626. package/xata-http/migrator.cjs.map +1 -1
  627. package/xata-http/migrator.d.cts +2 -1
  628. package/xata-http/migrator.d.ts +2 -1
  629. package/xata-http/migrator.js.map +1 -1
  630. package/xata-http/session.cjs +30 -2
  631. package/xata-http/session.cjs.map +1 -1
  632. package/xata-http/session.d.cts +13 -8
  633. package/xata-http/session.d.ts +13 -8
  634. package/xata-http/session.js +30 -2
  635. package/xata-http/session.js.map +1 -1
package/relations.cjs CHANGED
@@ -18,311 +18,722 @@ var __copyProps = (to, from, except, desc2) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var relations_exports = {};
20
20
  __export(relations_exports, {
21
+ AggregatedField: () => AggregatedField,
22
+ Count: () => Count,
21
23
  Many: () => Many,
22
24
  One: () => One,
23
25
  Relation: () => Relation,
24
26
  Relations: () => Relations,
25
- createMany: () => createMany,
26
- createOne: () => createOne,
27
- createTableRelationsHelpers: () => createTableRelationsHelpers,
28
- extractTablesRelationalConfig: () => extractTablesRelationalConfig,
29
- getOperators: () => getOperators,
27
+ RelationsBuilderColumn: () => RelationsBuilderColumn,
28
+ RelationsBuilderTable: () => RelationsBuilderTable,
29
+ RelationsHelperStatic: () => RelationsHelperStatic,
30
+ createRelationsHelper: () => createRelationsHelper,
31
+ defineRelations: () => defineRelations,
32
+ fieldSelectionToSQL: () => fieldSelectionToSQL,
30
33
  getOrderByOperators: () => getOrderByOperators,
34
+ getTableAsAliasSQL: () => getTableAsAliasSQL,
31
35
  mapRelationalRow: () => mapRelationalRow,
32
- normalizeRelation: () => normalizeRelation,
33
- relations: () => relations
36
+ operators: () => operators,
37
+ orderByOperators: () => orderByOperators,
38
+ relationExtrasToSQL: () => relationExtrasToSQL,
39
+ relationToSQL: () => relationToSQL,
40
+ relationsFilterToSQL: () => relationsFilterToSQL,
41
+ relationsOrderToSQL: () => relationsOrderToSQL
34
42
  });
35
43
  module.exports = __toCommonJS(relations_exports);
36
44
  var import_table = require("./table.cjs");
45
+ var import_table2 = require("./table.cjs");
46
+ var import_alias = require("./alias.cjs");
37
47
  var import_column = require("./column.cjs");
38
48
  var import_entity = require("./entity.cjs");
39
- var import_primary_keys = require("./pg-core/primary-keys.cjs");
49
+ var import_errors = require("./errors.cjs");
40
50
  var import_expressions = require("./sql/expressions/index.cjs");
41
51
  var import_sql = require("./sql/sql.cjs");
42
- class Relation {
43
- constructor(sourceTable, referencedTable, relationName) {
44
- this.sourceTable = sourceTable;
45
- this.referencedTable = referencedTable;
46
- this.relationName = relationName;
47
- this.referencedTableName = referencedTable[import_table.Table.Symbol.Name];
48
- }
49
- static [import_entity.entityKind] = "Relation";
50
- referencedTableName;
51
- fieldName;
52
- }
53
52
  class Relations {
54
- constructor(table, config) {
55
- this.table = table;
53
+ constructor(schema, tables, config) {
54
+ this.schema = schema;
55
+ this.tables = tables;
56
56
  this.config = config;
57
+ for (const [tsName, table] of Object.entries(tables)) {
58
+ if (config[tsName]?.$drizzleTypeError)
59
+ continue;
60
+ const isTable = (0, import_entity.is)(table, import_table.Table);
61
+ const isView = (0, import_entity.is)(table, import_sql.View);
62
+ if (!(isTable || isView))
63
+ continue;
64
+ this.tableNamesMap[(0, import_table.getTableUniqueName)(table)] = tsName;
65
+ this.tablesConfig[tsName] = {
66
+ table,
67
+ tsName,
68
+ dbName: table[import_table.Table.Symbol.Name],
69
+ schema: table[import_table.Table.Symbol.Schema],
70
+ columns: table[import_table.Table.Symbol.Columns],
71
+ relations: config[tsName] || {}
72
+ };
73
+ }
74
+ for (const tableConfig of Object.values(this.tablesConfig)) {
75
+ for (const [relationFieldName, relation] of Object.entries(tableConfig.relations)) {
76
+ if (!(0, import_entity.is)(relation, Relation)) {
77
+ continue;
78
+ }
79
+ relation.sourceTable = tableConfig.table;
80
+ relation.fieldName = relationFieldName;
81
+ }
82
+ }
83
+ for (const tableConfig of Object.values(this.tablesConfig)) {
84
+ for (const [relationFieldName, relation] of Object.entries(tableConfig.relations)) {
85
+ const relationPrintName = `relations -> ${tableConfig.tsName}.${relationFieldName}`;
86
+ if (!(0, import_entity.is)(relation, Relation)) {
87
+ continue;
88
+ }
89
+ if (typeof relation.alias === "string" && !relation.alias) {
90
+ throw new Error(`${relationPrintName}: "alias" cannot be an empty string - omit it if you don't need it`);
91
+ }
92
+ if (relation.sourceColumns?.length === 0) {
93
+ throw new Error(`${relationPrintName}: "from" cannot be an empty array`);
94
+ }
95
+ if (relation.targetColumns?.length === 0) {
96
+ throw new Error(`${relationPrintName}: "to" cannot be an empty array`);
97
+ }
98
+ if (relation.sourceColumns && relation.targetColumns) {
99
+ if (relation.sourceColumns.length !== relation.targetColumns.length) {
100
+ throw new Error(
101
+ `${relationPrintName}: "from" and "to" fields must have the same length`
102
+ );
103
+ }
104
+ if (relation.through) {
105
+ if (relation.through.source.length !== relation.through.target.length || relation.through.source.length !== relation.sourceColumns.length || relation.through.target.length !== relation.targetColumns.length) {
106
+ throw new Error(
107
+ `${relationPrintName}: ".through(column)" must be used either on all columns in "from" and "to" or not defined on any of them`
108
+ );
109
+ }
110
+ for (const column of relation.through.source) {
111
+ if (tables[column._.tableName] !== relation.throughTable) {
112
+ throw new Error(
113
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
114
+ );
115
+ }
116
+ }
117
+ for (const column of relation.through.target) {
118
+ if (tables[column._.tableName] !== relation.throughTable) {
119
+ throw new Error(
120
+ `${relationPrintName}: ".through(column)" must be used on the same table by all columns of the relation`
121
+ );
122
+ }
123
+ }
124
+ }
125
+ continue;
126
+ }
127
+ if (relation.sourceColumns || relation.targetColumns) {
128
+ throw new Error(
129
+ `${relationPrintName}: relation must have either both "from" and "to" defined, or none of them`
130
+ );
131
+ }
132
+ let reverseRelation;
133
+ const targetTableTsName = this.tableNamesMap[(0, import_table.getTableUniqueName)(relation.targetTable)];
134
+ if (!targetTableTsName) {
135
+ throw new Error(
136
+ `Table "${(0, import_table.getTableUniqueName)(relation.targetTable)}" not found in provided TS schema`
137
+ );
138
+ }
139
+ const reverseTableConfig = this.tablesConfig[targetTableTsName];
140
+ if (!reverseTableConfig) {
141
+ throw new Error(
142
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and no reverse relations of table "${targetTableTsName}" were found"`
143
+ );
144
+ }
145
+ if (relation.alias) {
146
+ const reverseRelations = Object.values(reverseTableConfig.relations).filter(
147
+ (it) => (0, import_entity.is)(it, Relation) && it.alias === relation.alias
148
+ );
149
+ if (reverseRelations.length > 1) {
150
+ throw new Error(
151
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and multiple relations with alias "${relation.alias}" found in table "${targetTableTsName}": ${reverseRelations.map((it) => `"${it.fieldName}"`).join(", ")}`
152
+ );
153
+ }
154
+ reverseRelation = reverseRelations[0];
155
+ if (!reverseRelation) {
156
+ throw new Error(
157
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and there is no reverse relation of table "${targetTableTsName}" with alias "${relation.alias}"`
158
+ );
159
+ }
160
+ } else {
161
+ const reverseRelations = Object.values(reverseTableConfig.relations).filter(
162
+ (it) => (0, import_entity.is)(it, Relation) && it.targetTable === relation.sourceTable && !it.alias
163
+ );
164
+ if (reverseRelations.length > 1) {
165
+ throw new Error(
166
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and multiple relations between "${targetTableTsName}" and "${(0, import_table.getTableUniqueName)(relation.sourceTable)}" were found.
167
+ Hint: you can specify "alias" on both sides of the relation with the same value`
168
+ );
169
+ }
170
+ reverseRelation = reverseRelations[0];
171
+ if (!reverseRelation) {
172
+ throw new Error(
173
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and no reverse relation of table "${targetTableTsName}" with target table "${(0, import_table.getTableUniqueName)(relation.sourceTable)}" was found`
174
+ );
175
+ }
176
+ }
177
+ if (!reverseRelation.sourceColumns || !reverseRelation.targetColumns) {
178
+ throw new Error(
179
+ `${relationPrintName}: not enough data provided to build the relation - "from"/"to" are not defined, and reverse relation "${targetTableTsName}.${reverseRelation.fieldName}" does not have "from"/"to" defined`
180
+ );
181
+ }
182
+ relation.sourceColumns = reverseRelation.targetColumns;
183
+ relation.targetColumns = reverseRelation.sourceColumns;
184
+ relation.through = reverseRelation.through ? {
185
+ source: reverseRelation.through.target,
186
+ target: reverseRelation.through.source
187
+ } : void 0;
188
+ relation.throughTable = reverseRelation.throughTable;
189
+ relation.isReversed = !relation.where;
190
+ relation.where = relation.where ?? reverseRelation.where;
191
+ }
192
+ }
57
193
  }
58
- static [import_entity.entityKind] = "Relations";
194
+ static [import_entity.entityKind] = "RelationsV2";
195
+ /** table DB name -> schema table key */
196
+ tableNamesMap = {};
197
+ tablesConfig = {};
59
198
  }
60
- class One extends Relation {
61
- constructor(sourceTable, referencedTable, config, isNullable) {
62
- super(sourceTable, referencedTable, config?.relationName);
63
- this.config = config;
64
- this.isNullable = isNullable;
199
+ class Relation {
200
+ constructor(targetTable) {
201
+ this.targetTable = targetTable;
65
202
  }
66
- static [import_entity.entityKind] = "One";
67
- withFieldName(fieldName) {
68
- const relation = new One(
69
- this.sourceTable,
70
- this.referencedTable,
71
- this.config,
72
- this.isNullable
73
- );
74
- relation.fieldName = fieldName;
75
- return relation;
203
+ static [import_entity.entityKind] = "RelationV2";
204
+ fieldName;
205
+ sourceColumns;
206
+ targetColumns;
207
+ alias;
208
+ where;
209
+ sourceTable;
210
+ through;
211
+ throughTable;
212
+ isReversed;
213
+ }
214
+ class One extends Relation {
215
+ static [import_entity.entityKind] = "OneV2";
216
+ optional;
217
+ constructor(tables, targetTable, config) {
218
+ super(targetTable);
219
+ this.alias = config?.alias;
220
+ this.where = config?.where;
221
+ if (config?.from) {
222
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
223
+ this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
224
+ return it._.column;
225
+ });
226
+ }
227
+ if (config?.to) {
228
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
229
+ this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
230
+ return it._.column;
231
+ });
232
+ }
233
+ if (this.throughTable) {
234
+ this.through = Array.isArray(config?.from) ? {
235
+ source: config.from.map((c) => c._.through),
236
+ target: (config.to ?? []).map((c) => c._.through)
237
+ } : {
238
+ source: (config?.from ? [config.from] : []).map((c) => c._.through),
239
+ target: (config?.to ? [config.to] : []).map((c) => c._.through)
240
+ };
241
+ }
242
+ this.optional = config?.optional ?? true;
76
243
  }
77
244
  }
78
245
  class Many extends Relation {
79
- constructor(sourceTable, referencedTable, config) {
80
- super(sourceTable, referencedTable, config?.relationName);
246
+ constructor(tables, targetTable, config) {
247
+ super(targetTable);
81
248
  this.config = config;
249
+ this.alias = config?.alias;
250
+ this.where = config?.where;
251
+ if (config?.from) {
252
+ this.sourceColumns = (Array.isArray(config.from) ? config.from : [config.from]).map((it) => {
253
+ this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
254
+ return it._.column;
255
+ });
256
+ }
257
+ if (config?.to) {
258
+ this.targetColumns = (Array.isArray(config.to) ? config.to : [config.to]).map((it) => {
259
+ this.throughTable ??= it._.through ? tables[it._.through._.tableName] : void 0;
260
+ return it._.column;
261
+ });
262
+ }
263
+ if (this.throughTable) {
264
+ this.through = Array.isArray(config?.from) ? {
265
+ source: config.from.map((c) => c._.through),
266
+ target: (config.to ?? []).map((c) => c._.through)
267
+ } : {
268
+ source: (config?.from ? [config.from] : []).map((c) => c._.through),
269
+ target: (config?.to ? [config.to] : []).map((c) => c._.through)
270
+ };
271
+ }
82
272
  }
83
- static [import_entity.entityKind] = "Many";
84
- withFieldName(fieldName) {
85
- const relation = new Many(
86
- this.sourceTable,
87
- this.referencedTable,
88
- this.config
89
- );
90
- relation.fieldName = fieldName;
91
- return relation;
273
+ static [import_entity.entityKind] = "ManyV2";
274
+ }
275
+ class AggregatedField {
276
+ static [import_entity.entityKind] = "AggregatedField";
277
+ table;
278
+ onTable(table) {
279
+ this.table = table;
280
+ return this;
92
281
  }
93
282
  }
94
- function getOperators() {
95
- return {
96
- and: import_expressions.and,
97
- between: import_expressions.between,
98
- eq: import_expressions.eq,
99
- exists: import_expressions.exists,
100
- gt: import_expressions.gt,
101
- gte: import_expressions.gte,
102
- ilike: import_expressions.ilike,
103
- inArray: import_expressions.inArray,
104
- isNull: import_expressions.isNull,
105
- isNotNull: import_expressions.isNotNull,
106
- like: import_expressions.like,
107
- lt: import_expressions.lt,
108
- lte: import_expressions.lte,
109
- ne: import_expressions.ne,
110
- not: import_expressions.not,
111
- notBetween: import_expressions.notBetween,
112
- notExists: import_expressions.notExists,
113
- notLike: import_expressions.notLike,
114
- notIlike: import_expressions.notIlike,
115
- notInArray: import_expressions.notInArray,
116
- or: import_expressions.or,
117
- sql: import_sql.sql
118
- };
283
+ class Count extends AggregatedField {
284
+ static [import_entity.entityKind] = "AggregatedFieldCount";
285
+ query;
286
+ getSQL() {
287
+ if (!this.query) {
288
+ if (!this.table)
289
+ throw new Error("Table must be set before building aggregate field");
290
+ this.query = import_sql.sql`select count(*) as ${import_sql.sql.identifier("r")} from ${getTableAsAliasSQL(this.table)}`.mapWith(Number);
291
+ }
292
+ return this.query;
293
+ }
119
294
  }
295
+ const operators = {
296
+ and: import_expressions.and,
297
+ between: import_expressions.between,
298
+ eq: import_expressions.eq,
299
+ exists: import_expressions.exists,
300
+ gt: import_expressions.gt,
301
+ gte: import_expressions.gte,
302
+ ilike: import_expressions.ilike,
303
+ inArray: import_expressions.inArray,
304
+ isNull: import_expressions.isNull,
305
+ isNotNull: import_expressions.isNotNull,
306
+ like: import_expressions.like,
307
+ lt: import_expressions.lt,
308
+ lte: import_expressions.lte,
309
+ ne: import_expressions.ne,
310
+ not: import_expressions.not,
311
+ notBetween: import_expressions.notBetween,
312
+ notExists: import_expressions.notExists,
313
+ notLike: import_expressions.notLike,
314
+ notIlike: import_expressions.notIlike,
315
+ notInArray: import_expressions.notInArray,
316
+ or: import_expressions.or,
317
+ sql: import_sql.sql
318
+ };
319
+ const orderByOperators = {
320
+ sql: import_sql.sql,
321
+ asc: import_expressions.asc,
322
+ desc: import_expressions.desc
323
+ };
120
324
  function getOrderByOperators() {
121
- return {
122
- sql: import_sql.sql,
123
- asc: import_expressions.asc,
124
- desc: import_expressions.desc
125
- };
325
+ return orderByOperators;
126
326
  }
127
- function extractTablesRelationalConfig(schema, configHelpers) {
128
- if (Object.keys(schema).length === 1 && "default" in schema && !(0, import_entity.is)(schema["default"], import_table.Table)) {
129
- schema = schema["default"];
130
- }
131
- const tableNamesMap = {};
132
- const relationsBuffer = {};
133
- const tablesConfig = {};
134
- for (const [key, value] of Object.entries(schema)) {
135
- if ((0, import_entity.is)(value, import_table.Table)) {
136
- const dbName = (0, import_table.getTableUniqueName)(value);
137
- const bufferedRelations = relationsBuffer[dbName];
138
- tableNamesMap[dbName] = key;
139
- tablesConfig[key] = {
140
- tsName: key,
141
- dbName: value[import_table.Table.Symbol.Name],
142
- schema: value[import_table.Table.Symbol.Schema],
143
- columns: value[import_table.Table.Symbol.Columns],
144
- relations: bufferedRelations?.relations ?? {},
145
- primaryKey: bufferedRelations?.primaryKey ?? []
146
- };
147
- for (const column of Object.values(
148
- value[import_table.Table.Symbol.Columns]
149
- )) {
150
- if (column.primary) {
151
- tablesConfig[key].primaryKey.push(column);
327
+ function mapRelationalRow(row, buildQueryResultSelection, mapColumnValue = (value) => value, parseJson = false, path) {
328
+ for (const selectionItem of buildQueryResultSelection) {
329
+ if (selectionItem.selection) {
330
+ const currentPath = `${path ? `${path}.` : ""}${selectionItem.key}`;
331
+ if (row[selectionItem.key] === null)
332
+ continue;
333
+ if (parseJson)
334
+ row[selectionItem.key] = JSON.parse(row[selectionItem.key]);
335
+ if (selectionItem.isArray) {
336
+ for (const item of row[selectionItem.key]) {
337
+ mapRelationalRow(
338
+ item,
339
+ selectionItem.selection,
340
+ mapColumnValue,
341
+ false,
342
+ currentPath
343
+ );
152
344
  }
345
+ continue;
153
346
  }
154
- const extraConfig = value[import_table.Table.Symbol.ExtraConfigBuilder]?.(value[import_table.Table.Symbol.ExtraConfigColumns]);
155
- if (extraConfig) {
156
- for (const configEntry of Object.values(extraConfig)) {
157
- if ((0, import_entity.is)(configEntry, import_primary_keys.PrimaryKeyBuilder)) {
158
- tablesConfig[key].primaryKey.push(...configEntry.columns);
159
- }
160
- }
161
- }
162
- } else if ((0, import_entity.is)(value, Relations)) {
163
- const dbName = (0, import_table.getTableUniqueName)(value.table);
164
- const tableName = tableNamesMap[dbName];
165
- const relations2 = value.config(
166
- configHelpers(value.table)
347
+ mapRelationalRow(
348
+ row[selectionItem.key],
349
+ selectionItem.selection,
350
+ mapColumnValue,
351
+ false,
352
+ currentPath
167
353
  );
168
- let primaryKey;
169
- for (const [relationName, relation] of Object.entries(relations2)) {
170
- if (tableName) {
171
- const tableConfig = tablesConfig[tableName];
172
- tableConfig.relations[relationName] = relation;
173
- if (primaryKey) {
174
- tableConfig.primaryKey.push(...primaryKey);
175
- }
176
- } else {
177
- if (!(dbName in relationsBuffer)) {
178
- relationsBuffer[dbName] = {
179
- relations: {},
180
- primaryKey
181
- };
182
- }
183
- relationsBuffer[dbName].relations[relationName] = relation;
184
- }
185
- }
354
+ continue;
355
+ }
356
+ const field = selectionItem.field;
357
+ const value = mapColumnValue(row[selectionItem.key]);
358
+ if (value === null)
359
+ continue;
360
+ let decoder;
361
+ if ((0, import_entity.is)(field, import_column.Column)) {
362
+ decoder = field;
363
+ } else if ((0, import_entity.is)(field, import_sql.SQL)) {
364
+ decoder = field.decoder;
365
+ } else if ((0, import_entity.is)(field, import_sql.SQL.Aliased)) {
366
+ decoder = field.sql.decoder;
367
+ } else {
368
+ decoder = field.getSQL().decoder;
186
369
  }
370
+ row[selectionItem.key] = decoder.mapFromDriverValue(value);
187
371
  }
188
- return { tables: tablesConfig, tableNamesMap };
189
- }
190
- function relations(table, relations2) {
191
- return new Relations(
192
- table,
193
- (helpers) => Object.fromEntries(
194
- Object.entries(relations2(helpers)).map(([key, value]) => [
195
- key,
196
- value.withFieldName(key)
197
- ])
198
- )
199
- );
200
- }
201
- function createOne(sourceTable) {
202
- return function one(table, config) {
203
- return new One(
204
- sourceTable,
205
- table,
206
- config,
207
- config?.fields.reduce((res, f) => res && f.notNull, true) ?? false
208
- );
209
- };
372
+ return row;
210
373
  }
211
- function createMany(sourceTable) {
212
- return function many(referencedTable, config) {
213
- return new Many(sourceTable, referencedTable, config);
214
- };
374
+ class RelationsBuilderTable {
375
+ static [import_entity.entityKind] = "RelationsBuilderTable";
376
+ _;
377
+ constructor(table, key) {
378
+ this._ = {
379
+ name: key,
380
+ table
381
+ };
382
+ }
383
+ getSQL() {
384
+ return this._.table.getSQL();
385
+ }
215
386
  }
216
- function normalizeRelation(schema, tableNamesMap, relation) {
217
- if ((0, import_entity.is)(relation, One) && relation.config) {
218
- return {
219
- fields: relation.config.fields,
220
- references: relation.config.references
387
+ class RelationsBuilderColumn {
388
+ static [import_entity.entityKind] = "RelationsBuilderColumn";
389
+ _;
390
+ constructor(column, tableName, key, through) {
391
+ this._ = {
392
+ tableName,
393
+ data: void 0,
394
+ column,
395
+ through,
396
+ key
221
397
  };
222
398
  }
223
- const referencedTableTsName = tableNamesMap[(0, import_table.getTableUniqueName)(relation.referencedTable)];
224
- if (!referencedTableTsName) {
225
- throw new Error(
226
- `Table "${relation.referencedTable[import_table.Table.Symbol.Name]}" not found in schema`
399
+ through(column) {
400
+ return new RelationsBuilderColumn(
401
+ this._.column,
402
+ this._.tableName,
403
+ this._.key,
404
+ column
227
405
  );
228
406
  }
229
- const referencedTableConfig = schema[referencedTableTsName];
230
- if (!referencedTableConfig) {
231
- throw new Error(`Table "${referencedTableTsName}" not found in schema`);
407
+ getSQL() {
408
+ return this._.column.getSQL();
232
409
  }
233
- const sourceTable = relation.sourceTable;
234
- const sourceTableTsName = tableNamesMap[(0, import_table.getTableUniqueName)(sourceTable)];
235
- if (!sourceTableTsName) {
236
- throw new Error(
237
- `Table "${sourceTable[import_table.Table.Symbol.Name]}" not found in schema`
238
- );
410
+ }
411
+ class RelationsHelperStatic {
412
+ static [import_entity.entityKind] = "RelationsHelperStatic";
413
+ _;
414
+ constructor(tables) {
415
+ this._ = {
416
+ tables
417
+ };
418
+ const one = {};
419
+ const many = {};
420
+ for (const [tableName, table] of Object.entries(tables)) {
421
+ one[tableName] = (config) => {
422
+ return new One(tables, table, config);
423
+ };
424
+ many[tableName] = (config) => {
425
+ return new Many(tables, table, config);
426
+ };
427
+ }
428
+ this.one = one;
429
+ this.many = many;
239
430
  }
240
- const reverseRelations = [];
241
- for (const referencedTableRelation of Object.values(
242
- referencedTableConfig.relations
243
- )) {
244
- if (relation.relationName && relation !== referencedTableRelation && referencedTableRelation.relationName === relation.relationName || !relation.relationName && referencedTableRelation.referencedTable === relation.sourceTable) {
245
- reverseRelations.push(referencedTableRelation);
431
+ one;
432
+ many;
433
+ /** @internal - to be reworked */
434
+ aggs = {
435
+ count() {
436
+ return new Count();
437
+ }
438
+ };
439
+ }
440
+ function createRelationsHelper(schema) {
441
+ const schemaTables = Object.fromEntries(
442
+ Object.entries(schema).filter((e) => (0, import_entity.is)(e[1], import_table.Table) || (0, import_entity.is)(e[1], import_sql.View))
443
+ );
444
+ const helperStatic = new RelationsHelperStatic(schemaTables);
445
+ const tables = Object.entries(schema).reduce((acc, [tKey, value]) => {
446
+ if ((0, import_entity.is)(value, import_table.Table) || (0, import_entity.is)(value, import_sql.View)) {
447
+ const rTable = new RelationsBuilderTable(value, tKey);
448
+ const columns = Object.entries(value[import_table2.Columns]).reduce(
449
+ (acc2, [cKey, column]) => {
450
+ const rbColumn = new RelationsBuilderColumn(column, tKey, cKey);
451
+ acc2[cKey] = rbColumn;
452
+ return acc2;
453
+ },
454
+ {}
455
+ );
456
+ acc[tKey] = Object.assign(rTable, columns);
457
+ }
458
+ return acc;
459
+ }, {});
460
+ return Object.assign(helperStatic, tables);
461
+ }
462
+ function defineRelations(schema, relations) {
463
+ return new Relations(
464
+ schema,
465
+ schema,
466
+ relations(createRelationsHelper(schema))
467
+ );
468
+ }
469
+ function fieldSelectionToSQL(table, target) {
470
+ const field = table[import_table2.Columns][target];
471
+ return field ? (0, import_entity.is)(field, import_column.Column) ? field : (0, import_entity.is)(field, import_sql.SQL.Aliased) ? import_sql.sql`${table}.${import_sql.sql.identifier(field.fieldAlias)}` : import_sql.sql`${table}.${import_sql.sql.identifier(target)}` : import_sql.sql`${table}.${import_sql.sql.identifier(target)}`;
472
+ }
473
+ function relationsFieldFilterToSQL(column, filter) {
474
+ if (typeof filter !== "object")
475
+ return (0, import_expressions.eq)(column, filter);
476
+ const entries = Object.entries(filter);
477
+ if (!entries.length)
478
+ return void 0;
479
+ const parts = [];
480
+ for (const [target, value] of entries) {
481
+ if (value === void 0)
482
+ continue;
483
+ switch (target) {
484
+ case "NOT": {
485
+ const res = relationsFieldFilterToSQL(column, value);
486
+ if (!res)
487
+ continue;
488
+ parts.push((0, import_expressions.not)(res));
489
+ continue;
490
+ }
491
+ case "OR": {
492
+ if (!value.length)
493
+ continue;
494
+ parts.push(
495
+ (0, import_expressions.or)(
496
+ ...value.map((subFilter) => relationsFieldFilterToSQL(column, subFilter))
497
+ )
498
+ );
499
+ continue;
500
+ }
501
+ case "AND": {
502
+ if (!value.length)
503
+ continue;
504
+ parts.push(
505
+ (0, import_expressions.and)(
506
+ ...value.map((subFilter) => relationsFieldFilterToSQL(column, subFilter))
507
+ )
508
+ );
509
+ continue;
510
+ }
511
+ case "isNotNull":
512
+ case "isNull": {
513
+ if (!value)
514
+ continue;
515
+ parts.push(operators[target](column));
516
+ continue;
517
+ }
518
+ case "in": {
519
+ parts.push(operators.inArray(column, value));
520
+ continue;
521
+ }
522
+ case "notIn": {
523
+ parts.push(operators.notInArray(column, value));
524
+ continue;
525
+ }
526
+ default: {
527
+ parts.push(
528
+ operators[target](
529
+ column,
530
+ value
531
+ )
532
+ );
533
+ continue;
534
+ }
246
535
  }
247
536
  }
248
- if (reverseRelations.length > 1) {
249
- throw relation.relationName ? new Error(
250
- `There are multiple relations with name "${relation.relationName}" in table "${referencedTableTsName}"`
251
- ) : new Error(
252
- `There are multiple relations between "${referencedTableTsName}" and "${relation.sourceTable[import_table.Table.Symbol.Name]}". Please specify relation name`
253
- );
537
+ if (!parts.length)
538
+ return void 0;
539
+ return (0, import_expressions.and)(...parts);
540
+ }
541
+ function relationsFilterToSQL(table, filter, tableRelations = {}, tablesRelations = {}, tableNamesMap = {}, casing, depth = 0) {
542
+ const entries = Object.entries(filter);
543
+ if (!entries.length)
544
+ return void 0;
545
+ const parts = [];
546
+ for (const [target, value] of entries) {
547
+ if (value === void 0)
548
+ continue;
549
+ switch (target) {
550
+ case "RAW": {
551
+ const processed = typeof value === "function" ? value(table, operators) : value.getSQL();
552
+ parts.push(processed);
553
+ continue;
554
+ }
555
+ case "OR": {
556
+ if (!value?.length)
557
+ continue;
558
+ parts.push(
559
+ (0, import_expressions.or)(
560
+ ...value.map(
561
+ (subFilter) => relationsFilterToSQL(table, subFilter, tableRelations, tablesRelations, tableNamesMap, casing, depth)
562
+ )
563
+ )
564
+ );
565
+ continue;
566
+ }
567
+ case "AND": {
568
+ if (!value?.length)
569
+ continue;
570
+ parts.push(
571
+ (0, import_expressions.and)(
572
+ ...value.map(
573
+ (subFilter) => relationsFilterToSQL(table, subFilter, tableRelations, tablesRelations, tableNamesMap, casing, depth)
574
+ )
575
+ )
576
+ );
577
+ continue;
578
+ }
579
+ case "NOT": {
580
+ if (value === void 0)
581
+ continue;
582
+ const built = relationsFilterToSQL(
583
+ table,
584
+ value,
585
+ tableRelations,
586
+ tablesRelations,
587
+ tableNamesMap,
588
+ casing,
589
+ depth
590
+ );
591
+ if (!built)
592
+ continue;
593
+ parts.push((0, import_expressions.not)(built));
594
+ continue;
595
+ }
596
+ default: {
597
+ if (table[import_table2.Columns][target]) {
598
+ const column = fieldSelectionToSQL(table, target);
599
+ const colFilter = relationsFieldFilterToSQL(
600
+ column,
601
+ value
602
+ );
603
+ if (colFilter)
604
+ parts.push(colFilter);
605
+ continue;
606
+ }
607
+ const relation = tableRelations[target];
608
+ if (!relation) {
609
+ throw new import_errors.DrizzleError({
610
+ message: `Unknown relational filter field: "${target}"`
611
+ });
612
+ }
613
+ const targetTable = (0, import_alias.aliasedTable)(relation.targetTable, `f${depth}`);
614
+ const throughTable = relation.throughTable ? (0, import_alias.aliasedTable)(relation.throughTable, `ft${depth}`) : void 0;
615
+ const targetConfig = tablesRelations[tableNamesMap[(0, import_table.getTableUniqueName)(relation.targetTable)]];
616
+ const {
617
+ filter: relationFilter,
618
+ joinCondition
619
+ } = relationToSQL(casing, relation, table, targetTable, throughTable);
620
+ const subfilter = typeof value === "boolean" ? void 0 : relationsFilterToSQL(
621
+ targetTable,
622
+ value,
623
+ targetConfig.relations,
624
+ tablesRelations,
625
+ tableNamesMap,
626
+ casing,
627
+ depth + 1
628
+ );
629
+ const filter2 = (0, import_expressions.and)(
630
+ relationFilter,
631
+ subfilter
632
+ );
633
+ const subquery = throughTable ? import_sql.sql`(select * from ${getTableAsAliasSQL(targetTable)} inner join ${getTableAsAliasSQL(throughTable)} on ${joinCondition}${import_sql.sql` where ${filter2}`.if(filter2)} limit 1)` : import_sql.sql`(select * from ${getTableAsAliasSQL(targetTable)}${import_sql.sql` where ${filter2}`.if(filter2)} limit 1)`;
634
+ if (filter2)
635
+ parts.push((value ? import_expressions.exists : import_expressions.notExists)(subquery));
636
+ }
637
+ }
254
638
  }
255
- if (reverseRelations[0] && (0, import_entity.is)(reverseRelations[0], One) && reverseRelations[0].config) {
256
- return {
257
- fields: reverseRelations[0].config.references,
258
- references: reverseRelations[0].config.fields
259
- };
639
+ return (0, import_expressions.and)(...parts);
640
+ }
641
+ function relationsOrderToSQL(table, orders) {
642
+ if (typeof orders === "function") {
643
+ const data = orders(table, orderByOperators);
644
+ return (0, import_entity.is)(data, import_sql.SQL) ? data : Array.isArray(data) ? data.length ? import_sql.sql.join(data.map((o) => (0, import_entity.is)(o, import_sql.SQL) ? o : (0, import_expressions.asc)(o)), import_sql.sql`, `) : void 0 : (0, import_entity.is)(data, import_column.Column) ? (0, import_expressions.asc)(data) : void 0;
260
645
  }
261
- throw new Error(
262
- `There is not enough information to infer relation "${sourceTableTsName}.${relation.fieldName}"`
646
+ const entries = Object.entries(orders).filter(([_, value]) => value);
647
+ if (!entries.length)
648
+ return void 0;
649
+ return import_sql.sql.join(
650
+ entries.map(([target, value]) => (value === "asc" ? import_expressions.asc : import_expressions.desc)(fieldSelectionToSQL(table, target))),
651
+ import_sql.sql`, `
263
652
  );
264
653
  }
265
- function createTableRelationsHelpers(sourceTable) {
654
+ function relationExtrasToSQL(table, extras) {
655
+ const subqueries = [];
656
+ const selection = [];
657
+ for (const [key, extra] of Object.entries(
658
+ typeof extras === "function" ? extras(table, { sql: operators.sql }) : extras
659
+ )) {
660
+ if (!extra)
661
+ continue;
662
+ const query = import_sql.sql`(${extra.getSQL()}) as ${import_sql.sql.identifier(key)}`;
663
+ query.decoder = extra.getSQL().decoder;
664
+ subqueries.push(query);
665
+ selection.push({
666
+ key,
667
+ field: query
668
+ });
669
+ }
266
670
  return {
267
- one: createOne(sourceTable),
268
- many: createMany(sourceTable)
671
+ sql: subqueries.length ? import_sql.sql.join(subqueries, import_sql.sql`, `) : void 0,
672
+ selection
269
673
  };
270
674
  }
271
- function mapRelationalRow(tablesConfig, tableConfig, row, buildQueryResultSelection, mapColumnValue = (value) => value) {
272
- const result = {};
273
- for (const [
274
- selectionItemIndex,
275
- selectionItem
276
- ] of buildQueryResultSelection.entries()) {
277
- if (selectionItem.isJson) {
278
- const relation = tableConfig.relations[selectionItem.tsKey];
279
- const rawSubRows = row[selectionItemIndex];
280
- const subRows = typeof rawSubRows === "string" ? JSON.parse(rawSubRows) : rawSubRows;
281
- result[selectionItem.tsKey] = (0, import_entity.is)(relation, One) ? subRows && mapRelationalRow(
282
- tablesConfig,
283
- tablesConfig[selectionItem.relationTableTsKey],
284
- subRows,
285
- selectionItem.selection,
286
- mapColumnValue
287
- ) : subRows.map(
288
- (subRow) => mapRelationalRow(
289
- tablesConfig,
290
- tablesConfig[selectionItem.relationTableTsKey],
291
- subRow,
292
- selectionItem.selection,
293
- mapColumnValue
294
- )
675
+ function relationToSQL(casing, relation, sourceTable, targetTable, throughTable) {
676
+ if (relation.through) {
677
+ const outerColumnWhere = relation.sourceColumns.map((s, i) => {
678
+ const t = relation.through.source[i];
679
+ return (0, import_expressions.eq)(
680
+ import_sql.sql`${sourceTable}.${import_sql.sql.identifier(casing.getColumnCasing(s))}`,
681
+ import_sql.sql`${throughTable}.${import_sql.sql.identifier((0, import_entity.is)(t._.column, import_column.Column) ? casing.getColumnCasing(t._.column) : t._.key)}`
295
682
  );
296
- } else {
297
- const value = mapColumnValue(row[selectionItemIndex]);
298
- const field = selectionItem.field;
299
- let decoder;
300
- if ((0, import_entity.is)(field, import_column.Column)) {
301
- decoder = field;
302
- } else if ((0, import_entity.is)(field, import_sql.SQL)) {
303
- decoder = field.decoder;
304
- } else {
305
- decoder = field.sql.decoder;
306
- }
307
- result[selectionItem.tsKey] = value === null ? null : decoder.mapFromDriverValue(value);
308
- }
683
+ });
684
+ const innerColumnWhere = relation.targetColumns.map((s, i) => {
685
+ const t = relation.through.target[i];
686
+ return (0, import_expressions.eq)(
687
+ import_sql.sql`${throughTable}.${import_sql.sql.identifier((0, import_entity.is)(t._.column, import_column.Column) ? casing.getColumnCasing(t._.column) : t._.key)}`,
688
+ import_sql.sql`${targetTable}.${import_sql.sql.identifier(casing.getColumnCasing(s))}`
689
+ );
690
+ });
691
+ return {
692
+ filter: (0, import_expressions.and)(
693
+ relation.where ? relationsFilterToSQL(relation.isReversed ? targetTable : sourceTable, relation.where) : void 0,
694
+ ...outerColumnWhere
695
+ ),
696
+ joinCondition: (0, import_expressions.and)(...innerColumnWhere)
697
+ };
309
698
  }
310
- return result;
699
+ const columnWhere = relation.sourceColumns.map((s, i) => {
700
+ const t = relation.targetColumns[i];
701
+ return (0, import_expressions.eq)(
702
+ import_sql.sql`${sourceTable}.${import_sql.sql.identifier(casing.getColumnCasing(s))}`,
703
+ import_sql.sql`${targetTable}.${import_sql.sql.identifier(casing.getColumnCasing(t))}`
704
+ );
705
+ });
706
+ const fullWhere = (0, import_expressions.and)(
707
+ ...columnWhere,
708
+ relation.where ? relationsFilterToSQL(relation.isReversed ? targetTable : sourceTable, relation.where) : void 0
709
+ );
710
+ return { filter: fullWhere };
711
+ }
712
+ function getTableAsAliasSQL(table) {
713
+ return import_sql.sql`${table[import_table.IsAlias] ? import_sql.sql`${import_sql.sql`${import_sql.sql.identifier(table[import_table.Schema] ?? "")}.`.if(table[import_table.Schema])}${import_sql.sql.identifier(table[import_table.OriginalName])} as ${table}` : table}`;
311
714
  }
312
715
  // Annotate the CommonJS export names for ESM import in node:
313
716
  0 && (module.exports = {
717
+ AggregatedField,
718
+ Count,
314
719
  Many,
315
720
  One,
316
721
  Relation,
317
722
  Relations,
318
- createMany,
319
- createOne,
320
- createTableRelationsHelpers,
321
- extractTablesRelationalConfig,
322
- getOperators,
723
+ RelationsBuilderColumn,
724
+ RelationsBuilderTable,
725
+ RelationsHelperStatic,
726
+ createRelationsHelper,
727
+ defineRelations,
728
+ fieldSelectionToSQL,
323
729
  getOrderByOperators,
730
+ getTableAsAliasSQL,
324
731
  mapRelationalRow,
325
- normalizeRelation,
326
- relations
732
+ operators,
733
+ orderByOperators,
734
+ relationExtrasToSQL,
735
+ relationToSQL,
736
+ relationsFilterToSQL,
737
+ relationsOrderToSQL
327
738
  });
328
739
  //# sourceMappingURL=relations.cjs.map