@cuboapp/api-backend 1.0.1 → 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 +2 -2
- package/pnpm-lock.yaml +9 -2
- package/src/helpers/query.ts +6 -1
- package/src/helpers/withes.ts +10 -2
- package/src/index.ts +3 -3
- package/src/types/basic.ts +3 -1
- package/src/types/db.ts +3 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cuboapp/api-backend",
|
|
3
|
-
"version": "1.0.
|
|
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",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@cuboapp/config": "^0.0.6",
|
|
15
|
-
"@cuboapp/types": "^1.0.
|
|
15
|
+
"@cuboapp/types": "^1.0.20",
|
|
16
16
|
"lodash-es": "^4.17.21",
|
|
17
17
|
"pg": "^8.13.3",
|
|
18
18
|
"sequelize": "^6.37.6"
|
package/pnpm-lock.yaml
CHANGED
|
@@ -12,8 +12,8 @@ importers:
|
|
|
12
12
|
specifier: ^0.0.6
|
|
13
13
|
version: 0.0.6
|
|
14
14
|
'@cuboapp/types':
|
|
15
|
-
specifier: ^1.0.
|
|
16
|
-
version: 1.0.
|
|
15
|
+
specifier: ^1.0.20
|
|
16
|
+
version: 1.0.20
|
|
17
17
|
lodash-es:
|
|
18
18
|
specifier: ^4.17.21
|
|
19
19
|
version: 4.17.21
|
|
@@ -42,6 +42,9 @@ packages:
|
|
|
42
42
|
'@cuboapp/types@1.0.19':
|
|
43
43
|
resolution: {integrity: sha512-CZyut9MmCT+5bBBbZF3ETiJnRbgnExvx/+zT/UKj4NxZKlqQAnzX8hWIFMBbed3YoSkbS+cdZcJE26k5mLNPjw==}
|
|
44
44
|
|
|
45
|
+
'@cuboapp/types@1.0.20':
|
|
46
|
+
resolution: {integrity: sha512-PDeJ4GOsVA6OhC1ryd8XwBrnZ7k6mB48GEnMLDdgw3ACRxV2KaWc/5bDrZUdfRjggOAXmwEhX+8rvqfNvp93hg==}
|
|
47
|
+
|
|
45
48
|
'@types/debug@4.1.12':
|
|
46
49
|
resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==}
|
|
47
50
|
|
|
@@ -239,6 +242,10 @@ snapshots:
|
|
|
239
242
|
dependencies:
|
|
240
243
|
'@types/events': 3.0.3
|
|
241
244
|
|
|
245
|
+
'@cuboapp/types@1.0.20':
|
|
246
|
+
dependencies:
|
|
247
|
+
'@types/events': 3.0.3
|
|
248
|
+
|
|
242
249
|
'@types/debug@4.1.12':
|
|
243
250
|
dependencies:
|
|
244
251
|
'@types/ms': 2.1.0
|
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,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
|
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) {
|
|
@@ -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?.
|
|
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?.
|
|
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,7 +16,7 @@ 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
21
|
excludeHooks?: ('beforeGetMany' | 'beforeGetOne' | 'afterCreate' | 'afterUpdate' | 'afterDelete' | 'afterGetMany' | 'afterGetOne')[]
|
|
20
22
|
|