Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking change - Switch ORM naming strategy to CamelCase #986

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
"index:commands": "adonis-kit index build/commands"
},
"dependencies": {
"@adonisjs/presets": "^2.1.1",
"@adonisjs/presets": "^2.2.1",
"@faker-js/faker": "^8.3.1",
"@poppinss/hooks": "^7.2.2",
"@poppinss/macroable": "^1.0.1",
"@poppinss/utils": "^6.7.0",
"@poppinss/utils": "^6.7.1",
"fast-deep-equal": "^3.1.3",
"igniculus": "^1.5.0",
"kleur": "^4.1.5",
Expand All @@ -71,40 +71,40 @@
"tarn": "^3.0.2"
},
"devDependencies": {
"@adonisjs/assembler": "^7.0.0",
"@adonisjs/core": "^6.2.0",
"@adonisjs/assembler": "^7.1.0",
"@adonisjs/core": "^6.2.1",
"@adonisjs/eslint-config": "^1.2.1",
"@adonisjs/prettier-config": "^1.2.1",
"@adonisjs/tsconfig": "^1.2.1",
"@commitlint/cli": "^18.4.4",
"@commitlint/config-conventional": "^18.4.4",
"@commitlint/cli": "^18.5.0",
"@commitlint/config-conventional": "^18.5.0",
"@japa/assert": "^2.1.0",
"@japa/file-system": "^2.1.1",
"@japa/file-system": "^2.2.0",
"@japa/runner": "^3.1.1",
"@swc/core": "^1.3.102",
"@swc/core": "^1.3.105",
"@types/chance": "^1.1.6",
"@types/luxon": "^3.4.0",
"@types/node": "^20.10.8",
"@types/luxon": "^3.4.2",
"@types/node": "^20.11.5",
"@types/pluralize": "^0.0.33",
"@types/pretty-hrtime": "^1.0.3",
"@types/qs": "^6.9.11",
"@vinejs/vine": "^1.7.0",
"better-sqlite3": "^9.2.2",
"c8": "^9.0.0",
"better-sqlite3": "^9.3.0",
"c8": "^9.1.0",
"chance": "^1.1.11",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"del-cli": "^5.0.0",
"dotenv": "^16.0.3",
"dotenv": "^16.3.2",
"eslint": "^8.56.0",
"fs-extra": "^11.2.0",
"github-label-sync": "^2.3.1",
"husky": "^8.0.3",
"luxon": "^3.4.4",
"mysql2": "^3.7.0",
"mysql2": "^3.7.1",
"np": "^9.2.0",
"pg": "^8.11.0",
"prettier": "^3.1.1",
"prettier": "^3.2.4",
"reflect-metadata": "^0.2.0",
"sqlite3": "^5.1.7",
"tedious": "^16.6.1",
Expand Down
4 changes: 2 additions & 2 deletions src/database/paginator/simple_paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

import { stringify } from 'qs'
import { CamelCaseNamingStrategy } from '../../orm/naming_strategies/camel_case.js'
import { SimplePaginatorContract, SimplePaginatorMetaKeys } from '../../types/querybuilder.js'
import { SnakeCaseNamingStrategy } from '../../orm/naming_strategies/snake_case.js'

/**
* Simple paginator works with the data set provided by the standard
Expand All @@ -25,7 +25,7 @@ export class SimplePaginator extends Array implements SimplePaginatorContract<an
*/
static namingStrategy: {
paginationMetaKeys(): SimplePaginatorMetaKeys
} = new SnakeCaseNamingStrategy()
} = new CamelCaseNamingStrategy()

/**
* Can be defined at per instance level as well
Expand Down
12 changes: 6 additions & 6 deletions src/orm/base_model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ import {
ManyToManyRelationOptions,
} from '../../types/relations.js'

import { ModelKeys } from '../model_keys/index.js'
import * as errors from '../../errors.js'
import { Preloader } from '../preloader/index.js'
import { HasOne } from '../relations/has_one/index.js'
import { proxyHandler } from './proxy_handler.js'
import { ModelKeys } from '../model_keys/index.js'
import { HasOne } from '../relations/has_one/index.js'
import { HasMany } from '../relations/has_many/index.js'
import { BelongsTo } from '../relations/belongs_to/index.js'
import { ManyToMany } from '../relations/many_to_many/index.js'
import { HasManyThrough } from '../relations/has_many_through/index.js'
import { CamelCaseNamingStrategy } from '../naming_strategies/camel_case.js'
import { LazyLoadAggregates } from '../relations/aggregates_loader/lazy_load.js'
import {
isObject,
collectValues,
ensureRelation,
managedTransaction,
normalizeCherryPickObject,
} from '../../utils/index.js'
import { SnakeCaseNamingStrategy } from '../naming_strategies/snake_case.js'
import { LazyLoadAggregates } from '../relations/aggregates_loader/lazy_load.js'
import * as errors from '../../errors.js'

const MANY_RELATIONS = ['hasMany', 'manyToMany', 'hasManyThrough']
const DATE_TIME_TYPES = {
Expand Down Expand Up @@ -99,7 +99,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