-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3e3f72
commit 18c64df
Showing
15 changed files
with
769 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/api/src/dto/cursor-pagination/cursor-pagination.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Expose } from 'class-transformer'; | ||
import { PageOptionsDto } from './page-options.dto'; | ||
|
||
export class CursorPaginationDto { | ||
@ApiProperty() | ||
@Expose() | ||
readonly limit: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly afterCursor?: string; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly beforeCursor?: string; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly totalRecords: number; | ||
|
||
constructor( | ||
totalRecords: number, | ||
afterCursor: string, | ||
beforeCursor: string, | ||
pageOptions: PageOptionsDto, | ||
) { | ||
this.limit = pageOptions.limit; | ||
this.afterCursor = afterCursor; | ||
this.beforeCursor = beforeCursor; | ||
this.totalRecords = totalRecords; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/api/src/dto/cursor-pagination/page-options.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { DEFAULT_PAGE_LIMIT } from '../../constants'; | ||
import { NumberFieldOptional, StringFieldOptional } from '../../decorators'; | ||
|
||
export class PageOptionsDto { | ||
@StringFieldOptional() | ||
afterCursor?: string; | ||
|
||
@StringFieldOptional() | ||
beforeCursor?: string; | ||
|
||
@NumberFieldOptional({ | ||
minimum: 1, | ||
default: DEFAULT_PAGE_LIMIT, | ||
int: true, | ||
}) | ||
readonly limit: number = DEFAULT_PAGE_LIMIT; | ||
|
||
@StringFieldOptional() | ||
readonly q?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Expose } from 'class-transformer'; | ||
import { CursorPaginationDto } from './cursor-pagination.dto'; | ||
|
||
export class CursorPaginatedDto<TData> { | ||
@ApiProperty({ type: [Object] }) | ||
@Expose() | ||
readonly data: TData[]; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
pagination: CursorPaginationDto; | ||
|
||
constructor(data: TData[], meta: CursorPaginationDto) { | ||
this.data = data; | ||
this.pagination = meta; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/api/src/dto/offset-pagination/offset-pagination.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Expose } from 'class-transformer'; | ||
import { PageOptionsDto } from './page-options.dto'; | ||
|
||
export class OffsetPaginationDto { | ||
@ApiProperty() | ||
@Expose() | ||
readonly limit: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly currentPage: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly nextPage?: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly previousPage?: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly totalRecords: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly totalPages: number; | ||
|
||
constructor(totalRecords: number, pageOptions: PageOptionsDto) { | ||
this.limit = pageOptions.limit; | ||
this.currentPage = pageOptions.offset / pageOptions.limit + 1; | ||
this.nextPage = | ||
this.currentPage < this.totalPages ? this.currentPage + 1 : undefined; | ||
this.previousPage = | ||
this.currentPage > 1 && this.currentPage - 1 < this.totalPages | ||
? this.currentPage - 1 | ||
: undefined; | ||
this.totalRecords = totalRecords; | ||
this.totalPages = | ||
this.limit > 0 ? Math.ceil(totalRecords / pageOptions.limit) : 0; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/api/src/dto/offset-pagination/page-options.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { DEFAULT_PAGE_LIMIT, Order } from '../../constants'; | ||
import { | ||
EnumFieldOptional, | ||
NumberFieldOptional, | ||
StringFieldOptional, | ||
} from '../../decorators'; | ||
|
||
export class PageOptionsDto { | ||
@NumberFieldOptional({ | ||
minimum: 1, | ||
default: DEFAULT_PAGE_LIMIT, | ||
int: true, | ||
}) | ||
readonly limit: number = DEFAULT_PAGE_LIMIT; | ||
|
||
@NumberFieldOptional({ | ||
minimum: 0, | ||
default: 0, | ||
int: true, | ||
}) | ||
readonly offset: number = 0; | ||
|
||
@StringFieldOptional() | ||
readonly q?: string; | ||
|
||
@EnumFieldOptional(() => Order, { default: Order.ASC }) | ||
readonly order: Order = Order.ASC; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Expose } from 'class-transformer'; | ||
import { OffsetPaginationDto } from './offset-pagination.dto'; | ||
|
||
export class OffsetPaginatedDto<TData> { | ||
@ApiProperty({ type: [Object] }) | ||
@Expose() | ||
readonly data: TData[]; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
pagination: OffsetPaginationDto; | ||
|
||
constructor(data: TData[], meta: OffsetPaginationDto) { | ||
this.data = data; | ||
this.pagination = meta; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
DEFAULT_CURRENT_PAGE, | ||
DEFAULT_PAGE_LIMIT, | ||
Order, | ||
} from '../../constants'; | ||
import { | ||
EnumFieldOptional, | ||
NumberFieldOptional, | ||
StringFieldOptional, | ||
} from '../../decorators'; | ||
|
||
export class PageOptionsDto { | ||
@NumberFieldOptional({ | ||
minimum: 1, | ||
default: DEFAULT_PAGE_LIMIT, | ||
int: true, | ||
}) | ||
readonly limit: number = DEFAULT_PAGE_LIMIT; | ||
|
||
@NumberFieldOptional({ | ||
minimum: 1, | ||
default: DEFAULT_CURRENT_PAGE, | ||
int: true, | ||
}) | ||
readonly page: number = DEFAULT_CURRENT_PAGE; | ||
|
||
@StringFieldOptional() | ||
readonly q?: string; | ||
|
||
@EnumFieldOptional(() => Order, { default: Order.ASC }) | ||
readonly order: Order = Order.ASC; | ||
|
||
get offset() { | ||
return this.page ? (this.page - 1) * this.limit : 0; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/api/src/dto/page-pagination/page-pagination.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Expose } from 'class-transformer'; | ||
import { PageOptionsDto } from './page-options.dto'; | ||
|
||
export class PagePaginationDto { | ||
@ApiProperty() | ||
@Expose() | ||
readonly limit: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly currentPage: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly nextPage?: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly previousPage?: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly totalRecords: number; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
readonly totalPages: number; | ||
|
||
constructor(totalRecords: number, pageOptions: PageOptionsDto) { | ||
this.limit = pageOptions.limit; | ||
this.currentPage = pageOptions.page; | ||
this.nextPage = | ||
this.currentPage < this.totalPages ? this.currentPage + 1 : undefined; | ||
this.previousPage = | ||
this.currentPage > 1 && this.currentPage - 1 < this.totalPages | ||
? this.currentPage - 1 | ||
: undefined; | ||
this.totalRecords = totalRecords; | ||
this.totalPages = | ||
this.limit > 0 ? Math.ceil(totalRecords / pageOptions.limit) : 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Expose } from 'class-transformer'; | ||
import { PagePaginationDto } from './page-pagination.dto'; | ||
|
||
export class PagePaginatedDto<TData> { | ||
@ApiProperty({ type: [Object] }) | ||
@Expose() | ||
readonly data: TData[]; | ||
|
||
@ApiProperty() | ||
@Expose() | ||
pagination: PagePaginationDto; | ||
|
||
constructor(data: TData[], meta: PagePaginationDto) { | ||
this.data = data; | ||
this.pagination = meta; | ||
} | ||
} |
Oops, something went wrong.