Skip to content

Commit

Permalink
Merge pull request #155 from AthennaIO/develop
Browse files Browse the repository at this point in the history
chore(fake): make paginate method use findMany
  • Loading branch information
jlenon7 authored Apr 24, 2024
2 parents 4662932 + fca797d commit b493c08
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "4.44.0",
"version": "4.45.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
8 changes: 6 additions & 2 deletions src/database/builders/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
* file that was distributed with this source code.
*/

import type {
Collection,
PaginatedResponse,
PaginationOptions
} from '@athenna/common'
import type { Operations } from '#src/types/Operations'
import type { Direction, ModelColumns } from '#src/types'
import type { Collection, PaginatedResponse } from '@athenna/common'
import type { Driver as DriverImpl } from '#src/database/drivers/Driver'

export class QueryBuilder<T = any, Driver extends DriverImpl = any> {
Expand Down Expand Up @@ -169,7 +173,7 @@ export class QueryBuilder<T = any, Driver extends DriverImpl = any> {
* Find many values in database and return as paginated response.
*/
public async paginate(
page = 0,
page: PaginationOptions | number = { page: 0, limit: 10, resourceUrl: '/' },
limit = 10,
resourceUrl = '/'
): Promise<PaginatedResponse> {
Expand Down
11 changes: 8 additions & 3 deletions src/database/drivers/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
*/

import type { Knex, TableBuilder } from 'knex'
import type { ModelSchema } from '#src/models/schemas/ModelSchema'
import type { Transaction } from '#src/database/transactions/Transaction'
import type { Direction, ConnectionOptions, Operations } from '#src/types'
import { NotFoundDataException } from '#src/exceptions/NotFoundDataException'
import { Collection, Options, type PaginatedResponse } from '@athenna/common'
import type { ModelSchema } from 'src/models/schemas/ModelSchema.js'
import {
Collection,
Options,
type PaginatedResponse,
type PaginationOptions
} from '@athenna/common'

export abstract class Driver<Client = any, QB = any> {
/**
Expand Down Expand Up @@ -370,7 +375,7 @@ export abstract class Driver<Client = any, QB = any> {
* Find many values in database and return as paginated response.
*/
public abstract paginate<T = any>(
page?: number,
page?: PaginationOptions | number,
limit?: number,
resourceUrl?: string
): Promise<PaginatedResponse<T>>
Expand Down
10 changes: 8 additions & 2 deletions src/database/drivers/FakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,17 @@ export class FakeDriver {
* Find many values in database and return as paginated response.
*/
public static async paginate<T = any>(
page = 0,
page = { page: 0, limit: 10, resourceUrl: '/' },
limit = 10,
resourceUrl = '/'
): Promise<PaginatedResponse<T>> {
return Exec.pagination([{}], 1, { page, limit, resourceUrl })
if (Is.Number(page)) {
page = { page, limit, resourceUrl }
}

const data = await this.findMany()

return Exec.pagination(data, data.length, page)
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/database/drivers/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
Is,
Json,
Options,
type PaginatedResponse
type PaginatedResponse,
type PaginationOptions
} from '@athenna/common'

import { debug } from '#src/debug'
Expand Down Expand Up @@ -549,10 +550,14 @@ export class MongoDriver extends Driver<Connection, Collection> {
* Find many values in database and return as paginated response.
*/
public async paginate<T = any>(
page = 0,
page: PaginationOptions | number = { page: 0, limit: 10, resourceUrl: '/' },
limit = 10,
resourceUrl = '/'
): Promise<PaginatedResponse<T>> {
if (Is.Number(page)) {
page = { page, limit, resourceUrl }
}

const pipeline = this.createPipeline({
clearWhere: false,
clearOrWhere: false,
Expand All @@ -568,9 +573,9 @@ export class MongoDriver extends Driver<Connection, Collection> {

const count = result[0]?.count || 0

const data = await this.offset(page).limit(limit).findMany()
const data = await this.offset(page.page).limit(page.limit).findMany()

return Exec.pagination(data, count, { page, limit, resourceUrl })
return Exec.pagination(data, count, page)
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/database/drivers/MySqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ import { ConnectionFactory } from '#src/factories/ConnectionFactory'
import type { ConnectionOptions, Direction, Operations } from '#src/types'
import { MigrationSource } from '#src/database/migrations/MigrationSource'
import { WrongMethodException } from '#src/exceptions/WrongMethodException'
import { Exec, Is, Options, type PaginatedResponse } from '@athenna/common'
import { PROTECTED_QUERY_METHODS } from '#src/constants/ProtectedQueryMethods'
import { NotConnectedDatabaseException } from '#src/exceptions/NotConnectedDatabaseException'
import {
Exec,
Is,
Options,
type PaginatedResponse,
type PaginationOptions
} from '@athenna/common'

export class MySqlDriver extends Driver<Knex, Knex.QueryBuilder> {
/**
Expand Down Expand Up @@ -366,19 +372,23 @@ export class MySqlDriver extends Driver<Knex, Knex.QueryBuilder> {
* Find many values in database and return as paginated response.
*/
public async paginate<T = any>(
page = 0,
page: PaginationOptions | number = { page: 0, limit: 10, resourceUrl: '/' },
limit = 10,
resourceUrl = '/'
): Promise<PaginatedResponse<T>> {
if (Is.Number(page)) {
page = { page, limit, resourceUrl }
}

const [{ count }] = await this.qb
.clone()
.clearOrder()
.clearSelect()
.count({ count: '*' })

const data = await this.offset(page).limit(limit).findMany()
const data = await this.offset(page.page).limit(page.limit).findMany()

return Exec.pagination(data, parseInt(count), { page, limit, resourceUrl })
return Exec.pagination(data, parseInt(count), page)
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/database/drivers/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ import { ConnectionFactory } from '#src/factories/ConnectionFactory'
import type { ConnectionOptions, Direction, Operations } from '#src/types'
import { MigrationSource } from '#src/database/migrations/MigrationSource'
import { WrongMethodException } from '#src/exceptions/WrongMethodException'
import { Exec, Is, Options, type PaginatedResponse } from '@athenna/common'
import { PROTECTED_QUERY_METHODS } from '#src/constants/ProtectedQueryMethods'
import { NotConnectedDatabaseException } from '#src/exceptions/NotConnectedDatabaseException'
import {
Exec,
Is,
Options,
type PaginatedResponse,
type PaginationOptions
} from '@athenna/common'

export class PostgresDriver extends Driver<Knex, Knex.QueryBuilder> {
/**
Expand Down Expand Up @@ -372,19 +378,23 @@ export class PostgresDriver extends Driver<Knex, Knex.QueryBuilder> {
* Find many values in database and return as paginated response.
*/
public async paginate<T = any>(
page = 0,
page: PaginationOptions | number = { page: 0, limit: 10, resourceUrl: '/' },
limit = 10,
resourceUrl = '/'
): Promise<PaginatedResponse<T>> {
if (Is.Number(page)) {
page = { page, limit, resourceUrl }
}

const [{ count }] = await this.qb
.clone()
.clearOrder()
.clearSelect()
.count({ count: '*' })

const data = await this.offset(page).limit(limit).findMany()
const data = await this.offset(page.page).limit(page.limit).findMany()

return Exec.pagination(data, parseInt(count), { page, limit, resourceUrl })
return Exec.pagination(data, parseInt(count), page)
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/database/drivers/SqliteDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ import { ConnectionFactory } from '#src/factories/ConnectionFactory'
import type { ConnectionOptions, Direction, Operations } from '#src/types'
import { MigrationSource } from '#src/database/migrations/MigrationSource'
import { WrongMethodException } from '#src/exceptions/WrongMethodException'
import { Exec, Is, Options, type PaginatedResponse } from '@athenna/common'
import { PROTECTED_QUERY_METHODS } from '#src/constants/ProtectedQueryMethods'
import { NotConnectedDatabaseException } from '#src/exceptions/NotConnectedDatabaseException'
import {
Exec,
Is,
Options,
type PaginatedResponse,
type PaginationOptions
} from '@athenna/common'

export class SqliteDriver extends Driver<Knex, Knex.QueryBuilder> {
/**
Expand Down Expand Up @@ -370,19 +376,23 @@ export class SqliteDriver extends Driver<Knex, Knex.QueryBuilder> {
* Find many values in database and return as paginated response.
*/
public async paginate<T = any>(
page = 0,
page: PaginationOptions | number = { page: 0, limit: 10, resourceUrl: '/' },
limit = 10,
resourceUrl = '/'
): Promise<PaginatedResponse<T>> {
if (Is.Number(page)) {
page = { page, limit, resourceUrl }
}

const [{ count }] = await this.qb
.clone()
.clearOrder()
.clearSelect()
.count({ count: '*' })

const data = await this.offset(page).limit(limit).findMany()
const data = await this.offset(page.page).limit(page.limit).findMany()

return Exec.pagination(data, parseInt(count), { page, limit, resourceUrl })
return Exec.pagination(data, parseInt(count), page)
}

/**
Expand Down
27 changes: 26 additions & 1 deletion src/models/BaseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import { Database } from '#src/facades/Database'
import type { ModelRelations } from '#src/types'
import { faker, type Faker } from '@faker-js/faker'
import { ModelSchema } from '#src/models/schemas/ModelSchema'
import { Collection, Is, Json, Options, String } from '@athenna/common'
import {
Collection,
Is,
Json,
Options,
String,
type PaginatedResponse,
type PaginationOptions
} from '@athenna/common'
import { ModelFactory } from '#src/models/factories/ModelFactory'
import { ModelGenerator } from '#src/models/factories/ModelGenerator'
import { ModelQueryBuilder } from '#src/models/builders/ModelQueryBuilder'
Expand Down Expand Up @@ -233,6 +241,23 @@ export class BaseModel {
return query.findMany()
}

/**
* Find many values in database and return paginated.
*/
public static async paginate<T extends typeof BaseModel>(
this: T,
options?: PaginationOptions,
where?: Partial<InstanceType<T>>
): Promise<PaginatedResponse<InstanceType<T>>> {
const query = this.query()

if (where) {
query.where(where)
}

return query.paginate(options)
}

/**
* Find many values in database and return
* as a collection instance.
Expand Down
24 changes: 23 additions & 1 deletion src/models/builders/ModelQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ import type {
ModelColumns,
ModelRelations
} from '#src/types'
import {
Collection,
Is,
Options,
type PaginationOptions
} from '@athenna/common'
import type { BaseModel } from '#src/models/BaseModel'
import { Collection, Is, Options } from '@athenna/common'
import type { Driver } from '#src/database/drivers/Driver'
import { QueryBuilder } from '#src/database/builders/QueryBuilder'
import type { ModelSchema } from '#src/models/schemas/ModelSchema'
Expand Down Expand Up @@ -227,6 +232,23 @@ export class ModelQueryBuilder<
return this.generator.generateMany(data)
}

/**
* Find many values in database and return paginated.
*/
public async paginate(
page: PaginationOptions | number = { page: 0, limit: 10, resourceUrl: '/' },
limit = 10,
resourceUrl = '/'
) {
this.setInternalQueries()

const data = await super.paginate(page, limit, resourceUrl)

data.data = await this.generator.generateMany(data.data)

return data
}

/**
* Find many values in database and return
* as a collection instance.
Expand Down

0 comments on commit b493c08

Please sign in to comment.