@goatlab/fluent 0.6.20 → 0.6.21

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
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>[]>;
@@ -79,4 +83,10 @@ export declare abstract class BaseConnector<ModelDTO, InputDTO, OutputDTO> {
79
83
  withPivot(): this;
80
84
  protected hasManyThrough(): void;
81
85
  private prepareInput;
86
+ validateInput(validationClass: {
87
+ new (): ModelDTO;
88
+ }, input: InputDTO): Promise<{
89
+ errors: ValidationError[] | null;
90
+ result: Awaited<InputDTO>;
91
+ }>;
82
92
  }
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseConnector = void 0;
4
4
  const typed_path_1 = require("typed-path");
5
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");
6
8
  class BaseConnector {
7
9
  constructor() {
8
10
  this.generatedKeyPath = (0, typed_path_1.typedPath)();
@@ -382,5 +384,19 @@ class BaseConnector {
382
384
  cols.filter((elem, pos, arr) => arr.indexOf(elem) === pos);
383
385
  return cols;
384
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
+ }
385
401
  }
386
402
  exports.BaseConnector = BaseConnector;
package/dist/index.d.ts CHANGED
@@ -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 { 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
  }