@simplysm/orm-common 13.0.100 → 14.0.1

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 (238) hide show
  1. package/dist/create-db-context.d.ts +10 -10
  2. package/dist/create-db-context.js +312 -276
  3. package/dist/create-db-context.js.map +1 -6
  4. package/dist/ddl/column-ddl.d.ts +4 -4
  5. package/dist/ddl/column-ddl.js +41 -35
  6. package/dist/ddl/column-ddl.js.map +1 -6
  7. package/dist/ddl/initialize.d.ts +17 -17
  8. package/dist/ddl/initialize.js +200 -142
  9. package/dist/ddl/initialize.js.map +1 -6
  10. package/dist/ddl/relation-ddl.d.ts +6 -6
  11. package/dist/ddl/relation-ddl.js +55 -48
  12. package/dist/ddl/relation-ddl.js.map +1 -6
  13. package/dist/ddl/schema-ddl.d.ts +4 -4
  14. package/dist/ddl/schema-ddl.js +21 -15
  15. package/dist/ddl/schema-ddl.js.map +1 -6
  16. package/dist/ddl/table-ddl.d.ts +20 -20
  17. package/dist/ddl/table-ddl.js +139 -93
  18. package/dist/ddl/table-ddl.js.map +1 -6
  19. package/dist/define-db-context.js +10 -13
  20. package/dist/define-db-context.js.map +1 -6
  21. package/dist/errors/db-transaction-error.d.ts +15 -15
  22. package/dist/errors/db-transaction-error.d.ts.map +1 -1
  23. package/dist/errors/db-transaction-error.js +53 -19
  24. package/dist/errors/db-transaction-error.js.map +1 -6
  25. package/dist/exec/executable.d.ts +23 -23
  26. package/dist/exec/executable.js +94 -40
  27. package/dist/exec/executable.js.map +1 -6
  28. package/dist/exec/queryable.d.ts +97 -97
  29. package/dist/exec/queryable.js +1310 -1204
  30. package/dist/exec/queryable.js.map +1 -6
  31. package/dist/exec/search-parser.d.ts +31 -31
  32. package/dist/exec/search-parser.d.ts.map +1 -1
  33. package/dist/exec/search-parser.js +158 -59
  34. package/dist/exec/search-parser.js.map +1 -6
  35. package/dist/expr/expr-unit.d.ts +4 -4
  36. package/dist/expr/expr-unit.js +24 -18
  37. package/dist/expr/expr-unit.js.map +1 -6
  38. package/dist/expr/expr.d.ts +6 -6
  39. package/dist/expr/expr.js +1872 -1844
  40. package/dist/expr/expr.js.map +1 -6
  41. package/dist/index.js +23 -1
  42. package/dist/index.js.map +1 -6
  43. package/dist/models/system-migration.js +7 -7
  44. package/dist/models/system-migration.js.map +1 -6
  45. package/dist/query-builder/base/expr-renderer-base.d.ts +10 -10
  46. package/dist/query-builder/base/expr-renderer-base.js +27 -21
  47. package/dist/query-builder/base/expr-renderer-base.js.map +1 -6
  48. package/dist/query-builder/base/query-builder-base.d.ts +21 -21
  49. package/dist/query-builder/base/query-builder-base.d.ts.map +1 -1
  50. package/dist/query-builder/base/query-builder-base.js +90 -80
  51. package/dist/query-builder/base/query-builder-base.js.map +1 -6
  52. package/dist/query-builder/mssql/mssql-expr-renderer.d.ts +4 -4
  53. package/dist/query-builder/mssql/mssql-expr-renderer.d.ts.map +1 -1
  54. package/dist/query-builder/mssql/mssql-expr-renderer.js +447 -420
  55. package/dist/query-builder/mssql/mssql-expr-renderer.js.map +1 -6
  56. package/dist/query-builder/mssql/mssql-query-builder.js +483 -443
  57. package/dist/query-builder/mssql/mssql-query-builder.js.map +1 -6
  58. package/dist/query-builder/mysql/mysql-expr-renderer.d.ts +4 -4
  59. package/dist/query-builder/mysql/mysql-expr-renderer.d.ts.map +1 -1
  60. package/dist/query-builder/mysql/mysql-expr-renderer.js +451 -419
  61. package/dist/query-builder/mysql/mysql-expr-renderer.js.map +1 -6
  62. package/dist/query-builder/mysql/mysql-query-builder.js +570 -479
  63. package/dist/query-builder/mysql/mysql-query-builder.js.map +1 -6
  64. package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts +4 -4
  65. package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts.map +1 -1
  66. package/dist/query-builder/postgresql/postgresql-expr-renderer.js +449 -422
  67. package/dist/query-builder/postgresql/postgresql-expr-renderer.js.map +1 -6
  68. package/dist/query-builder/postgresql/postgresql-query-builder.js +511 -460
  69. package/dist/query-builder/postgresql/postgresql-query-builder.js.map +1 -6
  70. package/dist/query-builder/query-builder.d.ts +1 -1
  71. package/dist/query-builder/query-builder.js +13 -13
  72. package/dist/query-builder/query-builder.js.map +1 -6
  73. package/dist/schema/factory/column-builder.d.ts +84 -84
  74. package/dist/schema/factory/column-builder.js +248 -185
  75. package/dist/schema/factory/column-builder.js.map +1 -6
  76. package/dist/schema/factory/index-builder.d.ts +38 -38
  77. package/dist/schema/factory/index-builder.js +144 -85
  78. package/dist/schema/factory/index-builder.js.map +1 -6
  79. package/dist/schema/factory/relation-builder.d.ts +91 -91
  80. package/dist/schema/factory/relation-builder.d.ts.map +1 -1
  81. package/dist/schema/factory/relation-builder.js +274 -136
  82. package/dist/schema/factory/relation-builder.js.map +1 -6
  83. package/dist/schema/procedure-builder.d.ts +51 -51
  84. package/dist/schema/procedure-builder.d.ts.map +1 -1
  85. package/dist/schema/procedure-builder.js +205 -131
  86. package/dist/schema/procedure-builder.js.map +1 -6
  87. package/dist/schema/table-builder.d.ts +55 -55
  88. package/dist/schema/table-builder.d.ts.map +1 -1
  89. package/dist/schema/table-builder.js +274 -205
  90. package/dist/schema/table-builder.js.map +1 -6
  91. package/dist/schema/view-builder.d.ts +44 -44
  92. package/dist/schema/view-builder.d.ts.map +1 -1
  93. package/dist/schema/view-builder.js +189 -116
  94. package/dist/schema/view-builder.js.map +1 -6
  95. package/dist/types/column.js +60 -30
  96. package/dist/types/column.js.map +1 -6
  97. package/dist/types/db-context-def.d.ts +9 -9
  98. package/dist/types/db-context-def.js +2 -1
  99. package/dist/types/db-context-def.js.map +1 -6
  100. package/dist/types/db.d.ts +47 -47
  101. package/dist/types/db.js +15 -5
  102. package/dist/types/db.js.map +1 -6
  103. package/dist/types/expr.d.ts +81 -81
  104. package/dist/types/expr.d.ts.map +1 -1
  105. package/dist/types/expr.js +3 -1
  106. package/dist/types/expr.js.map +1 -6
  107. package/dist/types/query-def.d.ts +46 -46
  108. package/dist/types/query-def.d.ts.map +1 -1
  109. package/dist/types/query-def.js +31 -24
  110. package/dist/types/query-def.js.map +1 -6
  111. package/dist/utils/result-parser.js +362 -221
  112. package/dist/utils/result-parser.js.map +1 -6
  113. package/package.json +5 -7
  114. package/src/create-db-context.ts +31 -31
  115. package/src/ddl/column-ddl.ts +4 -4
  116. package/src/ddl/initialize.ts +38 -38
  117. package/src/ddl/relation-ddl.ts +6 -6
  118. package/src/ddl/schema-ddl.ts +4 -4
  119. package/src/ddl/table-ddl.ts +24 -24
  120. package/src/errors/db-transaction-error.ts +13 -13
  121. package/src/exec/executable.ts +25 -25
  122. package/src/exec/queryable.ts +134 -134
  123. package/src/exec/search-parser.ts +50 -50
  124. package/src/expr/expr-unit.ts +4 -4
  125. package/src/expr/expr.ts +13 -13
  126. package/src/index.ts +8 -8
  127. package/src/models/system-migration.ts +1 -1
  128. package/src/query-builder/base/expr-renderer-base.ts +21 -21
  129. package/src/query-builder/base/query-builder-base.ts +33 -33
  130. package/src/query-builder/mssql/mssql-expr-renderer.ts +11 -11
  131. package/src/query-builder/mssql/mssql-query-builder.ts +11 -11
  132. package/src/query-builder/mysql/mysql-expr-renderer.ts +15 -15
  133. package/src/query-builder/mysql/mysql-query-builder.ts +3 -3
  134. package/src/query-builder/postgresql/postgresql-expr-renderer.ts +9 -9
  135. package/src/query-builder/postgresql/postgresql-query-builder.ts +7 -7
  136. package/src/query-builder/query-builder.ts +1 -1
  137. package/src/schema/factory/column-builder.ts +86 -86
  138. package/src/schema/factory/index-builder.ts +38 -38
  139. package/src/schema/factory/relation-builder.ts +93 -93
  140. package/src/schema/procedure-builder.ts +52 -52
  141. package/src/schema/table-builder.ts +56 -56
  142. package/src/schema/view-builder.ts +45 -45
  143. package/src/types/column.ts +1 -1
  144. package/src/types/db-context-def.ts +15 -15
  145. package/src/types/db.ts +50 -50
  146. package/src/types/expr.ts +103 -103
  147. package/src/types/query-def.ts +50 -50
  148. package/src/utils/result-parser.ts +39 -39
  149. package/README.md +0 -192
  150. package/docs/core.md +0 -234
  151. package/docs/expression.md +0 -234
  152. package/docs/query-builder.md +0 -93
  153. package/docs/queryable.md +0 -198
  154. package/docs/schema-builders.md +0 -463
  155. package/docs/types.md +0 -445
  156. package/docs/utilities.md +0 -27
  157. package/tests/db-context/create-db-context.spec.ts +0 -193
  158. package/tests/db-context/define-db-context.spec.ts +0 -17
  159. package/tests/ddl/basic.expected.ts +0 -341
  160. package/tests/ddl/basic.spec.ts +0 -557
  161. package/tests/ddl/column-builder.expected.ts +0 -310
  162. package/tests/ddl/column-builder.spec.ts +0 -525
  163. package/tests/ddl/index-builder.expected.ts +0 -38
  164. package/tests/ddl/index-builder.spec.ts +0 -148
  165. package/tests/ddl/procedure-builder.expected.ts +0 -52
  166. package/tests/ddl/procedure-builder.spec.ts +0 -128
  167. package/tests/ddl/relation-builder.expected.ts +0 -36
  168. package/tests/ddl/relation-builder.spec.ts +0 -171
  169. package/tests/ddl/table-builder.expected.ts +0 -113
  170. package/tests/ddl/table-builder.spec.ts +0 -399
  171. package/tests/ddl/view-builder.expected.ts +0 -38
  172. package/tests/ddl/view-builder.spec.ts +0 -116
  173. package/tests/dml/delete.expected.ts +0 -96
  174. package/tests/dml/delete.spec.ts +0 -127
  175. package/tests/dml/insert.expected.ts +0 -192
  176. package/tests/dml/insert.spec.ts +0 -210
  177. package/tests/dml/update.expected.ts +0 -176
  178. package/tests/dml/update.spec.ts +0 -222
  179. package/tests/dml/upsert.expected.ts +0 -215
  180. package/tests/dml/upsert.spec.ts +0 -190
  181. package/tests/errors/queryable-errors.spec.ts +0 -126
  182. package/tests/escape.spec.ts +0 -59
  183. package/tests/examples/pivot.expected.ts +0 -211
  184. package/tests/examples/pivot.spec.ts +0 -200
  185. package/tests/examples/sampling.expected.ts +0 -69
  186. package/tests/examples/sampling.spec.ts +0 -42
  187. package/tests/examples/unpivot.expected.ts +0 -120
  188. package/tests/examples/unpivot.spec.ts +0 -161
  189. package/tests/exec/search-parser.spec.ts +0 -267
  190. package/tests/executable/basic.expected.ts +0 -18
  191. package/tests/executable/basic.spec.ts +0 -54
  192. package/tests/expr/comparison.expected.ts +0 -282
  193. package/tests/expr/comparison.spec.ts +0 -334
  194. package/tests/expr/conditional.expected.ts +0 -134
  195. package/tests/expr/conditional.spec.ts +0 -249
  196. package/tests/expr/date.expected.ts +0 -332
  197. package/tests/expr/date.spec.ts +0 -459
  198. package/tests/expr/math.expected.ts +0 -62
  199. package/tests/expr/math.spec.ts +0 -59
  200. package/tests/expr/string.expected.ts +0 -218
  201. package/tests/expr/string.spec.ts +0 -300
  202. package/tests/expr/utility.expected.ts +0 -147
  203. package/tests/expr/utility.spec.ts +0 -155
  204. package/tests/select/basic.expected.ts +0 -322
  205. package/tests/select/basic.spec.ts +0 -433
  206. package/tests/select/filter.expected.ts +0 -357
  207. package/tests/select/filter.spec.ts +0 -954
  208. package/tests/select/group.expected.ts +0 -169
  209. package/tests/select/group.spec.ts +0 -159
  210. package/tests/select/join.expected.ts +0 -582
  211. package/tests/select/join.spec.ts +0 -692
  212. package/tests/select/order.expected.ts +0 -150
  213. package/tests/select/order.spec.ts +0 -140
  214. package/tests/select/recursive-cte.expected.ts +0 -244
  215. package/tests/select/recursive-cte.spec.ts +0 -514
  216. package/tests/select/result-meta.spec.ts +0 -270
  217. package/tests/select/subquery.expected.ts +0 -363
  218. package/tests/select/subquery.spec.ts +0 -441
  219. package/tests/select/view.expected.ts +0 -155
  220. package/tests/select/view.spec.ts +0 -235
  221. package/tests/select/window.expected.ts +0 -345
  222. package/tests/select/window.spec.ts +0 -433
  223. package/tests/setup/MockExecutor.ts +0 -18
  224. package/tests/setup/TestDbContext.ts +0 -59
  225. package/tests/setup/models/Company.ts +0 -13
  226. package/tests/setup/models/Employee.ts +0 -10
  227. package/tests/setup/models/MonthlySales.ts +0 -11
  228. package/tests/setup/models/Post.ts +0 -16
  229. package/tests/setup/models/Sales.ts +0 -10
  230. package/tests/setup/models/User.ts +0 -19
  231. package/tests/setup/procedure/GetAllUsers.ts +0 -9
  232. package/tests/setup/procedure/GetUserById.ts +0 -12
  233. package/tests/setup/test-utils.ts +0 -72
  234. package/tests/setup/views/ActiveUsers.ts +0 -8
  235. package/tests/setup/views/UserSummary.ts +0 -11
  236. package/tests/types/nullable-queryable-record.spec.ts +0 -97
  237. package/tests/utils/result-parser-perf.spec.ts +0 -143
  238. package/tests/utils/result-parser.spec.ts +0 -667
@@ -1,692 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { createTestDb } from "../setup/TestDbContext";
3
- import { Post } from "../setup/models/Post";
4
- import { User } from "../setup/models/User";
5
- import { Company } from "../setup/models/Company";
6
- import { expr } from "../../src/expr/expr";
7
- import { createQueryBuilder } from "../../src/query-builder/query-builder";
8
- import { dialects } from "../setup/test-utils";
9
- import "../setup/test-utils"; // toMatchSql matcher
10
- import * as expected from "./join.expected";
11
-
12
- describe("SELECT - JOIN", () => {
13
- describe("Basic", () => {
14
- const db = createTestDb();
15
- const def = db
16
- .user()
17
- .join("post", (q, c) => q.from(Post).where((item) => [expr.eq(item.userId, c.id)]))
18
- .getSelectQueryDef();
19
-
20
- it("Verify QueryDef", () => {
21
- expect(def).toEqual({
22
- type: "select",
23
- as: "T1",
24
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
25
- select: {
26
- "id": { type: "column", path: ["T1", "id"] },
27
- "name": { type: "column", path: ["T1", "name"] },
28
- "email": { type: "column", path: ["T1", "email"] },
29
- "age": { type: "column", path: ["T1", "age"] },
30
- "isActive": { type: "column", path: ["T1", "isActive"] },
31
- "companyId": { type: "column", path: ["T1", "companyId"] },
32
- "createdAt": { type: "column", path: ["T1", "createdAt"] },
33
- "post.id": { type: "column", path: ["T1.post", "id"] },
34
- "post.userId": { type: "column", path: ["T1.post", "userId"] },
35
- "post.title": { type: "column", path: ["T1.post", "title"] },
36
- "post.content": { type: "column", path: ["T1.post", "content"] },
37
- "post.viewCount": { type: "column", path: ["T1.post", "viewCount"] },
38
- "post.publishedAt": { type: "column", path: ["T1.post", "publishedAt"] },
39
- },
40
- joins: [
41
- {
42
- type: "select",
43
- as: "T1.post",
44
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
45
- isSingle: false,
46
- where: [
47
- {
48
- type: "eq",
49
- source: { type: "column", path: ["T1.post", "userId"] },
50
- target: { type: "column", path: ["T1", "id"] },
51
- },
52
- ],
53
- },
54
- ],
55
- });
56
- });
57
-
58
- it.each(dialects)("[%s] Verify SQL", (dialect) => {
59
- const builder = createQueryBuilder(dialect);
60
- expect(builder.build(def)).toMatchSql(expected.joinBasic[dialect]);
61
- });
62
- });
63
-
64
- describe("joinSingle", () => {
65
- const db = createTestDb();
66
- const def = db
67
- .post()
68
- .joinSingle("user", (q, c) => q.from(User).where((item) => [expr.eq(item.id, c.userId)]))
69
- .getSelectQueryDef();
70
-
71
- it("Verify QueryDef", () => {
72
- expect(def).toEqual({
73
- type: "select",
74
- as: "T1",
75
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
76
- select: {
77
- "id": { type: "column", path: ["T1", "id"] },
78
- "userId": { type: "column", path: ["T1", "userId"] },
79
- "title": { type: "column", path: ["T1", "title"] },
80
- "content": { type: "column", path: ["T1", "content"] },
81
- "viewCount": { type: "column", path: ["T1", "viewCount"] },
82
- "publishedAt": { type: "column", path: ["T1", "publishedAt"] },
83
- "user.id": { type: "column", path: ["T1.user", "id"] },
84
- "user.name": { type: "column", path: ["T1.user", "name"] },
85
- "user.email": { type: "column", path: ["T1.user", "email"] },
86
- "user.age": { type: "column", path: ["T1.user", "age"] },
87
- "user.isActive": { type: "column", path: ["T1.user", "isActive"] },
88
- "user.companyId": { type: "column", path: ["T1.user", "companyId"] },
89
- "user.createdAt": { type: "column", path: ["T1.user", "createdAt"] },
90
- },
91
- joins: [
92
- {
93
- type: "select",
94
- as: "T1.user",
95
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
96
- isSingle: true,
97
- where: [
98
- {
99
- type: "eq",
100
- source: { type: "column", path: ["T1.user", "id"] },
101
- target: { type: "column", path: ["T1", "userId"] },
102
- },
103
- ],
104
- },
105
- ],
106
- });
107
- });
108
-
109
- it.each(dialects)("[%s] Verify SQL", (dialect) => {
110
- const builder = createQueryBuilder(dialect);
111
- expect(builder.build(def)).toMatchSql(expected.joinSingle[dialect]);
112
- });
113
- });
114
-
115
- it("join after select", () => {
116
- const db = createTestDb();
117
- const def = db
118
- .user()
119
- .select((item) => ({ id: item.id, name: item.name }))
120
- .join("post", (q, c) => q.from(Post).where((item) => [expr.eq(item.userId, c.id)]))
121
- .getSelectQueryDef();
122
-
123
- expect(def).toEqual({
124
- type: "select",
125
- as: "T1",
126
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
127
- select: {
128
- "id": { type: "column", path: ["T1", "id"] },
129
- "name": { type: "column", path: ["T1", "name"] },
130
- "post.id": { type: "column", path: ["T1.post", "id"] },
131
- "post.userId": { type: "column", path: ["T1.post", "userId"] },
132
- "post.title": { type: "column", path: ["T1.post", "title"] },
133
- "post.content": { type: "column", path: ["T1.post", "content"] },
134
- "post.viewCount": { type: "column", path: ["T1.post", "viewCount"] },
135
- "post.publishedAt": { type: "column", path: ["T1.post", "publishedAt"] },
136
- },
137
- joins: [
138
- {
139
- type: "select",
140
- as: "T1.post",
141
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
142
- isSingle: false,
143
- where: [
144
- {
145
- type: "eq",
146
- source: { type: "column", path: ["T1.post", "userId"] },
147
- target: { type: "column", path: ["T1", "id"] },
148
- },
149
- ],
150
- },
151
- ],
152
- });
153
- });
154
-
155
- it("Multiple join", () => {
156
- const db = createTestDb();
157
- const def = db
158
- .user()
159
- .join("posts", (q, c) => q.from(Post).where((item) => [expr.eq(item.userId, c.id)]))
160
- .join("company", (q, c) => q.from(Company).where((item) => [expr.eq(item.id, c.companyId)]))
161
- .getSelectQueryDef();
162
-
163
- expect(def).toEqual({
164
- type: "select",
165
- as: "T1",
166
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
167
- select: {
168
- "id": { type: "column", path: ["T1", "id"] },
169
- "name": { type: "column", path: ["T1", "name"] },
170
- "email": { type: "column", path: ["T1", "email"] },
171
- "age": { type: "column", path: ["T1", "age"] },
172
- "isActive": { type: "column", path: ["T1", "isActive"] },
173
- "companyId": { type: "column", path: ["T1", "companyId"] },
174
- "createdAt": { type: "column", path: ["T1", "createdAt"] },
175
- "posts.id": { type: "column", path: ["T1.posts", "id"] },
176
- "posts.userId": { type: "column", path: ["T1.posts", "userId"] },
177
- "posts.title": { type: "column", path: ["T1.posts", "title"] },
178
- "posts.content": { type: "column", path: ["T1.posts", "content"] },
179
- "posts.viewCount": { type: "column", path: ["T1.posts", "viewCount"] },
180
- "posts.publishedAt": { type: "column", path: ["T1.posts", "publishedAt"] },
181
- "company.id": { type: "column", path: ["T1.company", "id"] },
182
- "company.name": { type: "column", path: ["T1.company", "name"] },
183
- "company.foundedAt": { type: "column", path: ["T1.company", "foundedAt"] },
184
- },
185
- joins: [
186
- {
187
- type: "select",
188
- as: "T1.posts",
189
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
190
- isSingle: false,
191
- where: [
192
- {
193
- type: "eq",
194
- source: { type: "column", path: ["T1.posts", "userId"] },
195
- target: { type: "column", path: ["T1", "id"] },
196
- },
197
- ],
198
- },
199
- {
200
- type: "select",
201
- as: "T1.company",
202
- from: { database: "TestDb", schema: "TestSchema", name: "Company" },
203
- isSingle: false,
204
- where: [
205
- {
206
- type: "eq",
207
- source: { type: "column", path: ["T1.company", "id"] },
208
- target: { type: "column", path: ["T1", "companyId"] },
209
- },
210
- ],
211
- },
212
- ],
213
- });
214
- });
215
-
216
- describe("multi-level joinSingle", () => {
217
- const db = createTestDb();
218
- const def = db
219
- .post()
220
- .joinSingle("user", (q, c) =>
221
- q
222
- .from(User)
223
- .joinSingle("company", (q2, c2) =>
224
- q2.from(Company).where((item) => [expr.eq(item.id, c2.companyId)]),
225
- )
226
- .where((item) => [expr.eq(item.id, c.userId)]),
227
- )
228
- .getSelectQueryDef();
229
-
230
- it("Verify QueryDef", () => {
231
- expect(def).toEqual({
232
- type: "select",
233
- as: "T1",
234
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
235
- select: {
236
- "id": { type: "column", path: ["T1", "id"] },
237
- "userId": { type: "column", path: ["T1", "userId"] },
238
- "title": { type: "column", path: ["T1", "title"] },
239
- "content": { type: "column", path: ["T1", "content"] },
240
- "viewCount": { type: "column", path: ["T1", "viewCount"] },
241
- "publishedAt": { type: "column", path: ["T1", "publishedAt"] },
242
- "user.id": { type: "column", path: ["T1.user", "id"] },
243
- "user.name": { type: "column", path: ["T1.user", "name"] },
244
- "user.email": { type: "column", path: ["T1.user", "email"] },
245
- "user.age": { type: "column", path: ["T1.user", "age"] },
246
- "user.isActive": { type: "column", path: ["T1.user", "isActive"] },
247
- "user.companyId": { type: "column", path: ["T1.user", "companyId"] },
248
- "user.createdAt": { type: "column", path: ["T1.user", "createdAt"] },
249
- "user.company.id": { type: "column", path: ["T1.user", "company.id"] },
250
- "user.company.name": { type: "column", path: ["T1.user", "company.name"] },
251
- "user.company.foundedAt": { type: "column", path: ["T1.user", "company.foundedAt"] },
252
- },
253
- joins: [
254
- {
255
- type: "select",
256
- as: "T1.user",
257
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
258
- isSingle: true,
259
- select: {
260
- "id": { type: "column", path: ["T1.user", "id"] },
261
- "name": { type: "column", path: ["T1.user", "name"] },
262
- "email": { type: "column", path: ["T1.user", "email"] },
263
- "age": { type: "column", path: ["T1.user", "age"] },
264
- "isActive": { type: "column", path: ["T1.user", "isActive"] },
265
- "companyId": { type: "column", path: ["T1.user", "companyId"] },
266
- "createdAt": { type: "column", path: ["T1.user", "createdAt"] },
267
- "company.id": { type: "column", path: ["T1.user.company", "id"] },
268
- "company.name": { type: "column", path: ["T1.user.company", "name"] },
269
- "company.foundedAt": { type: "column", path: ["T1.user.company", "foundedAt"] },
270
- },
271
- where: [
272
- {
273
- type: "eq",
274
- source: { type: "column", path: ["T1.user", "id"] },
275
- target: { type: "column", path: ["T1", "userId"] },
276
- },
277
- ],
278
- joins: [
279
- {
280
- type: "select",
281
- as: "T1.user.company",
282
- from: { database: "TestDb", schema: "TestSchema", name: "Company" },
283
- isSingle: true,
284
- where: [
285
- {
286
- type: "eq",
287
- source: { type: "column", path: ["T1.user.company", "id"] },
288
- target: { type: "column", path: ["T1.user", "companyId"] },
289
- },
290
- ],
291
- },
292
- ],
293
- },
294
- ],
295
- });
296
- });
297
-
298
- it.each(dialects)("[%s] Verify SQL", (dialect) => {
299
- const builder = createQueryBuilder(dialect);
300
- expect(builder.build(def)).toMatchSql(expected.joinSingleMultiLevel[dialect]);
301
- });
302
- });
303
-
304
- describe("joinSingle + LATERAL (orderBy + top)", () => {
305
- const db = createTestDb();
306
- const def = db
307
- .user()
308
- .joinSingle("latestPost", (qr, c) =>
309
- qr
310
- .from(Post)
311
- .where((item) => [expr.eq(item.userId, c.id)])
312
- .orderBy((item) => item.publishedAt, "DESC")
313
- .top(1),
314
- )
315
- .getSelectQueryDef();
316
-
317
- it("Verify QueryDef - includes orderBy, top", () => {
318
- const join = def.joins![0];
319
- expect(join.orderBy).toEqual([
320
- [{ type: "column", path: ["T1.latestPost", "publishedAt"] }, "DESC"],
321
- ]);
322
- expect(join.top).toBe(1);
323
- expect(join.isSingle).toBe(true);
324
- });
325
-
326
- it.each(dialects)("[%s] Verify SQL", (dialect) => {
327
- const builder = createQueryBuilder(dialect);
328
- expect(builder.build(def)).toMatchSql(expected.joinSingleLateral[dialect]);
329
- });
330
- });
331
-
332
- describe("joinSingle + LATERAL (select aggregation)", () => {
333
- const db = createTestDb();
334
- const def = db
335
- .user()
336
- .joinSingle("postStats", (qr, c) =>
337
- qr
338
- .from(Post)
339
- .where((item) => [expr.eq(item.userId, c.id)])
340
- .select(() => ({ cnt: expr.count() })),
341
- )
342
- .getSelectQueryDef();
343
-
344
- it("Verify QueryDef - includes select", () => {
345
- const join = def.joins![0];
346
- expect(join.select).toBeDefined();
347
- expect(join.select!["cnt"]).toEqual({ type: "count" });
348
- });
349
-
350
- it.each(dialects)("[%s] Verify SQL", (dialect) => {
351
- const builder = createQueryBuilder(dialect);
352
- expect(builder.build(def)).toMatchSql(expected.joinSingleLateralAgg[dialect]);
353
- });
354
- });
355
-
356
- });
357
-
358
- describe("SELECT - INCLUDE", () => {
359
- it("FK (N:1)", () => {
360
- const db = createTestDb();
361
- const def = db
362
- .post()
363
- .include((item) => item.user)
364
- .getSelectQueryDef();
365
-
366
- expect(def).toEqual({
367
- type: "select",
368
- as: "T1",
369
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
370
- select: {
371
- "id": { type: "column", path: ["T1", "id"] },
372
- "userId": { type: "column", path: ["T1", "userId"] },
373
- "title": { type: "column", path: ["T1", "title"] },
374
- "content": { type: "column", path: ["T1", "content"] },
375
- "viewCount": { type: "column", path: ["T1", "viewCount"] },
376
- "publishedAt": { type: "column", path: ["T1", "publishedAt"] },
377
- "user.id": { type: "column", path: ["T1.user", "id"] },
378
- "user.name": { type: "column", path: ["T1.user", "name"] },
379
- "user.email": { type: "column", path: ["T1.user", "email"] },
380
- "user.age": { type: "column", path: ["T1.user", "age"] },
381
- "user.isActive": { type: "column", path: ["T1.user", "isActive"] },
382
- "user.companyId": { type: "column", path: ["T1.user", "companyId"] },
383
- "user.createdAt": { type: "column", path: ["T1.user", "createdAt"] },
384
- },
385
- joins: [
386
- {
387
- type: "select",
388
- as: "T1.user",
389
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
390
- isSingle: true,
391
- where: [
392
- {
393
- type: "eq",
394
- source: { type: "column", path: ["T1.user", "id"] },
395
- target: { type: "column", path: ["T1", "userId"] },
396
- },
397
- ],
398
- },
399
- ],
400
- });
401
- });
402
-
403
- it("FKT (1:N)", () => {
404
- const db = createTestDb();
405
- const def = db
406
- .user()
407
- .include((item) => item.posts)
408
- .getSelectQueryDef();
409
-
410
- expect(def).toEqual({
411
- type: "select",
412
- as: "T1",
413
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
414
- select: {
415
- "id": { type: "column", path: ["T1", "id"] },
416
- "name": { type: "column", path: ["T1", "name"] },
417
- "email": { type: "column", path: ["T1", "email"] },
418
- "age": { type: "column", path: ["T1", "age"] },
419
- "isActive": { type: "column", path: ["T1", "isActive"] },
420
- "companyId": { type: "column", path: ["T1", "companyId"] },
421
- "createdAt": { type: "column", path: ["T1", "createdAt"] },
422
- "posts.id": { type: "column", path: ["T1.posts", "id"] },
423
- "posts.userId": { type: "column", path: ["T1.posts", "userId"] },
424
- "posts.title": { type: "column", path: ["T1.posts", "title"] },
425
- "posts.content": { type: "column", path: ["T1.posts", "content"] },
426
- "posts.viewCount": { type: "column", path: ["T1.posts", "viewCount"] },
427
- "posts.publishedAt": { type: "column", path: ["T1.posts", "publishedAt"] },
428
- },
429
- joins: [
430
- {
431
- type: "select",
432
- as: "T1.posts",
433
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
434
- isSingle: false,
435
- where: [
436
- {
437
- type: "eq",
438
- source: { type: "column", path: ["T1.posts", "userId"] },
439
- target: { type: "column", path: ["T1", "id"] },
440
- },
441
- ],
442
- },
443
- ],
444
- });
445
- });
446
-
447
- it("Multi-level include (FK -> FK)", () => {
448
- const db = createTestDb();
449
- const def = db
450
- .post()
451
- .include((item) => item.user.company)
452
- .getSelectQueryDef();
453
-
454
- expect(def).toEqual({
455
- type: "select",
456
- as: "T1",
457
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
458
- select: {
459
- "id": { type: "column", path: ["T1", "id"] },
460
- "userId": { type: "column", path: ["T1", "userId"] },
461
- "title": { type: "column", path: ["T1", "title"] },
462
- "content": { type: "column", path: ["T1", "content"] },
463
- "viewCount": { type: "column", path: ["T1", "viewCount"] },
464
- "publishedAt": { type: "column", path: ["T1", "publishedAt"] },
465
- "user.id": { type: "column", path: ["T1.user", "id"] },
466
- "user.name": { type: "column", path: ["T1.user", "name"] },
467
- "user.email": { type: "column", path: ["T1.user", "email"] },
468
- "user.age": { type: "column", path: ["T1.user", "age"] },
469
- "user.isActive": { type: "column", path: ["T1.user", "isActive"] },
470
- "user.companyId": { type: "column", path: ["T1.user", "companyId"] },
471
- "user.createdAt": { type: "column", path: ["T1.user", "createdAt"] },
472
- "user.company.id": { type: "column", path: ["T1.user.company", "id"] },
473
- "user.company.name": { type: "column", path: ["T1.user.company", "name"] },
474
- "user.company.foundedAt": { type: "column", path: ["T1.user.company", "foundedAt"] },
475
- },
476
- joins: [
477
- {
478
- type: "select",
479
- as: "T1.user",
480
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
481
- isSingle: true,
482
- where: [
483
- {
484
- type: "eq",
485
- source: { type: "column", path: ["T1.user", "id"] },
486
- target: { type: "column", path: ["T1", "userId"] },
487
- },
488
- ],
489
- },
490
- {
491
- type: "select",
492
- as: "T1.user.company",
493
- from: { database: "TestDb", schema: "TestSchema", name: "Company" },
494
- isSingle: true,
495
- where: [
496
- {
497
- type: "eq",
498
- source: { type: "column", path: ["T1.user.company", "id"] },
499
- target: { type: "column", path: ["T1.user", "companyId"] },
500
- },
501
- ],
502
- },
503
- ],
504
- });
505
- });
506
-
507
- it("Combination of include + select", () => {
508
- const db = createTestDb();
509
- const def = db
510
- .post()
511
- .include((item) => item.user)
512
- .select((item) => ({
513
- title: item.title,
514
- userName: item.user!.name,
515
- }))
516
- .getSelectQueryDef();
517
-
518
- expect(def).toEqual({
519
- type: "select",
520
- as: "T1",
521
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
522
- select: {
523
- title: { type: "column", path: ["T1", "title"] },
524
- userName: { type: "column", path: ["T1.user", "name"] },
525
- },
526
- joins: [
527
- {
528
- type: "select",
529
- as: "T1.user",
530
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
531
- isSingle: true,
532
- where: [
533
- {
534
- type: "eq",
535
- source: { type: "column", path: ["T1.user", "id"] },
536
- target: { type: "column", path: ["T1", "userId"] },
537
- },
538
- ],
539
- },
540
- ],
541
- });
542
- });
543
-
544
- it("Combination of include + where", () => {
545
- const db = createTestDb();
546
- const def = db
547
- .post()
548
- .include((item) => item.user)
549
- .where((item) => [expr.eq(item.user!.isActive, true)])
550
- .getSelectQueryDef();
551
-
552
- expect(def).toEqual({
553
- type: "select",
554
- as: "T1",
555
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
556
- select: {
557
- "id": { type: "column", path: ["T1", "id"] },
558
- "userId": { type: "column", path: ["T1", "userId"] },
559
- "title": { type: "column", path: ["T1", "title"] },
560
- "content": { type: "column", path: ["T1", "content"] },
561
- "viewCount": { type: "column", path: ["T1", "viewCount"] },
562
- "publishedAt": { type: "column", path: ["T1", "publishedAt"] },
563
- "user.id": { type: "column", path: ["T1.user", "id"] },
564
- "user.name": { type: "column", path: ["T1.user", "name"] },
565
- "user.email": { type: "column", path: ["T1.user", "email"] },
566
- "user.age": { type: "column", path: ["T1.user", "age"] },
567
- "user.isActive": { type: "column", path: ["T1.user", "isActive"] },
568
- "user.companyId": { type: "column", path: ["T1.user", "companyId"] },
569
- "user.createdAt": { type: "column", path: ["T1.user", "createdAt"] },
570
- },
571
- where: [
572
- {
573
- type: "eq",
574
- source: { type: "column", path: ["T1.user", "isActive"] },
575
- target: { type: "value", value: true },
576
- },
577
- ],
578
- joins: [
579
- {
580
- type: "select",
581
- as: "T1.user",
582
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
583
- isSingle: true,
584
- where: [
585
- {
586
- type: "eq",
587
- source: { type: "column", path: ["T1.user", "id"] },
588
- target: { type: "column", path: ["T1", "userId"] },
589
- },
590
- ],
591
- },
592
- ],
593
- });
594
- });
595
-
596
- describe("3 depth include (FK -> FKT -> FK)", () => {
597
- // Post -> user(FK) -> posts(FKT) -> user(FK)
598
- const db = createTestDb();
599
- const def = db
600
- .post()
601
- .include((item) => item.user.posts.user)
602
- .getSelectQueryDef();
603
-
604
- it("Verify QueryDef", () => {
605
- expect(def).toEqual({
606
- type: "select",
607
- as: "T1",
608
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
609
- select: {
610
- "id": { type: "column", path: ["T1", "id"] },
611
- "userId": { type: "column", path: ["T1", "userId"] },
612
- "title": { type: "column", path: ["T1", "title"] },
613
- "content": { type: "column", path: ["T1", "content"] },
614
- "viewCount": { type: "column", path: ["T1", "viewCount"] },
615
- "publishedAt": { type: "column", path: ["T1", "publishedAt"] },
616
- "user.id": { type: "column", path: ["T1.user", "id"] },
617
- "user.name": { type: "column", path: ["T1.user", "name"] },
618
- "user.email": { type: "column", path: ["T1.user", "email"] },
619
- "user.age": { type: "column", path: ["T1.user", "age"] },
620
- "user.isActive": { type: "column", path: ["T1.user", "isActive"] },
621
- "user.companyId": { type: "column", path: ["T1.user", "companyId"] },
622
- "user.createdAt": { type: "column", path: ["T1.user", "createdAt"] },
623
- "user.posts.id": { type: "column", path: ["T1.user.posts", "id"] },
624
- "user.posts.userId": { type: "column", path: ["T1.user.posts", "userId"] },
625
- "user.posts.title": { type: "column", path: ["T1.user.posts", "title"] },
626
- "user.posts.content": { type: "column", path: ["T1.user.posts", "content"] },
627
- "user.posts.viewCount": { type: "column", path: ["T1.user.posts", "viewCount"] },
628
- "user.posts.publishedAt": { type: "column", path: ["T1.user.posts", "publishedAt"] },
629
- "user.posts.user.id": { type: "column", path: ["T1.user.posts.user", "id"] },
630
- "user.posts.user.name": { type: "column", path: ["T1.user.posts.user", "name"] },
631
- "user.posts.user.email": { type: "column", path: ["T1.user.posts.user", "email"] },
632
- "user.posts.user.age": { type: "column", path: ["T1.user.posts.user", "age"] },
633
- "user.posts.user.isActive": { type: "column", path: ["T1.user.posts.user", "isActive"] },
634
- "user.posts.user.companyId": {
635
- type: "column",
636
- path: ["T1.user.posts.user", "companyId"],
637
- },
638
- "user.posts.user.createdAt": {
639
- type: "column",
640
- path: ["T1.user.posts.user", "createdAt"],
641
- },
642
- },
643
- joins: [
644
- {
645
- type: "select",
646
- as: "T1.user",
647
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
648
- isSingle: true,
649
- where: [
650
- {
651
- type: "eq",
652
- source: { type: "column", path: ["T1.user", "id"] },
653
- target: { type: "column", path: ["T1", "userId"] },
654
- },
655
- ],
656
- },
657
- {
658
- type: "select",
659
- as: "T1.user.posts",
660
- from: { database: "TestDb", schema: "TestSchema", name: "Post" },
661
- isSingle: false,
662
- where: [
663
- {
664
- type: "eq",
665
- source: { type: "column", path: ["T1.user.posts", "userId"] },
666
- target: { type: "column", path: ["T1.user", "id"] },
667
- },
668
- ],
669
- },
670
- {
671
- type: "select",
672
- as: "T1.user.posts.user",
673
- from: { database: "TestDb", schema: "TestSchema", name: "User" },
674
- isSingle: true,
675
- where: [
676
- {
677
- type: "eq",
678
- source: { type: "column", path: ["T1.user.posts.user", "id"] },
679
- target: { type: "column", path: ["T1.user.posts", "userId"] },
680
- },
681
- ],
682
- },
683
- ],
684
- });
685
- });
686
-
687
- it.each(dialects)("[%s] Verify SQL", (dialect) => {
688
- const builder = createQueryBuilder(dialect);
689
- expect(builder.build(def)).toMatchSql(expected.include3Depth[dialect]);
690
- });
691
- });
692
- });