Skip to content

Commit

Permalink
fix(client-content): omit backreferencing relation from an unique
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Feb 23, 2024
1 parent 3eb70e3 commit 449d531
Show file tree
Hide file tree
Showing 16 changed files with 512 additions and 241 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/ee/*/temp
/ee/*/node_modules
/packages/playground/api/client
/packages/client-content/tests/client
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type JSONArray = readonly JSONValue[]
}

private generateTypeEntityCode(model: Model.Schema, entity: Model.Entity): string {
let code = `export type ${entity.name} = {\n`
let code = `export type ${entity.name} <OverRelation extends string | never = never> = {\n`
code += '\tname: \'' + entity.name + '\'\n'
code += '\tunique:\n'
code += this.formatUniqueFields(model, entity)
Expand All @@ -41,7 +41,7 @@ export type JSONArray = readonly JSONValue[]
let hasManyCode = ''
acceptEveryFieldVisitor(model, entity, {
visitHasMany: ctx => {
hasManyCode += `\t\t${ctx.relation.name}: ${ctx.targetEntity.name}\n`
hasManyCode += `\t\t${ctx.relation.name}: ${ctx.targetEntity.name}${ctx.targetRelation?.type === Model.RelationType.ManyHasOne ? `<'${ctx.targetRelation.name}'>` : ''}\n`
},
visitHasOne: ctx => {
hasOneCode += `\t\t${ctx.relation.name}: ${ctx.targetEntity.name}\n`
Expand Down Expand Up @@ -117,9 +117,9 @@ export type JSONArray = readonly JSONValue[]
const fields = getFieldsForUniqueWhere(model, entity)
let code = ''
for (const field of fields) {
code += '\t\t| { '
code += '\t\t| Omit<{ '
code += field.map(it => `${it}: ${uniqueType(model, entity, entity.fields[it])}`).join(', ')
code += ' }\n'
code += '}, OverRelation>\n'
}
return code
}
Expand Down
54 changes: 27 additions & 27 deletions packages/client-content-generator/tests/generateEnittyTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ describe('generate entities', () => {
export type JSONObject = { readonly [K in string]?: JSONValue }
export type JSONArray = readonly JSONValue[]
export type Foo = {
export type Foo <OverRelation extends string | never = never> = {
name: 'Foo'
unique:
| { id: string }
| Omit<{ id: string}, OverRelation>
columns: {
id: string
stringCol: string | null
Expand Down Expand Up @@ -59,10 +59,10 @@ describe('generate entities', () => {
export type JSONObject = { readonly [K in string]?: JSONValue }
export type JSONArray = readonly JSONValue[]
export type Foo = {
export type Foo <OverRelation extends string | never = never> = {
name: 'Foo'
unique:
| { id: string }
| Omit<{ id: string}, OverRelation>
columns: {
id: string
enumCol: FooEnumCol | null
Expand Down Expand Up @@ -94,11 +94,11 @@ describe('generate entities', () => {
export type JSONObject = { readonly [K in string]?: JSONValue }
export type JSONArray = readonly JSONValue[]
export type Foo = {
export type Foo <OverRelation extends string | never = never> = {
name: 'Foo'
unique:
| { id: string }
| { oneHasOneInverseRel: Bar['unique'] }
| Omit<{ id: string}, OverRelation>
| Omit<{ oneHasOneInverseRel: Bar['unique']}, OverRelation>
columns: {
id: string
}
Expand All @@ -110,11 +110,11 @@ describe('generate entities', () => {
hasManyBy: {
}
}
export type Bar = {
export type Bar <OverRelation extends string | never = never> = {
name: 'Bar'
unique:
| { id: string }
| { oneHasOneOwningRel: Foo['unique'] }
| Omit<{ id: string}, OverRelation>
| Omit<{ oneHasOneOwningRel: Foo['unique']}, OverRelation>
columns: {
id: string
}
Expand Down Expand Up @@ -148,26 +148,26 @@ describe('generate entities', () => {
export type JSONObject = { readonly [K in string]?: JSONValue }
export type JSONArray = readonly JSONValue[]
export type Foo = {
export type Foo <OverRelation extends string | never = never> = {
name: 'Foo'
unique:
| { id: string }
| { oneHasManyRel: Bar['unique'] }
| Omit<{ id: string}, OverRelation>
| Omit<{ oneHasManyRel: Bar['unique']}, OverRelation>
columns: {
id: string
}
hasOne: {
}
hasMany: {
oneHasManyRel: Bar
oneHasManyRel: Bar<'manyHasOneRel'>
}
hasManyBy: {
}
}
export type Bar = {
export type Bar <OverRelation extends string | never = never> = {
name: 'Bar'
unique:
| { id: string }
| Omit<{ id: string}, OverRelation>
columns: {
id: string
}
Expand Down Expand Up @@ -201,10 +201,10 @@ describe('generate entities', () => {
export type JSONObject = { readonly [K in string]?: JSONValue }
export type JSONArray = readonly JSONValue[]
export type Foo = {
export type Foo <OverRelation extends string | never = never> = {
name: 'Foo'
unique:
| { id: string }
| Omit<{ id: string}, OverRelation>
columns: {
id: string
}
Expand All @@ -216,10 +216,10 @@ describe('generate entities', () => {
hasManyBy: {
}
}
export type Bar = {
export type Bar <OverRelation extends string | never = never> = {
name: 'Bar'
unique:
| { id: string }
| Omit<{ id: string}, OverRelation>
columns: {
id: string
}
Expand Down Expand Up @@ -253,28 +253,28 @@ describe('generate entities', () => {
export type JSONObject = { readonly [K in string]?: JSONValue }
export type JSONArray = readonly JSONValue[]
export type Foo = {
export type Foo <OverRelation extends string | never = never> = {
name: 'Foo'
unique:
| { id: string }
| { locales: FooLocale['unique'] }
| Omit<{ id: string}, OverRelation>
| Omit<{ locales: FooLocale['unique']}, OverRelation>
columns: {
id: string
}
hasOne: {
}
hasMany: {
locales: FooLocale
locales: FooLocale<'foo'>
}
hasManyBy: {
localesByLocale: { entity: FooLocale; by: {locale: string} }
}
}
export type FooLocale = {
export type FooLocale <OverRelation extends string | never = never> = {
name: 'FooLocale'
unique:
| { id: string }
| { locale: string, foo: Foo['unique'] }
| Omit<{ id: string}, OverRelation>
| Omit<{ locale: string, foo: Foo['unique']}, OverRelation>
columns: {
id: string
locale: string
Expand Down
3 changes: 3 additions & 0 deletions packages/client-content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"@contember/graphql-client": "workspace:*",
"@contember/schema": "^1.3.6"
},
"devDependencies": {
"@contember/client-content-generator": "workspace:*"
},
"repository": {
"type": "git",
"url": "https://github.com/contember/interface.git",
Expand Down
140 changes: 0 additions & 140 deletions packages/client-content/tests/cases/unit/lib.ts

This file was deleted.

Loading

0 comments on commit 449d531

Please sign in to comment.