drizzle-orm 0.38.4 → 0.39.0-0cc153d

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