@cuboapp/api-backend 1.0.28 → 1.0.30

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.28",
3
+ "version": "1.0.30",
4
4
  "description": "Backend Api for CuboApp",
5
5
  "main": "src/index.ts",
6
6
  "repository": "git@github.com:cuboapp/api-backend.git",
@@ -8,12 +8,12 @@
8
8
  "license": "MIT",
9
9
  "type": "module",
10
10
  "dependencies": {
11
- "@cuboapp/constants": "2.0.7",
12
- "@cuboapp/crdt": "1.0.6",
11
+ "@cuboapp/constants": "2.0.8",
12
+ "@cuboapp/crdt": "1.0.9",
13
13
  "@cuboapp/database": "1.0.4",
14
- "@cuboapp/types": "2.0.12",
14
+ "@cuboapp/types": "2.0.13",
15
15
  "@cuboapp/utils": "1.0.10",
16
- "@cuboapp/ws": "1.0.5",
16
+ "@cuboapp/ws": "1.0.6",
17
17
  "pg": "^8.17.2",
18
18
  "yjs": "^13.6.29"
19
19
  },
@@ -16,6 +16,7 @@ import { CuboBackendApiDbConnectionType, CuboCrudFindQuery, CuboCrudRequest } fr
16
16
  import { ApiHelpersConvert } from './convert'
17
17
  import { ApiHelpersData } from './data'
18
18
 
19
+ import { cloneDeep } from '@cuboapp/utils'
19
20
  import { ApiHelpersWithes } from './withes'
20
21
 
21
22
  export class ApiHelpers<T extends CuboApiEntitiesMap<T>, A> {
@@ -316,13 +317,17 @@ export class ApiHelpers<T extends CuboApiEntitiesMap<T>, A> {
316
317
  }
317
318
 
318
319
  this.connecting[type] = new Promise(async (success) => {
319
- const { iaas, pk_key, pk_type, ...config } = this.api.options.db.options
320
+ if (this.api.options.db.connection) {
321
+ this.connections[type] = this.api.options.db.connection
322
+ } else {
323
+ const { iaas, pk_key, pk_type, ...config } = this.api.options.db.options
320
324
 
321
- this.connections[type] = createDatabase(config, JSON.parse(JSON.stringify({ iaas, pk_key, pk_type })))
322
- await this.connections[type].connect()
325
+ this.connections[type] = createDatabase(config, JSON.parse(JSON.stringify({ iaas, pk_key, pk_type })))
326
+ await this.connections[type].connect()
323
327
 
324
- if (config.dialect === 'postgres' && config.schema) {
325
- await this.connections[type].connection.query(`SET schema '${config.schema}';`)
328
+ if (config.dialect === 'postgres' && config.schema) {
329
+ await this.connections[type].connection.query(`SET schema '${config.schema}';`)
330
+ }
326
331
  }
327
332
 
328
333
  success(this.connections[type])
@@ -353,17 +358,18 @@ export class ApiHelpers<T extends CuboApiEntitiesMap<T>, A> {
353
358
  queryDto?: Partial<CuboCrudFindQuery>,
354
359
  isCount?: boolean
355
360
  ): Promise<Partial<CuboCrudFindQuery>> {
356
- const withes = queryDto?.withes !== undefined ? queryDto.withes : []
357
- const selects = queryDto?.selects !== undefined ? queryDto.selects : []
358
- const joins = queryDto?.joins !== undefined ? queryDto.joins : []
359
- const conditions = queryDto?.conditions !== undefined ? queryDto.conditions : []
360
- const havings = queryDto?.havings !== undefined ? queryDto.havings : []
361
- const sorts = queryDto?.sorts !== undefined ? queryDto.sorts : []
362
- const replacements = queryDto?.replacements !== undefined ? queryDto?.replacements : {}
363
- const groupBy = queryDto?.groupBy !== undefined ? queryDto?.groupBy : undefined
364
- const withCrdt = queryDto?.withCrdt !== undefined ? queryDto?.withCrdt : false
365
-
366
- const { sort, limit: _limit, page, with: _withes, ...query } = req.query || {}
361
+ const dto: Partial<CuboCrudFindQuery> = cloneDeep(queryDto || {})
362
+
363
+ const withes = dto.withes ?? []
364
+ const selects = dto.selects ?? []
365
+ const joins = dto.joins ?? []
366
+ const conditions = dto.conditions ?? []
367
+ const havings = dto.havings ?? []
368
+ const sorts = dto.sorts ?? []
369
+ const replacements = dto.replacements ?? {}
370
+ const groupBy = dto.groupBy ?? undefined
371
+
372
+ const { sort, limit: _limit, page, with: _withes, ...query } = cloneDeep(req.query || {})
367
373
 
368
374
  // plain conditions (nested are in withes.ts)
369
375
  for (const key in query || {}) {
@@ -374,6 +380,7 @@ export class ApiHelpers<T extends CuboApiEntitiesMap<T>, A> {
374
380
  const defaultField = CUBO_CRUD_DEFAULT_FIELDS.find((f) => f.alias === parts[0])
375
381
 
376
382
  if (!conditionField && !defaultField) {
383
+ console.log(entity.alias, entity.fields)
377
384
  throw new Error('condition field "' + parts[0] + '" not found in entity "' + entity.alias + '"')
378
385
  }
379
386
 
package/src/index.ts CHANGED
@@ -137,6 +137,9 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown>
137
137
  ): Promise<CuboCrudGetManyResponse<T[K]>> {
138
138
  opts = opts || {}
139
139
  req = req || {}
140
+
141
+ req.query = cloneDeep(req.query || {})
142
+
140
143
  const entity = await this.getEntity(entityAlias)
141
144
  const augmentation = this.options?.augmentations?.[entityAlias]
142
145
 
@@ -147,7 +150,6 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown>
147
150
  // get all joins
148
151
  const queryDto = await this.helpers.withes.prepare(req, entity, opts)
149
152
  queryDto.withDeleted = opts.queryOptions?.withDeleted
150
- // queryDto.withCrdt = opts.queryOptions?.withCrdt
151
153
 
152
154
  // create find query
153
155
  const regularQuery = await this.helpers.createFindQuery(req, entity, queryDto)
@@ -200,7 +202,6 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown>
200
202
  // get all joins
201
203
  const queryDto = await this.helpers.withes.prepare(req, entity, opts)
202
204
  queryDto.withDeleted = opts.queryOptions?.withDeleted
203
- queryDto.withCrdt = opts.queryOptions?.withCrdt
204
205
  queryDto.limit = 1
205
206
 
206
207
  const query = await this.helpers.createFindQuery(req, entity, queryDto)
@@ -283,13 +284,11 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>, A extends unknown>
283
284
  if (!req.query?.id) {
284
285
  throw 'Debounce "updateOne" allowed only with "id"'
285
286
  }
286
- console.log('call debounced updateOne')
287
287
 
288
288
  const key = `${entityAlias}:${+req.query.id}` as `${string}:${number}`
289
289
  let ex = this.debounces.get(key)
290
290
  if (ex) {
291
291
  if (ex.started > +new Date() - opts.debounce) {
292
- console.log('clear execution')
293
292
  Promise.reject(ex.promise)
294
293
  } else {
295
294
  return ex.promise