@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,263 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { createTestDB } from "../tests/util/db.js";
3
+ import { buildFind } from "./buildFind.js";
4
+ describe("buildFind", () => {
5
+ const { db } = createTestDB();
6
+ test("builds query with N:1 relation include", () => {
7
+ const result = buildFind(db.config, "post", {
8
+ select: ["id", "title"],
9
+ include: {
10
+ author: {
11
+ select: ["name", "id"],
12
+ },
13
+ },
14
+ });
15
+ expect(result).toEqual({
16
+ table: {
17
+ schema: "orm",
18
+ name: "post",
19
+ alias: "a",
20
+ model: "post",
21
+ },
22
+ columns: [
23
+ {
24
+ table: "a",
25
+ name: "id",
26
+ alias: "a_0",
27
+ path: ["id"],
28
+ },
29
+ {
30
+ table: "a",
31
+ name: "title",
32
+ alias: "a_1",
33
+ path: ["title"],
34
+ },
35
+ {
36
+ table: "b",
37
+ name: "name",
38
+ alias: "b_0",
39
+ path: ["author", "name"],
40
+ },
41
+ {
42
+ table: "b",
43
+ name: "id",
44
+ alias: "b_1",
45
+ path: ["author", "id"],
46
+ },
47
+ ],
48
+ joins: [
49
+ {
50
+ path: ["author"],
51
+ relation: "author",
52
+ orderBy: [],
53
+ type: "INNER",
54
+ table: {
55
+ schema: "orm",
56
+ name: "user",
57
+ alias: "b",
58
+ model: "user",
59
+ },
60
+ where: null,
61
+ columns: [
62
+ {
63
+ from: {
64
+ table: "a",
65
+ name: "author_id",
66
+ },
67
+ to: {
68
+ table: "b",
69
+ name: "id",
70
+ },
71
+ },
72
+ ],
73
+ },
74
+ ],
75
+ where: null,
76
+ orderBy: [],
77
+ limit: undefined,
78
+ offset: undefined,
79
+ tableIndex: 2,
80
+ });
81
+ });
82
+ test("builds query with optional N:1 relation include", () => {
83
+ const { db } = createTestDB();
84
+ const result = buildFind(db.config, "post", {
85
+ select: ["id"],
86
+ include: {
87
+ backgroundColor: {
88
+ select: ["name"],
89
+ },
90
+ },
91
+ });
92
+ expect(result).toEqual({
93
+ table: {
94
+ schema: "orm",
95
+ name: "post",
96
+ alias: "a",
97
+ model: "post",
98
+ },
99
+ columns: [
100
+ {
101
+ table: "a",
102
+ name: "id",
103
+ alias: "a_0",
104
+ path: ["id"],
105
+ },
106
+ {
107
+ table: "b",
108
+ name: "name",
109
+ alias: "b_0",
110
+ path: ["backgroundColor", "name"],
111
+ },
112
+ {
113
+ table: "b",
114
+ name: "hex",
115
+ alias: "b_1",
116
+ path: ["backgroundColor", "hex"],
117
+ },
118
+ ],
119
+ joins: [
120
+ {
121
+ path: ["backgroundColor"],
122
+ relation: "backgroundColor",
123
+ orderBy: [],
124
+ type: "LEFT",
125
+ table: {
126
+ schema: "orm",
127
+ name: "color",
128
+ alias: "b",
129
+ model: "color",
130
+ },
131
+ where: null,
132
+ columns: [
133
+ {
134
+ from: {
135
+ table: "a",
136
+ name: "background_color_value",
137
+ },
138
+ to: {
139
+ table: "b",
140
+ name: "hex",
141
+ },
142
+ },
143
+ ],
144
+ },
145
+ ],
146
+ where: null,
147
+ orderBy: [],
148
+ limit: undefined,
149
+ offset: undefined,
150
+ tableIndex: 2,
151
+ });
152
+ });
153
+ test("automatically joins relations referenced in orderBy", () => {
154
+ const { db } = createTestDB();
155
+ const result = buildFind(db.config, "post", {
156
+ select: ["id", "title"],
157
+ orderBy: ["author.name"],
158
+ });
159
+ expect(result).toEqual({
160
+ table: {
161
+ schema: "orm",
162
+ name: "post",
163
+ alias: "a",
164
+ model: "post",
165
+ },
166
+ columns: [
167
+ {
168
+ table: "a",
169
+ name: "id",
170
+ alias: "a_0",
171
+ path: ["id"],
172
+ },
173
+ {
174
+ table: "a",
175
+ name: "title",
176
+ alias: "a_1",
177
+ path: ["title"],
178
+ },
179
+ {
180
+ table: "b",
181
+ name: "id",
182
+ alias: "b_0",
183
+ path: ["author", "id"],
184
+ },
185
+ ],
186
+ joins: [
187
+ {
188
+ relation: "author",
189
+ path: ["author"],
190
+ orderBy: [],
191
+ type: "INNER",
192
+ table: {
193
+ schema: "orm",
194
+ name: "user",
195
+ alias: "b",
196
+ model: "user",
197
+ },
198
+ where: null,
199
+ columns: [
200
+ {
201
+ from: {
202
+ table: "a",
203
+ name: "author_id",
204
+ },
205
+ to: {
206
+ table: "b",
207
+ name: "id",
208
+ },
209
+ },
210
+ ],
211
+ },
212
+ ],
213
+ where: null,
214
+ orderBy: [
215
+ {
216
+ column: {
217
+ table: "b",
218
+ name: "name",
219
+ },
220
+ direction: "ASC",
221
+ },
222
+ ],
223
+ limit: undefined,
224
+ offset: undefined,
225
+ tableIndex: 2,
226
+ });
227
+ });
228
+ test("combines ordering, limits and offsets from nested queries", () => {
229
+ const { db } = createTestDB();
230
+ const result = buildFind(db.config, "post", {
231
+ select: ["id"],
232
+ limit: 10,
233
+ offset: 5,
234
+ orderBy: ["author.id"],
235
+ include: {
236
+ author: {
237
+ select: ["id"],
238
+ limit: 5,
239
+ offset: 10,
240
+ orderBy: [["name", "desc"]],
241
+ },
242
+ },
243
+ });
244
+ expect(result.limit).toBe(5); // Takes minimum of limits
245
+ expect(result.offset).toBe(10); // Takes maximum of offsets
246
+ expect(result.orderBy).toEqual([
247
+ {
248
+ column: {
249
+ table: "b",
250
+ name: "id",
251
+ },
252
+ direction: "ASC",
253
+ },
254
+ {
255
+ column: {
256
+ table: "b",
257
+ name: "name",
258
+ },
259
+ direction: "DESC",
260
+ },
261
+ ]);
262
+ });
263
+ });
@@ -0,0 +1,5 @@
1
+ import { NormalizedConfig } from "@casekit/orm2-config";
2
+ import { ModelDefinitions, ModelName, OperatorDefinitions } from "@casekit/orm2-schema";
3
+ import { UpdateParams } from "#types/UpdateParams.js";
4
+ import { UpdateBuilder } from "./types.js";
5
+ export declare const buildUpdate: (config: NormalizedConfig, modelName: string, query: UpdateParams<ModelDefinitions, OperatorDefinitions, ModelName<ModelDefinitions>>, path?: string[], tableIndex?: number) => UpdateBuilder;
@@ -0,0 +1,34 @@
1
+ import { getField } from "@casekit/orm2-config";
2
+ import { makeTableAlias } from "#util/makeTableAlias.js";
3
+ import { hasClauses } from "../util/hasClauses.js";
4
+ import { buildWhere } from "./buildWhere.js";
5
+ export const buildUpdate = (config, modelName, query, path = [], tableIndex = 0) => {
6
+ const model = config.models[modelName];
7
+ if (!model)
8
+ throw new Error(`Model "${modelName}" not found`);
9
+ const table = {
10
+ schema: model.schema,
11
+ name: model.table,
12
+ alias: makeTableAlias(tableIndex++),
13
+ model: modelName,
14
+ };
15
+ const set = Object.entries(query.set).map(([field, value]) => [
16
+ getField(model, field).column,
17
+ value,
18
+ ]);
19
+ if (!hasClauses(query.where)) {
20
+ throw new Error("Update queries must have a where clause");
21
+ }
22
+ const where = buildWhere(config, table, query.where);
23
+ const returning = query.returning?.map((f, index) => ({
24
+ name: model.fields[f].column,
25
+ alias: `${table.alias}_${index}`,
26
+ path: [...path, f],
27
+ })) ?? [];
28
+ return {
29
+ table,
30
+ set,
31
+ where,
32
+ returning,
33
+ };
34
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,130 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { SQLStatement, sql } from "@casekit/sql";
3
+ import { unindent } from "@casekit/unindent";
4
+ import { $like } from "#operators.js";
5
+ import { createTestDB } from "../tests/util/db.js";
6
+ import { buildUpdate } from "./buildUpdate.js";
7
+ describe("buildUpdate", () => {
8
+ const { db } = createTestDB();
9
+ test("builds basic update query", () => {
10
+ const result = buildUpdate(db.config, "user", {
11
+ set: { name: "John", email: "john@example.com" },
12
+ where: { id: 1 },
13
+ });
14
+ expect(result).toEqual({
15
+ table: {
16
+ schema: "orm",
17
+ name: "user",
18
+ alias: "a",
19
+ model: "user",
20
+ },
21
+ set: [
22
+ ["name", "John"],
23
+ ["email", "john@example.com"],
24
+ ],
25
+ where: sql `"a"."id" = ${1}`,
26
+ returning: [],
27
+ });
28
+ });
29
+ test("builds update query with returning clause", () => {
30
+ const result = buildUpdate(db.config, "user", {
31
+ set: { name: "John" },
32
+ where: { id: 1 },
33
+ returning: ["id", "name"],
34
+ });
35
+ expect(result).toEqual({
36
+ table: {
37
+ schema: "orm",
38
+ name: "user",
39
+ alias: "a",
40
+ model: "user",
41
+ },
42
+ set: [["name", "John"]],
43
+ where: sql `"a"."id" = ${1}`,
44
+ returning: [
45
+ {
46
+ name: "id",
47
+ alias: "a_0",
48
+ path: ["id"],
49
+ },
50
+ {
51
+ name: "name",
52
+ alias: "a_1",
53
+ path: ["name"],
54
+ },
55
+ ],
56
+ });
57
+ });
58
+ test("throws error for nonexistent model", () => {
59
+ expect(() => buildUpdate(db.config, "nonexistent", {
60
+ set: { name: "John" },
61
+ where: { id: 1 },
62
+ })).toThrow('Model "nonexistent" not found');
63
+ });
64
+ test("throws error when where clause is empty", () => {
65
+ expect(() => buildUpdate(db.config, "user", {
66
+ set: { name: "John" },
67
+ where: {},
68
+ })).toThrow("Update queries must have a where clause");
69
+ });
70
+ test("throws error when where clause is undefined", () => {
71
+ expect(() => buildUpdate(db.config, "user", {
72
+ set: { name: "John" },
73
+ // @ts-expect-error testing runtime behavior
74
+ where: undefined,
75
+ })).toThrow("Update queries must have a where clause");
76
+ });
77
+ test("handles nested paths in returning clause", () => {
78
+ const result = buildUpdate(db.config, "user", {
79
+ set: { name: "John" },
80
+ where: { id: 1 },
81
+ returning: ["id", "name"],
82
+ }, ["parent"]);
83
+ expect(result.returning).toEqual([
84
+ {
85
+ name: "id",
86
+ alias: "a_0",
87
+ path: ["parent", "id"],
88
+ },
89
+ {
90
+ name: "name",
91
+ alias: "a_1",
92
+ path: ["parent", "name"],
93
+ },
94
+ ]);
95
+ });
96
+ test("uses provided table index for alias", () => {
97
+ const result = buildUpdate(db.config, "user", {
98
+ set: { name: "John" },
99
+ where: { id: 1 },
100
+ }, [], 5);
101
+ expect(result.table.alias).toBe("f");
102
+ });
103
+ test("handles complex where clauses", () => {
104
+ const result = buildUpdate(db.config, "user", {
105
+ set: { name: "John" },
106
+ where: {
107
+ id: 1,
108
+ name: "test",
109
+ email: { [$like]: "%@example.com" },
110
+ },
111
+ });
112
+ expect(result).toEqual({
113
+ table: {
114
+ schema: "orm",
115
+ name: "user",
116
+ alias: "a",
117
+ model: "user",
118
+ },
119
+ set: [["name", "John"]],
120
+ where: expect.any(SQLStatement),
121
+ returning: [],
122
+ });
123
+ expect(result.where?.pretty).toEqual(unindent `
124
+ "a"."id" = $1
125
+ AND "a"."name" = $2
126
+ AND "a"."email" LIKE $3
127
+ `);
128
+ expect(result.where?.values).toEqual([1, "test", "%@example.com"]);
129
+ });
130
+ });
@@ -0,0 +1,6 @@
1
+ import { NormalizedConfig } from "@casekit/orm2-config";
2
+ import { ModelDefinitions, ModelName, OperatorDefinitions } from "@casekit/orm2-schema";
3
+ import { SQLStatement } from "@casekit/sql";
4
+ import { WhereClause } from "../types/WhereClause.js";
5
+ import { Table } from "./types.js";
6
+ export declare const buildWhere: (config: NormalizedConfig, table: Table, where: WhereClause<ModelDefinitions, OperatorDefinitions, ModelName<ModelDefinitions>>) => SQLStatement;
@@ -0,0 +1,63 @@
1
+ import { sql } from "@casekit/sql";
2
+ import { $and, $not, $or, defaultOperators } from "../operators.js";
3
+ export const buildWhere = (config, table, where) => {
4
+ // NB. Object.entries does not iterate over the symbol keys
5
+ // of an object, so this is a neat way for us to get just the
6
+ // clauses that relate to specific fields, and not logic operators
7
+ // like $and, $or, $not.
8
+ const clauses = Object.entries(where).map(([field, value]) => {
9
+ const tableAlias = table.alias;
10
+ const column = config.models[table.model]?.fields[field]?.column;
11
+ if (!column)
12
+ throw new Error("Unrecognised field: " + field);
13
+ // either null or undefined counts as null, to avoid surprises
14
+ if (value === null || value === undefined) {
15
+ return sql `${sql.ident(tableAlias)}.${sql.ident(column)} IS NULL`;
16
+ }
17
+ if (value === true) {
18
+ return sql `${sql.ident(tableAlias)}.${sql.ident(column)} IS TRUE`;
19
+ }
20
+ if (value === false) {
21
+ return sql `${sql.ident(tableAlias)}.${sql.ident(column)} IS FALSE`;
22
+ }
23
+ // value types
24
+ if (typeof value === "string" ||
25
+ typeof value === "number" ||
26
+ typeof value === "boolean" ||
27
+ value instanceof Date ||
28
+ Object.getOwnPropertySymbols(value).length === 0) {
29
+ return sql `${sql.ident(tableAlias)}.${sql.ident(column)} = ${sql.value(value)}`;
30
+ }
31
+ //operator clauses - more than one can be specified
32
+ const subclauses = Object.getOwnPropertySymbols(value).map((op) => {
33
+ const opValue = value[op];
34
+ const operator = op in config.operators.where
35
+ ? config.operators.where[op]
36
+ : op in defaultOperators
37
+ ? defaultOperators[op]
38
+ : null;
39
+ if (!operator) {
40
+ throw new Error(`Unrecognised query operator or value: ${JSON.stringify(opValue)}`);
41
+ }
42
+ return operator({
43
+ table: sql.ident(tableAlias),
44
+ column: sql.ident(column),
45
+ }, opValue ?? null);
46
+ });
47
+ return sql.join(subclauses, " AND ");
48
+ });
49
+ // logical operators
50
+ if (where[$and]) {
51
+ const subclauses = where[$and].map((clause) => buildWhere(config, table, clause));
52
+ clauses.push(sql `(${sql.join(subclauses, " AND ")})`);
53
+ }
54
+ if (where[$or]) {
55
+ const subclauses = where[$or].map((clause) => buildWhere(config, table, clause));
56
+ clauses.push(sql `(${sql.join(subclauses, " OR ")})`);
57
+ }
58
+ if (where[$not]) {
59
+ const subclause = buildWhere(config, table, where[$not]);
60
+ clauses.push(sql `NOT ${subclause}`);
61
+ }
62
+ return sql.join(clauses, " AND ");
63
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,154 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { $and, $eq, $gt, $gte, $ilike, $in, $is, $like, $lt, $lte, $ne, $not, $or, } from "../operators.js";
3
+ import { orm } from "../orm.js";
4
+ import { buildWhere } from "./buildWhere.js";
5
+ describe("buildWhere", () => {
6
+ const db = orm({
7
+ models: {
8
+ user: {
9
+ fields: {
10
+ id: { column: "id", type: "integer", primaryKey: true },
11
+ name: { column: "name", type: "text" },
12
+ active: { column: "active", type: "boolean" },
13
+ createdAt: { column: "created_at", type: "timestamp" },
14
+ },
15
+ },
16
+ },
17
+ operators: { where: {} },
18
+ });
19
+ const table = {
20
+ schema: "public",
21
+ model: "user",
22
+ alias: "u",
23
+ name: "user",
24
+ };
25
+ test("builds simple equality clause", () => {
26
+ const where = { id: 1 };
27
+ const result = buildWhere(db.config, table, where);
28
+ expect(result.text).toBe('"u"."id" = $1');
29
+ expect(result.values).toEqual([1]);
30
+ });
31
+ test("builds IS NULL clause", () => {
32
+ const where = { name: null };
33
+ const result = buildWhere(db.config, table, where);
34
+ expect(result.text).toBe('"u"."name" IS NULL');
35
+ });
36
+ test("builds boolean clauses", () => {
37
+ const whereTrue = { active: true };
38
+ const whereFalse = { active: false };
39
+ expect(buildWhere(db.config, table, whereTrue).text).toBe('"u"."active" IS TRUE');
40
+ expect(buildWhere(db.config, table, whereFalse).text).toBe('"u"."active" IS FALSE');
41
+ });
42
+ test("builds AND clause", () => {
43
+ const where = { [$and]: [{ id: 1 }, { name: "test" }] };
44
+ const result = buildWhere(db.config, table, where);
45
+ expect(result.text).toBe('("u"."id" = $1 AND "u"."name" = $2)');
46
+ expect(result.values).toEqual([1, "test"]);
47
+ });
48
+ test("builds OR clause", () => {
49
+ const where = { [$or]: [{ id: 1 }, { name: "test" }] };
50
+ const result = buildWhere(db.config, table, where);
51
+ expect(result.text).toBe('("u"."id" = $1 OR "u"."name" = $2)');
52
+ expect(result.values).toEqual([1, "test"]);
53
+ });
54
+ test("builds NOT clause", () => {
55
+ const where = { [$not]: { active: true } };
56
+ const result = buildWhere(db.config, table, where);
57
+ expect(result.text).toBe('NOT "u"."active" IS TRUE');
58
+ });
59
+ test("throws error for unrecognized field", () => {
60
+ const where = { unknownField: 1 };
61
+ expect(() => buildWhere(db.config, table, where)).toThrow("Unrecognised field");
62
+ });
63
+ test("handles Date objects", () => {
64
+ const date = new Date("2023-01-01");
65
+ const where = { createdAt: date };
66
+ const result = buildWhere(db.config, table, where);
67
+ expect(result.text).toBe('"u"."created_at" = $1');
68
+ expect(result.values).toEqual([date]);
69
+ });
70
+ test("builds $eq operator clause", () => {
71
+ const where = { id: { [$eq]: 1 } };
72
+ const result = buildWhere(db.config, table, where);
73
+ expect(result.text).toBe('"u"."id" = $1');
74
+ expect(result.values).toEqual([1]);
75
+ });
76
+ test("builds $gt operator clause", () => {
77
+ const where = { id: { [$gt]: 1 } };
78
+ const result = buildWhere(db.config, table, where);
79
+ expect(result.text).toBe('"u"."id" > $1');
80
+ expect(result.values).toEqual([1]);
81
+ });
82
+ test("builds $gte operator clause", () => {
83
+ const where = { id: { [$gte]: 1 } };
84
+ const result = buildWhere(db.config, table, where);
85
+ expect(result.text).toBe('"u"."id" >= $1');
86
+ expect(result.values).toEqual([1]);
87
+ });
88
+ test("builds $ilike operator clause", () => {
89
+ const where = { name: { [$ilike]: "%test%" } };
90
+ const result = buildWhere(db.config, table, where);
91
+ expect(result.text).toBe('"u"."name" ILIKE $1');
92
+ expect(result.values).toEqual(["%test%"]);
93
+ });
94
+ test("builds $is operator clause", () => {
95
+ const where = { name: { [$is]: null } };
96
+ const result = buildWhere(db.config, table, where);
97
+ expect(result.text).toBe('"u"."name" IS NULL');
98
+ expect(result.values).toEqual([]);
99
+ });
100
+ test("builds $not operator clause", () => {
101
+ const where = { name: { [$not]: null } };
102
+ const result = buildWhere(db.config, table, where);
103
+ expect(result.text).toBe('"u"."name" IS NOT NULL');
104
+ expect(result.values).toEqual([]);
105
+ });
106
+ test("builds $like operator clause", () => {
107
+ const where = { name: { [$like]: "%test%" } };
108
+ const result = buildWhere(db.config, table, where);
109
+ expect(result.text).toBe('"u"."name" LIKE $1');
110
+ expect(result.values).toEqual(["%test%"]);
111
+ });
112
+ test("builds $lt operator clause", () => {
113
+ const where = { id: { [$lt]: 1 } };
114
+ const result = buildWhere(db.config, table, where);
115
+ expect(result.text).toBe('"u"."id" < $1');
116
+ expect(result.values).toEqual([1]);
117
+ });
118
+ test("builds $lte operator clause", () => {
119
+ const where = { id: { [$lte]: 1 } };
120
+ const result = buildWhere(db.config, table, where);
121
+ expect(result.text).toBe('"u"."id" <= $1');
122
+ expect(result.values).toEqual([1]);
123
+ });
124
+ test("builds $ne operator clause", () => {
125
+ const where = { id: { [$ne]: 1 } };
126
+ const result = buildWhere(db.config, table, where);
127
+ expect(result.text).toBe('"u"."id" != $1');
128
+ expect(result.values).toEqual([1]);
129
+ });
130
+ test("builds $not operator clause with null", () => {
131
+ const where = { name: { [$not]: null } };
132
+ const result = buildWhere(db.config, table, where);
133
+ expect(result.text).toBe('"u"."name" IS NOT NULL');
134
+ });
135
+ test("throws error when $not operator receives non-null or boolean value", () => {
136
+ const where = { name: { [$not]: "test" } };
137
+ expect(() => buildWhere(db.config, table, where)).toThrow("Invalid value passed to $not operator");
138
+ });
139
+ test("builds $in operator clause", () => {
140
+ const where = { id: { [$in]: [1, 2, 3] } };
141
+ const result = buildWhere(db.config, table, where);
142
+ expect(result.text).toBe('"u"."id" IN ($1, $2, $3)');
143
+ expect(result.values).toEqual([1, 2, 3]);
144
+ });
145
+ test("throws error when $in operator receives non-array value", () => {
146
+ const where = { id: { [$in]: 1 } };
147
+ expect(() => buildWhere(db.config, table, where)).toThrow("Non-array passed to IN clause");
148
+ });
149
+ test("throws error for unrecognised operator", () => {
150
+ const unknownSymbol = Symbol("unknown");
151
+ const where = { id: { [unknownSymbol]: 1 } };
152
+ expect(() => buildWhere(db.config, table, where)).toThrow("Unrecognised query operator or value");
153
+ });
154
+ });