Skip to content

Commit

Permalink
feat: change naming strategy to output camelCase key names in seriali…
Browse files Browse the repository at this point in the history
…zed output

Breaking change: This commit changes the output of the APIs or anywhere else that
model is serialized to JSON. You can switch the naming strategy to snake_case
within your apps
  • Loading branch information
thetutlage committed Jan 23, 2024
1 parent 718c68f commit c835376
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 78 deletions.
3 changes: 2 additions & 1 deletion src/orm/base_model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
import { SnakeCaseNamingStrategy } from '../naming_strategies/snake_case.js'

Check failure on line 64 in src/orm/base_model/index.ts

View workflow job for this annotation

GitHub Actions / typecheck

'SnakeCaseNamingStrategy' is declared but its value is never read.

Check failure on line 64 in src/orm/base_model/index.ts

View workflow job for this annotation

GitHub Actions / typecheck

'SnakeCaseNamingStrategy' is declared but its value is never read.
import { LazyLoadAggregates } from '../relations/aggregates_loader/lazy_load.js'
import * as errors from '../../errors.js'
import { CamelCaseNamingStrategy } from '../naming_strategies/camel_case.js'

const MANY_RELATIONS = ['hasMany', 'manyToMany', 'hasManyThrough']
const DATE_TIME_TYPES = {
Expand Down Expand Up @@ -99,7 +100,7 @@ class BaseModelImpl implements LucidRow {
/**
* Naming strategy for model properties
*/
static namingStrategy = new SnakeCaseNamingStrategy()
static namingStrategy = new CamelCaseNamingStrategy()

/**
* Primary key is required to build relationships across models
Expand Down
1 change: 1 addition & 0 deletions src/orm/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './decorators/date_time.js'
export { BaseModel, scope } from './base_model/index.js'
export { ModelQueryBuilder } from './query_builder/index.js'
export { SnakeCaseNamingStrategy } from './naming_strategies/snake_case.js'
export { CamelCaseNamingStrategy } from './naming_strategies/camel_case.js'
110 changes: 110 additions & 0 deletions src/orm/naming_strategies/camel_case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <[email protected]>
*
* 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'
import { ModelRelations } from '../../types/relations.js'
import { NamingStrategyContract, LucidModel } from '../../types/model.js'

/**
* Camelcase naming strategy for the model to use camelcase keys
* for the serialized output.
*/
export class CamelCaseNamingStrategy implements NamingStrategyContract {
/**
* The default table name for the given model
*/
tableName(model: LucidModel): string {
return string.pluralize(string.snakeCase(model.name))
}

/**
* The database column name for a given model attribute
*/
columnName(_: LucidModel, attributeName: string): string {
return string.snakeCase(attributeName)
}

/**
* The post serialization name for a given model attribute
*/
serializedName(_: LucidModel, attributeName: string): string {
return string.camelCase(attributeName)
}

/**
* The local key for a given model relationship
*/
relationLocalKey(
relation: ModelRelations<LucidModel, LucidModel>['__opaque_type'],
model: LucidModel,
relatedModel: LucidModel
): string {
if (relation === 'belongsTo') {
return relatedModel.primaryKey
}

return model.primaryKey
}

/**
* The foreign key for a given model relationship
*/
relationForeignKey(
relation: ModelRelations<LucidModel, LucidModel>['__opaque_type'],
model: LucidModel,
relatedModel: LucidModel
): string {
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(_: 'manyToMany', model: LucidModel, relatedModel: LucidModel): string {
return string.snakeCase([relatedModel.name, model.name].sort().join('_'))
}

/**
* Pivot foreign key for many to many relationship
*/
relationPivotForeignKey(_: 'manyToMany', model: LucidModel): string {
return string.snakeCase(`${model.name}_${model.primaryKey}`)
}

/**
* Keys for the pagination meta
*/
paginationMetaKeys(): {
total: string
perPage: string
currentPage: string
lastPage: string
firstPage: string
firstPageUrl: string
lastPageUrl: string
nextPageUrl: string
previousPageUrl: string
} {
return {
total: 'total',
perPage: 'perPage',
currentPage: 'currentPage',
lastPage: 'lastPage',
firstPage: 'firstPage',
firstPageUrl: 'firstPageUrl',
lastPageUrl: 'lastPageUrl',
nextPageUrl: 'nextPageUrl',
previousPageUrl: 'previousPageUrl',
}
}
}
Loading

0 comments on commit c835376

Please sign in to comment.