diff --git a/package.json b/package.json index c65d8d1d..86f1dc14 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/database/paginator/simple_paginator.ts b/src/database/paginator/simple_paginator.ts index d9cae8b8..a5709bd9 100644 --- a/src/database/paginator/simple_paginator.ts +++ b/src/database/paginator/simple_paginator.ts @@ -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 @@ -25,7 +25,7 @@ export class SimplePaginator extends Array implements SimplePaginatorContract + * + * 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['__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['__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', + } + } +} diff --git a/test/database/query_builder.spec.ts b/test/database/query_builder.spec.ts index c0352c37..b81e807e 100644 --- a/test/database/query_builder.spec.ts +++ b/test/database/query_builder.spec.ts @@ -10312,14 +10312,14 @@ test.group('Query Builder | paginate', (group) => { assert.deepEqual(users.getMeta(), { total: 18, - per_page: 5, - current_page: 1, - last_page: 4, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=4', - next_page_url: '/users?page=2', - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 4, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=4', + nextPageUrl: '/users?page=2', + previousPageUrl: null, }) await connection.disconnect() @@ -10347,14 +10347,14 @@ test.group('Query Builder | paginate', (group) => { assert.isTrue(users.hasTotal) assert.deepEqual(users.getMeta(), { total: 18, - per_page: 5, - current_page: 1, - last_page: 4, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=4', - next_page_url: '/users?page=2', - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 4, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=4', + nextPageUrl: '/users?page=2', + previousPageUrl: null, }) await connection.disconnect() @@ -10381,14 +10381,14 @@ test.group('Query Builder | paginate', (group) => { assert.isTrue(users.hasTotal) assert.deepEqual(users.getMeta(), { total: 18, - per_page: 5, - current_page: 1, - last_page: 4, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=4', - next_page_url: '/users?page=2', - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 4, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=4', + nextPageUrl: '/users?page=2', + previousPageUrl: null, }) await connection.disconnect() @@ -10416,14 +10416,14 @@ test.group('Query Builder | paginate', (group) => { assert.deepEqual(users.getMeta(), { total: 18, - per_page: 5, - current_page: 4, - last_page: 4, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=4', - next_page_url: null, - previous_page_url: '/users?page=3', + perPage: 5, + currentPage: 4, + lastPage: 4, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=4', + nextPageUrl: null, + previousPageUrl: '/users?page=3', }) await connection.disconnect() @@ -10456,14 +10456,14 @@ test.group('Query Builder | paginate', (group) => { assert.isTrue(users.hasTotal) assert.deepEqual(users.getMeta(), { total: 18, - per_page: 5, - current_page: 1, - last_page: 4, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=4', - next_page_url: '/users?page=2', - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 4, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=4', + nextPageUrl: '/users?page=2', + previousPageUrl: null, }) await connection.disconnect() @@ -10498,14 +10498,14 @@ test.group('Query Builder | paginate', (group) => { assert.isTrue(results.hasTotal) assert.deepEqual(results.getMeta(), { total: 2, - per_page: 1, - current_page: 1, - last_page: 2, - first_page: 1, - first_page_url: '/users-country-ids?page=1', - last_page_url: '/users-country-ids?page=2', - next_page_url: '/users-country-ids?page=2', - previous_page_url: null, + perPage: 1, + currentPage: 1, + lastPage: 2, + firstPage: 1, + firstPageUrl: '/users-country-ids?page=1', + lastPageUrl: '/users-country-ids?page=2', + nextPageUrl: '/users-country-ids?page=2', + previousPageUrl: null, }) await connection.disconnect() @@ -10640,14 +10640,14 @@ test.group('Query Builder | paginate', (group) => { assert.deepEqual(users.getMeta(), { total: 1, - per_page: 5, - current_page: 1, - last_page: 1, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=1', - next_page_url: null, - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 1, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=1', + nextPageUrl: null, + previousPageUrl: null, }) await connection.disconnect() diff --git a/test/orm/base_model.spec.ts b/test/orm/base_model.spec.ts index dc07d465..205c4b1b 100644 --- a/test/orm/base_model.spec.ts +++ b/test/orm/base_model.spec.ts @@ -249,7 +249,7 @@ test.group('Base model | boot', (group) => { } User.boot() - assert.deepEqual(User.$keys.attributesToSerialized.get('userName'), 'user_name') + assert.deepEqual(User.$keys.attributesToSerialized.get('userName'), 'userName') }) test('resolve attribute name from column name', async ({ fs, assert }) => { @@ -289,7 +289,7 @@ test.group('Base model | boot', (group) => { } User.boot() - assert.deepEqual(User.$keys.columnsToSerialized.get('user_name'), 'user_name') + assert.deepEqual(User.$keys.columnsToSerialized.get('user_name'), 'userName') }) test('resolve attribute name from serializeAs name', async ({ fs, assert }) => { @@ -309,7 +309,7 @@ test.group('Base model | boot', (group) => { } User.boot() - assert.deepEqual(User.$keys.serializedToAttributes.get('user_name'), 'userName') + assert.deepEqual(User.$keys.serializedToAttributes.get('userName'), 'userName') }) test('resolve column name from serializeAs name', async ({ fs, assert }) => { @@ -329,7 +329,7 @@ test.group('Base model | boot', (group) => { } User.boot() - assert.deepEqual(User.$keys.serializedToColumns.get('user_name'), 'user_name') + assert.deepEqual(User.$keys.serializedToColumns.get('userName'), 'user_name') }) }) @@ -2113,7 +2113,7 @@ test.group('Base Model | serializeRelations', () => { assert.deepEqual(user.serializeRelations(), { profile: { username: 'virk', - user_id: 1, + userId: 1, }, }) }) @@ -2150,7 +2150,7 @@ test.group('Base Model | serializeRelations', () => { assert.deepEqual(user.serializeRelations(), { userProfile: { username: 'virk', - user_id: 1, + userId: 1, }, }) }) @@ -2322,12 +2322,12 @@ test.group('Base Model | serializeRelations', () => { assert.deepEqual( user.serializeRelations({ profile: { - fields: ['user_id'], + fields: ['userId'], }, }), { profile: { - user_id: 1, + userId: 1, }, } ) @@ -2371,7 +2371,7 @@ test.group('Base Model | serializeRelations', () => { }), { profile: { - user_id: 1, + userId: 1, username: 'virk', }, } @@ -2506,7 +2506,7 @@ test.group('Base Model | toJSON', (group) => { const user = new User() user.username = 'virk' - assert.deepEqual(user.toJSON(), { username: 'virk', full_name: 'VIRK' }) + assert.deepEqual(user.toJSON(), { username: 'virk', fullName: 'VIRK' }) }) test('do not add computed property when it returns undefined', async ({ fs, assert }) => { @@ -2585,7 +2585,7 @@ test.group('Base Model | toJSON', (group) => { assert.deepEqual(user.toJSON(), { username: 'virk', - full_name: 'VIRK', + fullName: 'VIRK', meta: { postsCount: 10, }, @@ -2623,7 +2623,7 @@ test.group('Base Model | toJSON', (group) => { assert.deepEqual(user.toJSON(), { username: 'virk', - full_name: 'VIRK', + fullName: 'VIRK', posts: { count: 10, }, @@ -6191,7 +6191,7 @@ test.group('Base Model | date', (group) => { created_at: DateTime.local().toISODate(), }) const user = await User.find(1) - assert.match(user!.toJSON().created_at, /\d{4}-\d{2}-\d{2}/) + assert.match(user!.toJSON().createdAt, /\d{4}-\d{2}-\d{2}/) }) test('do not attempt to serialize, when already a string', async ({ fs, assert }) => { @@ -6223,7 +6223,7 @@ test.group('Base Model | date', (group) => { created_at: DateTime.local().toISODate(), }) const user = await User.find(1) - assert.equal(user!.toJSON().created_at, DateTime.local().minus({ days: 1 }).toISODate()) + assert.equal(user!.toJSON().createdAt, DateTime.local().minus({ days: 1 }).toISODate()) }) }) @@ -6667,7 +6667,7 @@ test.group('Base Model | datetime', (group) => { const user = await User.find(1) assert.match( - user!.toJSON().joined_at, + user!.toJSON().joinedAt, /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}(\+|\-)\d{2}:\d{2}/ ) }) @@ -6705,7 +6705,7 @@ test.group('Base Model | datetime', (group) => { }) const user = await User.find(1) - assert.equal(user!.toJSON().joined_at, DateTime.local().minus({ days: 1 }).toISODate()) + assert.equal(user!.toJSON().joinedAt, DateTime.local().minus({ days: 1 }).toISODate()) }) test('force update when enabledForceUpdate method is called', async ({ fs, assert }) => { @@ -6822,14 +6822,14 @@ test.group('Base Model | paginate', (group) => { assert.isTrue(users.hasTotal) assert.deepEqual(users.getMeta(), { total: 18, - per_page: 5, - current_page: 1, - last_page: 4, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=4', - next_page_url: '/users?page=2', - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 4, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=4', + nextPageUrl: '/users?page=2', + previousPageUrl: null, }) }) @@ -6867,14 +6867,14 @@ test.group('Base Model | paginate', (group) => { }) assert.deepEqual(meta, { total: 18, - per_page: 5, - current_page: 1, - last_page: 4, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=4', - next_page_url: '/users?page=2', - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 4, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=4', + nextPageUrl: '/users?page=2', + previousPageUrl: null, }) }) @@ -6915,14 +6915,14 @@ test.group('Base Model | paginate', (group) => { assert.isTrue(users.hasTotal) assert.deepEqual(users.getMeta(), { total: 18, - per_page: 5, - current_page: 1, - last_page: 4, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=4', - next_page_url: '/users?page=2', - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 4, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=4', + nextPageUrl: '/users?page=2', + previousPageUrl: null, }) }) @@ -7030,14 +7030,14 @@ test.group('Base Model | paginate', (group) => { assert.isTrue(users.hasTotal) assert.deepEqual(users.getMeta(), { total: 1, - per_page: 5, - current_page: 1, - last_page: 1, - first_page: 1, - first_page_url: '/users?page=1', - last_page_url: '/users?page=1', - next_page_url: null, - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 1, + firstPage: 1, + firstPageUrl: '/users?page=1', + lastPageUrl: '/users?page=1', + nextPageUrl: null, + previousPageUrl: null, }) }) }) @@ -7493,7 +7493,7 @@ test.group('Base model | inheritance', (group) => { meta: undefined, prepare: undefined, serialize: undefined, - serializeAs: 'user_id', + serializeAs: 'userId', }, ], [ @@ -7533,7 +7533,7 @@ test.group('Base model | inheritance', (group) => { meta: undefined, prepare: undefined, serialize: undefined, - serializeAs: 'user_id', + serializeAs: 'userId', }, ], ]) @@ -7612,7 +7612,7 @@ test.group('Base model | inheritance', (group) => { 'fullName', { meta: undefined, - serializeAs: 'full_name', + serializeAs: 'fullName', }, ], [ @@ -7638,7 +7638,7 @@ test.group('Base model | inheritance', (group) => { 'fullName', { meta: undefined, - serializeAs: 'full_name', + serializeAs: 'fullName', }, ], ]) @@ -7733,7 +7733,7 @@ test.group('Base model | inheritance', (group) => { 'fullName', { meta: undefined, - serializeAs: 'full_name', + serializeAs: 'fullName', }, ], ]) diff --git a/test/orm/model_has_many.spec.ts b/test/orm/model_has_many.spec.ts index 39eb8606..2a5bd023 100644 --- a/test/orm/model_has_many.spec.ts +++ b/test/orm/model_has_many.spec.ts @@ -5291,14 +5291,14 @@ test.group('Model | HasMany | paginate', (group) => { assert.isTrue(posts.hasTotal) assert.deepEqual(posts.getMeta(), { total: 18, - per_page: 5, - current_page: 1, - last_page: 4, - first_page: 1, - first_page_url: '/posts?page=1', - last_page_url: '/posts?page=4', - next_page_url: '/posts?page=2', - previous_page_url: null, + perPage: 5, + currentPage: 1, + lastPage: 4, + firstPage: 1, + firstPageUrl: '/posts?page=1', + lastPageUrl: '/posts?page=4', + nextPageUrl: '/posts?page=2', + previousPageUrl: null, }) }) diff --git a/test/orm/model_has_many_through.spec.ts b/test/orm/model_has_many_through.spec.ts index bbf8f979..0f6841a8 100644 --- a/test/orm/model_has_many_through.spec.ts +++ b/test/orm/model_has_many_through.spec.ts @@ -3112,14 +3112,14 @@ test.group('Model | Has Many Through | pagination', (group) => { assert.isTrue(posts.hasTotal) assert.deepEqual(posts.getMeta(), { total: 3, - per_page: 2, - current_page: 1, - last_page: 2, - first_page: 1, - first_page_url: '/posts?page=1', - last_page_url: '/posts?page=2', - next_page_url: '/posts?page=2', - previous_page_url: null, + perPage: 2, + currentPage: 1, + lastPage: 2, + firstPage: 1, + firstPageUrl: '/posts?page=1', + lastPageUrl: '/posts?page=2', + nextPageUrl: '/posts?page=2', + previousPageUrl: null, }) }) diff --git a/test/orm/model_many_to_many.spec.ts b/test/orm/model_many_to_many.spec.ts index 00f27565..bb26c522 100644 --- a/test/orm/model_many_to_many.spec.ts +++ b/test/orm/model_many_to_many.spec.ts @@ -7385,14 +7385,14 @@ test.group('Model | ManyToMany | pagination', (group) => { assert.isTrue(skills.hasTotal) assert.deepEqual(skills.getMeta(), { total: 2, - per_page: 1, - current_page: 1, - last_page: 2, - first_page: 1, - first_page_url: '/skills?page=1', - last_page_url: '/skills?page=2', - next_page_url: '/skills?page=2', - previous_page_url: null, + perPage: 1, + currentPage: 1, + lastPage: 2, + firstPage: 1, + firstPageUrl: '/skills?page=1', + lastPageUrl: '/skills?page=2', + nextPageUrl: '/skills?page=2', + previousPageUrl: null, }) })