@cuboapp/api-backend 1.0.33 → 1.0.34

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.33",
3
+ "version": "1.0.34",
4
4
  "description": "Backend Api for CuboApp",
5
5
  "main": "src/index.ts",
6
6
  "repository": "git@github.com:cuboapp/api-backend.git",
@@ -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) {
@@ -395,6 +401,42 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown>
395
401
 
396
402
  return true
397
403
  }
404
+
405
+ async deleteMany<K extends Extract<keyof T, string>>(
406
+ entityAlias: K,
407
+ req: CuboCrudRequest,
408
+ opts?: CuboCrudMethodOptions
409
+ ): Promise<boolean> {
410
+ opts = opts || {}
411
+ const entity = await this.helpers.getEntityByAlias(entityAlias)
412
+ const augmentation = this.options?.augmentations?.[entityAlias]
413
+
414
+ if (augmentation?.beforeDelete) {
415
+ opts.queryOptions = await augmentation.beforeDelete(req, opts)
416
+ }
417
+
418
+ const { rows: items } = await this.getMany(entityAlias, req, opts)
419
+ if (!items) {
420
+ return false
421
+ }
422
+
423
+ const dto = await this.helpers.data.prepare('delete', entity!.fields, req.body || {}, opts?.performer_id)
424
+ if (!Object.keys(dto)) {
425
+ throw { status: 400, text: 'No keys to update' }
426
+ }
427
+
428
+ const db = await this.helpers.getConnection('write')
429
+
430
+ for (const item of items) {
431
+ await db.update(entity.alias, 'id = :id', { id: (item as any).id }, dto, { transaction: opts?.transaction, log: opts?.log })
432
+
433
+ if (this.options.hooks?.onAfterDelete && !opts?.excludeHooks?.includes('baseHooks')) {
434
+ await this.options.hooks.onAfterDelete(entity, item, opts)
435
+ }
436
+ }
437
+
438
+ return true
439
+ }
398
440
  }
399
441
 
400
442
  export * from './helpers'