Skip to content

Commit 6cb9656

Browse files
committed
fix(client-content): omit backreferencing relation from an unique
1 parent 3eb70e3 commit 6cb9656

File tree

17 files changed

+514
-242
lines changed

17 files changed

+514
-242
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/ee/*/temp
77
/ee/*/node_modules
88
/packages/playground/api/client
9+
/packages/client-content/tests/client

packages/client-content-generator/src/EntityTypeSchemaGenerator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type JSONArray = readonly JSONValue[]
3232
}
3333

3434
private generateTypeEntityCode(model: Model.Schema, entity: Model.Entity): string {
35-
let code = `export type ${entity.name} = {\n`
35+
let code = `export type ${entity.name} <OverRelation extends string | never = never> = {\n`
3636
code += '\tname: \'' + entity.name + '\'\n'
3737
code += '\tunique:\n'
3838
code += this.formatUniqueFields(model, entity)
@@ -41,7 +41,7 @@ export type JSONArray = readonly JSONValue[]
4141
let hasManyCode = ''
4242
acceptEveryFieldVisitor(model, entity, {
4343
visitHasMany: ctx => {
44-
hasManyCode += `\t\t${ctx.relation.name}: ${ctx.targetEntity.name}\n`
44+
hasManyCode += `\t\t${ctx.relation.name}: ${ctx.targetEntity.name}${ctx.targetRelation?.type === Model.RelationType.ManyHasOne ? `<'${ctx.targetRelation.name}'>` : ''}\n`
4545
},
4646
visitHasOne: ctx => {
4747
hasOneCode += `\t\t${ctx.relation.name}: ${ctx.targetEntity.name}\n`
@@ -117,9 +117,9 @@ export type JSONArray = readonly JSONValue[]
117117
const fields = getFieldsForUniqueWhere(model, entity)
118118
let code = ''
119119
for (const field of fields) {
120-
code += '\t\t| { '
120+
code += '\t\t| Omit<{ '
121121
code += field.map(it => `${it}: ${uniqueType(model, entity, entity.fields[it])}`).join(', ')
122-
code += ' }\n'
122+
code += '}, OverRelation>\n'
123123
}
124124
return code
125125
}

packages/client-content-generator/tests/generateEnittyTypes.test.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ describe('generate entities', () => {
1515
export type JSONObject = { readonly [K in string]?: JSONValue }
1616
export type JSONArray = readonly JSONValue[]
1717
18-
export type Foo = {
18+
export type Foo <OverRelation extends string | never = never> = {
1919
name: 'Foo'
2020
unique:
21-
| { id: string }
21+
| Omit<{ id: string}, OverRelation>
2222
columns: {
2323
id: string
2424
stringCol: string | null
@@ -59,10 +59,10 @@ describe('generate entities', () => {
5959
export type JSONObject = { readonly [K in string]?: JSONValue }
6060
export type JSONArray = readonly JSONValue[]
6161
62-
export type Foo = {
62+
export type Foo <OverRelation extends string | never = never> = {
6363
name: 'Foo'
6464
unique:
65-
| { id: string }
65+
| Omit<{ id: string}, OverRelation>
6666
columns: {
6767
id: string
6868
enumCol: FooEnumCol | null
@@ -94,11 +94,11 @@ describe('generate entities', () => {
9494
export type JSONObject = { readonly [K in string]?: JSONValue }
9595
export type JSONArray = readonly JSONValue[]
9696
97-
export type Foo = {
97+
export type Foo <OverRelation extends string | never = never> = {
9898
name: 'Foo'
9999
unique:
100-
| { id: string }
101-
| { oneHasOneInverseRel: Bar['unique'] }
100+
| Omit<{ id: string}, OverRelation>
101+
| Omit<{ oneHasOneInverseRel: Bar['unique']}, OverRelation>
102102
columns: {
103103
id: string
104104
}
@@ -110,11 +110,11 @@ describe('generate entities', () => {
110110
hasManyBy: {
111111
}
112112
}
113-
export type Bar = {
113+
export type Bar <OverRelation extends string | never = never> = {
114114
name: 'Bar'
115115
unique:
116-
| { id: string }
117-
| { oneHasOneOwningRel: Foo['unique'] }
116+
| Omit<{ id: string}, OverRelation>
117+
| Omit<{ oneHasOneOwningRel: Foo['unique']}, OverRelation>
118118
columns: {
119119
id: string
120120
}
@@ -148,26 +148,26 @@ describe('generate entities', () => {
148148
export type JSONObject = { readonly [K in string]?: JSONValue }
149149
export type JSONArray = readonly JSONValue[]
150150
151-
export type Foo = {
151+
export type Foo <OverRelation extends string | never = never> = {
152152
name: 'Foo'
153153
unique:
154-
| { id: string }
155-
| { oneHasManyRel: Bar['unique'] }
154+
| Omit<{ id: string}, OverRelation>
155+
| Omit<{ oneHasManyRel: Bar['unique']}, OverRelation>
156156
columns: {
157157
id: string
158158
}
159159
hasOne: {
160160
}
161161
hasMany: {
162-
oneHasManyRel: Bar
162+
oneHasManyRel: Bar<'manyHasOneRel'>
163163
}
164164
hasManyBy: {
165165
}
166166
}
167-
export type Bar = {
167+
export type Bar <OverRelation extends string | never = never> = {
168168
name: 'Bar'
169169
unique:
170-
| { id: string }
170+
| Omit<{ id: string}, OverRelation>
171171
columns: {
172172
id: string
173173
}
@@ -201,10 +201,10 @@ describe('generate entities', () => {
201201
export type JSONObject = { readonly [K in string]?: JSONValue }
202202
export type JSONArray = readonly JSONValue[]
203203
204-
export type Foo = {
204+
export type Foo <OverRelation extends string | never = never> = {
205205
name: 'Foo'
206206
unique:
207-
| { id: string }
207+
| Omit<{ id: string}, OverRelation>
208208
columns: {
209209
id: string
210210
}
@@ -216,10 +216,10 @@ describe('generate entities', () => {
216216
hasManyBy: {
217217
}
218218
}
219-
export type Bar = {
219+
export type Bar <OverRelation extends string | never = never> = {
220220
name: 'Bar'
221221
unique:
222-
| { id: string }
222+
| Omit<{ id: string}, OverRelation>
223223
columns: {
224224
id: string
225225
}
@@ -253,28 +253,28 @@ describe('generate entities', () => {
253253
export type JSONObject = { readonly [K in string]?: JSONValue }
254254
export type JSONArray = readonly JSONValue[]
255255
256-
export type Foo = {
256+
export type Foo <OverRelation extends string | never = never> = {
257257
name: 'Foo'
258258
unique:
259-
| { id: string }
260-
| { locales: FooLocale['unique'] }
259+
| Omit<{ id: string}, OverRelation>
260+
| Omit<{ locales: FooLocale['unique']}, OverRelation>
261261
columns: {
262262
id: string
263263
}
264264
hasOne: {
265265
}
266266
hasMany: {
267-
locales: FooLocale
267+
locales: FooLocale<'foo'>
268268
}
269269
hasManyBy: {
270270
localesByLocale: { entity: FooLocale; by: {locale: string} }
271271
}
272272
}
273-
export type FooLocale = {
273+
export type FooLocale <OverRelation extends string | never = never> = {
274274
name: 'FooLocale'
275275
unique:
276-
| { id: string }
277-
| { locale: string, foo: Foo['unique'] }
276+
| Omit<{ id: string}, OverRelation>
277+
| Omit<{ locale: string, foo: Foo['unique']}, OverRelation>
278278
columns: {
279279
id: string
280280
locale: string

packages/client-content/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
"@contember/graphql-client": "workspace:*",
4040
"@contember/schema": "^1.3.6"
4141
},
42+
"devDependencies": {
43+
"@contember/client-content-generator": "workspace:*"
44+
},
4245
"repository": {
4346
"type": "git",
4447
"url": "https://github.com/contember/interface.git",

packages/client-content/tests/cases/unit/lib.ts

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)