drizzle-orm 0.39.2-1177ad8 → 0.39.2-aaa57ff

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