@cuboapp/api-backend 1.0.2 → 1.0.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuboapp/api-backend",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Backend Api for CuboApp",
5
5
  "main": "src/index.ts",
6
6
  "repository": "git@github.com:cuboapp/api-backend.git",
@@ -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,6 +121,7 @@ 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}
@@ -129,6 +132,8 @@ export class ApiHelpersQuery<T extends CuboApiEntitiesMap<T>> {
129
132
  selects,
130
133
  joins,
131
134
  conditions,
135
+ groupBy,
136
+ havings,
132
137
  sorts,
133
138
  replacements,
134
139
  sql
@@ -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<{ withes: CuboCrudWith[]; joins: string[]; selects?: string[]; conditions?: string[]; replacements?: string[] }> {
29
+ ): Promise<CuboCrudFindQuery> {
28
30
  const normalKey = key.replace(CUBO_CRUD_QUERY_KEY_REGEX, '')
29
31
 
30
32
  if (normalKey !== key) {
@@ -133,6 +135,8 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
133
135
  withes,
134
136
  joins: opts.joins,
135
137
  conditions: opts.conditions || undefined,
138
+ groupBy: opts.groupBy || undefined,
139
+ havings: opts.havings || undefined,
136
140
  replacements: opts.replacements || undefined
137
141
  }
138
142
 
@@ -225,7 +229,9 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
225
229
  const joins: string[] = [...(opts?.queryOptions?.joins || [])]
226
230
  const selects: string[] = [...(opts?.queryOptions?.selects || [])]
227
231
  const conditions: string[] = [...(opts?.queryOptions?.conditions || [])]
232
+ const havings: string[] = [...(opts?.queryOptions?.havings || [])]
228
233
  const replacements: Record<string, any> = { ...(opts?.queryOptions?.replacements || {}) }
234
+ const groupBy: string = opts?.queryOptions?.groupBy || undefined
229
235
 
230
236
  const withes: CuboCrudWith[] = []
231
237
  const sorts: string[] = []
@@ -260,6 +266,8 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
260
266
  joins,
261
267
  replacements,
262
268
  conditions,
269
+ groupBy,
270
+ havings,
263
271
  conditionKey: key,
264
272
  conditionValue
265
273
  })
@@ -279,6 +287,6 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
279
287
 
280
288
  // console.log({ sorts, withes, joins, selects, conditions, replacements })
281
289
 
282
- return { sorts, withes, joins, selects, conditions, replacements }
290
+ return { sorts, withes, joins, selects, conditions, groupBy, havings, replacements }
283
291
  }
284
292
  }
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?.user_id || 0)
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
  }
@@ -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?.user_id || 0)
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?.user_id)
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
  }
@@ -23,7 +23,7 @@ export type CuboCrudRequest = {
23
23
  }
24
24
 
25
25
  export type CuboCrudOptions = {
26
- user_id?: number
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,7 +16,7 @@ export type CuboCrudQueryOptions = {
14
16
 
15
17
  export type CuboCrudMethodOptions<E extends object = {}> = {
16
18
  transaction?: Transaction
17
- user_id?: number
19
+ performer_id?: number
18
20
  extra?: any
19
21
  excludeHooks?: ('beforeGetMany' | 'beforeGetOne' | 'afterCreate' | 'afterUpdate' | 'afterDelete' | 'afterGetMany' | 'afterGetOne')[]
20
22