@contember/engine-content-api 2.0.2 → 2.0.4
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/dist/development/src/ExecutionContainer.cjs +4 -3
- package/dist/development/src/ExecutionContainer.cjs.map +1 -1
- package/dist/development/src/ExecutionContainer.js +4 -3
- package/dist/development/src/ExecutionContainer.js.map +1 -1
- package/dist/development/src/mapper/Result.cjs.map +1 -1
- package/dist/development/src/mapper/Result.js.map +1 -1
- package/dist/development/src/mapper/select/optimizer/WhereOptimizer.cjs +6 -2
- package/dist/development/src/mapper/select/optimizer/WhereOptimizer.cjs.map +1 -1
- package/dist/development/src/mapper/select/optimizer/WhereOptimizer.js +6 -2
- package/dist/development/src/mapper/select/optimizer/WhereOptimizer.js.map +1 -1
- package/dist/development/src/mapper/update/Updater.cjs +15 -3
- package/dist/development/src/mapper/update/Updater.cjs.map +1 -1
- package/dist/development/src/mapper/update/Updater.js +16 -4
- package/dist/development/src/mapper/update/Updater.js.map +1 -1
- package/dist/production/src/ExecutionContainer.cjs +4 -3
- package/dist/production/src/ExecutionContainer.cjs.map +1 -1
- package/dist/production/src/ExecutionContainer.js +4 -3
- package/dist/production/src/ExecutionContainer.js.map +1 -1
- package/dist/production/src/mapper/Result.cjs.map +1 -1
- package/dist/production/src/mapper/Result.js.map +1 -1
- package/dist/production/src/mapper/select/optimizer/WhereOptimizer.cjs +6 -2
- package/dist/production/src/mapper/select/optimizer/WhereOptimizer.cjs.map +1 -1
- package/dist/production/src/mapper/select/optimizer/WhereOptimizer.js +6 -2
- package/dist/production/src/mapper/select/optimizer/WhereOptimizer.js.map +1 -1
- package/dist/production/src/mapper/update/Updater.cjs +15 -3
- package/dist/production/src/mapper/update/Updater.cjs.map +1 -1
- package/dist/production/src/mapper/update/Updater.js +16 -4
- package/dist/production/src/mapper/update/Updater.js.map +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/ExecutionContainer.d.ts +3 -2
- package/dist/types/ExecutionContainer.d.ts.map +1 -1
- package/dist/types/mapper/Result.d.ts +2 -2
- package/dist/types/mapper/Result.d.ts.map +1 -1
- package/dist/types/mapper/select/optimizer/WhereOptimizer.d.ts +6 -1
- package/dist/types/mapper/select/optimizer/WhereOptimizer.d.ts.map +1 -1
- package/dist/types/mapper/update/Updater.d.ts +1 -5
- package/dist/types/mapper/update/Updater.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +10 -10
- package/src/ExecutionContainer.ts +5 -4
- package/src/mapper/Result.ts +1 -1
- package/src/mapper/select/optimizer/WhereOptimizer.ts +10 -1
- package/src/mapper/update/Updater.ts +16 -5
- package/tests/cases/integration/graphQlRequests/update/additionalFilter.test.ts +51 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-content-api",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "./dist/production/index.js",
|
|
6
6
|
"typings": "./dist/types/index.d.ts",
|
|
@@ -26,18 +26,18 @@
|
|
|
26
26
|
"generate": "gql-gen --config graphql.codegen.yml"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@contember/database": "2.0.
|
|
30
|
-
"@contember/dic": "2.0.
|
|
31
|
-
"@contember/graphql-utils": "2.0.
|
|
32
|
-
"@contember/logger": "2.0.
|
|
33
|
-
"@contember/schema": "2.0.
|
|
34
|
-
"@contember/schema-definition": "2.0.
|
|
35
|
-
"@contember/schema-utils": "2.0.
|
|
29
|
+
"@contember/database": "2.0.4",
|
|
30
|
+
"@contember/dic": "2.0.4",
|
|
31
|
+
"@contember/graphql-utils": "2.0.4",
|
|
32
|
+
"@contember/logger": "2.0.4",
|
|
33
|
+
"@contember/schema": "2.0.4",
|
|
34
|
+
"@contember/schema-definition": "2.0.4",
|
|
35
|
+
"@contember/schema-utils": "2.0.4",
|
|
36
36
|
"fast-deep-equal": "^3.1.3"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@contember/database-tester": "2.0.
|
|
40
|
-
"@contember/schema-definition": "2.0.
|
|
39
|
+
"@contember/database-tester": "2.0.4",
|
|
40
|
+
"@contember/schema-definition": "2.0.4",
|
|
41
41
|
"@types/node": "^20.16.11",
|
|
42
42
|
"graphql": "^16.9.0",
|
|
43
43
|
"pg": "^8.12.0"
|
|
@@ -31,7 +31,7 @@ import { Client, DatabaseMetadata, SelectBuilder as DbSelectBuilder } from '@con
|
|
|
31
31
|
import { Providers } from '@contember/schema-utils'
|
|
32
32
|
import { PaginatedHasManyExecutionHandler } from './extensions/paginatedHasMany/PaginatedHasManyExecutionHandler'
|
|
33
33
|
import { PaginatedHasManyFieldProvider } from './extensions/paginatedHasMany/PaginatedHasManyFieldProvider'
|
|
34
|
-
import { WhereOptimizer } from './mapper/select/optimizer/WhereOptimizer'
|
|
34
|
+
import { WhereOptimizer, WhereOptimizerOptions } from './mapper/select/optimizer/WhereOptimizer'
|
|
35
35
|
import { ConditionOptimizer } from './mapper/select/optimizer/ConditionOptimizer'
|
|
36
36
|
import { GraphQlQueryAstFactory, MutationResolver, ReadResolver, ValidationResolver } from './resolvers'
|
|
37
37
|
import {
|
|
@@ -69,6 +69,7 @@ export class ExecutionContainerFactory {
|
|
|
69
69
|
|
|
70
70
|
constructor(
|
|
71
71
|
private readonly providers: Providers,
|
|
72
|
+
private readonly whereOptimizerOptions?: WhereOptimizerOptions,
|
|
72
73
|
) {
|
|
73
74
|
}
|
|
74
75
|
|
|
@@ -108,7 +109,7 @@ export class ExecutionContainerFactory {
|
|
|
108
109
|
.addService('pathFactory', () =>
|
|
109
110
|
new PathFactory())
|
|
110
111
|
.addService('whereOptimized', ({ schema }) =>
|
|
111
|
-
new WhereOptimizer(schema.model, new ConditionOptimizer()))
|
|
112
|
+
new WhereOptimizer(schema.model, new ConditionOptimizer(), this.whereOptimizerOptions))
|
|
112
113
|
.addService('whereBuilder', ({ joinBuilder, conditionBuilder, pathFactory, whereOptimized, schema }) =>
|
|
113
114
|
new WhereBuilder(
|
|
114
115
|
schema.model,
|
|
@@ -166,8 +167,8 @@ export class ExecutionContainerFactory {
|
|
|
166
167
|
new JunctionTableManager(schema.model, predicateFactory, whereBuilder, connectJunctionHandler, disconnectJunctionHandler, pathFactory))
|
|
167
168
|
.addService('deleteExecutor', ({ predicateFactory, updateBuilderFactory, whereBuilder, pathFactory, schema }) =>
|
|
168
169
|
new DeleteExecutor(schema.model, predicateFactory, whereBuilder, updateBuilderFactory, pathFactory))
|
|
169
|
-
.addService('updater', ({
|
|
170
|
-
new Updater(schema.model,
|
|
170
|
+
.addService('updater', ({ updateBuilderFactory, schema }) =>
|
|
171
|
+
new Updater(schema.model, updateBuilderFactory))
|
|
171
172
|
.addService('inserter', ({ insertBuilderFactory, providers, schema, schemaDatabaseMetadata }) =>
|
|
172
173
|
new Inserter(schema.model, schemaDatabaseMetadata, insertBuilderFactory, providers))
|
|
173
174
|
.addService('mapperFactory', ({ predicatesInjector, selectBuilderFactory, uniqueWhereExpander, whereBuilder, junctionTableManager, deleteExecutor, updater, inserter, pathFactory, providers, schema, schemaDatabaseMetadata }) => {
|
package/src/mapper/Result.ts
CHANGED
|
@@ -178,7 +178,7 @@ export class MutationEntryNotFoundError implements MutationResultInterface {
|
|
|
178
178
|
hints: MutationResultHint[] = []
|
|
179
179
|
message: string
|
|
180
180
|
|
|
181
|
-
constructor(public readonly paths: Path[], public readonly where: Input.UniqueWhere | Input.
|
|
181
|
+
constructor(public readonly paths: Path[], public readonly where: Input.UniqueWhere | Input.OptionalWhere) {
|
|
182
182
|
this.message = 'for input ' + JSON.stringify(where)
|
|
183
183
|
}
|
|
184
184
|
}
|
|
@@ -18,16 +18,25 @@ export interface WhereOptimizationHints {
|
|
|
18
18
|
|
|
19
19
|
type OptimizedOperand = Input.OptionalWhere | undefined | boolean
|
|
20
20
|
|
|
21
|
+
export type WhereOptimizerOptions = {
|
|
22
|
+
disable?: boolean
|
|
23
|
+
maxCrossOptimizationInput?: number
|
|
24
|
+
}
|
|
25
|
+
|
|
21
26
|
export class WhereOptimizer {
|
|
22
27
|
private static eliminableRelations = new Set<Model.AnyRelationContext['type']>(['oneHasMany', 'oneHasOneInverse', 'oneHasOneOwning'])
|
|
23
28
|
|
|
24
29
|
constructor(
|
|
25
30
|
private readonly model: Model.Schema,
|
|
26
31
|
private readonly conditionOptimizer: ConditionOptimizer,
|
|
32
|
+
private readonly options?: WhereOptimizerOptions,
|
|
27
33
|
) {
|
|
28
34
|
}
|
|
29
35
|
|
|
30
36
|
public optimize(where: Input.OptionalWhere, entity: Model.Entity, { relationPath = [], evaluatedPredicates = [] }: WhereOptimizationHints = {}): Input.OptionalWhere {
|
|
37
|
+
if (this.options?.disable) {
|
|
38
|
+
return where
|
|
39
|
+
}
|
|
31
40
|
let processedRelationPath: ExtendedRelationContext[] = []
|
|
32
41
|
for (const i in relationPath) {
|
|
33
42
|
const el = relationPath[i]
|
|
@@ -134,7 +143,7 @@ export class WhereOptimizer {
|
|
|
134
143
|
private crossOptimize(operands: Input.OptionalWhere[], entity: Model.Entity, replacement: Input.Condition, relationPath: ExtendedRelationContext[]): (Input.OptionalWhere | boolean)[] {
|
|
135
144
|
let result: (Input.OptionalWhere | boolean)[] = operands
|
|
136
145
|
let copied = false
|
|
137
|
-
const count = result.length
|
|
146
|
+
const count = Math.min(result.length, this.options?.maxCrossOptimizationInput ?? 50)
|
|
138
147
|
for (let i = 0; i < count; i++) {
|
|
139
148
|
for (let j = 0; j < count; j++) {
|
|
140
149
|
const a = result[j]
|
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
import { SqlUpdateInputProcessor, SqlUpdateInputProcessorResult } from './SqlUpdateInputProcessor'
|
|
2
2
|
import { UpdateInputVisitor } from '../../inputProcessing'
|
|
3
3
|
import { Input, Model } from '@contember/schema'
|
|
4
|
-
import { PredicateFactory } from '../../acl'
|
|
5
4
|
import { UpdateBuilderFactory } from './UpdateBuilderFactory'
|
|
6
5
|
import { Mapper } from '../Mapper'
|
|
7
6
|
import { acceptFieldVisitor } from '@contember/schema-utils'
|
|
8
|
-
import { MutationNoResultError, MutationNothingToDo, MutationResultList, MutationUpdateOk, NothingToDoReason } from '../Result'
|
|
7
|
+
import { MutationEntryNotFoundError, MutationNoResultError, MutationNothingToDo, MutationResultList, MutationUpdateOk, NothingToDoReason } from '../Result'
|
|
9
8
|
import { UpdateBuilder } from './UpdateBuilder'
|
|
10
9
|
import { rowDataToFieldValues } from '../ColumnValue'
|
|
11
|
-
import { DatabaseMetadata } from '@contember/database'
|
|
12
10
|
import { MapperInput } from '../types'
|
|
13
11
|
|
|
14
12
|
export class Updater {
|
|
15
13
|
constructor(
|
|
16
14
|
private readonly schema: Model.Schema,
|
|
17
|
-
private readonly schemaDatabaseMetadata: DatabaseMetadata,
|
|
18
|
-
private readonly predicateFactory: PredicateFactory,
|
|
19
15
|
private readonly updateBuilderFactory: UpdateBuilderFactory,
|
|
20
16
|
) {}
|
|
21
17
|
|
|
@@ -66,6 +62,21 @@ export class Updater {
|
|
|
66
62
|
const result = await updateBuilder.execute(mapper)
|
|
67
63
|
|
|
68
64
|
if (!result.executed) {
|
|
65
|
+
if (filter && Object.keys(filter).length > 0) {
|
|
66
|
+
// direct update was not invoked, but we still need to check if the row matches the filter
|
|
67
|
+
const where: Input.OptionalWhere = {
|
|
68
|
+
and: [
|
|
69
|
+
filter,
|
|
70
|
+
{
|
|
71
|
+
[entity.primary]: { eq: primaryValue },
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
}
|
|
75
|
+
const count = await mapper.count(entity, where)
|
|
76
|
+
if (!count) {
|
|
77
|
+
return [new MutationEntryNotFoundError([], where)]
|
|
78
|
+
}
|
|
79
|
+
}
|
|
69
80
|
resultList.unshift(new MutationNothingToDo([], NothingToDoReason.noData))
|
|
70
81
|
} else if (result.affectedRows !== 1) {
|
|
71
82
|
return [new MutationNoResultError([])]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { test } from 'bun:test'
|
|
2
|
-
import { execute, sqlTransaction } from '../../../../src/test'
|
|
3
|
-
import { SchemaBuilder } from '@contember/schema-definition'
|
|
2
|
+
import { execute, failedTransaction, sqlTransaction } from '../../../../src/test'
|
|
3
|
+
import { c, createSchema, SchemaBuilder } from '@contember/schema-definition'
|
|
4
4
|
import { Model } from '@contember/schema'
|
|
5
5
|
import { GQL, SQL } from '../../../../src/tags'
|
|
6
6
|
import { testUuid } from '../../../../src/testUuid'
|
|
@@ -59,3 +59,52 @@ test('applies a filter', async () => {
|
|
|
59
59
|
})
|
|
60
60
|
})
|
|
61
61
|
|
|
62
|
+
|
|
63
|
+
namespace FilterModel {
|
|
64
|
+
export class Article {
|
|
65
|
+
publishedAt = c.dateTimeColumn()
|
|
66
|
+
tags = c.manyHasMany(Tag)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class Tag {
|
|
70
|
+
name = c.stringColumn()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
test('applies a filter with only relation update', async () => {
|
|
75
|
+
await execute({
|
|
76
|
+
schema: createSchema(FilterModel).model,
|
|
77
|
+
query: GQL`mutation {
|
|
78
|
+
updateArticle(
|
|
79
|
+
by: {id: "${testUuid(1)}"},
|
|
80
|
+
filter: {publishedAt: {isNull: false}},
|
|
81
|
+
data: {tags: {connect: {id: "${testUuid(2)}"}}}
|
|
82
|
+
) {
|
|
83
|
+
ok
|
|
84
|
+
errorMessage
|
|
85
|
+
}
|
|
86
|
+
}`,
|
|
87
|
+
executes: [
|
|
88
|
+
...failedTransaction([
|
|
89
|
+
{
|
|
90
|
+
sql: SQL`select "root_"."id" from "public"."article" as "root_" where "root_"."id" = ?`,
|
|
91
|
+
parameters: [testUuid(1)],
|
|
92
|
+
response: { rows: [{ id: testUuid(1) }] },
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
sql: SQL`select count(*) as "row_count" from "public"."article" as "root_" where not("root_"."published_at" is null) and "root_"."id" = ?`,
|
|
96
|
+
parameters: [testUuid(1)],
|
|
97
|
+
response: { rows: [{ row_count: 0 }] },
|
|
98
|
+
},
|
|
99
|
+
]),
|
|
100
|
+
],
|
|
101
|
+
return: {
|
|
102
|
+
data: {
|
|
103
|
+
updateArticle: {
|
|
104
|
+
ok: false,
|
|
105
|
+
errorMessage: `Execution has failed:\nunknown field: NotFoundOrDenied (for input {\"and\":[{\"publishedAt\":{\"isNull\":false}},{\"id\":{\"eq\":\"123e4567-e89b-12d3-a456-000000000001\"}}]})`,
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
})
|
|
110
|
+
})
|