diff --git a/commands/generate_models.ts b/commands/generate_models.ts index 87e6299..d2ec602 100644 --- a/commands/generate_models.ts +++ b/commands/generate_models.ts @@ -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 }) } } } diff --git a/src/model/index.ts b/src/model/index.ts index faa43c3..4143476 100644 --- a/src/model/index.ts +++ b/src/model/index.ts @@ -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 @@ -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( + (relationships, relationship) => { + return [...relationships, ...relationship.getDefinitions(model.name)] + }, + [] + ) } return models.filter((model) => !model.isPivotTable) diff --git a/src/model/relationship.ts b/src/model/relationship.ts index a9ab3df..63a1c9e 100644 --- a/src/model/relationship.ts +++ b/src/model/relationship.ts @@ -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 @@ -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, @@ -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) } @@ -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)) @@ -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) { diff --git a/stubs/generate/model.stub b/stubs/generate/model.stub index 9e0b514..9e91c2e 100644 --- a/stubs/generate/model.stub +++ b/stubs/generate/model.stub @@ -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 }} } {{{