@cuboapp/api-backend 1.0.2 → 1.0.4
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.
- package/package.json +1 -1
- package/src/helpers/query.ts +9 -1
- package/src/helpers/withes.ts +19 -5
- package/src/index.ts +4 -4
- package/src/types/basic.ts +3 -1
- package/src/types/db.ts +13 -2
package/package.json
CHANGED
package/src/helpers/query.ts
CHANGED
|
@@ -19,8 +19,10 @@ export class ApiHelpersQuery<T extends CuboApiEntitiesMap<T>> {
|
|
|
19
19
|
const selects = queryDto?.selects !== undefined ? queryDto.selects : []
|
|
20
20
|
const joins = queryDto?.joins !== undefined ? queryDto.joins : []
|
|
21
21
|
const conditions = queryDto?.conditions !== undefined ? queryDto.conditions : []
|
|
22
|
+
const havings = queryDto?.havings !== undefined ? queryDto.havings : []
|
|
22
23
|
const sorts = queryDto?.sorts !== undefined ? queryDto.sorts : []
|
|
23
24
|
const replacements = queryDto?.replacements !== undefined ? queryDto?.replacements : {}
|
|
25
|
+
const groupBy = queryDto?.groupBy !== undefined ? queryDto?.groupBy : undefined
|
|
24
26
|
|
|
25
27
|
const { sort, limit: _limit, page, with: _withes, ...query } = req.query || {}
|
|
26
28
|
|
|
@@ -94,7 +96,7 @@ export class ApiHelpersQuery<T extends CuboApiEntitiesMap<T>> {
|
|
|
94
96
|
|
|
95
97
|
let offsetString = ''
|
|
96
98
|
let limitString = ''
|
|
97
|
-
let groupByString = ''
|
|
99
|
+
let groupByString = queryDto?.groupBy ?? ''
|
|
98
100
|
|
|
99
101
|
if (!isCount && perPage > 0) {
|
|
100
102
|
if (filtersOffset > 0) {
|
|
@@ -119,16 +121,22 @@ export class ApiHelpersQuery<T extends CuboApiEntitiesMap<T>> {
|
|
|
119
121
|
${joins.join('\n')}
|
|
120
122
|
${conditions.length > 0 ? `where (${conditions.join(') and (')})` : ''}
|
|
121
123
|
${groupByString}
|
|
124
|
+
${havings.length > 0 ? `having (${havings.join(') and (')})` : ''}
|
|
122
125
|
${sorts.length > 0 && !isCount ? 'order by ' + sorts.join(', ') : ''}
|
|
123
126
|
${limitString}
|
|
124
127
|
${offsetString}
|
|
125
128
|
`
|
|
126
129
|
|
|
130
|
+
// console.log('conditions', { conditions })
|
|
131
|
+
// console.log('sql', sql)
|
|
132
|
+
|
|
127
133
|
return {
|
|
128
134
|
withes,
|
|
129
135
|
selects,
|
|
130
136
|
joins,
|
|
131
137
|
conditions,
|
|
138
|
+
groupBy,
|
|
139
|
+
havings,
|
|
132
140
|
sorts,
|
|
133
141
|
replacements,
|
|
134
142
|
sql
|
package/src/helpers/withes.ts
CHANGED
|
@@ -19,12 +19,14 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
|
|
|
19
19
|
joins?: string[]
|
|
20
20
|
sorts?: string[]
|
|
21
21
|
conditions?: string[]
|
|
22
|
+
havings?: string[]
|
|
23
|
+
groupBy?: string
|
|
22
24
|
conditionKey?: string
|
|
23
25
|
conditionValue?: any
|
|
24
26
|
sortKey?: string
|
|
25
27
|
sortDirection?: 'asc' | 'desc'
|
|
26
28
|
}
|
|
27
|
-
): Promise<
|
|
29
|
+
): Promise<CuboCrudFindQuery> {
|
|
28
30
|
const normalKey = key.replace(CUBO_CRUD_QUERY_KEY_REGEX, '')
|
|
29
31
|
|
|
30
32
|
if (normalKey !== key) {
|
|
@@ -74,12 +76,18 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
|
|
|
74
76
|
throw new Error('relation for key not found: "' + partKey + '"')
|
|
75
77
|
}
|
|
76
78
|
|
|
79
|
+
let relationEntity: CuboEntity | undefined
|
|
80
|
+
|
|
77
81
|
if (relation_entity_id) {
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
82
|
+
relationEntity = await this.helpers.entities.getById(relation_entity_id)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!relationEntity && relation_entity_alias) {
|
|
86
|
+
relationEntity = await this.helpers.entities.getByAlias(relation_entity_alias)
|
|
81
87
|
}
|
|
82
88
|
|
|
89
|
+
lastEntity = relationEntity
|
|
90
|
+
|
|
83
91
|
if (!lastEntity) {
|
|
84
92
|
throw new Error('entity for relation key not found: "' + partKey + '"')
|
|
85
93
|
}
|
|
@@ -133,6 +141,8 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
|
|
|
133
141
|
withes,
|
|
134
142
|
joins: opts.joins,
|
|
135
143
|
conditions: opts.conditions || undefined,
|
|
144
|
+
groupBy: opts.groupBy || undefined,
|
|
145
|
+
havings: opts.havings || undefined,
|
|
136
146
|
replacements: opts.replacements || undefined
|
|
137
147
|
}
|
|
138
148
|
|
|
@@ -225,7 +235,9 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
|
|
|
225
235
|
const joins: string[] = [...(opts?.queryOptions?.joins || [])]
|
|
226
236
|
const selects: string[] = [...(opts?.queryOptions?.selects || [])]
|
|
227
237
|
const conditions: string[] = [...(opts?.queryOptions?.conditions || [])]
|
|
238
|
+
const havings: string[] = [...(opts?.queryOptions?.havings || [])]
|
|
228
239
|
const replacements: Record<string, any> = { ...(opts?.queryOptions?.replacements || {}) }
|
|
240
|
+
const groupBy: string = opts?.queryOptions?.groupBy || undefined
|
|
229
241
|
|
|
230
242
|
const withes: CuboCrudWith[] = []
|
|
231
243
|
const sorts: string[] = []
|
|
@@ -260,6 +272,8 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
|
|
|
260
272
|
joins,
|
|
261
273
|
replacements,
|
|
262
274
|
conditions,
|
|
275
|
+
groupBy,
|
|
276
|
+
havings,
|
|
263
277
|
conditionKey: key,
|
|
264
278
|
conditionValue
|
|
265
279
|
})
|
|
@@ -279,6 +293,6 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
|
|
|
279
293
|
|
|
280
294
|
// console.log({ sorts, withes, joins, selects, conditions, replacements })
|
|
281
295
|
|
|
282
|
-
return { sorts, withes, joins, selects, conditions, replacements }
|
|
296
|
+
return { sorts, withes, joins, selects, conditions, groupBy, havings, replacements }
|
|
283
297
|
}
|
|
284
298
|
}
|
package/src/index.ts
CHANGED
|
@@ -223,7 +223,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
223
223
|
opts.queryOptions = await augmentation.beforeCreate(req, opts)
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
-
const dto = await this.helpers.data.prepare('create', entity!.fields, req.body, opts?.
|
|
226
|
+
const dto = await this.helpers.data.prepare('create', entity!.fields, req.body, opts?.performer_id || 0)
|
|
227
227
|
if (!Object.keys(dto)) {
|
|
228
228
|
throw { status: 400, text: 'No keys to update' }
|
|
229
229
|
}
|
|
@@ -273,7 +273,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
273
273
|
const entity = await this.helpers.entities.getByAlias(entityAlias)
|
|
274
274
|
const augmentation = this.augmentations?.[entityAlias]
|
|
275
275
|
|
|
276
|
-
if (augmentation?.beforeUpdate) {
|
|
276
|
+
if (augmentation?.beforeUpdate && !opts?.excludeHooks?.includes('beforeUpdate')) {
|
|
277
277
|
opts.queryOptions = await augmentation.beforeUpdate(req, opts)
|
|
278
278
|
}
|
|
279
279
|
|
|
@@ -283,7 +283,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
283
283
|
throw { status: 404, text: 'Entity not found' }
|
|
284
284
|
}
|
|
285
285
|
|
|
286
|
-
const dto = await this.helpers.data.prepare('update', entity!.fields, req.body, opts?.
|
|
286
|
+
const dto = await this.helpers.data.prepare('update', entity!.fields, req.body, opts?.performer_id || 0)
|
|
287
287
|
if (!Object.keys(dto)) {
|
|
288
288
|
throw { status: 400, text: 'No keys to update' }
|
|
289
289
|
}
|
|
@@ -334,7 +334,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
334
334
|
|
|
335
335
|
const item: any = await this.getOne(entityAlias, req, opts)
|
|
336
336
|
|
|
337
|
-
const dto = await this.helpers.data.prepare('delete', entity!.fields, req.body || {}, opts?.
|
|
337
|
+
const dto = await this.helpers.data.prepare('delete', entity!.fields, req.body || {}, opts?.performer_id)
|
|
338
338
|
if (!Object.keys(dto)) {
|
|
339
339
|
throw { status: 400, text: 'No keys to update' }
|
|
340
340
|
}
|
package/src/types/basic.ts
CHANGED
|
@@ -23,7 +23,7 @@ export type CuboCrudRequest = {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export type CuboCrudOptions = {
|
|
26
|
-
|
|
26
|
+
performer_id?: number
|
|
27
27
|
transaction?: Transaction
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -37,8 +37,10 @@ export type CuboCrudDto = {
|
|
|
37
37
|
export type CuboCrudFindQuery = {
|
|
38
38
|
selects: string[]
|
|
39
39
|
conditions: string[]
|
|
40
|
+
havings: string[]
|
|
40
41
|
replacements: Record<string, any>
|
|
41
42
|
sorts: string[]
|
|
43
|
+
groupBy: string
|
|
42
44
|
offset: number
|
|
43
45
|
limit: number
|
|
44
46
|
|
package/src/types/db.ts
CHANGED
|
@@ -3,10 +3,12 @@ import { type Transaction } from 'sequelize'
|
|
|
3
3
|
export type CuboCrudQueryOptions = {
|
|
4
4
|
selects?: string[]
|
|
5
5
|
conditions?: string[]
|
|
6
|
+
havings?: string[]
|
|
6
7
|
replacements?: Record<string, any>
|
|
7
8
|
joins?: string[]
|
|
8
9
|
withes?: string[]
|
|
9
10
|
sorts?: string[]
|
|
11
|
+
groupBy?: string
|
|
10
12
|
withDeleted?: boolean
|
|
11
13
|
|
|
12
14
|
extra?: any
|
|
@@ -14,9 +16,18 @@ export type CuboCrudQueryOptions = {
|
|
|
14
16
|
|
|
15
17
|
export type CuboCrudMethodOptions<E extends object = {}> = {
|
|
16
18
|
transaction?: Transaction
|
|
17
|
-
|
|
19
|
+
performer_id?: number
|
|
18
20
|
extra?: any
|
|
19
|
-
excludeHooks?: (
|
|
21
|
+
excludeHooks?: (
|
|
22
|
+
| 'beforeGetMany'
|
|
23
|
+
| 'beforeGetOne'
|
|
24
|
+
| 'afterCreate'
|
|
25
|
+
| 'afterUpdate'
|
|
26
|
+
| 'afterDelete'
|
|
27
|
+
| 'afterGetMany'
|
|
28
|
+
| 'afterGetOne'
|
|
29
|
+
| 'beforeUpdate'
|
|
30
|
+
)[]
|
|
20
31
|
|
|
21
32
|
log?: boolean
|
|
22
33
|
|