@goatlab/fluent 0.6.19 → 0.6.22

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,6 +1,10 @@
1
1
  import { TypedPathWrapper } from 'typed-path';
2
2
  import { Collection } from '@goatlab/js-utils';
3
- import { Filter, DaoOutput, BaseDaoExtendedAttributes, LogicOperator, Primitives, PrimitivesArray } from './types';
3
+ import { Filter, DaoOutput, LogicOperator, Primitives, PrimitivesArray } from './types';
4
+ import { ValidationError } from 'class-validator';
5
+ export declare type Newable<T> = {
6
+ new (...args: any[]): T;
7
+ };
4
8
  export interface FluentConnectorInterface<InputDTO, OutputDTO> {
5
9
  get(): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
6
10
  all(filter: Filter): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
@@ -42,7 +46,6 @@ export declare abstract class BaseConnector<ModelDTO, InputDTO, OutputDTO> {
42
46
  protected relationQuery: any;
43
47
  protected modelRelations: any;
44
48
  isMongoDB: boolean;
45
- protected getExtendedCreateAttributes: () => BaseDaoExtendedAttributes;
46
49
  constructor();
47
50
  findByIds(ids: string[]): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
48
51
  get(): Promise<DaoOutput<InputDTO, OutputDTO>[]>;
@@ -80,4 +83,10 @@ export declare abstract class BaseConnector<ModelDTO, InputDTO, OutputDTO> {
80
83
  withPivot(): this;
81
84
  protected hasManyThrough(): void;
82
85
  private prepareInput;
86
+ validateInput(validationClass: {
87
+ new (): ModelDTO;
88
+ }, input: InputDTO): Promise<{
89
+ errors: ValidationError[] | null;
90
+ result: Awaited<InputDTO>;
91
+ }>;
83
92
  }
@@ -2,8 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseConnector = void 0;
4
4
  const typed_path_1 = require("typed-path");
5
- const dates_1 = require("@goatlab/dates");
6
5
  const js_utils_1 = require("@goatlab/js-utils");
6
+ const class_transformer_1 = require("class-transformer");
7
+ const class_validator_1 = require("class-validator");
7
8
  class BaseConnector {
8
9
  constructor() {
9
10
  this.generatedKeyPath = (0, typed_path_1.typedPath)();
@@ -24,15 +25,6 @@ class BaseConnector {
24
25
  this.getFirst = false;
25
26
  this.relations = undefined;
26
27
  this.loadModels = false;
27
- this.getExtendedCreateAttributes = () => {
28
- const date = dates_1.Dates.currentIsoString();
29
- return {
30
- id: `${js_utils_1.Ids.objectIdString()}_local`,
31
- updated: date,
32
- created: date,
33
- roles: []
34
- };
35
- };
36
28
  this.chainReference = [];
37
29
  this.whereArray = [];
38
30
  this.orWhereArray = [];
@@ -392,5 +384,19 @@ class BaseConnector {
392
384
  cols.filter((elem, pos, arr) => arr.indexOf(elem) === pos);
393
385
  return cols;
394
386
  }
387
+ async validateInput(validationClass, input) {
388
+ const validationOptions = {
389
+ whitelist: true,
390
+ skipMissingProperties: false,
391
+ forbidUnknownValues: true,
392
+ stopAtFirstError: false,
393
+ };
394
+ const instance = (0, class_transformer_1.plainToInstance)(validationClass, { ...input });
395
+ const errors = await (0, class_validator_1.validate)(instance, validationOptions);
396
+ return {
397
+ errors: errors && errors.length ? errors : null,
398
+ result: errors && errors.length ? undefined : instance,
399
+ };
400
+ }
395
401
  }
396
402
  exports.BaseConnector = BaseConnector;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ApiHideProperty, ApiProperty, Column, HideField, InputType, ObjectType, OmitType, PartialType, getModelSchemaRef } from './core/types';
2
- import type { BaseDaoExtendedAttributes, BaseDataElement, DaoOutput, Deleted, Filter, LogicOperator, PaginatedData, Paginator, Paths, Primitives, PrimitivesArray, Sure, WhereClause } from './types';
2
+ import type { BaseDataElement, DaoOutput, Deleted, Filter, LogicOperator, PaginatedData, Paginator, Paths, Primitives, PrimitivesArray, Sure, WhereClause } from './types';
3
3
  import { PaginationLinks, PaginationMeta } from './core/dtos/pagination.dto';
4
4
  import { Access } from './core/dtos/access.dto';
5
5
  import { BaseConnector } from './BaseConnector';
@@ -13,6 +13,11 @@ import { getOutputKeys } from './outputKeys';
13
13
  import { loadRelations } from './loadRelations';
14
14
  import { modelGeneratorDataSource } from './generatorDatasource';
15
15
  import { Cache } from './cache';
16
+ import { ValidationError } from 'class-validator';
16
17
  export { DataSource } from 'typeorm';
17
18
  export { Access, ApiHideProperty, ApiProperty, BaseConnector, Collection, Column, Decorators, Fluent, getModelSchemaRef, getOutputKeys, HideField, InputType, loadRelations, modelGeneratorDataSource, ObjectType, OmitType, PaginationLinks, PaginationMeta, PartialType, TypeOrmConnector, Cache };
18
- export type { BaseDaoExtendedAttributes, BaseDataElement, DaoOutput, Deleted, Filter, FluentConnectorInterface, LogicOperator, PaginatedData, Paginator, Paths, Primitives, PrimitivesArray, Sure, WhereClause, SchemaObject };
19
+ interface ValidatedInput<T> {
20
+ errors: ValidationError[] | null;
21
+ result: Awaited<T>;
22
+ }
23
+ export type { BaseDataElement, DaoOutput, Deleted, Filter, FluentConnectorInterface, LogicOperator, PaginatedData, Paginator, Paths, Primitives, PrimitivesArray, Sure, WhereClause, SchemaObject, ValidatedInput };
@@ -69,14 +69,22 @@ const loadRelations = async ({ data, relations, modelRelations, provider, self,
69
69
  let relatedResults = js_utils_1.Arrays.collapse(await Promise.all(relationPromises));
70
70
  const pivotInverseKey = relationModel.inverseJoinColumns[0].propertyName;
71
71
  relatedResults = relatedResults.map(r => {
72
- return { ...r, pivot: pivotResults.find(p => p[pivotInverseKey] === r.id) };
72
+ return {
73
+ ...r,
74
+ pivot: pivotResults.find(p => p[pivotInverseKey] === r.id)
75
+ };
73
76
  });
74
77
  const groupedPivot = js_utils_1.Arrays.groupBy(pivotResults, r => r[relationModel.joinColumns[0].propertyName]);
75
78
  const groupedRelated = js_utils_1.Arrays.groupBy(relatedResults, r => r.id);
76
79
  data.forEach(d => {
77
80
  groupedPivot[d.id]?.forEach(gp => {
78
- d[relationModel.propertyPath] =
79
- groupedRelated[gp[relationModel.inverseJoinColumns[0].propertyName]];
81
+ if (!d[relationModel.propertyPath]) {
82
+ d[relationModel.propertyPath] = [];
83
+ }
84
+ d[relationModel.propertyPath] = [
85
+ ...d[relationModel.propertyPath],
86
+ ...groupedRelated[gp[relationModel.inverseJoinColumns[0].propertyName]]
87
+ ];
80
88
  });
81
89
  });
82
90
  }