翻译进度
3
分块数量
0
参与人数

命名策略

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。


The NamingStrategy class allows you to override the conventional names used by the ORM. For example: Instead of using the snake_case for the column names, you can provide your own custom camelCase naming strategy.

Every naming strategy must implement the NamingStrategyContract contract and define all the required methods.

import { SnakeCaseNamingStrategy } from '@ioc:Adonis/Lucid/Orm'

class CamelCaseNamingStrategy extends SnakeCaseNamingStrategy {
  //... define all the required methods
}

Assign it to a model

class User extends BaseModel {
  public static namingStrategy = new CamelCaseNamingStrategy()
}

Or assign it to the Base model directly. Make sure to do it only once and import the file when booting the framework.

import { BaseModel } from '@ioc:Adonis/Lucid/Orm'
BaseModel.namingStrategy = new CamelCaseNamingStrategy()

Methods/Properties

Following is the list of methods/properties you must define in the naming strategy class.

tableName

Return the default table name for the model. The default naming strategy converts the model name to snake_case and pluralizes it.

import { string } from '@ioc:Adonis/Core/Helpers'
import { SnakeCaseNamingStrategy, BaseModel } from '@ioc:Adonis/Lucid/Orm'

class CamelCaseNamingStrategy extends SnakeCaseNamingStrategy {
  public tableName(model: typeof BaseModel) {
    return string.pluralize(string.snakeCase(model.name))
  }
}

columnName

Return the database column name for a given model property. The default naming strategy converts the model property to snake_case.

import { string } from '@ioc:Adonis/Core/Helpers'
import { SnakeCaseNamingStrategy, BaseModel } from '@ioc:Adonis/Lucid/Orm'

class CamelCaseNamingStrategy extends SnakeCaseNamingStrategy {
  public columnName(_model: typeof BaseModel, propertyName: string) {
    return string.snakeCase(propertyName)
  }
}

serializedName

Return name to be used when serializing the model properties to JSON. The default naming strategy converts the model property to snake_case.

import { string } from '@ioc:Adonis/Core/Helpers'
import { SnakeCaseNamingStrategy, BaseModel } from '@ioc:Adonis/Lucid/Orm'

class CamelCaseNamingStrategy extends SnakeCaseNamingStrategy {
  public serializedName(_model: typeof BaseModel, propertyName: string) {
    return string.snakeCase(propertyName)
  }
}

relationLocalKey

Return the local key for a given relationship. The default behavior is to use the primaryKey as the local key for all the relationships except belongsTo.

import { string } from '@ioc:Adonis/Core/Helpers'
import { SnakeCaseNamingStrategy, BaseModel } from '@ioc:Adonis/Lucid/Orm'

class CamelCaseNamingStrategy extends SnakeCaseNamingStrategy {
  public relationLocalKey(
    relation: string,
    model: typeof BaseModel,
    relatedModel: typeof BaseModel
  ) {
    if (relation === 'belongsTo') {
      return relatedModel.primaryKey
    }

    return model.primaryKey
  }
}

relationForeignKey

Return the foreign key for a given relationship. The default naming strategy combines the modelName and the primaryKey column name and converts them to camelCase.

The foreignKey points to the model property and not the database column name. We derive the database column name from the model property name.

import { string } from '@ioc:Adonis/Core/Helpers'
import { SnakeCaseNamingStrategy, BaseModel } from '@ioc:Adonis/Lucid/Orm'

class CamelCaseNamingStrategy extends SnakeCaseNamingStrategy {
  public relationForeignKey(
    relation: string,
    model: typeof BaseModel,
    relatedModel: typeof BaseModel
  ) {
    if (relation === 'belongsTo') {
      return string.camelCase(`${relatedModel.name}_${relatedModel.primaryKey}`)
    }

    return string.camelCase(`${model.name}_${model.primaryKey}`)
  }
}

relationPivotTable

Return the pivot table name for the manyToMany relationship. The default naming strategy concatenates the model names together and sorts them alphabetically.

import { string } from '@ioc:Adonis/Core/Helpers'
import { SnakeCaseNamingStrategy, BaseModel } from '@ioc:Adonis/Lucid/Orm'

class CamelCaseNamingStrategy extends SnakeCaseNamingStrategy {
  public relationPivotTable(
    _relation: 'manyToMany',
    model: typeof BaseModel,
    relatedModel: typeof BaseModel
  ) {
    return string.snakeCase(
      [relatedModel.name, model.name]
        .sort()
        .join('_')
    )
  }
}

relationPivotForeignKey

Return the foreign key name inside the pivot table. The method is invoked for both the models involved in a manyToMany relationship.

import { string } from '@ioc:Adonis/Core/Helpers'
import { SnakeCaseNamingStrategy, BaseModel } from '@ioc:Adonis/Lucid/Orm'

class CamelCaseNamingStrategy extends SnakeCaseNamingStrategy {
  public relationPivotForeignKey(
    _relation: 'manyToMany',
    model: typeof BaseModel
  ) {
    return string.snakeCase(`${model.name}_${model.primaryKey}`)
  }
}

paginationMetaKeys

Return the keys to generate the metadata for the paginator. The default naming strategy uses snake_case names.

import { string } from '@ioc:Adonis/Core/Helpers'
import { SnakeCaseNamingStrategy, BaseModel } from '@ioc:Adonis/Lucid/Orm'

class CamelCaseNamingStrategy extends SnakeCaseNamingStrategy {
  public paginationMetaKeys() {
    return {
      total: 'total',
      perPage: 'per_page',
      currentPage: 'current_page',
      lastPage: 'last_page',
      firstPage: 'first_page',
      firstPageUrl: 'first_page_url',
      lastPageUrl: 'last_page_url',
      nextPageUrl: 'next_page_url',
      previousPageUrl: 'previous_page_url',
    }
  }
}

If you are paginating results using the Database module directly, you must register the naming strategy with the SimplePaginator class.

import Database from '@ioc:Adonis/Lucid/Database'

Database.SimplePaginator.namingStrategy = new CamelCaseNamingStrategy()

The above example will configure the naming strategy for the paginator globally. However, you can also define the naming strategy for a given .paginate method call.

import Database from '@ioc:Adonis/Lucid/Database'

const paginator = await Database.from('users').paginate()
paginator.namingStrategy = new CamelCaseNamingStrategy()

return paginator.toJSON()

本文章首发在 LearnKu.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

讨论数量: 0
发起讨论 只看当前版本


暂无话题~