drizzle-orm 0.39.2-1177ad8 → 0.39.2-8628c8a

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 (587) hide show
  1. package/aws-data-api/pg/driver.cjs +1 -5
  2. package/aws-data-api/pg/driver.cjs.map +1 -1
  3. package/aws-data-api/pg/driver.d.cts +0 -1
  4. package/aws-data-api/pg/driver.d.ts +0 -1
  5. package/aws-data-api/pg/driver.js +1 -5
  6. package/aws-data-api/pg/driver.js.map +1 -1
  7. package/aws-data-api/pg/session.cjs +4 -15
  8. package/aws-data-api/pg/session.cjs.map +1 -1
  9. package/aws-data-api/pg/session.d.cts +3 -13
  10. package/aws-data-api/pg/session.d.ts +3 -13
  11. package/aws-data-api/pg/session.js +4 -15
  12. package/aws-data-api/pg/session.js.map +1 -1
  13. package/bun-sql/driver.cjs +1 -5
  14. package/bun-sql/driver.cjs.map +1 -1
  15. package/bun-sql/driver.js +1 -5
  16. package/bun-sql/driver.js.map +1 -1
  17. package/bun-sql/session.cjs +9 -21
  18. package/bun-sql/session.cjs.map +1 -1
  19. package/bun-sql/session.d.cts +3 -12
  20. package/bun-sql/session.d.ts +3 -12
  21. package/bun-sql/session.js +9 -21
  22. package/bun-sql/session.js.map +1 -1
  23. package/column-builder.cjs.map +1 -1
  24. package/column-builder.d.cts +6 -5
  25. package/column-builder.d.ts +6 -5
  26. package/column-builder.js.map +1 -1
  27. package/gel/driver.cjs +97 -0
  28. package/gel/driver.cjs.map +1 -0
  29. package/gel/driver.d.cts +38 -0
  30. package/gel/driver.d.ts +38 -0
  31. package/gel/driver.js +74 -0
  32. package/gel/driver.js.map +1 -0
  33. package/{cache/core → gel}/index.cjs +6 -4
  34. package/gel/index.cjs.map +1 -0
  35. package/gel/index.d.cts +2 -0
  36. package/gel/index.d.ts +2 -0
  37. package/gel/index.js +3 -0
  38. package/gel/index.js.map +1 -0
  39. package/gel/migrator.cjs +5 -0
  40. package/gel/migrator.cjs.map +1 -0
  41. package/gel/migrator.d.cts +1 -0
  42. package/gel/migrator.d.ts +1 -0
  43. package/gel/migrator.js +4 -0
  44. package/gel/migrator.js.map +1 -0
  45. package/gel/session.cjs +139 -0
  46. package/gel/session.cjs.map +1 -0
  47. package/gel/session.d.cts +47 -0
  48. package/gel/session.d.ts +47 -0
  49. package/gel/session.js +113 -0
  50. package/gel/session.js.map +1 -0
  51. package/gel-core/alias.cjs +32 -0
  52. package/gel-core/alias.cjs.map +1 -0
  53. package/gel-core/alias.d.cts +4 -0
  54. package/gel-core/alias.d.ts +4 -0
  55. package/gel-core/alias.js +8 -0
  56. package/gel-core/alias.js.map +1 -0
  57. package/gel-core/checks.cjs +58 -0
  58. package/gel-core/checks.cjs.map +1 -0
  59. package/gel-core/checks.d.cts +18 -0
  60. package/gel-core/checks.d.ts +18 -0
  61. package/gel-core/checks.js +32 -0
  62. package/gel-core/checks.js.map +1 -0
  63. package/gel-core/columns/all.cjs +72 -0
  64. package/gel-core/columns/all.cjs.map +1 -0
  65. package/gel-core/columns/all.d.cts +43 -0
  66. package/gel-core/columns/all.d.ts +43 -0
  67. package/gel-core/columns/all.js +48 -0
  68. package/gel-core/columns/all.js.map +1 -0
  69. package/gel-core/columns/bigint.cjs +54 -0
  70. package/gel-core/columns/bigint.cjs.map +1 -0
  71. package/gel-core/columns/bigint.d.cts +23 -0
  72. package/gel-core/columns/bigint.d.ts +23 -0
  73. package/gel-core/columns/bigint.js +28 -0
  74. package/gel-core/columns/bigint.js.map +1 -0
  75. package/gel-core/columns/bigintT.cjs +60 -0
  76. package/gel-core/columns/bigintT.cjs.map +1 -0
  77. package/gel-core/columns/bigintT.d.cts +24 -0
  78. package/gel-core/columns/bigintT.d.ts +24 -0
  79. package/gel-core/columns/bigintT.js +34 -0
  80. package/gel-core/columns/bigintT.js.map +1 -0
  81. package/gel-core/columns/boolean.cjs +53 -0
  82. package/gel-core/columns/boolean.cjs.map +1 -0
  83. package/gel-core/columns/boolean.d.cts +22 -0
  84. package/gel-core/columns/boolean.d.ts +22 -0
  85. package/gel-core/columns/boolean.js +27 -0
  86. package/gel-core/columns/boolean.js.map +1 -0
  87. package/gel-core/columns/bytes.cjs +56 -0
  88. package/gel-core/columns/bytes.cjs.map +1 -0
  89. package/gel-core/columns/bytes.d.cts +22 -0
  90. package/gel-core/columns/bytes.d.ts +22 -0
  91. package/gel-core/columns/bytes.js +30 -0
  92. package/gel-core/columns/bytes.js.map +1 -0
  93. package/gel-core/columns/common.cjs +213 -0
  94. package/gel-core/columns/common.cjs.map +1 -0
  95. package/gel-core/columns/common.d.cts +147 -0
  96. package/gel-core/columns/common.d.ts +147 -0
  97. package/gel-core/columns/common.js +184 -0
  98. package/gel-core/columns/common.js.map +1 -0
  99. package/gel-core/columns/custom.cjs +77 -0
  100. package/gel-core/columns/custom.cjs.map +1 -0
  101. package/gel-core/columns/custom.d.cts +155 -0
  102. package/gel-core/columns/custom.d.ts +155 -0
  103. package/gel-core/columns/custom.js +51 -0
  104. package/gel-core/columns/custom.js.map +1 -0
  105. package/gel-core/columns/date-duration.cjs +56 -0
  106. package/gel-core/columns/date-duration.cjs.map +1 -0
  107. package/gel-core/columns/date-duration.d.cts +23 -0
  108. package/gel-core/columns/date-duration.d.ts +23 -0
  109. package/gel-core/columns/date-duration.js +30 -0
  110. package/gel-core/columns/date-duration.js.map +1 -0
  111. package/{cache/core/cache.cjs → gel-core/columns/date.common.cjs} +12 -22
  112. package/gel-core/columns/date.common.cjs.map +1 -0
  113. package/gel-core/columns/date.common.d.cts +7 -0
  114. package/gel-core/columns/date.common.d.ts +7 -0
  115. package/gel-core/columns/date.common.js +13 -0
  116. package/gel-core/columns/date.common.js.map +1 -0
  117. package/gel-core/columns/decimal.cjs +56 -0
  118. package/gel-core/columns/decimal.cjs.map +1 -0
  119. package/gel-core/columns/decimal.d.cts +26 -0
  120. package/gel-core/columns/decimal.d.ts +26 -0
  121. package/gel-core/columns/decimal.js +30 -0
  122. package/gel-core/columns/decimal.js.map +1 -0
  123. package/gel-core/columns/double-precision.cjs +62 -0
  124. package/gel-core/columns/double-precision.cjs.map +1 -0
  125. package/gel-core/columns/double-precision.d.cts +23 -0
  126. package/gel-core/columns/double-precision.d.ts +23 -0
  127. package/gel-core/columns/double-precision.js +36 -0
  128. package/gel-core/columns/double-precision.js.map +1 -0
  129. package/gel-core/columns/duration.cjs +53 -0
  130. package/gel-core/columns/duration.cjs.map +1 -0
  131. package/gel-core/columns/duration.d.cts +23 -0
  132. package/gel-core/columns/duration.d.ts +23 -0
  133. package/gel-core/columns/duration.js +27 -0
  134. package/gel-core/columns/duration.js.map +1 -0
  135. package/gel-core/columns/index.cjs +65 -0
  136. package/gel-core/columns/index.cjs.map +1 -0
  137. package/gel-core/columns/index.d.cts +22 -0
  138. package/gel-core/columns/index.d.ts +22 -0
  139. package/gel-core/columns/index.js +23 -0
  140. package/gel-core/columns/index.js.map +1 -0
  141. package/gel-core/columns/int.common.cjs +67 -0
  142. package/gel-core/columns/int.common.cjs.map +1 -0
  143. package/gel-core/columns/int.common.d.cts +15 -0
  144. package/gel-core/columns/int.common.d.ts +15 -0
  145. package/gel-core/columns/int.common.js +43 -0
  146. package/gel-core/columns/int.common.js.map +1 -0
  147. package/gel-core/columns/integer.cjs +54 -0
  148. package/gel-core/columns/integer.cjs.map +1 -0
  149. package/gel-core/columns/integer.d.cts +23 -0
  150. package/gel-core/columns/integer.d.ts +23 -0
  151. package/gel-core/columns/integer.js +28 -0
  152. package/gel-core/columns/integer.js.map +1 -0
  153. package/gel-core/columns/json.cjs +56 -0
  154. package/gel-core/columns/json.cjs.map +1 -0
  155. package/gel-core/columns/json.d.cts +26 -0
  156. package/gel-core/columns/json.d.ts +26 -0
  157. package/gel-core/columns/json.js +30 -0
  158. package/gel-core/columns/json.js.map +1 -0
  159. package/gel-core/columns/localdate.cjs +57 -0
  160. package/gel-core/columns/localdate.cjs.map +1 -0
  161. package/gel-core/columns/localdate.d.cts +24 -0
  162. package/gel-core/columns/localdate.d.ts +24 -0
  163. package/gel-core/columns/localdate.js +31 -0
  164. package/gel-core/columns/localdate.js.map +1 -0
  165. package/gel-core/columns/localtime.cjs +57 -0
  166. package/gel-core/columns/localtime.cjs.map +1 -0
  167. package/gel-core/columns/localtime.d.cts +24 -0
  168. package/gel-core/columns/localtime.d.ts +24 -0
  169. package/gel-core/columns/localtime.js +31 -0
  170. package/gel-core/columns/localtime.js.map +1 -0
  171. package/gel-core/columns/real.cjs +57 -0
  172. package/gel-core/columns/real.cjs.map +1 -0
  173. package/gel-core/columns/real.d.cts +28 -0
  174. package/gel-core/columns/real.d.ts +28 -0
  175. package/gel-core/columns/real.js +31 -0
  176. package/gel-core/columns/real.js.map +1 -0
  177. package/gel-core/columns/relative-duration.cjs +56 -0
  178. package/gel-core/columns/relative-duration.cjs.map +1 -0
  179. package/gel-core/columns/relative-duration.d.cts +23 -0
  180. package/gel-core/columns/relative-duration.d.ts +23 -0
  181. package/gel-core/columns/relative-duration.js +30 -0
  182. package/gel-core/columns/relative-duration.js.map +1 -0
  183. package/gel-core/columns/smallint.cjs +54 -0
  184. package/gel-core/columns/smallint.cjs.map +1 -0
  185. package/gel-core/columns/smallint.d.cts +23 -0
  186. package/gel-core/columns/smallint.d.ts +23 -0
  187. package/gel-core/columns/smallint.js +28 -0
  188. package/gel-core/columns/smallint.js.map +1 -0
  189. package/gel-core/columns/text.cjs +54 -0
  190. package/gel-core/columns/text.cjs.map +1 -0
  191. package/gel-core/columns/text.d.cts +26 -0
  192. package/gel-core/columns/text.d.ts +26 -0
  193. package/gel-core/columns/text.js +28 -0
  194. package/gel-core/columns/text.js.map +1 -0
  195. package/gel-core/columns/timestamp.cjs +60 -0
  196. package/gel-core/columns/timestamp.cjs.map +1 -0
  197. package/gel-core/columns/timestamp.d.cts +28 -0
  198. package/gel-core/columns/timestamp.d.ts +28 -0
  199. package/gel-core/columns/timestamp.js +34 -0
  200. package/gel-core/columns/timestamp.js.map +1 -0
  201. package/gel-core/columns/timestamptz.cjs +60 -0
  202. package/gel-core/columns/timestamptz.cjs.map +1 -0
  203. package/gel-core/columns/timestamptz.d.cts +27 -0
  204. package/gel-core/columns/timestamptz.d.ts +27 -0
  205. package/gel-core/columns/timestamptz.js +34 -0
  206. package/gel-core/columns/timestamptz.js.map +1 -0
  207. package/gel-core/columns/uuid.cjs +53 -0
  208. package/gel-core/columns/uuid.cjs.map +1 -0
  209. package/gel-core/columns/uuid.d.cts +22 -0
  210. package/gel-core/columns/uuid.d.ts +22 -0
  211. package/gel-core/columns/uuid.js +27 -0
  212. package/gel-core/columns/uuid.js.map +1 -0
  213. package/gel-core/db.cjs +338 -0
  214. package/gel-core/db.cjs.map +1 -0
  215. package/gel-core/db.d.cts +281 -0
  216. package/gel-core/db.d.ts +281 -0
  217. package/gel-core/db.js +319 -0
  218. package/gel-core/db.js.map +1 -0
  219. package/gel-core/dialect.cjs +1142 -0
  220. package/gel-core/dialect.cjs.map +1 -0
  221. package/gel-core/dialect.d.cts +62 -0
  222. package/gel-core/dialect.d.ts +62 -0
  223. package/gel-core/dialect.js +1128 -0
  224. package/gel-core/dialect.js.map +1 -0
  225. package/gel-core/expressions.cjs +49 -0
  226. package/gel-core/expressions.cjs.map +1 -0
  227. package/gel-core/expressions.d.cts +8 -0
  228. package/gel-core/expressions.d.ts +8 -0
  229. package/gel-core/expressions.js +22 -0
  230. package/gel-core/expressions.js.map +1 -0
  231. package/gel-core/foreign-keys.cjs +100 -0
  232. package/gel-core/foreign-keys.cjs.map +1 -0
  233. package/gel-core/foreign-keys.d.cts +48 -0
  234. package/gel-core/foreign-keys.d.ts +48 -0
  235. package/gel-core/foreign-keys.js +74 -0
  236. package/gel-core/foreign-keys.js.map +1 -0
  237. package/gel-core/index.cjs +61 -0
  238. package/gel-core/index.cjs.map +1 -0
  239. package/gel-core/index.d.cts +20 -0
  240. package/gel-core/index.d.ts +20 -0
  241. package/gel-core/index.js +21 -0
  242. package/gel-core/index.js.map +1 -0
  243. package/gel-core/indexes.cjs +149 -0
  244. package/gel-core/indexes.cjs.map +1 -0
  245. package/gel-core/indexes.d.cts +79 -0
  246. package/gel-core/indexes.d.ts +79 -0
  247. package/gel-core/indexes.js +121 -0
  248. package/gel-core/indexes.js.map +1 -0
  249. package/gel-core/policies.cjs +58 -0
  250. package/gel-core/policies.cjs.map +1 -0
  251. package/gel-core/policies.d.cts +24 -0
  252. package/gel-core/policies.d.ts +24 -0
  253. package/gel-core/policies.js +33 -0
  254. package/gel-core/policies.js.map +1 -0
  255. package/gel-core/primary-keys.cjs +68 -0
  256. package/gel-core/primary-keys.cjs.map +1 -0
  257. package/gel-core/primary-keys.d.cts +30 -0
  258. package/gel-core/primary-keys.d.ts +30 -0
  259. package/gel-core/primary-keys.js +42 -0
  260. package/gel-core/primary-keys.js.map +1 -0
  261. package/gel-core/query-builders/count.cjs +73 -0
  262. package/gel-core/query-builders/count.cjs.map +1 -0
  263. package/gel-core/query-builders/count.d.cts +25 -0
  264. package/gel-core/query-builders/count.d.ts +25 -0
  265. package/gel-core/query-builders/count.js +49 -0
  266. package/gel-core/query-builders/count.js.map +1 -0
  267. package/gel-core/query-builders/delete.cjs +105 -0
  268. package/gel-core/query-builders/delete.cjs.map +1 -0
  269. package/gel-core/query-builders/delete.d.cts +99 -0
  270. package/gel-core/query-builders/delete.d.ts +99 -0
  271. package/gel-core/query-builders/delete.js +81 -0
  272. package/gel-core/query-builders/delete.js.map +1 -0
  273. package/gel-core/query-builders/index.cjs +35 -0
  274. package/gel-core/query-builders/index.cjs.map +1 -0
  275. package/gel-core/query-builders/index.d.cts +7 -0
  276. package/gel-core/query-builders/index.d.ts +7 -0
  277. package/gel-core/query-builders/index.js +8 -0
  278. package/gel-core/query-builders/index.js.map +1 -0
  279. package/gel-core/query-builders/insert.cjs +218 -0
  280. package/gel-core/query-builders/insert.cjs.map +1 -0
  281. package/gel-core/query-builders/insert.d.cts +116 -0
  282. package/gel-core/query-builders/insert.d.ts +116 -0
  283. package/gel-core/query-builders/insert.js +193 -0
  284. package/gel-core/query-builders/insert.js.map +1 -0
  285. package/gel-core/query-builders/query-builder.cjs +114 -0
  286. package/gel-core/query-builders/query-builder.cjs.map +1 -0
  287. package/gel-core/query-builders/query-builder.d.cts +40 -0
  288. package/gel-core/query-builders/query-builder.d.ts +40 -0
  289. package/gel-core/query-builders/query-builder.js +90 -0
  290. package/gel-core/query-builders/query-builder.js.map +1 -0
  291. package/gel-core/query-builders/query.cjs +139 -0
  292. package/gel-core/query-builders/query.cjs.map +1 -0
  293. package/gel-core/query-builders/query.d.cts +46 -0
  294. package/gel-core/query-builders/query.d.ts +46 -0
  295. package/gel-core/query-builders/query.js +116 -0
  296. package/gel-core/query-builders/query.js.map +1 -0
  297. package/gel-core/query-builders/raw.cjs +57 -0
  298. package/gel-core/query-builders/raw.cjs.map +1 -0
  299. package/gel-core/query-builders/raw.d.cts +22 -0
  300. package/gel-core/query-builders/raw.d.ts +22 -0
  301. package/gel-core/query-builders/raw.js +33 -0
  302. package/gel-core/query-builders/raw.js.map +1 -0
  303. package/gel-core/query-builders/refresh-materialized-view.cjs +77 -0
  304. package/gel-core/query-builders/refresh-materialized-view.cjs.map +1 -0
  305. package/gel-core/query-builders/refresh-materialized-view.d.cts +27 -0
  306. package/gel-core/query-builders/refresh-materialized-view.d.ts +27 -0
  307. package/gel-core/query-builders/refresh-materialized-view.js +53 -0
  308. package/gel-core/query-builders/refresh-materialized-view.js.map +1 -0
  309. package/gel-core/query-builders/select.cjs +775 -0
  310. package/gel-core/query-builders/select.cjs.map +1 -0
  311. package/gel-core/query-builders/select.d.cts +721 -0
  312. package/gel-core/query-builders/select.d.ts +721 -0
  313. package/gel-core/query-builders/select.js +748 -0
  314. package/gel-core/query-builders/select.js.map +1 -0
  315. package/gel-core/query-builders/select.types.cjs +17 -0
  316. package/gel-core/query-builders/select.types.cjs.map +1 -0
  317. package/gel-core/query-builders/select.types.d.cts +138 -0
  318. package/gel-core/query-builders/select.types.d.ts +138 -0
  319. package/gel-core/query-builders/select.types.js +1 -0
  320. package/gel-core/query-builders/update.cjs +226 -0
  321. package/gel-core/query-builders/update.cjs.map +1 -0
  322. package/gel-core/query-builders/update.d.cts +166 -0
  323. package/gel-core/query-builders/update.d.ts +166 -0
  324. package/gel-core/query-builders/update.js +205 -0
  325. package/gel-core/query-builders/update.js.map +1 -0
  326. package/gel-core/roles.cjs +57 -0
  327. package/gel-core/roles.cjs.map +1 -0
  328. package/gel-core/roles.d.cts +13 -0
  329. package/gel-core/roles.d.ts +13 -0
  330. package/gel-core/roles.js +32 -0
  331. package/gel-core/roles.js.map +1 -0
  332. package/gel-core/schema.cjs +74 -0
  333. package/gel-core/schema.cjs.map +1 -0
  334. package/gel-core/schema.d.cts +15 -0
  335. package/gel-core/schema.d.ts +15 -0
  336. package/gel-core/schema.js +48 -0
  337. package/gel-core/schema.js.map +1 -0
  338. package/gel-core/sequence.cjs +52 -0
  339. package/gel-core/sequence.cjs.map +1 -0
  340. package/gel-core/sequence.d.cts +18 -0
  341. package/gel-core/sequence.d.ts +18 -0
  342. package/gel-core/sequence.js +25 -0
  343. package/gel-core/sequence.js.map +1 -0
  344. package/gel-core/session.cjs +94 -0
  345. package/gel-core/session.cjs.map +1 -0
  346. package/gel-core/session.d.cts +56 -0
  347. package/gel-core/session.d.ts +56 -0
  348. package/gel-core/session.js +68 -0
  349. package/gel-core/session.js.map +1 -0
  350. package/{cache/core/types.cjs → gel-core/subquery.cjs} +3 -3
  351. package/gel-core/subquery.cjs.map +1 -0
  352. package/gel-core/subquery.d.cts +5 -0
  353. package/gel-core/subquery.d.ts +5 -0
  354. package/gel-core/subquery.js +1 -0
  355. package/gel-core/subquery.js.map +1 -0
  356. package/gel-core/table.cjs +100 -0
  357. package/gel-core/table.cjs.map +1 -0
  358. package/gel-core/table.d.cts +95 -0
  359. package/gel-core/table.d.ts +95 -0
  360. package/gel-core/table.js +71 -0
  361. package/gel-core/table.js.map +1 -0
  362. package/gel-core/unique-constraint.cjs +89 -0
  363. package/gel-core/unique-constraint.cjs.map +1 -0
  364. package/gel-core/unique-constraint.d.cts +25 -0
  365. package/gel-core/unique-constraint.d.ts +25 -0
  366. package/gel-core/unique-constraint.js +61 -0
  367. package/gel-core/unique-constraint.js.map +1 -0
  368. package/gel-core/utils.cjs +100 -0
  369. package/gel-core/utils.cjs.map +1 -0
  370. package/gel-core/utils.d.cts +51 -0
  371. package/gel-core/utils.d.ts +51 -0
  372. package/gel-core/utils.js +74 -0
  373. package/gel-core/utils.js.map +1 -0
  374. package/gel-core/view-base.cjs +33 -0
  375. package/gel-core/view-base.cjs.map +1 -0
  376. package/gel-core/view-base.d.cts +8 -0
  377. package/gel-core/view-base.d.ts +8 -0
  378. package/gel-core/view-base.js +9 -0
  379. package/gel-core/view-base.js.map +1 -0
  380. package/{cache/upstash/index.cjs → gel-core/view-common.cjs} +12 -6
  381. package/gel-core/view-common.cjs.map +1 -0
  382. package/gel-core/view-common.d.cts +1 -0
  383. package/gel-core/view-common.d.ts +1 -0
  384. package/gel-core/view-common.js +5 -0
  385. package/gel-core/view-common.js.map +1 -0
  386. package/gel-core/view.cjs +302 -0
  387. package/gel-core/view.cjs.map +1 -0
  388. package/gel-core/view.d.cts +150 -0
  389. package/gel-core/view.d.ts +150 -0
  390. package/gel-core/view.js +268 -0
  391. package/gel-core/view.js.map +1 -0
  392. package/neon-http/driver.cjs +2 -9
  393. package/neon-http/driver.cjs.map +1 -1
  394. package/neon-http/driver.d.cts +0 -2
  395. package/neon-http/driver.d.ts +0 -2
  396. package/neon-http/driver.js +2 -9
  397. package/neon-http/driver.js.map +1 -1
  398. package/neon-http/session.cjs +15 -25
  399. package/neon-http/session.cjs.map +1 -1
  400. package/neon-http/session.d.cts +3 -12
  401. package/neon-http/session.d.ts +3 -12
  402. package/neon-http/session.js +15 -25
  403. package/neon-http/session.js.map +1 -1
  404. package/neon-serverless/driver.cjs +2 -9
  405. package/neon-serverless/driver.cjs.map +1 -1
  406. package/neon-serverless/driver.d.cts +0 -2
  407. package/neon-serverless/driver.d.ts +0 -2
  408. package/neon-serverless/driver.js +2 -9
  409. package/neon-serverless/driver.js.map +1 -1
  410. package/neon-serverless/session.cjs +7 -21
  411. package/neon-serverless/session.cjs.map +1 -1
  412. package/neon-serverless/session.d.cts +3 -12
  413. package/neon-serverless/session.d.ts +3 -12
  414. package/neon-serverless/session.js +7 -21
  415. package/neon-serverless/session.js.map +1 -1
  416. package/node-postgres/driver.cjs +2 -9
  417. package/node-postgres/driver.cjs.map +1 -1
  418. package/node-postgres/driver.d.cts +0 -2
  419. package/node-postgres/driver.d.ts +0 -2
  420. package/node-postgres/driver.js +2 -9
  421. package/node-postgres/driver.js.map +1 -1
  422. package/node-postgres/session.cjs +6 -19
  423. package/node-postgres/session.cjs.map +1 -1
  424. package/node-postgres/session.d.cts +3 -13
  425. package/node-postgres/session.d.ts +3 -13
  426. package/node-postgres/session.js +6 -19
  427. package/node-postgres/session.js.map +1 -1
  428. package/package.json +1036 -364
  429. package/pg-core/db.cjs +0 -1
  430. package/pg-core/db.cjs.map +1 -1
  431. package/pg-core/db.d.cts +0 -4
  432. package/pg-core/db.d.ts +0 -4
  433. package/pg-core/db.js +0 -1
  434. package/pg-core/db.js.map +1 -1
  435. package/pg-core/query-builders/delete.cjs +1 -6
  436. package/pg-core/query-builders/delete.cjs.map +1 -1
  437. package/pg-core/query-builders/delete.d.cts +1 -2
  438. package/pg-core/query-builders/delete.d.ts +1 -2
  439. package/pg-core/query-builders/delete.js +1 -6
  440. package/pg-core/query-builders/delete.js.map +1 -1
  441. package/pg-core/query-builders/insert.cjs +1 -6
  442. package/pg-core/query-builders/insert.cjs.map +1 -1
  443. package/pg-core/query-builders/insert.d.cts +1 -2
  444. package/pg-core/query-builders/insert.d.ts +1 -2
  445. package/pg-core/query-builders/insert.js +1 -6
  446. package/pg-core/query-builders/insert.js.map +1 -1
  447. package/pg-core/query-builders/select.cjs +5 -31
  448. package/pg-core/query-builders/select.cjs.map +1 -1
  449. package/pg-core/query-builders/select.d.cts +2 -11
  450. package/pg-core/query-builders/select.d.ts +2 -11
  451. package/pg-core/query-builders/select.js +5 -31
  452. package/pg-core/query-builders/select.js.map +1 -1
  453. package/pg-core/query-builders/select.types.cjs.map +1 -1
  454. package/pg-core/query-builders/select.types.d.cts +0 -7
  455. package/pg-core/query-builders/select.types.d.ts +0 -7
  456. package/pg-core/query-builders/update.cjs +1 -6
  457. package/pg-core/query-builders/update.cjs.map +1 -1
  458. package/pg-core/query-builders/update.d.cts +3 -4
  459. package/pg-core/query-builders/update.d.ts +3 -4
  460. package/pg-core/query-builders/update.js +1 -6
  461. package/pg-core/query-builders/update.js.map +1 -1
  462. package/pg-core/session.cjs +1 -53
  463. package/pg-core/session.cjs.map +1 -1
  464. package/pg-core/session.d.cts +3 -14
  465. package/pg-core/session.d.ts +3 -14
  466. package/pg-core/session.js +2 -54
  467. package/pg-core/session.js.map +1 -1
  468. package/pg-core/table.cjs +2 -0
  469. package/pg-core/table.cjs.map +1 -1
  470. package/pg-core/table.js +2 -0
  471. package/pg-core/table.js.map +1 -1
  472. package/pg-core/utils.cjs +0 -16
  473. package/pg-core/utils.cjs.map +1 -1
  474. package/pg-core/utils.d.cts +4 -8
  475. package/pg-core/utils.d.ts +4 -8
  476. package/pg-core/utils.js +1 -16
  477. package/pg-core/utils.js.map +1 -1
  478. package/pg-proxy/driver.cjs +2 -7
  479. package/pg-proxy/driver.cjs.map +1 -1
  480. package/pg-proxy/driver.js +2 -7
  481. package/pg-proxy/driver.js.map +1 -1
  482. package/pg-proxy/session.cjs +5 -15
  483. package/pg-proxy/session.cjs.map +1 -1
  484. package/pg-proxy/session.d.cts +3 -12
  485. package/pg-proxy/session.d.ts +3 -12
  486. package/pg-proxy/session.js +5 -15
  487. package/pg-proxy/session.js.map +1 -1
  488. package/pglite/driver.cjs +2 -9
  489. package/pglite/driver.cjs.map +1 -1
  490. package/pglite/driver.d.cts +0 -2
  491. package/pglite/driver.d.ts +0 -2
  492. package/pglite/driver.js +2 -9
  493. package/pglite/driver.js.map +1 -1
  494. package/pglite/session.cjs +7 -19
  495. package/pglite/session.cjs.map +1 -1
  496. package/pglite/session.d.cts +3 -12
  497. package/pglite/session.d.ts +3 -12
  498. package/pglite/session.js +7 -19
  499. package/pglite/session.js.map +1 -1
  500. package/postgres-js/driver.cjs +1 -5
  501. package/postgres-js/driver.cjs.map +1 -1
  502. package/postgres-js/driver.js +1 -5
  503. package/postgres-js/driver.js.map +1 -1
  504. package/postgres-js/session.cjs +6 -18
  505. package/postgres-js/session.cjs.map +1 -1
  506. package/postgres-js/session.d.cts +3 -12
  507. package/postgres-js/session.d.ts +3 -12
  508. package/postgres-js/session.js +6 -18
  509. package/postgres-js/session.js.map +1 -1
  510. package/prisma/pg/session.cjs +1 -1
  511. package/prisma/pg/session.cjs.map +1 -1
  512. package/prisma/pg/session.js +1 -1
  513. package/prisma/pg/session.js.map +1 -1
  514. package/query-builders/query-builder.cjs.map +1 -1
  515. package/query-builders/query-builder.d.cts +1 -2
  516. package/query-builders/query-builder.d.ts +1 -2
  517. package/query-builders/query-builder.js.map +1 -1
  518. package/sql/sql.cjs +0 -7
  519. package/sql/sql.cjs.map +1 -1
  520. package/sql/sql.js +0 -7
  521. package/sql/sql.js.map +1 -1
  522. package/subquery.cjs +3 -4
  523. package/subquery.cjs.map +1 -1
  524. package/subquery.d.cts +1 -2
  525. package/subquery.d.ts +1 -2
  526. package/subquery.js +3 -4
  527. package/subquery.js.map +1 -1
  528. package/table.cjs.map +1 -1
  529. package/table.js.map +1 -1
  530. package/utils.cjs.map +1 -1
  531. package/utils.d.cts +0 -2
  532. package/utils.d.ts +0 -2
  533. package/utils.js.map +1 -1
  534. package/vercel-postgres/driver.cjs +2 -9
  535. package/vercel-postgres/driver.cjs.map +1 -1
  536. package/vercel-postgres/driver.d.cts +0 -2
  537. package/vercel-postgres/driver.d.ts +0 -2
  538. package/vercel-postgres/driver.js +2 -9
  539. package/vercel-postgres/driver.js.map +1 -1
  540. package/vercel-postgres/session.cjs +7 -21
  541. package/vercel-postgres/session.cjs.map +1 -1
  542. package/vercel-postgres/session.d.cts +3 -12
  543. package/vercel-postgres/session.d.ts +3 -12
  544. package/vercel-postgres/session.js +7 -21
  545. package/vercel-postgres/session.js.map +1 -1
  546. package/version.cjs +1 -1
  547. package/version.d.cts +1 -1
  548. package/version.d.ts +1 -1
  549. package/version.js +1 -1
  550. package/xata-http/driver.cjs +2 -7
  551. package/xata-http/driver.cjs.map +1 -1
  552. package/xata-http/driver.d.cts +0 -2
  553. package/xata-http/driver.d.ts +0 -2
  554. package/xata-http/driver.js +2 -7
  555. package/xata-http/driver.js.map +1 -1
  556. package/xata-http/session.cjs +7 -21
  557. package/xata-http/session.cjs.map +1 -1
  558. package/xata-http/session.d.cts +3 -12
  559. package/xata-http/session.d.ts +3 -12
  560. package/xata-http/session.js +7 -21
  561. package/xata-http/session.js.map +1 -1
  562. package/cache/core/cache.cjs.map +0 -1
  563. package/cache/core/cache.d.cts +0 -35
  564. package/cache/core/cache.d.ts +0 -35
  565. package/cache/core/cache.js +0 -22
  566. package/cache/core/cache.js.map +0 -1
  567. package/cache/core/index.cjs.map +0 -1
  568. package/cache/core/index.d.cts +0 -1
  569. package/cache/core/index.d.ts +0 -1
  570. package/cache/core/index.js +0 -2
  571. package/cache/core/index.js.map +0 -1
  572. package/cache/core/types.cjs.map +0 -1
  573. package/cache/core/types.d.cts +0 -22
  574. package/cache/core/types.d.ts +0 -22
  575. package/cache/core/types.js +0 -1
  576. package/cache/upstash/cache.cjs +0 -98
  577. package/cache/upstash/cache.cjs.map +0 -1
  578. package/cache/upstash/cache.d.cts +0 -23
  579. package/cache/upstash/cache.d.ts +0 -23
  580. package/cache/upstash/cache.js +0 -73
  581. package/cache/upstash/cache.js.map +0 -1
  582. package/cache/upstash/index.cjs.map +0 -1
  583. package/cache/upstash/index.d.cts +0 -1
  584. package/cache/upstash/index.d.ts +0 -1
  585. package/cache/upstash/index.js +0 -2
  586. package/cache/upstash/index.js.map +0 -1
  587. /package/{cache/core/types.js.map → gel-core/query-builders/select.types.js.map} +0 -0
@@ -0,0 +1,1128 @@
1
+ import { aliasedTable, aliasedTableColumn, mapColumnsInAliasedSQLToAlias, mapColumnsInSQLToAlias } from "../alias.js";
2
+ import { CasingCache } from "../casing.js";
3
+ import { Column } from "../column.js";
4
+ import { entityKind, is } from "../entity.js";
5
+ import { DrizzleError } from "../errors.js";
6
+ import { GelColumn, GelDecimal, GelJson, GelUUID } from "./columns/index.js";
7
+ import { GelTable } from "./table.js";
8
+ import {
9
+ getOperators,
10
+ getOrderByOperators,
11
+ Many,
12
+ normalizeRelation,
13
+ One
14
+ } from "../relations.js";
15
+ import { and, eq, View } from "../sql/index.js";
16
+ import {
17
+ Param,
18
+ SQL,
19
+ sql
20
+ } from "../sql/sql.js";
21
+ import { Subquery } from "../subquery.js";
22
+ import { getTableName, getTableUniqueName, Table } from "../table.js";
23
+ import { orderSelectedFields } from "../utils.js";
24
+ import { ViewBaseConfig } from "../view-common.js";
25
+ import { GelTimestamp } from "./columns/timestamp.js";
26
+ import { GelViewBase } from "./view-base.js";
27
+ class GelDialect {
28
+ static [entityKind] = "GelDialect";
29
+ /** @internal */
30
+ casing;
31
+ constructor(config) {
32
+ this.casing = new CasingCache(config?.casing);
33
+ }
34
+ // TODO can not migrate gel with drizzle
35
+ // async migrate(migrations: MigrationMeta[], session: GelSession, config: string | MigrationConfig): Promise<void> {
36
+ // const migrationsTable = typeof config === 'string'
37
+ // ? '__drizzle_migrations'
38
+ // : config.migrationsTable ?? '__drizzle_migrations';
39
+ // const migrationsSchema = typeof config === 'string' ? 'drizzle' : config.migrationsSchema ?? 'drizzle';
40
+ // const migrationTableCreate = sql`
41
+ // CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsSchema)}.${sql.identifier(migrationsTable)} (
42
+ // id SERIAL PRIMARY KEY,
43
+ // hash text NOT NULL,
44
+ // created_at bigint
45
+ // )
46
+ // `;
47
+ // await session.execute(sql`CREATE SCHEMA IF NOT EXISTS ${sql.identifier(migrationsSchema)}`);
48
+ // await session.execute(migrationTableCreate);
49
+ // const dbMigrations = await session.all<{ id: number; hash: string; created_at: string }>(
50
+ // sql`select id, hash, created_at from ${sql.identifier(migrationsSchema)}.${
51
+ // sql.identifier(migrationsTable)
52
+ // } order by created_at desc limit 1`,
53
+ // );
54
+ // const lastDbMigration = dbMigrations[0];
55
+ // await session.transaction(async (tx) => {
56
+ // for await (const migration of migrations) {
57
+ // if (
58
+ // !lastDbMigration
59
+ // || Number(lastDbMigration.created_at) < migration.folderMillis
60
+ // ) {
61
+ // for (const stmt of migration.sql) {
62
+ // await tx.execute(sql.raw(stmt));
63
+ // }
64
+ // await tx.execute(
65
+ // sql`insert into ${sql.identifier(migrationsSchema)}.${
66
+ // sql.identifier(migrationsTable)
67
+ // } ("hash", "created_at") values(${migration.hash}, ${migration.folderMillis})`,
68
+ // );
69
+ // }
70
+ // }
71
+ // });
72
+ // }
73
+ escapeName(name) {
74
+ return `"${name}"`;
75
+ }
76
+ escapeParam(num) {
77
+ return `$${num + 1}`;
78
+ }
79
+ escapeString(str) {
80
+ return `'${str.replace(/'/g, "''")}'`;
81
+ }
82
+ buildWithCTE(queries) {
83
+ if (!queries?.length)
84
+ return void 0;
85
+ const withSqlChunks = [sql`with `];
86
+ for (const [i, w] of queries.entries()) {
87
+ withSqlChunks.push(sql`${sql.identifier(w._.alias)} as (${w._.sql})`);
88
+ if (i < queries.length - 1) {
89
+ withSqlChunks.push(sql`, `);
90
+ }
91
+ }
92
+ withSqlChunks.push(sql` `);
93
+ return sql.join(withSqlChunks);
94
+ }
95
+ buildDeleteQuery({ table, where, returning, withList }) {
96
+ const withSql = this.buildWithCTE(withList);
97
+ const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
98
+ const whereSql = where ? sql` where ${where}` : void 0;
99
+ return sql`${withSql}delete from ${table}${whereSql}${returningSql}`;
100
+ }
101
+ buildUpdateSet(table, set) {
102
+ const tableColumns = table[Table.Symbol.Columns];
103
+ const columnNames = Object.keys(tableColumns).filter(
104
+ (colName) => set[colName] !== void 0 || tableColumns[colName]?.onUpdateFn !== void 0
105
+ );
106
+ const setSize = columnNames.length;
107
+ return sql.join(columnNames.flatMap((colName, i) => {
108
+ const col = tableColumns[colName];
109
+ const value = set[colName] ?? sql.param(col.onUpdateFn(), col);
110
+ const res = sql`${sql.identifier(this.casing.getColumnCasing(col))} = ${value}`;
111
+ if (i < setSize - 1) {
112
+ return [res, sql.raw(", ")];
113
+ }
114
+ return [res];
115
+ }));
116
+ }
117
+ buildUpdateQuery({ table, set, where, returning, withList, from, joins }) {
118
+ const withSql = this.buildWithCTE(withList);
119
+ const tableName = table[GelTable.Symbol.Name];
120
+ const tableSchema = table[GelTable.Symbol.Schema];
121
+ const origTableName = table[GelTable.Symbol.OriginalName];
122
+ const alias = tableName === origTableName ? void 0 : tableName;
123
+ const tableSql = sql`${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`}`;
124
+ const setSql = this.buildUpdateSet(table, set);
125
+ const fromSql = from && sql.join([sql.raw(" from "), this.buildFromTable(from)]);
126
+ const joinsSql = this.buildJoins(joins);
127
+ const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: !from })}` : void 0;
128
+ const whereSql = where ? sql` where ${where}` : void 0;
129
+ return sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
130
+ }
131
+ /**
132
+ * Builds selection SQL with provided fields/expressions
133
+ *
134
+ * Examples:
135
+ *
136
+ * `select <selection> from`
137
+ *
138
+ * `insert ... returning <selection>`
139
+ *
140
+ * If `isSingleTable` is true, then columns won't be prefixed with table name
141
+ */
142
+ buildSelection(fields, { isSingleTable = false } = {}) {
143
+ const columnsLen = fields.length;
144
+ const chunks = fields.flatMap(({ field }, i) => {
145
+ const chunk = [];
146
+ if (is(field, SQL.Aliased) && field.isSelectionField) {
147
+ chunk.push(sql.identifier(field.fieldAlias));
148
+ } else if (is(field, SQL.Aliased) || is(field, SQL)) {
149
+ const query = is(field, SQL.Aliased) ? field.sql : field;
150
+ if (isSingleTable) {
151
+ chunk.push(
152
+ new SQL(
153
+ query.queryChunks.map((c) => {
154
+ if (is(c, GelColumn)) {
155
+ return sql.identifier(this.casing.getColumnCasing(c));
156
+ }
157
+ return c;
158
+ })
159
+ )
160
+ );
161
+ } else {
162
+ chunk.push(query);
163
+ }
164
+ if (is(field, SQL.Aliased)) {
165
+ chunk.push(sql` as ${sql.identifier(field.fieldAlias)}`);
166
+ }
167
+ } else if (is(field, Column)) {
168
+ if (isSingleTable) {
169
+ chunk.push(sql.identifier(this.casing.getColumnCasing(field)));
170
+ } else {
171
+ chunk.push(field);
172
+ }
173
+ }
174
+ if (i < columnsLen - 1) {
175
+ chunk.push(sql`, `);
176
+ }
177
+ return chunk;
178
+ });
179
+ return sql.join(chunks);
180
+ }
181
+ buildJoins(joins) {
182
+ if (!joins || joins.length === 0) {
183
+ return void 0;
184
+ }
185
+ const joinsArray = [];
186
+ for (const [index, joinMeta] of joins.entries()) {
187
+ if (index === 0) {
188
+ joinsArray.push(sql` `);
189
+ }
190
+ const table = joinMeta.table;
191
+ const lateralSql = joinMeta.lateral ? sql` lateral` : void 0;
192
+ if (is(table, GelTable)) {
193
+ const tableName = table[GelTable.Symbol.Name];
194
+ const tableSchema = table[GelTable.Symbol.Schema];
195
+ const origTableName = table[GelTable.Symbol.OriginalName];
196
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
197
+ joinsArray.push(
198
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? sql`${sql.identifier(tableSchema)}.` : void 0}${sql.identifier(origTableName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
199
+ );
200
+ } else if (is(table, View)) {
201
+ const viewName = table[ViewBaseConfig].name;
202
+ const viewSchema = table[ViewBaseConfig].schema;
203
+ const origViewName = table[ViewBaseConfig].originalName;
204
+ const alias = viewName === origViewName ? void 0 : joinMeta.alias;
205
+ joinsArray.push(
206
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? sql`${sql.identifier(viewSchema)}.` : void 0}${sql.identifier(origViewName)}${alias && sql` ${sql.identifier(alias)}`} on ${joinMeta.on}`
207
+ );
208
+ } else {
209
+ joinsArray.push(
210
+ sql`${sql.raw(joinMeta.joinType)} join${lateralSql} ${table} on ${joinMeta.on}`
211
+ );
212
+ }
213
+ if (index < joins.length - 1) {
214
+ joinsArray.push(sql` `);
215
+ }
216
+ }
217
+ return sql.join(joinsArray);
218
+ }
219
+ buildFromTable(table) {
220
+ if (is(table, Table) && table[Table.Symbol.OriginalName] !== table[Table.Symbol.Name]) {
221
+ let fullName = sql`${sql.identifier(table[Table.Symbol.OriginalName])}`;
222
+ if (table[Table.Symbol.Schema]) {
223
+ fullName = sql`${sql.identifier(table[Table.Symbol.Schema])}.${fullName}`;
224
+ }
225
+ return sql`${fullName} ${sql.identifier(table[Table.Symbol.Name])}`;
226
+ }
227
+ return table;
228
+ }
229
+ buildSelectQuery({
230
+ withList,
231
+ fields,
232
+ fieldsFlat,
233
+ where,
234
+ having,
235
+ table,
236
+ joins,
237
+ orderBy,
238
+ groupBy,
239
+ limit,
240
+ offset,
241
+ lockingClause,
242
+ distinct,
243
+ setOperators
244
+ }) {
245
+ const fieldsList = fieldsFlat ?? orderSelectedFields(fields);
246
+ for (const f of fieldsList) {
247
+ if (is(f.field, Column) && getTableName(f.field.table) !== (is(table, Subquery) ? table._.alias : is(table, GelViewBase) ? table[ViewBaseConfig].name : is(table, SQL) ? void 0 : getTableName(table)) && !((table2) => joins?.some(
248
+ ({ alias }) => alias === (table2[Table.Symbol.IsAlias] ? getTableName(table2) : table2[Table.Symbol.BaseName])
249
+ ))(f.field.table)) {
250
+ const tableName = getTableName(f.field.table);
251
+ throw new Error(
252
+ `Your "${f.path.join("->")}" field references a column "${tableName}"."${f.field.name}", but the table "${tableName}" is not part of the query! Did you forget to join it?`
253
+ );
254
+ }
255
+ }
256
+ const isSingleTable = !joins || joins.length === 0;
257
+ const withSql = this.buildWithCTE(withList);
258
+ let distinctSql;
259
+ if (distinct) {
260
+ distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
261
+ }
262
+ const selection = this.buildSelection(fieldsList, { isSingleTable });
263
+ const tableSql = this.buildFromTable(table);
264
+ const joinsSql = this.buildJoins(joins);
265
+ const whereSql = where ? sql` where ${where}` : void 0;
266
+ const havingSql = having ? sql` having ${having}` : void 0;
267
+ let orderBySql;
268
+ if (orderBy && orderBy.length > 0) {
269
+ orderBySql = sql` order by ${sql.join(orderBy, sql`, `)}`;
270
+ }
271
+ let groupBySql;
272
+ if (groupBy && groupBy.length > 0) {
273
+ groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`;
274
+ }
275
+ const limitSql = typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
276
+ const offsetSql = offset ? sql` offset ${offset}` : void 0;
277
+ const lockingClauseSql = sql.empty();
278
+ if (lockingClause) {
279
+ const clauseSql = sql` for ${sql.raw(lockingClause.strength)}`;
280
+ if (lockingClause.config.of) {
281
+ clauseSql.append(
282
+ sql` of ${sql.join(
283
+ Array.isArray(lockingClause.config.of) ? lockingClause.config.of : [lockingClause.config.of],
284
+ sql`, `
285
+ )}`
286
+ );
287
+ }
288
+ if (lockingClause.config.noWait) {
289
+ clauseSql.append(sql` no wait`);
290
+ } else if (lockingClause.config.skipLocked) {
291
+ clauseSql.append(sql` skip locked`);
292
+ }
293
+ lockingClauseSql.append(clauseSql);
294
+ }
295
+ const finalQuery = sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClauseSql}`;
296
+ if (setOperators.length > 0) {
297
+ return this.buildSetOperations(finalQuery, setOperators);
298
+ }
299
+ return finalQuery;
300
+ }
301
+ buildSetOperations(leftSelect, setOperators) {
302
+ const [setOperator, ...rest] = setOperators;
303
+ if (!setOperator) {
304
+ throw new Error("Cannot pass undefined values to any set operator");
305
+ }
306
+ if (rest.length === 0) {
307
+ return this.buildSetOperationQuery({ leftSelect, setOperator });
308
+ }
309
+ return this.buildSetOperations(
310
+ this.buildSetOperationQuery({ leftSelect, setOperator }),
311
+ rest
312
+ );
313
+ }
314
+ buildSetOperationQuery({
315
+ leftSelect,
316
+ setOperator: { type, isAll, rightSelect, limit, orderBy, offset }
317
+ }) {
318
+ const leftChunk = sql`(${leftSelect.getSQL()}) `;
319
+ const rightChunk = sql`(${rightSelect.getSQL()})`;
320
+ let orderBySql;
321
+ if (orderBy && orderBy.length > 0) {
322
+ const orderByValues = [];
323
+ for (const singleOrderBy of orderBy) {
324
+ if (is(singleOrderBy, GelColumn)) {
325
+ orderByValues.push(sql.identifier(singleOrderBy.name));
326
+ } else if (is(singleOrderBy, SQL)) {
327
+ for (let i = 0; i < singleOrderBy.queryChunks.length; i++) {
328
+ const chunk = singleOrderBy.queryChunks[i];
329
+ if (is(chunk, GelColumn)) {
330
+ singleOrderBy.queryChunks[i] = sql.identifier(chunk.name);
331
+ }
332
+ }
333
+ orderByValues.push(sql`${singleOrderBy}`);
334
+ } else {
335
+ orderByValues.push(sql`${singleOrderBy}`);
336
+ }
337
+ }
338
+ orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `;
339
+ }
340
+ const limitSql = typeof limit === "object" || typeof limit === "number" && limit >= 0 ? sql` limit ${limit}` : void 0;
341
+ const operatorChunk = sql.raw(`${type} ${isAll ? "all " : ""}`);
342
+ const offsetSql = offset ? sql` offset ${offset}` : void 0;
343
+ return sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
344
+ }
345
+ buildInsertQuery({ table, values: valuesOrSelect, onConflict, returning, withList, select, overridingSystemValue_ }) {
346
+ const valuesSqlList = [];
347
+ const columns = table[Table.Symbol.Columns];
348
+ const colEntries = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());
349
+ const insertOrder = colEntries.map(
350
+ ([, column]) => sql.identifier(this.casing.getColumnCasing(column))
351
+ );
352
+ if (select) {
353
+ const select2 = valuesOrSelect;
354
+ if (is(select2, SQL)) {
355
+ valuesSqlList.push(select2);
356
+ } else {
357
+ valuesSqlList.push(select2.getSQL());
358
+ }
359
+ } else {
360
+ const values = valuesOrSelect;
361
+ valuesSqlList.push(sql.raw("values "));
362
+ for (const [valueIndex, value] of values.entries()) {
363
+ const valueList = [];
364
+ for (const [fieldName, col] of colEntries) {
365
+ const colValue = value[fieldName];
366
+ if (colValue === void 0 || is(colValue, Param) && colValue.value === void 0) {
367
+ if (col.defaultFn !== void 0) {
368
+ const defaultFnResult = col.defaultFn();
369
+ const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
370
+ valueList.push(defaultValue);
371
+ } else if (!col.default && col.onUpdateFn !== void 0) {
372
+ const onUpdateFnResult = col.onUpdateFn();
373
+ const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
374
+ valueList.push(newValue);
375
+ } else {
376
+ valueList.push(sql`default`);
377
+ }
378
+ } else {
379
+ valueList.push(colValue);
380
+ }
381
+ }
382
+ valuesSqlList.push(valueList);
383
+ if (valueIndex < values.length - 1) {
384
+ valuesSqlList.push(sql`, `);
385
+ }
386
+ }
387
+ }
388
+ const withSql = this.buildWithCTE(withList);
389
+ const valuesSql = sql.join(valuesSqlList);
390
+ const returningSql = returning ? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
391
+ const onConflictSql = onConflict ? sql` on conflict ${onConflict}` : void 0;
392
+ const overridingSql = overridingSystemValue_ === true ? sql`overriding system value ` : void 0;
393
+ return sql`${withSql}insert into ${table} ${insertOrder} ${overridingSql}${valuesSql}${onConflictSql}${returningSql}`;
394
+ }
395
+ buildRefreshMaterializedViewQuery({ view, concurrently, withNoData }) {
396
+ const concurrentlySql = concurrently ? sql` concurrently` : void 0;
397
+ const withNoDataSql = withNoData ? sql` with no data` : void 0;
398
+ return sql`refresh materialized view${concurrentlySql} ${view}${withNoDataSql}`;
399
+ }
400
+ prepareTyping(encoder) {
401
+ if (is(encoder, GelJson)) {
402
+ return "json";
403
+ } else if (is(encoder, GelDecimal)) {
404
+ return "decimal";
405
+ } else if (is(encoder, GelTimestamp)) {
406
+ return "timestamp";
407
+ } else if (is(encoder, GelUUID)) {
408
+ return "uuid";
409
+ } else {
410
+ return "none";
411
+ }
412
+ }
413
+ sqlToQuery(sql2, invokeSource) {
414
+ return sql2.toQuery({
415
+ casing: this.casing,
416
+ escapeName: this.escapeName,
417
+ escapeParam: this.escapeParam,
418
+ escapeString: this.escapeString,
419
+ prepareTyping: this.prepareTyping,
420
+ invokeSource
421
+ });
422
+ }
423
+ // buildRelationalQueryWithPK({
424
+ // fullSchema,
425
+ // schema,
426
+ // tableNamesMap,
427
+ // table,
428
+ // tableConfig,
429
+ // queryConfig: config,
430
+ // tableAlias,
431
+ // isRoot = false,
432
+ // joinOn,
433
+ // }: {
434
+ // fullSchema: Record<string, unknown>;
435
+ // schema: TablesRelationalConfig;
436
+ // tableNamesMap: Record<string, string>;
437
+ // table: GelTable;
438
+ // tableConfig: TableRelationalConfig;
439
+ // queryConfig: true | DBQueryConfig<'many', true>;
440
+ // tableAlias: string;
441
+ // isRoot?: boolean;
442
+ // joinOn?: SQL;
443
+ // }): BuildRelationalQueryResult<GelTable, GelColumn> {
444
+ // // For { "<relation>": true }, return a table with selection of all columns
445
+ // if (config === true) {
446
+ // const selectionEntries = Object.entries(tableConfig.columns);
447
+ // const selection: BuildRelationalQueryResult<GelTable, GelColumn>['selection'] = selectionEntries.map((
448
+ // [key, value],
449
+ // ) => ({
450
+ // dbKey: value.name,
451
+ // tsKey: key,
452
+ // field: value as GelColumn,
453
+ // relationTableTsKey: undefined,
454
+ // isJson: false,
455
+ // selection: [],
456
+ // }));
457
+ // return {
458
+ // tableTsKey: tableConfig.tsName,
459
+ // sql: table,
460
+ // selection,
461
+ // };
462
+ // }
463
+ // // let selection: BuildRelationalQueryResult<GelTable, GelColumn>['selection'] = [];
464
+ // // let selectionForBuild = selection;
465
+ // const aliasedColumns = Object.fromEntries(
466
+ // Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)]),
467
+ // );
468
+ // const aliasedRelations = Object.fromEntries(
469
+ // Object.entries(tableConfig.relations).map(([key, value]) => [key, aliasedRelation(value, tableAlias)]),
470
+ // );
471
+ // const aliasedFields = Object.assign({}, aliasedColumns, aliasedRelations);
472
+ // let where, hasUserDefinedWhere;
473
+ // if (config.where) {
474
+ // const whereSql = typeof config.where === 'function' ? config.where(aliasedFields, operators) : config.where;
475
+ // where = whereSql && mapColumnsInSQLToAlias(whereSql, tableAlias);
476
+ // hasUserDefinedWhere = !!where;
477
+ // }
478
+ // where = and(joinOn, where);
479
+ // // const fieldsSelection: { tsKey: string; value: GelColumn | SQL.Aliased; isExtra?: boolean }[] = [];
480
+ // let joins: Join[] = [];
481
+ // let selectedColumns: string[] = [];
482
+ // // Figure out which columns to select
483
+ // if (config.columns) {
484
+ // let isIncludeMode = false;
485
+ // for (const [field, value] of Object.entries(config.columns)) {
486
+ // if (value === undefined) {
487
+ // continue;
488
+ // }
489
+ // if (field in tableConfig.columns) {
490
+ // if (!isIncludeMode && value === true) {
491
+ // isIncludeMode = true;
492
+ // }
493
+ // selectedColumns.push(field);
494
+ // }
495
+ // }
496
+ // if (selectedColumns.length > 0) {
497
+ // selectedColumns = isIncludeMode
498
+ // ? selectedColumns.filter((c) => config.columns?.[c] === true)
499
+ // : Object.keys(tableConfig.columns).filter((key) => !selectedColumns.includes(key));
500
+ // }
501
+ // } else {
502
+ // // Select all columns if selection is not specified
503
+ // selectedColumns = Object.keys(tableConfig.columns);
504
+ // }
505
+ // // for (const field of selectedColumns) {
506
+ // // const column = tableConfig.columns[field]! as GelColumn;
507
+ // // fieldsSelection.push({ tsKey: field, value: column });
508
+ // // }
509
+ // let initiallySelectedRelations: {
510
+ // tsKey: string;
511
+ // queryConfig: true | DBQueryConfig<'many', false>;
512
+ // relation: Relation;
513
+ // }[] = [];
514
+ // // let selectedRelations: BuildRelationalQueryResult<GelTable, GelColumn>['selection'] = [];
515
+ // // Figure out which relations to select
516
+ // if (config.with) {
517
+ // initiallySelectedRelations = Object.entries(config.with)
518
+ // .filter((entry): entry is [typeof entry[0], NonNullable<typeof entry[1]>] => !!entry[1])
519
+ // .map(([tsKey, queryConfig]) => ({ tsKey, queryConfig, relation: tableConfig.relations[tsKey]! }));
520
+ // }
521
+ // const manyRelations = initiallySelectedRelations.filter((r) =>
522
+ // is(r.relation, Many)
523
+ // && (schema[tableNamesMap[r.relation.referencedTable[Table.Symbol.Name]]!]?.primaryKey.length ?? 0) > 0
524
+ // );
525
+ // // If this is the last Many relation (or there are no Many relations), we are on the innermost subquery level
526
+ // const isInnermostQuery = manyRelations.length < 2;
527
+ // const selectedExtras: {
528
+ // tsKey: string;
529
+ // value: SQL.Aliased;
530
+ // }[] = [];
531
+ // // Figure out which extras to select
532
+ // if (isInnermostQuery && config.extras) {
533
+ // const extras = typeof config.extras === 'function'
534
+ // ? config.extras(aliasedFields, { sql })
535
+ // : config.extras;
536
+ // for (const [tsKey, value] of Object.entries(extras)) {
537
+ // selectedExtras.push({
538
+ // tsKey,
539
+ // value: mapColumnsInAliasedSQLToAlias(value, tableAlias),
540
+ // });
541
+ // }
542
+ // }
543
+ // // Transform `fieldsSelection` into `selection`
544
+ // // `fieldsSelection` shouldn't be used after this point
545
+ // // for (const { tsKey, value, isExtra } of fieldsSelection) {
546
+ // // selection.push({
547
+ // // dbKey: is(value, SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey]!.name,
548
+ // // tsKey,
549
+ // // field: is(value, Column) ? aliasedTableColumn(value, tableAlias) : value,
550
+ // // relationTableTsKey: undefined,
551
+ // // isJson: false,
552
+ // // isExtra,
553
+ // // selection: [],
554
+ // // });
555
+ // // }
556
+ // let orderByOrig = typeof config.orderBy === 'function'
557
+ // ? config.orderBy(aliasedFields, orderByOperators)
558
+ // : config.orderBy ?? [];
559
+ // if (!Array.isArray(orderByOrig)) {
560
+ // orderByOrig = [orderByOrig];
561
+ // }
562
+ // const orderBy = orderByOrig.map((orderByValue) => {
563
+ // if (is(orderByValue, Column)) {
564
+ // return aliasedTableColumn(orderByValue, tableAlias) as GelColumn;
565
+ // }
566
+ // return mapColumnsInSQLToAlias(orderByValue, tableAlias);
567
+ // });
568
+ // const limit = isInnermostQuery ? config.limit : undefined;
569
+ // const offset = isInnermostQuery ? config.offset : undefined;
570
+ // // For non-root queries without additional config except columns, return a table with selection
571
+ // if (
572
+ // !isRoot
573
+ // && initiallySelectedRelations.length === 0
574
+ // && selectedExtras.length === 0
575
+ // && !where
576
+ // && orderBy.length === 0
577
+ // && limit === undefined
578
+ // && offset === undefined
579
+ // ) {
580
+ // return {
581
+ // tableTsKey: tableConfig.tsName,
582
+ // sql: table,
583
+ // selection: selectedColumns.map((key) => ({
584
+ // dbKey: tableConfig.columns[key]!.name,
585
+ // tsKey: key,
586
+ // field: tableConfig.columns[key] as GelColumn,
587
+ // relationTableTsKey: undefined,
588
+ // isJson: false,
589
+ // selection: [],
590
+ // })),
591
+ // };
592
+ // }
593
+ // const selectedRelationsWithoutPK:
594
+ // // Process all relations without primary keys, because they need to be joined differently and will all be on the same query level
595
+ // for (
596
+ // const {
597
+ // tsKey: selectedRelationTsKey,
598
+ // queryConfig: selectedRelationConfigValue,
599
+ // relation,
600
+ // } of initiallySelectedRelations
601
+ // ) {
602
+ // const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
603
+ // const relationTableName = relation.referencedTable[Table.Symbol.Name];
604
+ // const relationTableTsName = tableNamesMap[relationTableName]!;
605
+ // const relationTable = schema[relationTableTsName]!;
606
+ // if (relationTable.primaryKey.length > 0) {
607
+ // continue;
608
+ // }
609
+ // const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
610
+ // const joinOn = and(
611
+ // ...normalizedRelation.fields.map((field, i) =>
612
+ // eq(
613
+ // aliasedTableColumn(normalizedRelation.references[i]!, relationTableAlias),
614
+ // aliasedTableColumn(field, tableAlias),
615
+ // )
616
+ // ),
617
+ // );
618
+ // const builtRelation = this.buildRelationalQueryWithoutPK({
619
+ // fullSchema,
620
+ // schema,
621
+ // tableNamesMap,
622
+ // table: fullSchema[relationTableTsName] as GelTable,
623
+ // tableConfig: schema[relationTableTsName]!,
624
+ // queryConfig: selectedRelationConfigValue,
625
+ // tableAlias: relationTableAlias,
626
+ // joinOn,
627
+ // nestedQueryRelation: relation,
628
+ // });
629
+ // const field = sql`${sql.identifier(relationTableAlias)}.${sql.identifier('data')}`.as(selectedRelationTsKey);
630
+ // joins.push({
631
+ // on: sql`true`,
632
+ // table: new Subquery(builtRelation.sql as SQL, {}, relationTableAlias),
633
+ // alias: relationTableAlias,
634
+ // joinType: 'left',
635
+ // lateral: true,
636
+ // });
637
+ // selectedRelations.push({
638
+ // dbKey: selectedRelationTsKey,
639
+ // tsKey: selectedRelationTsKey,
640
+ // field,
641
+ // relationTableTsKey: relationTableTsName,
642
+ // isJson: true,
643
+ // selection: builtRelation.selection,
644
+ // });
645
+ // }
646
+ // const oneRelations = initiallySelectedRelations.filter((r): r is typeof r & { relation: One } =>
647
+ // is(r.relation, One)
648
+ // );
649
+ // // Process all One relations with PKs, because they can all be joined on the same level
650
+ // for (
651
+ // const {
652
+ // tsKey: selectedRelationTsKey,
653
+ // queryConfig: selectedRelationConfigValue,
654
+ // relation,
655
+ // } of oneRelations
656
+ // ) {
657
+ // const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
658
+ // const relationTableName = relation.referencedTable[Table.Symbol.Name];
659
+ // const relationTableTsName = tableNamesMap[relationTableName]!;
660
+ // const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
661
+ // const relationTable = schema[relationTableTsName]!;
662
+ // if (relationTable.primaryKey.length === 0) {
663
+ // continue;
664
+ // }
665
+ // const joinOn = and(
666
+ // ...normalizedRelation.fields.map((field, i) =>
667
+ // eq(
668
+ // aliasedTableColumn(normalizedRelation.references[i]!, relationTableAlias),
669
+ // aliasedTableColumn(field, tableAlias),
670
+ // )
671
+ // ),
672
+ // );
673
+ // const builtRelation = this.buildRelationalQueryWithPK({
674
+ // fullSchema,
675
+ // schema,
676
+ // tableNamesMap,
677
+ // table: fullSchema[relationTableTsName] as GelTable,
678
+ // tableConfig: schema[relationTableTsName]!,
679
+ // queryConfig: selectedRelationConfigValue,
680
+ // tableAlias: relationTableAlias,
681
+ // joinOn,
682
+ // });
683
+ // const field = sql`case when ${sql.identifier(relationTableAlias)} is null then null else json_build_array(${
684
+ // sql.join(
685
+ // builtRelation.selection.map(({ field }) =>
686
+ // is(field, SQL.Aliased)
687
+ // ? sql`${sql.identifier(relationTableAlias)}.${sql.identifier(field.fieldAlias)}`
688
+ // : is(field, Column)
689
+ // ? aliasedTableColumn(field, relationTableAlias)
690
+ // : field
691
+ // ),
692
+ // sql`, `,
693
+ // )
694
+ // }) end`.as(selectedRelationTsKey);
695
+ // const isLateralJoin = is(builtRelation.sql, SQL);
696
+ // joins.push({
697
+ // on: isLateralJoin ? sql`true` : joinOn,
698
+ // table: is(builtRelation.sql, SQL)
699
+ // ? new Subquery(builtRelation.sql, {}, relationTableAlias)
700
+ // : aliasedTable(builtRelation.sql, relationTableAlias),
701
+ // alias: relationTableAlias,
702
+ // joinType: 'left',
703
+ // lateral: is(builtRelation.sql, SQL),
704
+ // });
705
+ // selectedRelations.push({
706
+ // dbKey: selectedRelationTsKey,
707
+ // tsKey: selectedRelationTsKey,
708
+ // field,
709
+ // relationTableTsKey: relationTableTsName,
710
+ // isJson: true,
711
+ // selection: builtRelation.selection,
712
+ // });
713
+ // }
714
+ // let distinct: GelSelectConfig['distinct'];
715
+ // let tableFrom: GelTable | Subquery = table;
716
+ // // Process first Many relation - each one requires a nested subquery
717
+ // const manyRelation = manyRelations[0];
718
+ // if (manyRelation) {
719
+ // const {
720
+ // tsKey: selectedRelationTsKey,
721
+ // queryConfig: selectedRelationQueryConfig,
722
+ // relation,
723
+ // } = manyRelation;
724
+ // distinct = {
725
+ // on: tableConfig.primaryKey.map((c) => aliasedTableColumn(c as GelColumn, tableAlias)),
726
+ // };
727
+ // const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
728
+ // const relationTableName = relation.referencedTable[Table.Symbol.Name];
729
+ // const relationTableTsName = tableNamesMap[relationTableName]!;
730
+ // const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
731
+ // const joinOn = and(
732
+ // ...normalizedRelation.fields.map((field, i) =>
733
+ // eq(
734
+ // aliasedTableColumn(normalizedRelation.references[i]!, relationTableAlias),
735
+ // aliasedTableColumn(field, tableAlias),
736
+ // )
737
+ // ),
738
+ // );
739
+ // const builtRelationJoin = this.buildRelationalQueryWithPK({
740
+ // fullSchema,
741
+ // schema,
742
+ // tableNamesMap,
743
+ // table: fullSchema[relationTableTsName] as GelTable,
744
+ // tableConfig: schema[relationTableTsName]!,
745
+ // queryConfig: selectedRelationQueryConfig,
746
+ // tableAlias: relationTableAlias,
747
+ // joinOn,
748
+ // });
749
+ // const builtRelationSelectionField = sql`case when ${
750
+ // sql.identifier(relationTableAlias)
751
+ // } is null then '[]' else json_agg(json_build_array(${
752
+ // sql.join(
753
+ // builtRelationJoin.selection.map(({ field }) =>
754
+ // is(field, SQL.Aliased)
755
+ // ? sql`${sql.identifier(relationTableAlias)}.${sql.identifier(field.fieldAlias)}`
756
+ // : is(field, Column)
757
+ // ? aliasedTableColumn(field, relationTableAlias)
758
+ // : field
759
+ // ),
760
+ // sql`, `,
761
+ // )
762
+ // })) over (partition by ${sql.join(distinct.on, sql`, `)}) end`.as(selectedRelationTsKey);
763
+ // const isLateralJoin = is(builtRelationJoin.sql, SQL);
764
+ // joins.push({
765
+ // on: isLateralJoin ? sql`true` : joinOn,
766
+ // table: isLateralJoin
767
+ // ? new Subquery(builtRelationJoin.sql as SQL, {}, relationTableAlias)
768
+ // : aliasedTable(builtRelationJoin.sql as GelTable, relationTableAlias),
769
+ // alias: relationTableAlias,
770
+ // joinType: 'left',
771
+ // lateral: isLateralJoin,
772
+ // });
773
+ // // Build the "from" subquery with the remaining Many relations
774
+ // const builtTableFrom = this.buildRelationalQueryWithPK({
775
+ // fullSchema,
776
+ // schema,
777
+ // tableNamesMap,
778
+ // table,
779
+ // tableConfig,
780
+ // queryConfig: {
781
+ // ...config,
782
+ // where: undefined,
783
+ // orderBy: undefined,
784
+ // limit: undefined,
785
+ // offset: undefined,
786
+ // with: manyRelations.slice(1).reduce<NonNullable<typeof config['with']>>(
787
+ // (result, { tsKey, queryConfig: configValue }) => {
788
+ // result[tsKey] = configValue;
789
+ // return result;
790
+ // },
791
+ // {},
792
+ // ),
793
+ // },
794
+ // tableAlias,
795
+ // });
796
+ // selectedRelations.push({
797
+ // dbKey: selectedRelationTsKey,
798
+ // tsKey: selectedRelationTsKey,
799
+ // field: builtRelationSelectionField,
800
+ // relationTableTsKey: relationTableTsName,
801
+ // isJson: true,
802
+ // selection: builtRelationJoin.selection,
803
+ // });
804
+ // // selection = builtTableFrom.selection.map((item) =>
805
+ // // is(item.field, SQL.Aliased)
806
+ // // ? { ...item, field: sql`${sql.identifier(tableAlias)}.${sql.identifier(item.field.fieldAlias)}` }
807
+ // // : item
808
+ // // );
809
+ // // selectionForBuild = [{
810
+ // // dbKey: '*',
811
+ // // tsKey: '*',
812
+ // // field: sql`${sql.identifier(tableAlias)}.*`,
813
+ // // selection: [],
814
+ // // isJson: false,
815
+ // // relationTableTsKey: undefined,
816
+ // // }];
817
+ // // const newSelectionItem: (typeof selection)[number] = {
818
+ // // dbKey: selectedRelationTsKey,
819
+ // // tsKey: selectedRelationTsKey,
820
+ // // field,
821
+ // // relationTableTsKey: relationTableTsName,
822
+ // // isJson: true,
823
+ // // selection: builtRelationJoin.selection,
824
+ // // };
825
+ // // selection.push(newSelectionItem);
826
+ // // selectionForBuild.push(newSelectionItem);
827
+ // tableFrom = is(builtTableFrom.sql, GelTable)
828
+ // ? builtTableFrom.sql
829
+ // : new Subquery(builtTableFrom.sql, {}, tableAlias);
830
+ // }
831
+ // if (selectedColumns.length === 0 && selectedRelations.length === 0 && selectedExtras.length === 0) {
832
+ // throw new DrizzleError(`No fields selected for table "${tableConfig.tsName}" ("${tableAlias}")`);
833
+ // }
834
+ // let selection: BuildRelationalQueryResult<GelTable, GelColumn>['selection'];
835
+ // function prepareSelectedColumns() {
836
+ // return selectedColumns.map((key) => ({
837
+ // dbKey: tableConfig.columns[key]!.name,
838
+ // tsKey: key,
839
+ // field: tableConfig.columns[key] as GelColumn,
840
+ // relationTableTsKey: undefined,
841
+ // isJson: false,
842
+ // selection: [],
843
+ // }));
844
+ // }
845
+ // function prepareSelectedExtras() {
846
+ // return selectedExtras.map((item) => ({
847
+ // dbKey: item.value.fieldAlias,
848
+ // tsKey: item.tsKey,
849
+ // field: item.value,
850
+ // relationTableTsKey: undefined,
851
+ // isJson: false,
852
+ // selection: [],
853
+ // }));
854
+ // }
855
+ // if (isRoot) {
856
+ // selection = [
857
+ // ...prepareSelectedColumns(),
858
+ // ...prepareSelectedExtras(),
859
+ // ];
860
+ // }
861
+ // if (hasUserDefinedWhere || orderBy.length > 0) {
862
+ // tableFrom = new Subquery(
863
+ // this.buildSelectQuery({
864
+ // table: is(tableFrom, GelTable) ? aliasedTable(tableFrom, tableAlias) : tableFrom,
865
+ // fields: {},
866
+ // fieldsFlat: selectionForBuild.map(({ field }) => ({
867
+ // path: [],
868
+ // field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field,
869
+ // })),
870
+ // joins,
871
+ // distinct,
872
+ // }),
873
+ // {},
874
+ // tableAlias,
875
+ // );
876
+ // selectionForBuild = selection.map((item) =>
877
+ // is(item.field, SQL.Aliased)
878
+ // ? { ...item, field: sql`${sql.identifier(tableAlias)}.${sql.identifier(item.field.fieldAlias)}` }
879
+ // : item
880
+ // );
881
+ // joins = [];
882
+ // distinct = undefined;
883
+ // }
884
+ // const result = this.buildSelectQuery({
885
+ // table: is(tableFrom, GelTable) ? aliasedTable(tableFrom, tableAlias) : tableFrom,
886
+ // fields: {},
887
+ // fieldsFlat: selectionForBuild.map(({ field }) => ({
888
+ // path: [],
889
+ // field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field,
890
+ // })),
891
+ // where,
892
+ // limit,
893
+ // offset,
894
+ // joins,
895
+ // orderBy,
896
+ // distinct,
897
+ // });
898
+ // return {
899
+ // tableTsKey: tableConfig.tsName,
900
+ // sql: result,
901
+ // selection,
902
+ // };
903
+ // }
904
+ buildRelationalQueryWithoutPK({
905
+ fullSchema,
906
+ schema,
907
+ tableNamesMap,
908
+ table,
909
+ tableConfig,
910
+ queryConfig: config,
911
+ tableAlias,
912
+ nestedQueryRelation,
913
+ joinOn
914
+ }) {
915
+ let selection = [];
916
+ let limit, offset, orderBy = [], where;
917
+ const joins = [];
918
+ if (config === true) {
919
+ const selectionEntries = Object.entries(tableConfig.columns);
920
+ selection = selectionEntries.map(([key, value]) => ({
921
+ dbKey: value.name,
922
+ tsKey: key,
923
+ field: aliasedTableColumn(value, tableAlias),
924
+ relationTableTsKey: void 0,
925
+ isJson: false,
926
+ selection: []
927
+ }));
928
+ } else {
929
+ const aliasedColumns = Object.fromEntries(
930
+ Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)])
931
+ );
932
+ if (config.where) {
933
+ const whereSql = typeof config.where === "function" ? config.where(aliasedColumns, getOperators()) : config.where;
934
+ where = whereSql && mapColumnsInSQLToAlias(whereSql, tableAlias);
935
+ }
936
+ const fieldsSelection = [];
937
+ let selectedColumns = [];
938
+ if (config.columns) {
939
+ let isIncludeMode = false;
940
+ for (const [field, value] of Object.entries(config.columns)) {
941
+ if (value === void 0) {
942
+ continue;
943
+ }
944
+ if (field in tableConfig.columns) {
945
+ if (!isIncludeMode && value === true) {
946
+ isIncludeMode = true;
947
+ }
948
+ selectedColumns.push(field);
949
+ }
950
+ }
951
+ if (selectedColumns.length > 0) {
952
+ selectedColumns = isIncludeMode ? selectedColumns.filter((c) => config.columns?.[c] === true) : Object.keys(tableConfig.columns).filter((key) => !selectedColumns.includes(key));
953
+ }
954
+ } else {
955
+ selectedColumns = Object.keys(tableConfig.columns);
956
+ }
957
+ for (const field of selectedColumns) {
958
+ const column = tableConfig.columns[field];
959
+ fieldsSelection.push({ tsKey: field, value: column });
960
+ }
961
+ let selectedRelations = [];
962
+ if (config.with) {
963
+ selectedRelations = Object.entries(config.with).filter((entry) => !!entry[1]).map(([tsKey, queryConfig]) => ({ tsKey, queryConfig, relation: tableConfig.relations[tsKey] }));
964
+ }
965
+ let extras;
966
+ if (config.extras) {
967
+ extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql }) : config.extras;
968
+ for (const [tsKey, value] of Object.entries(extras)) {
969
+ fieldsSelection.push({
970
+ tsKey,
971
+ value: mapColumnsInAliasedSQLToAlias(value, tableAlias)
972
+ });
973
+ }
974
+ }
975
+ for (const { tsKey, value } of fieldsSelection) {
976
+ selection.push({
977
+ dbKey: is(value, SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
978
+ tsKey,
979
+ field: is(value, Column) ? aliasedTableColumn(value, tableAlias) : value,
980
+ relationTableTsKey: void 0,
981
+ isJson: false,
982
+ selection: []
983
+ });
984
+ }
985
+ let orderByOrig = typeof config.orderBy === "function" ? config.orderBy(aliasedColumns, getOrderByOperators()) : config.orderBy ?? [];
986
+ if (!Array.isArray(orderByOrig)) {
987
+ orderByOrig = [orderByOrig];
988
+ }
989
+ orderBy = orderByOrig.map((orderByValue) => {
990
+ if (is(orderByValue, Column)) {
991
+ return aliasedTableColumn(orderByValue, tableAlias);
992
+ }
993
+ return mapColumnsInSQLToAlias(orderByValue, tableAlias);
994
+ });
995
+ limit = config.limit;
996
+ offset = config.offset;
997
+ for (const {
998
+ tsKey: selectedRelationTsKey,
999
+ queryConfig: selectedRelationConfigValue,
1000
+ relation
1001
+ } of selectedRelations) {
1002
+ const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
1003
+ const relationTableName = getTableUniqueName(relation.referencedTable);
1004
+ const relationTableTsName = tableNamesMap[relationTableName];
1005
+ const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
1006
+ const joinOn2 = and(
1007
+ ...normalizedRelation.fields.map(
1008
+ (field2, i) => eq(
1009
+ aliasedTableColumn(normalizedRelation.references[i], relationTableAlias),
1010
+ aliasedTableColumn(field2, tableAlias)
1011
+ )
1012
+ )
1013
+ );
1014
+ const builtRelation = this.buildRelationalQueryWithoutPK({
1015
+ fullSchema,
1016
+ schema,
1017
+ tableNamesMap,
1018
+ table: fullSchema[relationTableTsName],
1019
+ tableConfig: schema[relationTableTsName],
1020
+ queryConfig: is(relation, One) ? selectedRelationConfigValue === true ? { limit: 1 } : { ...selectedRelationConfigValue, limit: 1 } : selectedRelationConfigValue,
1021
+ tableAlias: relationTableAlias,
1022
+ joinOn: joinOn2,
1023
+ nestedQueryRelation: relation
1024
+ });
1025
+ const field = sql`${sql.identifier(relationTableAlias)}.${sql.identifier("data")}`.as(selectedRelationTsKey);
1026
+ joins.push({
1027
+ on: sql`true`,
1028
+ table: new Subquery(builtRelation.sql, {}, relationTableAlias),
1029
+ alias: relationTableAlias,
1030
+ joinType: "left",
1031
+ lateral: true
1032
+ });
1033
+ selection.push({
1034
+ dbKey: selectedRelationTsKey,
1035
+ tsKey: selectedRelationTsKey,
1036
+ field,
1037
+ relationTableTsKey: relationTableTsName,
1038
+ isJson: true,
1039
+ selection: builtRelation.selection
1040
+ });
1041
+ }
1042
+ }
1043
+ if (selection.length === 0) {
1044
+ throw new DrizzleError({ message: `No fields selected for table "${tableConfig.tsName}" ("${tableAlias}")` });
1045
+ }
1046
+ let result;
1047
+ where = and(joinOn, where);
1048
+ if (nestedQueryRelation) {
1049
+ let field = sql`json_build_array(${sql.join(
1050
+ selection.map(
1051
+ ({ field: field2, tsKey, isJson }) => isJson ? sql`${sql.identifier(`${tableAlias}_${tsKey}`)}.${sql.identifier("data")}` : is(field2, SQL.Aliased) ? field2.sql : field2
1052
+ ),
1053
+ sql`, `
1054
+ )})`;
1055
+ if (is(nestedQueryRelation, Many)) {
1056
+ field = sql`coalesce(json_agg(${field}${orderBy.length > 0 ? sql` order by ${sql.join(orderBy, sql`, `)}` : void 0}), '[]'::json)`;
1057
+ }
1058
+ const nestedSelection = [{
1059
+ dbKey: "data",
1060
+ tsKey: "data",
1061
+ field: field.as("data"),
1062
+ isJson: true,
1063
+ relationTableTsKey: tableConfig.tsName,
1064
+ selection
1065
+ }];
1066
+ const needsSubquery = limit !== void 0 || offset !== void 0 || orderBy.length > 0;
1067
+ if (needsSubquery) {
1068
+ result = this.buildSelectQuery({
1069
+ table: aliasedTable(table, tableAlias),
1070
+ fields: {},
1071
+ fieldsFlat: [{
1072
+ path: [],
1073
+ field: sql.raw("*")
1074
+ }],
1075
+ where,
1076
+ limit,
1077
+ offset,
1078
+ orderBy,
1079
+ setOperators: []
1080
+ });
1081
+ where = void 0;
1082
+ limit = void 0;
1083
+ offset = void 0;
1084
+ orderBy = [];
1085
+ } else {
1086
+ result = aliasedTable(table, tableAlias);
1087
+ }
1088
+ result = this.buildSelectQuery({
1089
+ table: is(result, GelTable) ? result : new Subquery(result, {}, tableAlias),
1090
+ fields: {},
1091
+ fieldsFlat: nestedSelection.map(({ field: field2 }) => ({
1092
+ path: [],
1093
+ field: is(field2, Column) ? aliasedTableColumn(field2, tableAlias) : field2
1094
+ })),
1095
+ joins,
1096
+ where,
1097
+ limit,
1098
+ offset,
1099
+ orderBy,
1100
+ setOperators: []
1101
+ });
1102
+ } else {
1103
+ result = this.buildSelectQuery({
1104
+ table: aliasedTable(table, tableAlias),
1105
+ fields: {},
1106
+ fieldsFlat: selection.map(({ field }) => ({
1107
+ path: [],
1108
+ field: is(field, Column) ? aliasedTableColumn(field, tableAlias) : field
1109
+ })),
1110
+ joins,
1111
+ where,
1112
+ limit,
1113
+ offset,
1114
+ orderBy,
1115
+ setOperators: []
1116
+ });
1117
+ }
1118
+ return {
1119
+ tableTsKey: tableConfig.tsName,
1120
+ sql: result,
1121
+ selection
1122
+ };
1123
+ }
1124
+ }
1125
+ export {
1126
+ GelDialect
1127
+ };
1128
+ //# sourceMappingURL=dialect.js.map