Skip to content

Commit

Permalink
fixed(relationships): issue where generated foreignKey wasn't unique …
Browse files Browse the repository at this point in the history
…to a relation
  • Loading branch information
tomgobich committed Sep 17, 2024
1 parent 49538ec commit d60fbf4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
7 changes: 1 addition & 6 deletions commands/generate_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ export default class GenerateModels extends BaseCommand {
const models = Model.build(tables)

for (let model of models) {
await codemods.makeUsingStub(stubsRoot, 'generate/model.stub', {
model,
relationships: model.relationships.reduce<{ decorator: string; property: string; }[]>((relationships, relationship) => {
return [...relationships, ...relationship.getDefinitions(model.name)]
}, [])
})
await codemods.makeUsingStub(stubsRoot, 'generate/model.stub', { model })
}
}
}
11 changes: 8 additions & 3 deletions src/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { generators } from '@adonisjs/core/app'
import ModelColumn from './column.js'
import { TableSchema } from '../db/schema.js'
import ModelRelationship from './relationship.js'
import ModelRelationship, { ModelRelationshipDefinition } from './relationship.js'

export default class Model {
declare name: string
declare fileName: string
declare tableName: string
declare columns: ModelColumn[]
declare relationships: ModelRelationship[]
declare relationships: ModelRelationshipDefinition[]

isPivotTable: boolean = false

Expand All @@ -27,8 +27,13 @@ export default class Model {
const ships = relationships.get(model.name)
const values = [...(ships?.values() || [])]

model.relationships = values
model.isPivotTable = values.filter((relation) => relation.isManyToMany)?.length >= 2
model.relationships = values.reduce<ModelRelationshipDefinition[]>(
(relationships, relationship) => {
return [...relationships, ...relationship.getDefinitions(model.name)]
},
[]
)
}

return models.filter((model) => !model.isPivotTable)
Expand Down
14 changes: 10 additions & 4 deletions src/model/relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export type ModelRelationshipInfo = {
relatedModelName: string
}

export type ModelRelationshipDefinition = {
type: RelationshipTypes
decorator: string
property: string
}

export default class ModelRelationship {
declare foreignKey: string
declare parent: ModelRelationshipInfo
Expand All @@ -27,8 +33,6 @@ export default class ModelRelationship {
? [column.foreignKeyColumn, column.columnName]
: [column.columnName, column.foreignKeyColumn]

this.foreignKey = `${column.foreignKeyTable}.${column.foreignKeyColumn}`

this.parent = {
type: type,
tableName: parentModel!.tableName,
Expand All @@ -47,6 +51,8 @@ export default class ModelRelationship {
relatedModelName: parentModel!.name
}

this.foreignKey = `${this.parent.tableName}.${this.parent.tableColumn}_${this.child.tableName}.${this.child.tableColumn}`

this.#setTypes(type, column, models)
}

Expand All @@ -57,7 +63,7 @@ export default class ModelRelationship {
}

getDefinitions(modelName: string) {
const definitions = []
const definitions: ModelRelationshipDefinition[] = []

if (modelName === this.parent.modelName) {
definitions.push(this.#getDefinition(this.parent))
Expand All @@ -70,7 +76,7 @@ export default class ModelRelationship {
return definitions
}

#getDefinition(info: ModelRelationshipInfo) {
#getDefinition(info: ModelRelationshipInfo): ModelRelationshipDefinition {
let propertyName = string.camelCase(info.relatedModelName)

switch (info.type) {
Expand Down
10 changes: 4 additions & 6 deletions stubs/generate/model.stub
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { DateTime } from 'luxon'
import { BaseModel, column } from '@adonisjs/lucid/orm'

export default class {{ model.name }} extends BaseModel {
{{ #each model.columns as column }}
export default class {{ model.name }} extends BaseModel {{ '{' }}{{ #each model.columns as column }}
{{ column.getDecorator() }}
declare {{ column.name }}: {{ column.type }}
{{ /each }}
{{ #each relationships as relationship }}
{{ /each }}{{ #each model.relationships as relationship, index }}
{{ relationship.decorator }}
{{{ relationship.property }}}
{{ /each }}
{{{ relationship.property }}}{{ index + 1 < model.relationships.length ? '\n' : '' }}
{{ /each }}
}

{{{
Expand Down

0 comments on commit d60fbf4

Please sign in to comment.