Skip to content

Commit

Permalink
fixed(lint): lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgobich committed Sep 18, 2024
1 parent 219d86e commit 36d8d66
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const ignoreTables = ['adonis_schema', 'adonis_schema_versions']

/**
* parse schema information from the provided database connection
* @param db
* @returns
* @param db
* @returns
*/
export async function schema(db: Database) {
const knex = db.connection().getWriteClient()
Expand Down
34 changes: 26 additions & 8 deletions src/extractors/import_extractor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Model from "../model/index.js"
import Model from '../model/index.js'
import string from '@adonisjs/core/helpers/string'

class ModelImport {
Expand All @@ -8,7 +8,12 @@ class ModelImport {
isDefault = false
isType = false

constructor(name: string, namespace: string, isDefault: boolean | null = null, isType: boolean | null = null) {
constructor(
name: string,
namespace: string,
isDefault: boolean | null = null,
isType: boolean | null = null
) {
this.name = name
this.namespace = namespace
this.isDefault = isDefault ?? this.isDefault
Expand All @@ -18,7 +23,7 @@ class ModelImport {
/**
* converts model imports into import strings, grouped by import path
* @param imports
* @returns
* @returns
*/
static getStatements(imports: ModelImport[]) {
const groups = this.#getNamespaceGroups(imports)
Expand All @@ -41,7 +46,7 @@ class ModelImport {
/**
* group imports by their path
* @param imports
* @returns
* @returns
*/
static #getNamespaceGroups(imports: ModelImport[]) {
return imports.reduce<Record<string, ModelImport[]>>((groups, imp) => {
Expand Down Expand Up @@ -85,8 +90,8 @@ export default class ModelImportManager {

/**
* extract import statements from provided model
* @param model
* @returns
* @param model
* @returns
*/
extract(model: Model) {
this.add(new ModelImport('BaseModel', '@adonisjs/lucid/orm'))
Expand All @@ -100,11 +105,24 @@ export default class ModelImportManager {

model.relationships.map((definition) => {
if (definition.relatedModelName !== model.name) {
this.add(new ModelImport(definition.relatedModelName, `./${string.snakeCase(definition.relatedModelName)}.js`, true))
this.add(
new ModelImport(
definition.relatedModelName,
`./${string.snakeCase(definition.relatedModelName)}.js`,
true
)
)
}

this.add(new ModelImport(definition.type, '@adonisjs/lucid/orm'))
this.add(new ModelImport(string.pascalCase(definition.type), '@adonisjs/lucid/types/relations', false, true))
this.add(
new ModelImport(
string.pascalCase(definition.type),
'@adonisjs/lucid/types/relations',
false,
true
)
)
})

return ModelImport.getStatements([...this.#imports.values()])
Expand Down
67 changes: 39 additions & 28 deletions src/extractors/relationship_extractor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import RelationshipTypes from "../enums/relationship_types.js";
import ModelColumn from "../model/column.js";
import Model from "../model/index.js";
import string from '@adonisjs/core/helpers/string';
import RelationshipTypes from '../enums/relationship_types.js'
import ModelColumn from '../model/column.js'
import Model from '../model/index.js'
import string from '@adonisjs/core/helpers/string'

type RelationMap = {
type: RelationshipTypes
Expand Down Expand Up @@ -29,7 +29,7 @@ export class ModelRelationship {
declare relatedModelName: string
declare decoratorOptions: DecoratorOptions | undefined
declare map: RelationMap

declare decorator: string
declare property: string

Expand All @@ -49,8 +49,8 @@ export class ModelRelationship {

/**
* gets property name for the relationship definition
* @param relationMap
* @returns
* @param relationMap
* @returns
*/
#getPropertyName(relationMap: RelationMap) {
let propertyName = string.camelCase(relationMap.foreignKeyModel.name)
Expand All @@ -69,13 +69,15 @@ export class ModelRelationship {

/**
* gets the relationship's decorator options (if needed)
* @param relationMap
* @returns
* @param relationMap
* @returns
*/
#getDecoratorOptions(relationMap: RelationMap): DecoratorOptions | undefined {
switch (relationMap.type) {
case RelationshipTypes.MANY_TO_MANY:
const defaultPivotName = string.snakeCase([relationMap.model.name, relationMap.foreignKeyModel.name].sort().join('_'))
const defaultPivotName = string.snakeCase(
[relationMap.model.name, relationMap.foreignKeyModel.name].sort().join('_')
)

if (relationMap.pivotTableName === defaultPivotName) return

Expand All @@ -88,27 +90,27 @@ export class ModelRelationship {
if (relationMap.column.name === defaultBelongsName) return

return {
foreignKey: relationMap.column.name
foreignKey: relationMap.column.name,
}
case RelationshipTypes.HAS_MANY:
case RelationshipTypes.HAS_ONE:
const defaultHasName = string.camelCase(relationMap.model.name + 'Id')

if (relationMap.foreignKeyColumn.name === defaultHasName) return

return {
foreignKey: relationMap.foreignKeyColumn.name
foreignKey: relationMap.foreignKeyColumn.name,
}
}
}

/**
* converts the populated decorator options into a string for the stub
* @returns
* @returns
*/
#getDecoratorString() {
const keys = Object.keys(this.decoratorOptions || {}) as Array<keyof DecoratorOptions>

if (!keys.length) return ''

const inner = keys.reduce((str, key) => {
Expand All @@ -122,56 +124,63 @@ export class ModelRelationship {

/**
* gets definition decorator and property definition lines for the stub
* @returns
* @returns
*/
#getDefinition() {
return {
decorator: `@${this.type}(() => ${this.relatedModelName}${this.#getDecoratorString()})`,
property: `declare ${this.propertyName}: ${string.pascalCase(this.type)}<typeof ${this.relatedModelName}>`
property: `declare ${this.propertyName}: ${string.pascalCase(this.type)}<typeof ${this.relatedModelName}>`,
}
}
}

export default class ModelRelationshipManager {

constructor(protected models: Model[]) {}

/**
* extract relationships from the loaded models
* @returns
* @returns
*/
extract() {
const relationshpMaps = this.#getRelationshipMaps()
return relationshpMaps.map((map) => new ModelRelationship(map))
}

/**
* get mappings for the model's relationships with information
* get mappings for the model's relationships with information
* needed to populate their definitions
* @returns
* @returns
*/
#getRelationshipMaps() {
const belongsTos = this.#getBelongsTos()
const relationships: RelationMap[] = []

belongsTos.map((belongsTo) => {
const tableNamesSingular = this.models
.filter((model) => model.tableName !== belongsTo.column.tableName)
.map((model) => ({ singular: string.singular(model.tableName), model }))

// try to build a pivot table by matching table names with the current table name
const tableNameSingular = string.singular(belongsTo.column.tableName)
const startsWithTable = tableNamesSingular.find((name) => tableNameSingular.startsWith(name.singular))
const endsWithTable = tableNamesSingular.find((name) => tableNameSingular.endsWith(name.singular))
const startsWithTable = tableNamesSingular.find((name) =>
tableNameSingular.startsWith(name.singular)
)
const endsWithTable = tableNamesSingular.find((name) =>
tableNameSingular.endsWith(name.singular)
)
const pivotName = `${startsWithTable?.singular}_${endsWithTable?.singular}`

// if they match, consider it a pivot and build a many-to-many relationship from the belongsTo info
if (tableNameSingular === pivotName) {
const isStartsWith = startsWithTable?.model.name === belongsTo.foreignKeyModel.name
const relatedModel = isStartsWith ? endsWithTable!.model : startsWithTable!.model
const relatedColumn = isStartsWith
? relatedModel.columns.find((column) => column.tableName === endsWithTable?.model.tableName)
: relatedModel.columns.find((column) => column.tableName === startsWithTable?.model.tableName)
? relatedModel.columns.find(
(column) => column.tableName === endsWithTable?.model.tableName
)
: relatedModel.columns.find(
(column) => column.tableName === startsWithTable?.model.tableName
)

// mark the model as a pivot, so it can be ignored
belongsTo.model.isPivotTable = true
Expand Down Expand Up @@ -209,7 +218,7 @@ export default class ModelRelationshipManager {
/**
* get the belongs to relationships from the foreign key definitions on the models
* we'll work backwards from here
* @returns
* @returns
*/
#getBelongsTos() {
const belongsTos: RelationMap[] = []
Expand All @@ -219,7 +228,9 @@ export default class ModelRelationshipManager {
if (!column.foreignKeyTable) return

const foreignKeyModel = this.models.find((m) => m.tableName === column.foreignKeyTable)
const foreignKeyColumn = foreignKeyModel?.columns.find((c) => column.foreignKeyColumn === c.columnName)
const foreignKeyColumn = foreignKeyModel?.columns.find(
(c) => column.foreignKeyColumn === c.columnName
)

if (!foreignKeyColumn || !foreignKeyModel) return

Expand Down
Loading

0 comments on commit 36d8d66

Please sign in to comment.