@maioradv/nestjs-core 1.8.0 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { Type } from '@nestjs/common';
2
2
  import CursorMetaDto from './cursor-meta.dto';
3
+ import CursorQueryDto from './cursor-query.dto';
3
4
  interface IEdgeType<T> {
4
5
  cursor: string;
5
6
  node: T;
@@ -18,7 +19,7 @@ export declare class PaginatedGQLDto<T> implements IPaginatedType<T> {
18
19
  readonly edges: IEdgeType<T>[];
19
20
  readonly nodes: T[];
20
21
  readonly meta: CursorMetaDto;
21
- constructor(data: T[], meta: CursorMeta);
22
+ constructor(data: T[], meta: CursorMeta, query: CursorQueryDto);
22
23
  private getEdges;
23
24
  }
24
25
  export {};
@@ -50,20 +50,21 @@ function PaginatedGQL(classRef) {
50
50
  return PaginatedType;
51
51
  }
52
52
  class PaginatedGQLDto {
53
- constructor(data, meta) {
54
- this.edges = this.getEdges(data);
53
+ constructor(data, meta, query) {
54
+ this.edges = this.getEdges(data, query.cursorField);
55
55
  this.nodes = data;
56
+ const [firstIndex, lastIndex] = query.cursorOrder == 'asc' ? [0, this.edges.length - 1] : [this.edges.length - 1, 0];
56
57
  this.meta = new cursor_meta_dto_1.default({
57
58
  ...meta,
58
- start: +this.edges[0]?.cursor,
59
- end: +this.edges[this.edges.length - 1]?.cursor
59
+ start: +this.edges[firstIndex]?.cursor,
60
+ end: +this.edges[lastIndex]?.cursor
60
61
  });
61
62
  }
62
- getEdges(data) {
63
+ getEdges(data, cursor) {
63
64
  return data.map((value) => {
64
65
  return {
65
66
  node: value,
66
- cursor: value.id,
67
+ cursor: value[cursor],
67
68
  };
68
69
  });
69
70
  }
@@ -1,11 +1,14 @@
1
+ import { Sorting } from "../sorting";
1
2
  export default class CursorQueryDto {
2
3
  readonly after?: number;
3
4
  readonly before?: number;
4
5
  readonly limit?: number;
6
+ readonly cursorField?: string;
7
+ readonly cursorOrder?: Sorting;
5
8
  get skip(): number;
6
9
  get take(): number;
7
10
  get order(): Record<string, any>;
8
11
  get cursor(): {
9
- id: number;
12
+ [x: string]: number;
10
13
  };
11
14
  }
@@ -13,9 +13,12 @@ const swagger_1 = require("@nestjs/swagger");
13
13
  const class_transformer_1 = require("class-transformer");
14
14
  const class_validator_1 = require("class-validator");
15
15
  const graphql_1 = require("@nestjs/graphql");
16
+ const sorting_1 = require("../sorting");
16
17
  let CursorQueryDto = class CursorQueryDto {
17
18
  constructor() {
18
19
  this.limit = 50;
20
+ this.cursorField = 'id';
21
+ this.cursorOrder = sorting_1.Sorting.asc;
19
22
  }
20
23
  get skip() {
21
24
  if (this.after || this.before)
@@ -29,12 +32,12 @@ let CursorQueryDto = class CursorQueryDto {
29
32
  }
30
33
  get order() {
31
34
  return {
32
- id: 'asc'
35
+ [this.cursorField]: this.cursorOrder
33
36
  };
34
37
  }
35
38
  get cursor() {
36
39
  return this.after || this.before ? {
37
- id: this.after ?? this.before
40
+ [this.cursorField]: this.after ?? this.before
38
41
  } : undefined;
39
42
  }
40
43
  };
@@ -76,6 +79,28 @@ __decorate([
76
79
  (0, class_transformer_1.Type)(() => Number),
77
80
  __metadata("design:type", Number)
78
81
  ], CursorQueryDto.prototype, "limit", void 0);
82
+ __decorate([
83
+ (0, swagger_1.ApiProperty)({
84
+ required: false,
85
+ default: 'id',
86
+ }),
87
+ (0, graphql_1.Field)({ nullable: true }),
88
+ (0, class_validator_1.IsString)(),
89
+ (0, class_validator_1.IsOptional)(),
90
+ __metadata("design:type", String)
91
+ ], CursorQueryDto.prototype, "cursorField", void 0);
92
+ __decorate([
93
+ (0, swagger_1.ApiProperty)({
94
+ required: false,
95
+ enum: sorting_1.Sorting,
96
+ default: sorting_1.Sorting.asc,
97
+ }),
98
+ (0, graphql_1.Field)({ nullable: true }),
99
+ (0, class_validator_1.IsString)(),
100
+ (0, class_validator_1.IsEnum)(sorting_1.Sorting),
101
+ (0, class_validator_1.IsOptional)(),
102
+ __metadata("design:type", String)
103
+ ], CursorQueryDto.prototype, "cursorOrder", void 0);
79
104
  CursorQueryDto = __decorate([
80
105
  (0, graphql_1.ArgsType)()
81
106
  ], CursorQueryDto);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maioradv/nestjs-core",
3
- "version": "1.8.0",
3
+ "version": "1.8.1",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",