@casekit/orm2 0.0.0-20250322230249

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 (275) hide show
  1. package/build/builders/buildCount.d.ts +23 -0
  2. package/build/builders/buildCount.js +63 -0
  3. package/build/builders/buildCount.test.d.ts +1 -0
  4. package/build/builders/buildCount.test.js +144 -0
  5. package/build/builders/buildCreate.d.ts +5 -0
  6. package/build/builders/buildCreate.js +28 -0
  7. package/build/builders/buildCreate.test.d.ts +1 -0
  8. package/build/builders/buildCreate.test.js +47 -0
  9. package/build/builders/buildDelete.d.ts +5 -0
  10. package/build/builders/buildDelete.js +28 -0
  11. package/build/builders/buildDelete.test.d.ts +1 -0
  12. package/build/builders/buildDelete.test.js +108 -0
  13. package/build/builders/buildFind.d.ts +8 -0
  14. package/build/builders/buildFind.js +185 -0
  15. package/build/builders/buildFind.test.d.ts +1 -0
  16. package/build/builders/buildFind.test.js +263 -0
  17. package/build/builders/buildUpdate.d.ts +5 -0
  18. package/build/builders/buildUpdate.js +34 -0
  19. package/build/builders/buildUpdate.test.d.ts +1 -0
  20. package/build/builders/buildUpdate.test.js +130 -0
  21. package/build/builders/buildWhere.d.ts +6 -0
  22. package/build/builders/buildWhere.js +63 -0
  23. package/build/builders/buildWhere.test.d.ts +1 -0
  24. package/build/builders/buildWhere.test.js +154 -0
  25. package/build/builders/types.d.ts +87 -0
  26. package/build/builders/types.js +1 -0
  27. package/build/connection.d.ts +31 -0
  28. package/build/connection.js +206 -0
  29. package/build/index.d.ts +10 -0
  30. package/build/index.js +5 -0
  31. package/build/operators.d.ts +59 -0
  32. package/build/operators.js +44 -0
  33. package/build/orm.count.d.ts +14 -0
  34. package/build/orm.count.js +22 -0
  35. package/build/orm.createMany.d.ts +5 -0
  36. package/build/orm.createMany.js +26 -0
  37. package/build/orm.createOne.d.ts +5 -0
  38. package/build/orm.createOne.js +34 -0
  39. package/build/orm.d.ts +81 -0
  40. package/build/orm.deleteMany.d.ts +5 -0
  41. package/build/orm.deleteMany.js +26 -0
  42. package/build/orm.deleteOne.d.ts +5 -0
  43. package/build/orm.deleteOne.js +32 -0
  44. package/build/orm.findMany.d.ts +8 -0
  45. package/build/orm.findMany.js +64 -0
  46. package/build/orm.findOne.d.ts +5 -0
  47. package/build/orm.findOne.js +20 -0
  48. package/build/orm.js +243 -0
  49. package/build/orm.restrict.d.ts +6 -0
  50. package/build/orm.restrict.js +52 -0
  51. package/build/orm.updateMany.d.ts +5 -0
  52. package/build/orm.updateMany.js +29 -0
  53. package/build/orm.updateOne.d.ts +5 -0
  54. package/build/orm.updateOne.js +35 -0
  55. package/build/sql/countToSql.d.ts +3 -0
  56. package/build/sql/countToSql.js +11 -0
  57. package/build/sql/countToSql.test.d.ts +1 -0
  58. package/build/sql/countToSql.test.js +218 -0
  59. package/build/sql/createToSql.d.ts +3 -0
  60. package/build/sql/createToSql.js +27 -0
  61. package/build/sql/createToSql.test.d.ts +1 -0
  62. package/build/sql/createToSql.test.js +186 -0
  63. package/build/sql/deleteToSql.d.ts +3 -0
  64. package/build/sql/deleteToSql.js +15 -0
  65. package/build/sql/deleteToSql.test.d.ts +1 -0
  66. package/build/sql/deleteToSql.test.js +93 -0
  67. package/build/sql/findToSql.d.ts +3 -0
  68. package/build/sql/findToSql.js +33 -0
  69. package/build/sql/findToSql.test.d.ts +1 -0
  70. package/build/sql/findToSql.test.js +409 -0
  71. package/build/sql/updateToSql.d.ts +3 -0
  72. package/build/sql/updateToSql.js +16 -0
  73. package/build/sql/updateToSql.test.d.ts +1 -0
  74. package/build/sql/updateToSql.test.js +165 -0
  75. package/build/sql/util.d.ts +11 -0
  76. package/build/sql/util.js +36 -0
  77. package/build/sql/util.test.d.ts +1 -0
  78. package/build/sql/util.test.js +163 -0
  79. package/build/tests/connection.test.d.ts +1 -0
  80. package/build/tests/connection.test.js +304 -0
  81. package/build/tests/datatypes.test.d.ts +1 -0
  82. package/build/tests/datatypes.test.js +239 -0
  83. package/build/tests/operators.test.d.ts +1 -0
  84. package/build/tests/operators.test.js +125 -0
  85. package/build/tests/orm.count.middleware.test.d.ts +1 -0
  86. package/build/tests/orm.count.middleware.test.js +132 -0
  87. package/build/tests/orm.count.test-d.d.ts +1 -0
  88. package/build/tests/orm.count.test-d.js +60 -0
  89. package/build/tests/orm.count.test.d.ts +1 -0
  90. package/build/tests/orm.count.test.js +151 -0
  91. package/build/tests/orm.createMany.middleware.test.d.ts +1 -0
  92. package/build/tests/orm.createMany.middleware.test.js +63 -0
  93. package/build/tests/orm.createMany.test-d.d.ts +1 -0
  94. package/build/tests/orm.createMany.test-d.js +131 -0
  95. package/build/tests/orm.createMany.test.d.ts +1 -0
  96. package/build/tests/orm.createMany.test.js +392 -0
  97. package/build/tests/orm.createOne.middleware.test.d.ts +1 -0
  98. package/build/tests/orm.createOne.middleware.test.js +54 -0
  99. package/build/tests/orm.createOne.test-d.d.ts +1 -0
  100. package/build/tests/orm.createOne.test-d.js +113 -0
  101. package/build/tests/orm.createOne.test.d.ts +1 -0
  102. package/build/tests/orm.createOne.test.js +268 -0
  103. package/build/tests/orm.deleteMany.middleware.test.d.ts +1 -0
  104. package/build/tests/orm.deleteMany.middleware.test.js +77 -0
  105. package/build/tests/orm.deleteMany.test-d.d.ts +1 -0
  106. package/build/tests/orm.deleteMany.test-d.js +179 -0
  107. package/build/tests/orm.deleteMany.test.d.ts +1 -0
  108. package/build/tests/orm.deleteMany.test.js +394 -0
  109. package/build/tests/orm.deleteOne.middleware.test.d.ts +1 -0
  110. package/build/tests/orm.deleteOne.middleware.test.js +61 -0
  111. package/build/tests/orm.deleteOne.test-d.d.ts +1 -0
  112. package/build/tests/orm.deleteOne.test-d.js +179 -0
  113. package/build/tests/orm.deleteOne.test.d.ts +1 -0
  114. package/build/tests/orm.deleteOne.test.js +360 -0
  115. package/build/tests/orm.findMany.includeManyToMany.test.d.ts +1 -0
  116. package/build/tests/orm.findMany.includeManyToMany.test.js +335 -0
  117. package/build/tests/orm.findMany.includeManyToOne.test.d.ts +1 -0
  118. package/build/tests/orm.findMany.includeManyToOne.test.js +286 -0
  119. package/build/tests/orm.findMany.includeOneToMany.test.d.ts +1 -0
  120. package/build/tests/orm.findMany.includeOneToMany.test.js +530 -0
  121. package/build/tests/orm.findMany.middleware.test.d.ts +1 -0
  122. package/build/tests/orm.findMany.middleware.test.js +66 -0
  123. package/build/tests/orm.findMany.offsetLimit.test.d.ts +1 -0
  124. package/build/tests/orm.findMany.offsetLimit.test.js +108 -0
  125. package/build/tests/orm.findMany.orderBy.test.d.ts +1 -0
  126. package/build/tests/orm.findMany.orderBy.test.js +304 -0
  127. package/build/tests/orm.findMany.select.test.d.ts +1 -0
  128. package/build/tests/orm.findMany.select.test.js +278 -0
  129. package/build/tests/orm.findMany.test-d.d.ts +1 -0
  130. package/build/tests/orm.findMany.test-d.js +374 -0
  131. package/build/tests/orm.findMany.where.test.d.ts +1 -0
  132. package/build/tests/orm.findMany.where.test.js +383 -0
  133. package/build/tests/orm.findOne.middleware.test.d.ts +1 -0
  134. package/build/tests/orm.findOne.middleware.test.js +57 -0
  135. package/build/tests/orm.findOne.test-d.d.ts +1 -0
  136. package/build/tests/orm.findOne.test-d.js +377 -0
  137. package/build/tests/orm.findOne.test.d.ts +1 -0
  138. package/build/tests/orm.findOne.test.js +247 -0
  139. package/build/tests/orm.restrict.test-d.d.ts +1 -0
  140. package/build/tests/orm.restrict.test-d.js +105 -0
  141. package/build/tests/orm.restrict.test.d.ts +1 -0
  142. package/build/tests/orm.restrict.test.js +259 -0
  143. package/build/tests/orm.transact.test.d.ts +1 -0
  144. package/build/tests/orm.transact.test.js +48 -0
  145. package/build/tests/orm.updateMany.middleware.test.d.ts +1 -0
  146. package/build/tests/orm.updateMany.middleware.test.js +72 -0
  147. package/build/tests/orm.updateMany.test.d.ts +1 -0
  148. package/build/tests/orm.updateMany.test.js +210 -0
  149. package/build/tests/orm.updateOne.middleware.test.d.ts +1 -0
  150. package/build/tests/orm.updateOne.middleware.test.js +62 -0
  151. package/build/tests/orm.updateOne.test.d.ts +1 -0
  152. package/build/tests/orm.updateOne.test.js +209 -0
  153. package/build/tests/util/db.d.ts +1571 -0
  154. package/build/tests/util/db.js +10 -0
  155. package/build/tests/util/logger.d.ts +19 -0
  156. package/build/tests/util/logger.js +40 -0
  157. package/build/types/BaseFindParams.d.ts +1 -0
  158. package/build/types/BaseFindParams.js +1 -0
  159. package/build/types/CountParams.d.ts +16 -0
  160. package/build/types/CountParams.js +1 -0
  161. package/build/types/CountParams.test-d.d.ts +1 -0
  162. package/build/types/CountParams.test-d.js +89 -0
  163. package/build/types/CreateManyParams.d.ts +11 -0
  164. package/build/types/CreateManyParams.js +1 -0
  165. package/build/types/CreateManyParams.test-d.d.ts +1 -0
  166. package/build/types/CreateManyParams.test-d.js +83 -0
  167. package/build/types/CreateManyResult.d.ts +5 -0
  168. package/build/types/CreateManyResult.js +1 -0
  169. package/build/types/CreateManyResult.test-d.d.ts +1 -0
  170. package/build/types/CreateManyResult.test-d.js +22 -0
  171. package/build/types/CreateOneParams.d.ts +11 -0
  172. package/build/types/CreateOneParams.js +1 -0
  173. package/build/types/CreateOneParams.test-d.d.ts +1 -0
  174. package/build/types/CreateOneParams.test-d.js +56 -0
  175. package/build/types/CreateOneResult.d.ts +5 -0
  176. package/build/types/CreateOneResult.js +1 -0
  177. package/build/types/CreateOneResult.test-d.d.ts +1 -0
  178. package/build/types/CreateOneResult.test-d.js +23 -0
  179. package/build/types/CreateValues.d.ts +6 -0
  180. package/build/types/CreateValues.js +1 -0
  181. package/build/types/CreateValues.test-d.d.ts +1 -0
  182. package/build/types/CreateValues.test-d.js +60 -0
  183. package/build/types/DeleteManyResult.d.ts +5 -0
  184. package/build/types/DeleteManyResult.js +1 -0
  185. package/build/types/DeleteManyResult.test-d.d.ts +1 -0
  186. package/build/types/DeleteManyResult.test-d.js +23 -0
  187. package/build/types/DeleteOneResult.d.ts +5 -0
  188. package/build/types/DeleteOneResult.js +1 -0
  189. package/build/types/DeleteOneResult.test-d.d.ts +1 -0
  190. package/build/types/DeleteOneResult.test-d.js +23 -0
  191. package/build/types/DeleteParams.d.ts +7 -0
  192. package/build/types/DeleteParams.js +1 -0
  193. package/build/types/DeleteParams.test-d.d.ts +1 -0
  194. package/build/types/DeleteParams.test-d.js +46 -0
  195. package/build/types/FindParams.d.ts +22 -0
  196. package/build/types/FindParams.js +1 -0
  197. package/build/types/FindParams.test-d.d.ts +1 -0
  198. package/build/types/FindParams.test-d.js +107 -0
  199. package/build/types/FindResult.d.ts +13 -0
  200. package/build/types/FindResult.js +1 -0
  201. package/build/types/FindResult.test-d.d.ts +1 -0
  202. package/build/types/FindResult.test-d.js +30 -0
  203. package/build/types/IncludeClause.d.ts +5 -0
  204. package/build/types/IncludeClause.js +1 -0
  205. package/build/types/IncludeClause.test-d.d.ts +1 -0
  206. package/build/types/IncludeClause.test-d.js +69 -0
  207. package/build/types/Middleware.d.ts +51 -0
  208. package/build/types/Middleware.js +37 -0
  209. package/build/types/OptionalValues.d.ts +4 -0
  210. package/build/types/OptionalValues.js +1 -0
  211. package/build/types/OptionalValues.test-d.d.ts +1 -0
  212. package/build/types/OptionalValues.test-d.js +12 -0
  213. package/build/types/OrderByClause.d.ts +9 -0
  214. package/build/types/OrderByClause.js +1 -0
  215. package/build/types/OrderByClause.test-d.d.ts +1 -0
  216. package/build/types/OrderByClause.test-d.js +44 -0
  217. package/build/types/RequiredValues.d.ts +4 -0
  218. package/build/types/RequiredValues.js +1 -0
  219. package/build/types/RequiredValues.test-d.d.ts +1 -0
  220. package/build/types/RequiredValues.test-d.js +22 -0
  221. package/build/types/RestrictModels.d.ts +23 -0
  222. package/build/types/RestrictModels.js +1 -0
  223. package/build/types/RestrictModels.test-d.d.ts +1 -0
  224. package/build/types/RestrictModels.test-d.js +44 -0
  225. package/build/types/ReturningClause.d.ts +3 -0
  226. package/build/types/ReturningClause.js +1 -0
  227. package/build/types/ReturningClause.test-d.d.ts +1 -0
  228. package/build/types/ReturningClause.test-d.js +15 -0
  229. package/build/types/SelectClause.d.ts +3 -0
  230. package/build/types/SelectClause.js +1 -0
  231. package/build/types/SelectClause.test-d.d.ts +1 -0
  232. package/build/types/SelectClause.test-d.js +15 -0
  233. package/build/types/UpdateManyResult.d.ts +5 -0
  234. package/build/types/UpdateManyResult.js +1 -0
  235. package/build/types/UpdateManyResult.test-d.d.ts +1 -0
  236. package/build/types/UpdateManyResult.test-d.js +15 -0
  237. package/build/types/UpdateOneResult.d.ts +5 -0
  238. package/build/types/UpdateOneResult.js +1 -0
  239. package/build/types/UpdateOneResult.test-d.d.ts +1 -0
  240. package/build/types/UpdateOneResult.test-d.js +15 -0
  241. package/build/types/UpdateParams.d.ts +9 -0
  242. package/build/types/UpdateParams.js +1 -0
  243. package/build/types/UpdateParams.test-d.d.ts +1 -0
  244. package/build/types/UpdateParams.test-d.js +29 -0
  245. package/build/types/UpdateValues.d.ts +6 -0
  246. package/build/types/UpdateValues.js +1 -0
  247. package/build/types/UpdateValues.test-d.d.ts +1 -0
  248. package/build/types/UpdateValues.test-d.js +33 -0
  249. package/build/types/WhereClause.d.ts +29 -0
  250. package/build/types/WhereClause.js +1 -0
  251. package/build/types/WhereClause.test-d.d.ts +1 -0
  252. package/build/types/WhereClause.test-d.js +137 -0
  253. package/build/util/getIncludedToManySubqueries.d.ts +12 -0
  254. package/build/util/getIncludedToManySubqueries.js +63 -0
  255. package/build/util/getIncludedToManySubqueries.test.d.ts +1 -0
  256. package/build/util/getIncludedToManySubqueries.test.js +121 -0
  257. package/build/util/getLateralJoinValues.d.ts +4 -0
  258. package/build/util/getLateralJoinValues.js +30 -0
  259. package/build/util/getLateralJoinValues.test.d.ts +1 -0
  260. package/build/util/getLateralJoinValues.test.js +99 -0
  261. package/build/util/hasClauses.d.ts +1 -0
  262. package/build/util/hasClauses.js +9 -0
  263. package/build/util/hasClauses.test.d.ts +1 -0
  264. package/build/util/hasClauses.test.js +15 -0
  265. package/build/util/makeTableAlias.d.ts +1 -0
  266. package/build/util/makeTableAlias.js +10 -0
  267. package/build/util/makeTableAlias.test.d.ts +1 -0
  268. package/build/util/makeTableAlias.test.js +18 -0
  269. package/build/util/resultSchema.d.ts +10 -0
  270. package/build/util/resultSchema.js +42 -0
  271. package/build/util/rowToObject.d.ts +8 -0
  272. package/build/util/rowToObject.js +10 -0
  273. package/build/util/rowToObject.test.d.ts +1 -0
  274. package/build/util/rowToObject.test.js +68 -0
  275. package/package.json +67 -0
@@ -0,0 +1,409 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { sql } from "@casekit/sql";
3
+ import { unindent } from "@casekit/unindent";
4
+ import { findToSql } from "./findToSql.js";
5
+ describe("findToSql", () => {
6
+ test("generates basic select query", () => {
7
+ const statement = findToSql({
8
+ table: {
9
+ schema: "public",
10
+ name: "users",
11
+ alias: "a",
12
+ model: "user",
13
+ },
14
+ columns: [
15
+ { table: "a", name: "id", alias: "a_0", path: ["id"] },
16
+ { table: "a", name: "name", alias: "a_1", path: ["name"] },
17
+ ],
18
+ joins: [],
19
+ orderBy: [],
20
+ tableIndex: 0,
21
+ });
22
+ expect(statement.pretty).toBe(unindent `
23
+ SELECT
24
+ "a"."id" AS "a_0",
25
+ "a"."name" AS "a_1"
26
+ FROM
27
+ "public"."users" AS "a"
28
+ `);
29
+ });
30
+ test("generates query with joins", () => {
31
+ const statement = findToSql({
32
+ table: {
33
+ schema: "public",
34
+ name: "posts",
35
+ alias: "a",
36
+ model: "post",
37
+ },
38
+ columns: [
39
+ { table: "a", name: "id", alias: "a_0", path: ["id"] },
40
+ {
41
+ table: "b",
42
+ name: "name",
43
+ alias: "b_0",
44
+ path: ["author", "name"],
45
+ },
46
+ ],
47
+ joins: [
48
+ {
49
+ type: "LEFT",
50
+ path: ["author"],
51
+ relation: "author",
52
+ table: {
53
+ schema: "public",
54
+ name: "users",
55
+ alias: "b",
56
+ model: "user",
57
+ },
58
+ columns: [
59
+ {
60
+ from: { table: "a", name: "author_id" },
61
+ to: { table: "b", name: "id" },
62
+ },
63
+ ],
64
+ where: sql `"b"."deleted_at" IS NULL`,
65
+ },
66
+ ],
67
+ orderBy: [],
68
+ tableIndex: 0,
69
+ });
70
+ expect(statement.pretty).toBe(unindent `
71
+ SELECT
72
+ "a"."id" AS "a_0",
73
+ "b"."name" AS "b_0"
74
+ FROM
75
+ "public"."posts" AS "a"
76
+ LEFT JOIN "public"."users" AS "b" ON "a"."author_id" = "b"."id"
77
+ AND "b"."deleted_at" IS NULL
78
+ `);
79
+ });
80
+ test("generates query with compound key join", () => {
81
+ const statement = findToSql({
82
+ table: {
83
+ schema: "public",
84
+ name: "users",
85
+ alias: "a",
86
+ model: "user",
87
+ },
88
+ columns: [
89
+ { table: "a", name: "id", alias: "a_0", path: ["id"] },
90
+ {
91
+ table: "b",
92
+ name: "name",
93
+ alias: "b_0",
94
+ path: ["organization", "name"],
95
+ },
96
+ ],
97
+ joins: [
98
+ {
99
+ path: ["organization"],
100
+ type: "LEFT",
101
+ relation: "organization",
102
+ table: {
103
+ schema: "public",
104
+ name: "organisatations",
105
+ alias: "b",
106
+ model: "organization",
107
+ },
108
+ columns: [
109
+ {
110
+ from: { table: "a", name: "org_id" },
111
+ to: { table: "b", name: "id" },
112
+ },
113
+ {
114
+ from: { table: "a", name: "org_type" },
115
+ to: { table: "b", name: "type" },
116
+ },
117
+ ],
118
+ },
119
+ ],
120
+ orderBy: [],
121
+ tableIndex: 0,
122
+ });
123
+ expect(statement.pretty).toBe(unindent `
124
+ SELECT
125
+ "a"."id" AS "a_0",
126
+ "b"."name" AS "b_0"
127
+ FROM
128
+ "public"."users" AS "a"
129
+ LEFT JOIN "public"."organisatations" AS "b" ON "a"."org_id" = "b"."id"
130
+ AND "a"."org_type" = "b"."type"
131
+ `);
132
+ });
133
+ test("generates query with order by", () => {
134
+ const statement = findToSql({
135
+ table: {
136
+ schema: "public",
137
+ name: "users",
138
+ alias: "a",
139
+ model: "user",
140
+ },
141
+ columns: [{ table: "a", name: "id", alias: "a_0", path: ["id"] }],
142
+ joins: [],
143
+ orderBy: [
144
+ {
145
+ column: {
146
+ table: "a",
147
+ name: "id",
148
+ },
149
+ direction: "ASC",
150
+ },
151
+ ],
152
+ tableIndex: 0,
153
+ });
154
+ expect(statement.pretty).toBe(unindent `
155
+ SELECT
156
+ "a"."id" AS "a_0"
157
+ FROM
158
+ "public"."users" AS "a"
159
+ ORDER BY
160
+ "a"."id" ASC
161
+ `);
162
+ });
163
+ test("generates query with limit and offset", () => {
164
+ const statement = findToSql({
165
+ table: {
166
+ schema: "public",
167
+ name: "users",
168
+ alias: "a",
169
+ model: "user",
170
+ },
171
+ columns: [{ table: "a", name: "id", alias: "a_0", path: ["id"] }],
172
+ joins: [],
173
+ orderBy: [],
174
+ limit: 10,
175
+ offset: 20,
176
+ tableIndex: 0,
177
+ });
178
+ expect(statement.pretty).toBe(unindent `
179
+ SELECT
180
+ "a"."id" AS "a_0"
181
+ FROM
182
+ "public"."users" AS "a"
183
+ LIMIT
184
+ $1
185
+ OFFSET
186
+ $2
187
+ `);
188
+ });
189
+ test("generates basic select query with lateral", () => {
190
+ const statement = findToSql({
191
+ table: {
192
+ schema: "public",
193
+ name: "users",
194
+ alias: "a",
195
+ model: "user",
196
+ },
197
+ columns: [
198
+ { table: "a", name: "id", alias: "a_0", path: ["id"] },
199
+ { table: "a", name: "name", alias: "a_1", path: ["name"] },
200
+ ],
201
+ joins: [],
202
+ orderBy: [],
203
+ lateralBy: {
204
+ outerAlias: "b",
205
+ innerAlias: "c",
206
+ primaryKeys: [
207
+ {
208
+ column: "id",
209
+ type: "int",
210
+ values: [1, 2, 3],
211
+ },
212
+ ],
213
+ },
214
+ tableIndex: 0,
215
+ });
216
+ expect(statement.pretty).toBe(unindent `
217
+ SELECT
218
+ "b".*
219
+ FROM
220
+ (
221
+ SELECT
222
+ UNNEST(ARRAY[$1::int, $2::int, $3::int]) AS "id"
223
+ ) AS "c"
224
+ JOIN LATERAL (
225
+ SELECT
226
+ "a"."id" AS "a_0",
227
+ "a"."name" AS "a_1"
228
+ FROM
229
+ "public"."users" AS "a"
230
+ WHERE
231
+ (1 = 1)
232
+ AND ("c"."id" = "a"."id")
233
+ ) "b" ON TRUE
234
+ `);
235
+ });
236
+ test("generates query with joins and lateral", () => {
237
+ const statement = findToSql({
238
+ table: {
239
+ schema: "public",
240
+ name: "posts",
241
+ alias: "a",
242
+ model: "post",
243
+ },
244
+ columns: [
245
+ { table: "a", name: "id", alias: "a_0", path: ["id"] },
246
+ {
247
+ table: "b",
248
+ name: "name",
249
+ alias: "b_0",
250
+ path: ["author", "name"],
251
+ },
252
+ ],
253
+ joins: [
254
+ {
255
+ type: "LEFT",
256
+ relation: "author",
257
+ path: ["author"],
258
+ table: {
259
+ schema: "public",
260
+ name: "users",
261
+ alias: "b",
262
+ model: "user",
263
+ },
264
+ columns: [
265
+ {
266
+ from: { table: "a", name: "author_id" },
267
+ to: { table: "b", name: "id" },
268
+ },
269
+ ],
270
+ },
271
+ ],
272
+ orderBy: [],
273
+ lateralBy: {
274
+ outerAlias: "c",
275
+ innerAlias: "d",
276
+ primaryKeys: [
277
+ {
278
+ column: "id",
279
+ type: "int",
280
+ values: [1, 2, 3],
281
+ },
282
+ ],
283
+ },
284
+ tableIndex: 0,
285
+ });
286
+ expect(statement.pretty).toBe(unindent `
287
+ SELECT
288
+ "c".*
289
+ FROM
290
+ (
291
+ SELECT
292
+ UNNEST(ARRAY[$1::int, $2::int, $3::int]) AS "id"
293
+ ) AS "d"
294
+ JOIN LATERAL (
295
+ SELECT
296
+ "a"."id" AS "a_0",
297
+ "b"."name" AS "b_0"
298
+ FROM
299
+ "public"."posts" AS "a"
300
+ LEFT JOIN "public"."users" AS "b" ON "a"."author_id" = "b"."id"
301
+ WHERE
302
+ (1 = 1)
303
+ AND ("d"."id" = "a"."id")
304
+ ) "c" ON TRUE
305
+ `);
306
+ });
307
+ test("generates query with order by and lateral", () => {
308
+ const statement = findToSql({
309
+ table: {
310
+ schema: "public",
311
+ name: "users",
312
+ alias: "a",
313
+ model: "user",
314
+ },
315
+ columns: [{ table: "a", name: "id", alias: "a_0", path: ["id"] }],
316
+ joins: [],
317
+ orderBy: [
318
+ {
319
+ column: {
320
+ table: "a",
321
+ name: "id",
322
+ },
323
+ direction: "ASC",
324
+ },
325
+ ],
326
+ tableIndex: 0,
327
+ lateralBy: {
328
+ outerAlias: "b",
329
+ innerAlias: "c",
330
+ primaryKeys: [
331
+ {
332
+ column: "id",
333
+ type: "int",
334
+ values: [1, 2, 3],
335
+ },
336
+ ],
337
+ },
338
+ });
339
+ expect(statement.pretty).toBe(unindent `
340
+ SELECT
341
+ "b".*
342
+ FROM
343
+ (
344
+ SELECT
345
+ UNNEST(ARRAY[$1::int, $2::int, $3::int]) AS "id"
346
+ ) AS "c"
347
+ JOIN LATERAL (
348
+ SELECT
349
+ "a"."id" AS "a_0"
350
+ FROM
351
+ "public"."users" AS "a"
352
+ WHERE
353
+ (1 = 1)
354
+ AND ("c"."id" = "a"."id")
355
+ ORDER BY
356
+ "a"."id" ASC
357
+ ) "b" ON TRUE
358
+ `);
359
+ });
360
+ test("generates query with limit, offset and lateral", () => {
361
+ const statement = findToSql({
362
+ table: {
363
+ schema: "public",
364
+ name: "users",
365
+ alias: "a",
366
+ model: "user",
367
+ },
368
+ columns: [{ table: "a", name: "id", alias: "a_0", path: ["id"] }],
369
+ joins: [],
370
+ orderBy: [],
371
+ limit: 10,
372
+ offset: 20,
373
+ lateralBy: {
374
+ outerAlias: "b",
375
+ innerAlias: "c",
376
+ primaryKeys: [
377
+ {
378
+ column: "id",
379
+ type: "int",
380
+ values: [1, 2, 3],
381
+ },
382
+ ],
383
+ },
384
+ tableIndex: 0,
385
+ });
386
+ expect(statement.pretty).toBe(unindent `
387
+ SELECT
388
+ "b".*
389
+ FROM
390
+ (
391
+ SELECT
392
+ UNNEST(ARRAY[$1::int, $2::int, $3::int]) AS "id"
393
+ ) AS "c"
394
+ JOIN LATERAL (
395
+ SELECT
396
+ "a"."id" AS "a_0"
397
+ FROM
398
+ "public"."users" AS "a"
399
+ WHERE
400
+ (1 = 1)
401
+ AND ("c"."id" = "a"."id")
402
+ LIMIT
403
+ $4
404
+ OFFSET
405
+ $5
406
+ ) "b" ON TRUE
407
+ `);
408
+ });
409
+ });
@@ -0,0 +1,3 @@
1
+ import { SQLStatement } from "@casekit/sql";
2
+ import { UpdateBuilder } from "#builders/types.js";
3
+ export declare const updateToSql: (builder: UpdateBuilder) => SQLStatement;
@@ -0,0 +1,16 @@
1
+ import { sql } from "@casekit/sql";
2
+ import { returnedColumn, setClause, tableName } from "./util.js";
3
+ export const updateToSql = (builder) => {
4
+ const { table, set, where, returning } = builder;
5
+ const statement = sql `
6
+ UPDATE ${tableName(table)}
7
+ SET ${set.map(setClause)}
8
+ WHERE ${where}
9
+ `;
10
+ if (returning.length > 0) {
11
+ statement.append `
12
+ RETURNING ${sql.join(returning.map(returnedColumn))}
13
+ `;
14
+ }
15
+ return statement;
16
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,165 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { sql } from "@casekit/sql";
3
+ import { unindent } from "@casekit/unindent";
4
+ import { updateToSql } from "./updateToSql.js";
5
+ describe("updateToSql", () => {
6
+ test("generates basic update query", () => {
7
+ const statement = updateToSql({
8
+ table: {
9
+ schema: "public",
10
+ name: "users",
11
+ alias: "u",
12
+ model: "user",
13
+ },
14
+ set: [
15
+ ["name", "John"],
16
+ ["email", "john@example.com"],
17
+ ],
18
+ where: sql `id = 1`,
19
+ returning: [],
20
+ });
21
+ expect(statement.pretty).toBe(unindent `
22
+ UPDATE "public"."users" AS "u"
23
+ SET
24
+ "name" = $1,
25
+ "email" = $2
26
+ WHERE
27
+ id = 1
28
+ `);
29
+ expect(statement.values).toEqual(["John", "john@example.com"]);
30
+ });
31
+ test("can update array fields", () => {
32
+ const statement = updateToSql({
33
+ table: {
34
+ schema: "public",
35
+ name: "posts",
36
+ alias: "p",
37
+ model: "post",
38
+ },
39
+ set: [
40
+ ["title", "My first post"],
41
+ ["content", "Hello, world!"],
42
+ ["tags", ["hello", "world"]],
43
+ ],
44
+ returning: [
45
+ { name: "id", alias: "p_0", path: ["id"] },
46
+ { name: "tags", alias: "p_1", path: ["tags"] },
47
+ ],
48
+ where: sql `id = 1`,
49
+ });
50
+ expect(statement.pretty).toBe(unindent `
51
+ UPDATE "public"."posts" AS "p"
52
+ SET
53
+ "title" = $1,
54
+ "content" = $2,
55
+ "tags" = $3
56
+ WHERE
57
+ id = 1
58
+ RETURNING
59
+ "id" AS "p_0",
60
+ "tags" AS "p_1"
61
+ `);
62
+ expect(statement.values).toEqual([
63
+ "My first post",
64
+ "Hello, world!",
65
+ ["hello", "world"],
66
+ ]);
67
+ });
68
+ test("generates query with returning clause", () => {
69
+ const statement = updateToSql({
70
+ table: {
71
+ schema: "public",
72
+ name: "users",
73
+ alias: "u",
74
+ model: "user",
75
+ },
76
+ set: [["name", "John"]],
77
+ where: sql `id = 1`,
78
+ returning: [
79
+ { name: "id", alias: "u_0", path: ["id"] },
80
+ { name: "name", alias: "u_1", path: ["name"] },
81
+ ],
82
+ });
83
+ expect(statement.pretty).toBe(unindent `
84
+ UPDATE "public"."users" AS "u"
85
+ SET
86
+ "name" = $1
87
+ WHERE
88
+ id = 1
89
+ RETURNING
90
+ "id" AS "u_0",
91
+ "name" AS "u_1"
92
+ `);
93
+ expect(statement.values).toEqual(["John"]);
94
+ });
95
+ test("handles multiple set clauses", () => {
96
+ const statement = updateToSql({
97
+ table: {
98
+ schema: "public",
99
+ name: "users",
100
+ alias: "u",
101
+ model: "user",
102
+ },
103
+ set: [
104
+ ["first_name", "John"],
105
+ ["last_name", "Doe"],
106
+ ["email", "john@example.com"],
107
+ ],
108
+ where: sql `id = 1`,
109
+ returning: [],
110
+ });
111
+ expect(statement.pretty).toBe(unindent `
112
+ UPDATE "public"."users" AS "u"
113
+ SET
114
+ "first_name" = $1,
115
+ "last_name" = $2,
116
+ "email" = $3
117
+ WHERE
118
+ id = 1
119
+ `);
120
+ expect(statement.values).toEqual(["John", "Doe", "john@example.com"]);
121
+ });
122
+ test("handles complex where clause", () => {
123
+ const statement = updateToSql({
124
+ table: {
125
+ schema: "public",
126
+ name: "users",
127
+ alias: "u",
128
+ model: "user",
129
+ },
130
+ set: [["name", "John"]],
131
+ where: sql `id = ${1} AND email LIKE ${"@example.com"}`,
132
+ returning: [],
133
+ });
134
+ expect(statement.pretty).toBe(unindent `
135
+ UPDATE "public"."users" AS "u"
136
+ SET
137
+ "name" = $1
138
+ WHERE
139
+ id = $2
140
+ AND email LIKE $3
141
+ `);
142
+ expect(statement.values).toEqual(["John", 1, "@example.com"]);
143
+ });
144
+ test("handles null values", () => {
145
+ const statement = updateToSql({
146
+ table: {
147
+ schema: "public",
148
+ name: "users",
149
+ alias: "u",
150
+ model: "user",
151
+ },
152
+ set: [["deleted_at", null]],
153
+ where: sql `id = 1`,
154
+ returning: [],
155
+ });
156
+ expect(statement.pretty).toBe(unindent `
157
+ UPDATE "public"."users" AS "u"
158
+ SET
159
+ "deleted_at" = NULL
160
+ WHERE
161
+ id = 1
162
+ `);
163
+ expect(statement.values).toEqual([]);
164
+ });
165
+ });
@@ -0,0 +1,11 @@
1
+ import { SQLStatement } from "@casekit/sql";
2
+ import { Column, Join, LateralJoin, OrderBy, ReturnedColumn, SelectedColumn, Table } from "#builders/types.js";
3
+ export declare const tableName: (table: Table) => SQLStatement<import("pg").QueryResultRow>;
4
+ export declare const columnName: (column: Column) => SQLStatement<import("pg").QueryResultRow>;
5
+ export declare const selectColumn: ({ table, name, alias }: SelectedColumn) => SQLStatement<import("pg").QueryResultRow>;
6
+ export declare const returnedColumn: ({ name, alias }: ReturnedColumn) => SQLStatement<import("pg").QueryResultRow>;
7
+ export declare const sqlValue: (type: string) => (value: unknown) => SQLStatement<import("pg").QueryResultRow>;
8
+ export declare const unnestPk: (pk: LateralJoin["primaryKeys"][number]) => SQLStatement<import("pg").QueryResultRow>;
9
+ export declare const setClause: ([column, value]: [string, unknown]) => SQLStatement<import("pg").QueryResultRow>;
10
+ export declare const joinClause: (join: Join) => SQLStatement<import("pg").QueryResultRow>;
11
+ export declare const orderByColumn: (orderByColumn: OrderBy) => SQLStatement<import("pg").QueryResultRow>;
@@ -0,0 +1,36 @@
1
+ import { SQLStatement, sql } from "@casekit/sql";
2
+ export const tableName = (table) => {
3
+ return sql `${sql.ident(table.schema)}.${sql.ident(table.name)} AS ${sql.ident(table.alias)}`;
4
+ };
5
+ export const columnName = (column) => {
6
+ return sql `${sql.ident(column.table)}.${sql.ident(column.name)}`;
7
+ };
8
+ export const selectColumn = ({ table, name, alias }) => {
9
+ return sql `${sql.ident(table)}.${sql.ident(name)} AS ${sql.ident(alias)}`;
10
+ };
11
+ export const returnedColumn = ({ name, alias }) => {
12
+ return sql `${sql.ident(name)} AS ${sql.ident(alias)}`;
13
+ };
14
+ export const sqlValue = (type) => (value) => {
15
+ return sql `${value}::${new SQLStatement(type)}`;
16
+ };
17
+ export const unnestPk = (pk) => {
18
+ return sql `UNNEST(ARRAY[${sql.join(pk.values.map(sqlValue(pk.type)))}]) AS ${sql.ident(pk.column)}`;
19
+ };
20
+ export const setClause = ([column, value]) => {
21
+ return sql `${sql.ident(column)} = ${sql.value(value)}`;
22
+ };
23
+ export const joinClause = (join) => {
24
+ const pkClauses = join.columns.map(({ from, to }) => sql `${columnName(from)} = ${columnName(to)}`);
25
+ const clauses = join.where ? [...pkClauses, join.where] : pkClauses;
26
+ return sql `
27
+ ${join.type === "LEFT" ? sql `LEFT JOIN` : sql `JOIN`}
28
+ ${tableName(join.table)} ON ${sql.join(clauses, " AND ")}
29
+ `;
30
+ };
31
+ export const orderByColumn = (orderByColumn) => {
32
+ return sql `
33
+ ${columnName(orderByColumn.column)}
34
+ ${orderByColumn.direction === "ASC" ? sql `ASC` : sql `DESC`}
35
+ `;
36
+ };
@@ -0,0 +1 @@
1
+ export {};