@cuboapp/api-backend 1.0.21 → 1.0.23
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 +1 -1
- package/src/crdt/index.ts +2 -2
- package/src/helpers/convert.ts +2 -2
- package/src/helpers/index.ts +13 -3
- package/src/index.ts +30 -13
- package/src/types/basic.ts +1 -0
- package/src/types/db.ts +2 -1
- package/src/types/index.ts +2 -1
package/package.json
CHANGED
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 (
|
|
187
|
+
if (id === undefined) {
|
|
188
188
|
document.connections.forEach(({ connection }) => {
|
|
189
189
|
connection.close({
|
|
190
190
|
reason: 'Document not loaded',
|
package/src/helpers/convert.ts
CHANGED
|
@@ -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:
|
package/src/helpers/index.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
@@ -19,20 +19,22 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
19
19
|
|
|
20
20
|
async init() {
|
|
21
21
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
if (this.options.api !== undefined) {
|
|
23
|
+
this.auth = await this.request<{ variables: Record<string, any> }>('/auth/me?with=variables', {
|
|
24
|
+
withAuth: true
|
|
25
|
+
})
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
if (!this.auth) {
|
|
28
|
+
throw { code: 403, text: 'Авторизация Cubo-Backend не пройдена' }
|
|
29
|
+
}
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
if (this.auth.variables?.db !== undefined) {
|
|
32
|
+
this.options.db = this.options.db || {}
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
this.options.db.options = {
|
|
35
|
+
...(this.options.db.options || {}),
|
|
36
|
+
...(this.auth.variables?.db || {})
|
|
37
|
+
}
|
|
36
38
|
}
|
|
37
39
|
}
|
|
38
40
|
} catch (e) {
|
|
@@ -40,6 +42,19 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
40
42
|
}
|
|
41
43
|
}
|
|
42
44
|
|
|
45
|
+
async destroy() {
|
|
46
|
+
const read = await this.helpers.getConnection('read', false)
|
|
47
|
+
const write = await this.helpers.getConnection('write', false)
|
|
48
|
+
|
|
49
|
+
if (read) {
|
|
50
|
+
await read.disconnect()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (write) {
|
|
54
|
+
await write.disconnect()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
43
58
|
public async request<T>(url: string, opts?: RequestInit & { debug?: boolean; withAuth?: boolean; withoutContentType?: boolean }) {
|
|
44
59
|
const baseUrl = this.options?.api?.base_url || 'https://api.cubo.sh'
|
|
45
60
|
|
|
@@ -136,6 +151,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
136
151
|
// get all joins
|
|
137
152
|
const queryDto = await this.helpers.withes.prepare(req, entity, opts)
|
|
138
153
|
queryDto.withDeleted = opts.queryOptions?.withDeleted
|
|
154
|
+
queryDto.withCrdt = opts.queryOptions?.withCrdt
|
|
139
155
|
|
|
140
156
|
// create find query
|
|
141
157
|
const regularQuery = await this.helpers.createFindQuery(req, entity, queryDto)
|
|
@@ -188,6 +204,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
188
204
|
// get all joins
|
|
189
205
|
const queryDto = await this.helpers.withes.prepare(req, entity, opts)
|
|
190
206
|
queryDto.withDeleted = opts.queryOptions?.withDeleted
|
|
207
|
+
queryDto.withCrdt = opts.queryOptions?.withCrdt
|
|
191
208
|
queryDto.limit = 1
|
|
192
209
|
|
|
193
210
|
const query = await this.helpers.createFindQuery(req, entity, queryDto)
|
|
@@ -254,7 +271,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
254
271
|
response = await augmentation.afterCreate(response, req, opts)
|
|
255
272
|
}
|
|
256
273
|
|
|
257
|
-
if (opts.crdt
|
|
274
|
+
if (opts.crdt !== false && this.crdt?.isCrdtSupported(entity)) {
|
|
258
275
|
const state = await this.crdt.createState({ entityAlias, id: created_id, entity }, response)
|
|
259
276
|
|
|
260
277
|
const fieldKey = entity.fields.find((f) => f.type === 'binary').alias
|
|
@@ -306,7 +323,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
|
|
|
306
323
|
response = await augmentation.afterUpdate(response, req, opts)
|
|
307
324
|
}
|
|
308
325
|
|
|
309
|
-
if (opts.crdt
|
|
326
|
+
if (opts.crdt !== false && this.crdt?.isCrdtSupported(entity)) {
|
|
310
327
|
const context = { auth: opts?.extra?.auth }
|
|
311
328
|
await this.crdt.updateDocument({ entityAlias, id: item.id, entity }, response, context)
|
|
312
329
|
|
package/src/types/basic.ts
CHANGED
package/src/types/db.ts
CHANGED
package/src/types/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ export * from './basic'
|
|
|
10
10
|
export * from './db'
|
|
11
11
|
|
|
12
12
|
export type CuboBackendApiOptions<T extends CuboApiEntitiesMap<T>> = {
|
|
13
|
-
api
|
|
13
|
+
api?: {
|
|
14
14
|
base_url?: string
|
|
15
15
|
headers: Partial<Record<`cubo-${CuboAuthTokenType}`, string>> & Record<string, any>
|
|
16
16
|
}
|
|
@@ -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
|
|