@cuboapp/api-backend 1.0.33 → 1.0.35
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/src/helpers/data.ts +2 -6
- package/src/helpers/index.ts +1 -2
- package/src/index.ts +45 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cuboapp/api-backend",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.35",
|
|
4
4
|
"description": "Backend Api for CuboApp",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"repository": "git@github.com:cuboapp/api-backend.git",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@cuboapp/constants": "2.0.8",
|
|
12
12
|
"@cuboapp/crdt": "1.0.11",
|
|
13
|
-
"@cuboapp/database": "1.0.
|
|
13
|
+
"@cuboapp/database": "1.0.5",
|
|
14
14
|
"@cuboapp/types": "2.0.13",
|
|
15
15
|
"@cuboapp/utils": "1.0.10",
|
|
16
16
|
"@cuboapp/ws": "1.0.6",
|
package/src/helpers/data.ts
CHANGED
|
@@ -2,11 +2,7 @@ import { CUBO_ENTITY_FIELD_TYPE } from '@cuboapp/constants'
|
|
|
2
2
|
import { CuboApiEntitiesMap, CuboEntityField } from '@cuboapp/types'
|
|
3
3
|
import { keyBy } from '@cuboapp/utils'
|
|
4
4
|
|
|
5
|
-
import { ApiHelpers } from '.'
|
|
6
|
-
|
|
7
5
|
export class ApiHelpersData<T extends CuboApiEntitiesMap<T>> {
|
|
8
|
-
constructor(private helpers: ApiHelpers<T>) {}
|
|
9
|
-
|
|
10
6
|
public prepare(type: 'create' | 'update' | 'delete', fields: CuboEntityField[], dto?: any, performer_id?: number) {
|
|
11
7
|
const fieldsByAlias = keyBy(fields, (i) => i.alias)
|
|
12
8
|
|
|
@@ -15,11 +11,11 @@ export class ApiHelpersData<T extends CuboApiEntitiesMap<T>> {
|
|
|
15
11
|
|
|
16
12
|
switch (type) {
|
|
17
13
|
case 'create':
|
|
18
|
-
prepared.created_at = dto.created_at !== undefined ? dto.created_at : new Date()
|
|
14
|
+
prepared.created_at = dto.created_at !== undefined ? dto.created_at : new Date()
|
|
19
15
|
prepared.created_by = dto.created_by !== undefined ? dto.created_by : performer_id
|
|
20
16
|
break
|
|
21
17
|
case 'update':
|
|
22
|
-
prepared.modified_at = dto.modified_at !== undefined ? dto.modified_at : new Date()
|
|
18
|
+
prepared.modified_at = dto.modified_at !== undefined ? dto.modified_at : new Date()
|
|
23
19
|
prepared.modified_by = dto.modified_by !== undefined ? dto.modified_by : performer_id
|
|
24
20
|
|
|
25
21
|
if (dto?.deleted_at !== undefined) {
|
package/src/helpers/index.ts
CHANGED
|
@@ -24,7 +24,7 @@ export class ApiHelpers<T extends CuboApiEntitiesMap<T>, A> {
|
|
|
24
24
|
|
|
25
25
|
public withes = new ApiHelpersWithes<T>(this)
|
|
26
26
|
public convert = new ApiHelpersConvert<T>(this)
|
|
27
|
-
public data = new ApiHelpersData<T>(
|
|
27
|
+
public data = new ApiHelpersData<T>()
|
|
28
28
|
|
|
29
29
|
private cache: Record<string, any> = {}
|
|
30
30
|
private fetching: Record<string, Promise<any> | null> = {}
|
|
@@ -380,7 +380,6 @@ export class ApiHelpers<T extends CuboApiEntitiesMap<T>, A> {
|
|
|
380
380
|
const defaultField = CUBO_CRUD_DEFAULT_FIELDS.find((f) => f.alias === parts[0])
|
|
381
381
|
|
|
382
382
|
if (!conditionField && !defaultField) {
|
|
383
|
-
console.log(entity.alias, entity.fields)
|
|
384
383
|
throw new Error('condition field "' + parts[0] + '" not found in entity "' + entity.alias + '"')
|
|
385
384
|
}
|
|
386
385
|
|
package/src/index.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { CuboApiEntitiesMap } from '@cuboapp/types'
|
|
|
3
3
|
import { cloneDeep } from '@cuboapp/utils'
|
|
4
4
|
|
|
5
5
|
import { ApiHelpers } from './helpers'
|
|
6
|
-
import { CuboBackendApiAuth, CuboBackendApiOptions, CuboCrudGetManyResponse, CuboCrudMethodOptions, CuboCrudRequest } from './types'
|
|
7
6
|
import { CuboS3UploadFunctionOptions, CuboS3UploadFunctionResult, uploadToS3 } from './s3'
|
|
7
|
+
import { CuboBackendApiAuth, CuboBackendApiOptions, CuboCrudGetManyResponse, CuboCrudMethodOptions, CuboCrudRequest } from './types'
|
|
8
8
|
|
|
9
9
|
export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown> {
|
|
10
10
|
constructor(public options: CuboBackendApiOptions<T, A>) {}
|
|
@@ -52,6 +52,12 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown>
|
|
|
52
52
|
|
|
53
53
|
private debounces = new Map<`${string}:${number}`, { started: number; promise: Promise<any> }>()
|
|
54
54
|
|
|
55
|
+
public async transaction() {
|
|
56
|
+
const db = await this.helpers.getConnection('write')
|
|
57
|
+
|
|
58
|
+
return db.connection.transaction()
|
|
59
|
+
}
|
|
60
|
+
|
|
55
61
|
public async request<T>(url: string, opts?: RequestInit & { debug?: boolean; withAuth?: boolean; withoutContentType?: boolean }) {
|
|
56
62
|
const baseUrl = this.options?.api?.base_url || 'https://api.cubo.sh'
|
|
57
63
|
|
|
@@ -125,7 +131,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown>
|
|
|
125
131
|
public async uploadToS3(buffer: Buffer, opts: CuboS3UploadFunctionOptions): Promise<CuboS3UploadFunctionResult> {
|
|
126
132
|
return uploadToS3(buffer, opts)
|
|
127
133
|
}
|
|
128
|
-
|
|
134
|
+
|
|
129
135
|
private async getEntity(entityAlias: Extract<keyof T, string>) {
|
|
130
136
|
const entity = await this.helpers.getEntityByAlias(entityAlias)
|
|
131
137
|
if (!entity) {
|
|
@@ -255,6 +261,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown>
|
|
|
255
261
|
throw { status: 400, text: 'No keys to update' }
|
|
256
262
|
}
|
|
257
263
|
|
|
264
|
+
// console.log('dto', dto)
|
|
258
265
|
// const prepared = dbPrepareInsertQueryString(dto)
|
|
259
266
|
const db = await this.helpers.getConnection('write')
|
|
260
267
|
const [created_id] = await db.create(entity.alias, dto, { transaction: opts?.transaction, log: opts?.log })
|
|
@@ -395,6 +402,42 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown>
|
|
|
395
402
|
|
|
396
403
|
return true
|
|
397
404
|
}
|
|
405
|
+
|
|
406
|
+
async deleteMany<K extends Extract<keyof T, string>>(
|
|
407
|
+
entityAlias: K,
|
|
408
|
+
req: CuboCrudRequest,
|
|
409
|
+
opts?: CuboCrudMethodOptions
|
|
410
|
+
): Promise<boolean> {
|
|
411
|
+
opts = opts || {}
|
|
412
|
+
const entity = await this.helpers.getEntityByAlias(entityAlias)
|
|
413
|
+
const augmentation = this.options?.augmentations?.[entityAlias]
|
|
414
|
+
|
|
415
|
+
if (augmentation?.beforeDelete) {
|
|
416
|
+
opts.queryOptions = await augmentation.beforeDelete(req, opts)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const { rows: items } = await this.getMany(entityAlias, req, opts)
|
|
420
|
+
if (!items) {
|
|
421
|
+
return false
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const dto = await this.helpers.data.prepare('delete', entity!.fields, req.body || {}, opts?.performer_id)
|
|
425
|
+
if (!Object.keys(dto)) {
|
|
426
|
+
throw { status: 400, text: 'No keys to update' }
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const db = await this.helpers.getConnection('write')
|
|
430
|
+
|
|
431
|
+
for (const item of items) {
|
|
432
|
+
await db.update(entity.alias, 'id = :id', { id: (item as any).id }, dto, { transaction: opts?.transaction, log: opts?.log })
|
|
433
|
+
|
|
434
|
+
if (this.options.hooks?.onAfterDelete && !opts?.excludeHooks?.includes('baseHooks')) {
|
|
435
|
+
await this.options.hooks.onAfterDelete(entity, item, opts)
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return true
|
|
440
|
+
}
|
|
398
441
|
}
|
|
399
442
|
|
|
400
443
|
export * from './helpers'
|