@cuboapp/api-backend 1.0.21 → 1.0.22

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.21",
3
+ "version": "1.0.22",
4
4
  "description": "Backend Api for CuboApp",
5
5
  "main": "src/index.ts",
6
6
  "repository": "git@github.com:cuboapp/api-backend.git",
package/src/crdt/index.ts CHANGED
@@ -135,7 +135,7 @@ export class CuboCrdt<T extends CuboApiEntitiesMap<T>> {
135
135
  auth: context.auth
136
136
  },
137
137
  excludeHooks: [],
138
- queryOptions: { withDeleted: true }
138
+ queryOptions: { withDeleted: true, withCrdt: true }
139
139
  }
140
140
  )
141
141
  }
@@ -184,7 +184,7 @@ export class CuboCrdt<T extends CuboApiEntitiesMap<T>> {
184
184
  const entityMap = document.getMap(entityAlias)
185
185
  const id = entityMap.get('id')
186
186
 
187
- if (!id) {
187
+ if (id === undefined) {
188
188
  document.connections.forEach(({ connection }) => {
189
189
  connection.close({
190
190
  reason: 'Document not loaded',
@@ -1,6 +1,6 @@
1
+ import { CUBO_ENTITY_FIELD_TYPE } from '@cuboapp/constants'
1
2
  import { CuboApiEntitiesMap, CuboEntity, CuboEntityField } from '@cuboapp/types'
2
3
  import { get, set, unset } from '@cuboapp/utils'
3
- import { CUBO_ENTITY_FIELD_TYPE } from '@cuboapp/constants'
4
4
 
5
5
  import { CUBO_CRUD_DEFAULT_FIELDS } from '../constants'
6
6
  import { CuboCrudFindQuery, CuboCrudRequest } from '../types'
@@ -95,7 +95,7 @@ export class ApiHelpersConvert<T extends CuboApiEntitiesMap<T>> {
95
95
  case CUBO_ENTITY_FIELD_TYPE.DATE:
96
96
  return value
97
97
  case CUBO_ENTITY_FIELD_TYPE.FILE:
98
- console.warn('field type "file" is not implemented yet')
98
+ // console.warn('field type "file" is not implemented yet')
99
99
  return undefined
100
100
  case CUBO_ENTITY_FIELD_TYPE.JSON:
101
101
  case CUBO_ENTITY_FIELD_TYPE.BINARY:
@@ -306,12 +306,12 @@ export class ApiHelpers<T extends CuboApiEntitiesMap<T>> {
306
306
  }
307
307
  }
308
308
 
309
- public async getConnection(type: CuboBackendApiDbConnectionType) {
309
+ public async getConnection(type: CuboBackendApiDbConnectionType, create = true) {
310
310
  if (this.connections[type]) {
311
311
  return this.connections[type]
312
312
  }
313
313
 
314
- if (this.connecting[type] !== undefined) {
314
+ if (!create || this.connecting[type] !== undefined) {
315
315
  return this.connecting[type]
316
316
  }
317
317
 
@@ -361,6 +361,7 @@ export class ApiHelpers<T extends CuboApiEntitiesMap<T>> {
361
361
  const sorts = queryDto?.sorts !== undefined ? queryDto.sorts : []
362
362
  const replacements = queryDto?.replacements !== undefined ? queryDto?.replacements : {}
363
363
  const groupBy = queryDto?.groupBy !== undefined ? queryDto?.groupBy : undefined
364
+ const withCrdt = queryDto?.withCrdt !== undefined ? queryDto?.withCrdt : false
364
365
 
365
366
  const { sort, limit: _limit, page, with: _withes, ...query } = req.query || {}
366
367
 
@@ -446,7 +447,16 @@ export class ApiHelpers<T extends CuboApiEntitiesMap<T>> {
446
447
  }
447
448
 
448
449
  selects.push(...CUBO_CRUD_DEFAULT_FIELDS.map((f) => `t1.${f.alias}`))
449
- selects.push(...entity.fields.map((f) => `t1.${f.alias}`))
450
+
451
+ let entityFields = entity.fields
452
+
453
+ if (this.api.crdt?.isCrdtSupported(entity) && !withCrdt) {
454
+ const crdtKey = this.api.crdt?.getCrdtField(entity)
455
+
456
+ entityFields = entityFields.filter((f) => f.alias !== crdtKey?.alias)
457
+ }
458
+
459
+ selects.push(...entityFields.map((f) => `t1.${f.alias}`))
450
460
 
451
461
  // remove deleted rows
452
462
  if (!queryDto?.withDeleted) {
package/src/index.ts CHANGED
@@ -40,6 +40,19 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
40
40
  }
41
41
  }
42
42
 
43
+ async destroy() {
44
+ const read = await this.helpers.getConnection('read', false)
45
+ const write = await this.helpers.getConnection('write', false)
46
+
47
+ if (read) {
48
+ await read.disconnect()
49
+ }
50
+
51
+ if (write) {
52
+ await write.disconnect()
53
+ }
54
+ }
55
+
43
56
  public async request<T>(url: string, opts?: RequestInit & { debug?: boolean; withAuth?: boolean; withoutContentType?: boolean }) {
44
57
  const baseUrl = this.options?.api?.base_url || 'https://api.cubo.sh'
45
58
 
@@ -136,6 +149,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
136
149
  // get all joins
137
150
  const queryDto = await this.helpers.withes.prepare(req, entity, opts)
138
151
  queryDto.withDeleted = opts.queryOptions?.withDeleted
152
+ queryDto.withCrdt = opts.queryOptions?.withCrdt
139
153
 
140
154
  // create find query
141
155
  const regularQuery = await this.helpers.createFindQuery(req, entity, queryDto)
@@ -188,6 +202,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
188
202
  // get all joins
189
203
  const queryDto = await this.helpers.withes.prepare(req, entity, opts)
190
204
  queryDto.withDeleted = opts.queryOptions?.withDeleted
205
+ queryDto.withCrdt = opts.queryOptions?.withCrdt
191
206
  queryDto.limit = 1
192
207
 
193
208
  const query = await this.helpers.createFindQuery(req, entity, queryDto)
@@ -254,7 +269,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
254
269
  response = await augmentation.afterCreate(response, req, opts)
255
270
  }
256
271
 
257
- if (opts.crdt === true && this.crdt?.isCrdtSupported(entity)) {
272
+ if (opts.crdt !== false && this.crdt?.isCrdtSupported(entity)) {
258
273
  const state = await this.crdt.createState({ entityAlias, id: created_id, entity }, response)
259
274
 
260
275
  const fieldKey = entity.fields.find((f) => f.type === 'binary').alias
@@ -306,7 +321,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
306
321
  response = await augmentation.afterUpdate(response, req, opts)
307
322
  }
308
323
 
309
- if (opts.crdt === true && this.crdt?.isCrdtSupported(entity)) {
324
+ if (opts.crdt !== false && this.crdt?.isCrdtSupported(entity)) {
310
325
  const context = { auth: opts?.extra?.auth }
311
326
  await this.crdt.updateDocument({ entityAlias, id: item.id, entity }, response, context)
312
327
 
@@ -48,6 +48,7 @@ export type CuboCrudFindQuery = {
48
48
  withes: CuboCrudWith[]
49
49
 
50
50
  withDeleted?: boolean
51
+ withCrdt?: boolean
51
52
  is_count: boolean
52
53
  sql: string
53
54
  }
package/src/types/db.ts CHANGED
@@ -10,7 +10,8 @@ export type CuboCrudQueryOptions = {
10
10
  sorts?: string[]
11
11
  groupBy?: string
12
12
  withDeleted?: boolean
13
-
13
+ withCrdt?: boolean
14
+
14
15
  extra?: any
15
16
  }
16
17
 
@@ -23,6 +23,7 @@ export type CuboBackendApiOptions<T extends CuboApiEntitiesMap<T>> = {
23
23
  }
24
24
 
25
25
  crdt?: CuboCrdtConfiguration<T>
26
+
26
27
  augmentations?: Partial<CuboCrudAugmentationsStore<T>>
27
28
  }
28
29