@adonisjs/lucid
Version:
SQL ORM built on top of Active Record pattern
79 lines (78 loc) • 2.32 kB
JavaScript
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import string from '@poppinss/utils/string';
/**
* Uses snake case as the naming strategy for different model properties
*/
export class SnakeCaseNamingStrategy {
/**
* The default table name for the given model
*/
tableName(model) {
return string.pluralize(string.snakeCase(model.name));
}
/**
* The database column name for a given model attribute
*/
columnName(_, attributeName) {
return string.snakeCase(attributeName);
}
/**
* The post serialization name for a given model attribute
*/
serializedName(_, attributeName) {
return string.snakeCase(attributeName);
}
/**
* The local key for a given model relationship
*/
relationLocalKey(relation, model, relatedModel) {
if (relation === 'belongsTo') {
return relatedModel.primaryKey;
}
return model.primaryKey;
}
/**
* The foreign key for a given model relationship
*/
relationForeignKey(relation, model, relatedModel) {
if (relation === 'belongsTo') {
return string.camelCase(`${relatedModel.name}_${relatedModel.primaryKey}`);
}
return string.camelCase(`${model.name}_${model.primaryKey}`);
}
/**
* Pivot table name for many to many relationship
*/
relationPivotTable(_, model, relatedModel) {
return string.snakeCase([relatedModel.name, model.name].sort().join('_'));
}
/**
* Pivot foreign key for many to many relationship
*/
relationPivotForeignKey(_, model) {
return string.snakeCase(`${model.name}_${model.primaryKey}`);
}
/**
* Keys for the pagination meta
*/
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',
};
}
}