@strapi/database 4.15.0-alpha.0 → 4.15.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 (242) hide show
  1. package/README.md +3 -0
  2. package/dist/connection.d.ts +3 -0
  3. package/dist/connection.d.ts.map +1 -0
  4. package/dist/dialects/dialect.d.ts +27 -0
  5. package/dist/dialects/dialect.d.ts.map +1 -0
  6. package/dist/dialects/index.d.ts +5 -0
  7. package/dist/dialects/index.d.ts.map +1 -0
  8. package/dist/dialects/mysql/constants.d.ts +3 -0
  9. package/dist/dialects/mysql/constants.d.ts.map +1 -0
  10. package/dist/dialects/mysql/database-inspector.d.ts +12 -0
  11. package/dist/dialects/mysql/database-inspector.d.ts.map +1 -0
  12. package/dist/dialects/mysql/index.d.ts +20 -0
  13. package/dist/dialects/mysql/index.d.ts.map +1 -0
  14. package/dist/dialects/mysql/schema-inspector.d.ts +13 -0
  15. package/dist/dialects/mysql/schema-inspector.d.ts.map +1 -0
  16. package/dist/dialects/postgresql/index.d.ts +14 -0
  17. package/dist/dialects/postgresql/index.d.ts.map +1 -0
  18. package/dist/dialects/postgresql/schema-inspector.d.ts +14 -0
  19. package/dist/dialects/postgresql/schema-inspector.d.ts.map +1 -0
  20. package/dist/dialects/sqlite/index.d.ts +19 -0
  21. package/dist/dialects/sqlite/index.d.ts.map +1 -0
  22. package/dist/dialects/sqlite/schema-inspector.d.ts +13 -0
  23. package/dist/dialects/sqlite/schema-inspector.d.ts.map +1 -0
  24. package/dist/entity-manager/entity-repository.d.ts +4 -0
  25. package/dist/entity-manager/entity-repository.d.ts.map +1 -0
  26. package/dist/entity-manager/index.d.ts +5 -0
  27. package/dist/entity-manager/index.d.ts.map +1 -0
  28. package/dist/entity-manager/morph-relations.d.ts +13 -0
  29. package/dist/entity-manager/morph-relations.d.ts.map +1 -0
  30. package/dist/entity-manager/regular-relations.d.ts +83 -0
  31. package/dist/entity-manager/regular-relations.d.ts.map +1 -0
  32. package/dist/entity-manager/relations/cloning/regular-relations.d.ts +17 -0
  33. package/dist/entity-manager/relations/cloning/regular-relations.d.ts.map +1 -0
  34. package/dist/entity-manager/relations-orderer.d.ts +73 -0
  35. package/dist/entity-manager/relations-orderer.d.ts.map +1 -0
  36. package/dist/entity-manager/types.d.ts +97 -0
  37. package/dist/entity-manager/types.d.ts.map +1 -0
  38. package/dist/errors/database.d.ts +5 -0
  39. package/dist/errors/database.d.ts.map +1 -0
  40. package/dist/errors/index.d.ts +8 -0
  41. package/dist/errors/index.d.ts.map +1 -0
  42. package/dist/errors/invalid-date.d.ts +5 -0
  43. package/dist/errors/invalid-date.d.ts.map +1 -0
  44. package/dist/errors/invalid-datetime.d.ts +5 -0
  45. package/dist/errors/invalid-datetime.d.ts.map +1 -0
  46. package/dist/errors/invalid-relation.d.ts +5 -0
  47. package/dist/errors/invalid-relation.d.ts.map +1 -0
  48. package/dist/errors/invalid-time.d.ts +5 -0
  49. package/dist/errors/invalid-time.d.ts.map +1 -0
  50. package/dist/errors/not-null.d.ts +7 -0
  51. package/dist/errors/not-null.d.ts.map +1 -0
  52. package/dist/fields/biginteger.d.ts +4 -0
  53. package/dist/fields/biginteger.d.ts.map +1 -0
  54. package/dist/fields/boolean.d.ts +6 -0
  55. package/dist/fields/boolean.d.ts.map +1 -0
  56. package/dist/fields/date.d.ts +6 -0
  57. package/dist/fields/date.d.ts.map +1 -0
  58. package/dist/fields/datetime.d.ts +6 -0
  59. package/dist/fields/datetime.d.ts.map +1 -0
  60. package/dist/fields/field.d.ts +7 -0
  61. package/dist/fields/field.d.ts.map +1 -0
  62. package/dist/fields/index.d.ts +4 -0
  63. package/dist/fields/index.d.ts.map +1 -0
  64. package/dist/fields/json.d.ts +6 -0
  65. package/dist/fields/json.d.ts.map +1 -0
  66. package/dist/fields/number.d.ts +6 -0
  67. package/dist/fields/number.d.ts.map +1 -0
  68. package/dist/fields/shared/parsers.d.ts +4 -0
  69. package/dist/fields/shared/parsers.d.ts.map +1 -0
  70. package/dist/fields/string.d.ts +6 -0
  71. package/dist/fields/string.d.ts.map +1 -0
  72. package/dist/fields/time.d.ts +6 -0
  73. package/dist/fields/time.d.ts.map +1 -0
  74. package/dist/fields/timestamp.d.ts +6 -0
  75. package/dist/fields/timestamp.d.ts.map +1 -0
  76. package/dist/index.d.ts +46 -0
  77. package/dist/index.d.ts.map +1 -0
  78. package/dist/index.js +6189 -0
  79. package/dist/index.js.map +1 -0
  80. package/dist/index.mjs +6157 -0
  81. package/dist/index.mjs.map +1 -0
  82. package/dist/lifecycles/index.d.ts +17 -0
  83. package/dist/lifecycles/index.d.ts.map +1 -0
  84. package/dist/lifecycles/subscribers/index.d.ts +5 -0
  85. package/dist/lifecycles/subscribers/index.d.ts.map +1 -0
  86. package/dist/lifecycles/subscribers/models-lifecycles.d.ts +6 -0
  87. package/dist/lifecycles/subscribers/models-lifecycles.d.ts.map +1 -0
  88. package/dist/lifecycles/subscribers/timestamps.d.ts +3 -0
  89. package/dist/lifecycles/subscribers/timestamps.d.ts.map +1 -0
  90. package/dist/lifecycles/types.d.ts +25 -0
  91. package/dist/lifecycles/types.d.ts.map +1 -0
  92. package/dist/metadata/index.d.ts +10 -0
  93. package/dist/metadata/index.d.ts.map +1 -0
  94. package/dist/metadata/metadata.d.ts +22 -0
  95. package/dist/metadata/metadata.d.ts.map +1 -0
  96. package/dist/metadata/relations.d.ts +16 -0
  97. package/dist/metadata/relations.d.ts.map +1 -0
  98. package/dist/migrations/index.d.ts +12 -0
  99. package/dist/migrations/index.d.ts.map +1 -0
  100. package/dist/migrations/storage.d.ts +15 -0
  101. package/dist/migrations/storage.d.ts.map +1 -0
  102. package/dist/query/helpers/index.d.ts +8 -0
  103. package/dist/query/helpers/index.d.ts.map +1 -0
  104. package/dist/query/helpers/join.d.ts +30 -0
  105. package/dist/query/helpers/join.d.ts.map +1 -0
  106. package/dist/query/helpers/order-by.d.ts +14 -0
  107. package/dist/query/helpers/order-by.d.ts.map +1 -0
  108. package/dist/query/helpers/populate/apply.d.ts +11 -0
  109. package/dist/query/helpers/populate/apply.d.ts.map +1 -0
  110. package/dist/query/helpers/populate/index.d.ts +4 -0
  111. package/dist/query/helpers/populate/index.d.ts.map +1 -0
  112. package/dist/query/helpers/populate/process.d.ts +24 -0
  113. package/dist/query/helpers/populate/process.d.ts.map +1 -0
  114. package/dist/query/helpers/search.d.ts +4 -0
  115. package/dist/query/helpers/search.d.ts.map +1 -0
  116. package/dist/query/helpers/streams/index.d.ts +2 -0
  117. package/dist/query/helpers/streams/index.d.ts.map +1 -0
  118. package/dist/query/helpers/streams/readable.d.ts +39 -0
  119. package/dist/query/helpers/streams/readable.d.ts.map +1 -0
  120. package/dist/query/helpers/transform.d.ts +8 -0
  121. package/dist/query/helpers/transform.d.ts.map +1 -0
  122. package/dist/query/helpers/where.d.ts +19 -0
  123. package/dist/query/helpers/where.d.ts.map +1 -0
  124. package/dist/query/index.d.ts +3 -0
  125. package/dist/query/index.d.ts.map +1 -0
  126. package/dist/query/query-builder.d.ts +79 -0
  127. package/dist/query/query-builder.d.ts.map +1 -0
  128. package/dist/query/types.d.ts +8 -0
  129. package/dist/query/types.d.ts.map +1 -0
  130. package/dist/schema/builder.d.ts +33 -0
  131. package/dist/schema/builder.d.ts.map +1 -0
  132. package/dist/schema/diff.d.ts +7 -0
  133. package/dist/schema/diff.d.ts.map +1 -0
  134. package/dist/schema/index.d.ts +20 -0
  135. package/dist/schema/index.d.ts.map +1 -0
  136. package/dist/schema/schema.d.ts +4 -0
  137. package/dist/schema/schema.d.ts.map +1 -0
  138. package/dist/schema/storage.d.ts +10 -0
  139. package/dist/schema/storage.d.ts.map +1 -0
  140. package/dist/schema/types.d.ts +103 -0
  141. package/dist/schema/types.d.ts.map +1 -0
  142. package/dist/transaction-context.d.ts +22 -0
  143. package/dist/transaction-context.d.ts.map +1 -0
  144. package/dist/types/index.d.ts +169 -0
  145. package/dist/types/index.d.ts.map +1 -0
  146. package/dist/utils/content-types.d.ts +13 -0
  147. package/dist/utils/content-types.d.ts.map +1 -0
  148. package/dist/utils/knex.d.ts +12 -0
  149. package/dist/utils/knex.d.ts.map +1 -0
  150. package/dist/utils/types.d.ts +10 -0
  151. package/dist/utils/types.d.ts.map +1 -0
  152. package/dist/validations/index.d.ts +6 -0
  153. package/dist/validations/index.d.ts.map +1 -0
  154. package/dist/validations/relations/bidirectional.d.ts +12 -0
  155. package/dist/validations/relations/bidirectional.d.ts.map +1 -0
  156. package/dist/validations/relations/index.d.ts +7 -0
  157. package/dist/validations/relations/index.d.ts.map +1 -0
  158. package/package.json +21 -9
  159. package/.eslintignore +0 -2
  160. package/.eslintrc.js +0 -4
  161. package/jest.config.js +0 -6
  162. package/lib/__tests__/index.test.js +0 -93
  163. package/lib/__tests__/lifecycles.test.js +0 -55
  164. package/lib/connection.js +0 -64
  165. package/lib/dialects/dialect.js +0 -63
  166. package/lib/dialects/index.js +0 -53
  167. package/lib/dialects/mysql/constants.js +0 -6
  168. package/lib/dialects/mysql/database-inspector.js +0 -37
  169. package/lib/dialects/mysql/index.js +0 -92
  170. package/lib/dialects/mysql/schema-inspector.js +0 -234
  171. package/lib/dialects/postgresql/index.js +0 -65
  172. package/lib/dialects/postgresql/schema-inspector.js +0 -283
  173. package/lib/dialects/sqlite/index.js +0 -87
  174. package/lib/dialects/sqlite/schema-inspector.js +0 -151
  175. package/lib/entity-manager/__tests__/relations-orderer.test.js +0 -186
  176. package/lib/entity-manager/__tests__/sort-connect-array.test.js +0 -79
  177. package/lib/entity-manager/entity-repository.js +0 -164
  178. package/lib/entity-manager/index.js +0 -1385
  179. package/lib/entity-manager/morph-relations.js +0 -63
  180. package/lib/entity-manager/regular-relations.js +0 -506
  181. package/lib/entity-manager/relations/cloning/regular-relations.js +0 -76
  182. package/lib/entity-manager/relations-orderer.js +0 -225
  183. package/lib/errors/database.js +0 -12
  184. package/lib/errors/index.js +0 -17
  185. package/lib/errors/invalid-date.js +0 -14
  186. package/lib/errors/invalid-datetime.js +0 -14
  187. package/lib/errors/invalid-relation.js +0 -14
  188. package/lib/errors/invalid-time.js +0 -14
  189. package/lib/errors/not-null.js +0 -15
  190. package/lib/fields/biginteger.js +0 -17
  191. package/lib/fields/boolean.js +0 -39
  192. package/lib/fields/date.js +0 -16
  193. package/lib/fields/datetime.js +0 -19
  194. package/lib/fields/field.js +0 -17
  195. package/lib/fields/index.d.ts +0 -9
  196. package/lib/fields/index.js +0 -50
  197. package/lib/fields/json.js +0 -21
  198. package/lib/fields/number.js +0 -23
  199. package/lib/fields/shared/parsers.js +0 -71
  200. package/lib/fields/string.js +0 -17
  201. package/lib/fields/time.js +0 -17
  202. package/lib/fields/timestamp.js +0 -19
  203. package/lib/index.d.ts +0 -198
  204. package/lib/index.js +0 -129
  205. package/lib/lifecycles/index.d.ts +0 -51
  206. package/lib/lifecycles/index.js +0 -90
  207. package/lib/lifecycles/subscribers/index.d.ts +0 -11
  208. package/lib/lifecycles/subscribers/models-lifecycles.js +0 -19
  209. package/lib/lifecycles/subscribers/timestamps.js +0 -65
  210. package/lib/metadata/index.js +0 -244
  211. package/lib/metadata/relations.js +0 -578
  212. package/lib/migrations/index.d.ts +0 -9
  213. package/lib/migrations/index.js +0 -75
  214. package/lib/migrations/storage.js +0 -44
  215. package/lib/query/helpers/index.js +0 -11
  216. package/lib/query/helpers/join.js +0 -96
  217. package/lib/query/helpers/order-by.js +0 -70
  218. package/lib/query/helpers/populate/apply.js +0 -664
  219. package/lib/query/helpers/populate/index.js +0 -9
  220. package/lib/query/helpers/populate/process.js +0 -102
  221. package/lib/query/helpers/search.js +0 -84
  222. package/lib/query/helpers/streams/index.js +0 -5
  223. package/lib/query/helpers/streams/readable.js +0 -174
  224. package/lib/query/helpers/transform.js +0 -84
  225. package/lib/query/helpers/where.js +0 -365
  226. package/lib/query/index.js +0 -7
  227. package/lib/query/query-builder.js +0 -514
  228. package/lib/schema/__tests__/schema-diff.test.js +0 -231
  229. package/lib/schema/builder.js +0 -386
  230. package/lib/schema/diff.js +0 -399
  231. package/lib/schema/index.d.ts +0 -49
  232. package/lib/schema/index.js +0 -94
  233. package/lib/schema/schema.js +0 -202
  234. package/lib/schema/storage.js +0 -76
  235. package/lib/transaction-context.js +0 -68
  236. package/lib/types/index.d.ts +0 -6
  237. package/lib/types/index.js +0 -35
  238. package/lib/utils/content-types.js +0 -40
  239. package/lib/utils/knex.js +0 -22
  240. package/lib/validations/index.js +0 -20
  241. package/lib/validations/relations/bidirectional.js +0 -89
  242. package/lib/validations/relations/index.js +0 -14
@@ -1,514 +0,0 @@
1
- 'use strict';
2
-
3
- const _ = require('lodash/fp');
4
-
5
- const { DatabaseError } = require('../errors');
6
- const helpers = require('./helpers');
7
- const transactionCtx = require('../transaction-context');
8
-
9
- const createQueryBuilder = (uid, db, initialState = {}) => {
10
- const meta = db.metadata.get(uid);
11
- const { tableName } = meta;
12
-
13
- const state = _.defaults(
14
- {
15
- type: 'select',
16
- select: [],
17
- count: null,
18
- max: null,
19
- first: false,
20
- data: null,
21
- where: [],
22
- joins: [],
23
- populate: null,
24
- limit: null,
25
- offset: null,
26
- transaction: null,
27
- forUpdate: false,
28
- onConflict: null,
29
- merge: null,
30
- ignore: false,
31
- orderBy: [],
32
- groupBy: [],
33
- increments: [],
34
- decrements: [],
35
- aliasCounter: 0,
36
- },
37
- initialState
38
- );
39
-
40
- const getAlias = () => {
41
- const alias = `t${state.aliasCounter}`;
42
-
43
- state.aliasCounter += 1;
44
-
45
- return alias;
46
- };
47
-
48
- return {
49
- alias: getAlias(),
50
- getAlias,
51
- state,
52
-
53
- clone() {
54
- return createQueryBuilder(uid, db, state);
55
- },
56
-
57
- select(args) {
58
- state.type = 'select';
59
- state.select = _.uniq(_.castArray(args));
60
-
61
- return this;
62
- },
63
-
64
- addSelect(args) {
65
- state.select = _.uniq([...state.select, ..._.castArray(args)]);
66
-
67
- return this;
68
- },
69
-
70
- insert(data) {
71
- state.type = 'insert';
72
- state.data = data;
73
-
74
- return this;
75
- },
76
-
77
- onConflict(args) {
78
- state.onConflict = args;
79
-
80
- return this;
81
- },
82
-
83
- merge(args) {
84
- state.merge = args;
85
-
86
- return this;
87
- },
88
-
89
- ignore() {
90
- state.ignore = true;
91
-
92
- return this;
93
- },
94
-
95
- delete() {
96
- state.type = 'delete';
97
-
98
- return this;
99
- },
100
-
101
- ref(name) {
102
- return db.connection.ref(helpers.toColumnName(meta, name));
103
- },
104
-
105
- update(data) {
106
- state.type = 'update';
107
- state.data = data;
108
-
109
- return this;
110
- },
111
-
112
- increment(column, amount = 1) {
113
- state.type = 'update';
114
- state.increments.push({ column, amount });
115
-
116
- return this;
117
- },
118
-
119
- decrement(column, amount = 1) {
120
- state.type = 'update';
121
- state.decrements.push({ column, amount });
122
-
123
- return this;
124
- },
125
-
126
- count(count = 'id') {
127
- state.type = 'count';
128
- state.count = count;
129
-
130
- return this;
131
- },
132
-
133
- max(column) {
134
- state.type = 'max';
135
- state.max = column;
136
-
137
- return this;
138
- },
139
-
140
- where(where = {}) {
141
- if (!_.isPlainObject(where)) {
142
- throw new Error('Where must be an object');
143
- }
144
-
145
- state.where.push(where);
146
-
147
- return this;
148
- },
149
-
150
- limit(limit) {
151
- state.limit = limit;
152
- return this;
153
- },
154
-
155
- offset(offset) {
156
- state.offset = offset;
157
- return this;
158
- },
159
-
160
- orderBy(orderBy) {
161
- state.orderBy = orderBy;
162
- return this;
163
- },
164
-
165
- groupBy(groupBy) {
166
- state.groupBy = groupBy;
167
- return this;
168
- },
169
-
170
- populate(populate) {
171
- state.populate = populate;
172
- return this;
173
- },
174
-
175
- search(query) {
176
- state.search = query;
177
- return this;
178
- },
179
-
180
- transacting(transaction) {
181
- state.transaction = transaction;
182
- return this;
183
- },
184
-
185
- forUpdate() {
186
- state.forUpdate = true;
187
- return this;
188
- },
189
-
190
- init(params = {}) {
191
- const { _q, filters, where, select, limit, offset, orderBy, groupBy, populate } = params;
192
-
193
- if (!_.isNil(where)) {
194
- this.where(where);
195
- }
196
-
197
- if (!_.isNil(_q)) {
198
- this.search(_q);
199
- }
200
-
201
- if (!_.isNil(select)) {
202
- this.select(select);
203
- } else {
204
- this.select('*');
205
- }
206
-
207
- if (!_.isNil(limit)) {
208
- this.limit(limit);
209
- }
210
-
211
- if (!_.isNil(offset)) {
212
- this.offset(offset);
213
- }
214
-
215
- if (!_.isNil(orderBy)) {
216
- this.orderBy(orderBy);
217
- }
218
-
219
- if (!_.isNil(groupBy)) {
220
- this.groupBy(groupBy);
221
- }
222
-
223
- if (!_.isNil(populate)) {
224
- this.populate(populate);
225
- }
226
-
227
- if (!_.isNil(filters)) {
228
- this.filters(filters);
229
- }
230
-
231
- return this;
232
- },
233
-
234
- filters(filters) {
235
- state.filters = filters;
236
- },
237
-
238
- first() {
239
- state.first = true;
240
- return this;
241
- },
242
-
243
- join(join) {
244
- if (!join.targetField) {
245
- state.joins.push(join);
246
- return this;
247
- }
248
-
249
- const model = db.metadata.get(uid);
250
- const attribute = model.attributes[join.targetField];
251
-
252
- helpers.createJoin(
253
- { db, qb: this },
254
- {
255
- alias: this.alias,
256
- refAlias: join.alias,
257
- attributeName: join.targetField,
258
- attribute,
259
- }
260
- );
261
-
262
- return this;
263
- },
264
-
265
- mustUseAlias() {
266
- return ['select', 'count'].includes(state.type);
267
- },
268
-
269
- aliasColumn(key, alias) {
270
- if (typeof key !== 'string') {
271
- return key;
272
- }
273
-
274
- if (key.indexOf('.') >= 0) {
275
- return key;
276
- }
277
-
278
- if (!_.isNil(alias)) {
279
- return `${alias}.${key}`;
280
- }
281
-
282
- return this.mustUseAlias() ? `${this.alias}.${key}` : key;
283
- },
284
-
285
- raw(...args) {
286
- return db.connection.raw(...args);
287
- },
288
-
289
- shouldUseSubQuery() {
290
- return ['delete', 'update'].includes(state.type) && state.joins.length > 0;
291
- },
292
-
293
- runSubQuery() {
294
- this.select('id');
295
- const subQB = this.getKnexQuery();
296
-
297
- const nestedSubQuery = db.getConnection().select('id').from(subQB.as('subQuery'));
298
-
299
- return db.getConnection(tableName)[state.type]().whereIn('id', nestedSubQuery);
300
- },
301
-
302
- processState() {
303
- state.orderBy = helpers.processOrderBy(state.orderBy, { qb: this, uid, db });
304
-
305
- if (!_.isNil(state.filters)) {
306
- if (_.isFunction(state.filters)) {
307
- const filters = state.filters({ qb: this, uid, meta, db });
308
-
309
- if (!_.isNil(filters)) {
310
- state.where.push(filters);
311
- }
312
- } else {
313
- state.where.push(state.filters);
314
- }
315
- }
316
-
317
- state.where = helpers.processWhere(state.where, { qb: this, uid, db });
318
- state.populate = helpers.processPopulate(state.populate, { qb: this, uid, db });
319
- state.data = helpers.toRow(meta, state.data);
320
-
321
- this.processSelect();
322
- },
323
-
324
- shouldUseDistinct() {
325
- return state.joins.length > 0 && _.isEmpty(state.groupBy);
326
- },
327
-
328
- processSelect() {
329
- state.select = state.select.map((field) => helpers.toColumnName(meta, field));
330
-
331
- if (this.shouldUseDistinct()) {
332
- const joinsOrderByColumns = state.joins.flatMap((join) => {
333
- return _.keys(join.orderBy).map((key) => this.aliasColumn(key, join.alias));
334
- });
335
- const orderByColumns = state.orderBy.map(({ column }) => column);
336
-
337
- state.select = _.uniq([...joinsOrderByColumns, ...orderByColumns, ...state.select]);
338
- }
339
- },
340
-
341
- getKnexQuery() {
342
- if (!state.type) {
343
- this.select('*');
344
- }
345
-
346
- const aliasedTableName = this.mustUseAlias() ? `${tableName} as ${this.alias}` : tableName;
347
-
348
- const qb = db.getConnection(aliasedTableName);
349
-
350
- if (this.shouldUseSubQuery()) {
351
- return this.runSubQuery();
352
- }
353
-
354
- this.processState();
355
-
356
- switch (state.type) {
357
- case 'select': {
358
- qb.select(state.select.map((column) => this.aliasColumn(column)));
359
-
360
- if (this.shouldUseDistinct()) {
361
- qb.distinct();
362
- }
363
-
364
- break;
365
- }
366
- case 'count': {
367
- const dbColumnName = this.aliasColumn(helpers.toColumnName(meta, state.count));
368
-
369
- if (this.shouldUseDistinct()) {
370
- qb.countDistinct({ count: dbColumnName });
371
- } else {
372
- qb.count({ count: dbColumnName });
373
- }
374
- break;
375
- }
376
- case 'max': {
377
- const dbColumnName = this.aliasColumn(helpers.toColumnName(meta, state.max));
378
- qb.max({ max: dbColumnName });
379
- break;
380
- }
381
- case 'insert': {
382
- qb.insert(state.data);
383
-
384
- if (db.dialect.useReturning() && _.has('id', meta.attributes)) {
385
- qb.returning('id');
386
- }
387
-
388
- break;
389
- }
390
- case 'update': {
391
- if (state.data) {
392
- qb.update(state.data);
393
- }
394
- break;
395
- }
396
- case 'delete': {
397
- qb.delete();
398
-
399
- break;
400
- }
401
- case 'truncate': {
402
- db.truncate();
403
- break;
404
- }
405
- default: {
406
- throw new Error('Unknown query type');
407
- }
408
- }
409
-
410
- if (state.transaction) {
411
- qb.transacting(state.transaction);
412
- }
413
-
414
- if (state.forUpdate) {
415
- qb.forUpdate();
416
- }
417
-
418
- if (!_.isEmpty(state.increments)) {
419
- state.increments.forEach((incr) => qb.increment(incr.column, incr.amount));
420
- }
421
-
422
- if (!_.isEmpty(state.decrements)) {
423
- state.decrements.forEach((decr) => qb.decrement(decr.column, decr.amount));
424
- }
425
-
426
- if (state.onConflict) {
427
- if (state.merge) {
428
- qb.onConflict(state.onConflict).merge(state.merge);
429
- } else if (state.ignore) {
430
- qb.onConflict(state.onConflict).ignore();
431
- }
432
- }
433
-
434
- if (state.limit) {
435
- qb.limit(state.limit);
436
- }
437
-
438
- if (state.offset) {
439
- qb.offset(state.offset);
440
- }
441
-
442
- if (state.orderBy.length > 0) {
443
- qb.orderBy(state.orderBy);
444
- }
445
-
446
- if (state.first) {
447
- qb.first();
448
- }
449
-
450
- if (state.groupBy.length > 0) {
451
- qb.groupBy(state.groupBy);
452
- }
453
-
454
- // if there are joins and it is a delete or update use a sub query
455
- if (state.where) {
456
- helpers.applyWhere(qb, state.where);
457
- }
458
-
459
- // if there are joins and it is a delete or update use a sub query
460
- if (state.search) {
461
- qb.where((subQb) => {
462
- helpers.applySearch(subQb, state.search, { qb: this, db, uid });
463
- });
464
- }
465
-
466
- if (state.joins.length > 0) {
467
- helpers.applyJoins(qb, state.joins);
468
- }
469
-
470
- return qb;
471
- },
472
-
473
- async execute({ mapResults = true } = {}) {
474
- try {
475
- const qb = this.getKnexQuery();
476
-
477
- if (transactionCtx.get()) {
478
- qb.transacting(transactionCtx.get());
479
- }
480
-
481
- const rows = await qb;
482
-
483
- if (state.populate && !_.isNil(rows)) {
484
- await helpers.applyPopulate(_.castArray(rows), state.populate, {
485
- qb: this,
486
- uid,
487
- db,
488
- });
489
- }
490
-
491
- let results = rows;
492
- if (mapResults && state.type === 'select') {
493
- results = helpers.fromRow(meta, rows);
494
- }
495
-
496
- return results;
497
- } catch (error) {
498
- db.dialect.transformErrors(error);
499
- }
500
- },
501
-
502
- stream({ mapResults = true } = {}) {
503
- if (state.type === 'select') {
504
- return new helpers.ReadableQuery({ qb: this, db, uid, mapResults });
505
- }
506
-
507
- throw new DatabaseError(
508
- `query-builder.stream() has been called with an unsupported query type: "${state.type}"`
509
- );
510
- },
511
- };
512
- };
513
-
514
- module.exports = createQueryBuilder;