Skip to content

Commit

Permalink
feat: add virtual metadata to type metadata storage
Browse files Browse the repository at this point in the history
  • Loading branch information
LotfiMEZIANI committed May 22, 2022
1 parent ddd09e9 commit 6dc07b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/metadata/virtual-metadata.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { VirtualTypeOptions } from 'mongoose';

export interface VirtualMetadataInterface {
target: Function;
name: string;
options?: VirtualTypeOptions;
getter?: (...args: any[]) => any;
setter?: (...args: any[]) => any;
}
10 changes: 10 additions & 0 deletions lib/storages/type-metadata.storage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Type } from '@nestjs/common';
import { PropertyMetadata } from '../metadata/property-metadata.interface';
import { SchemaMetadata } from '../metadata/schema-metadata.interface';
import { VirtualMetadataInterface } from '../metadata/virtual-metadata.interface';
import { isTargetEqual } from '../utils/is-target-equal-util';

export class TypeMetadataStorageHost {
private schemas = new Array<SchemaMetadata>();
private properties = new Array<PropertyMetadata>();
private virtuals = new Array<VirtualMetadataInterface>();

addPropertyMetadata(metadata: PropertyMetadata) {
this.properties.unshift(metadata);
Expand All @@ -16,6 +18,10 @@ export class TypeMetadataStorageHost {
this.schemas.push(metadata);
}

addVirtualMetadata(metadata: VirtualMetadataInterface) {
this.virtuals.push(metadata);
}

getSchemaMetadataByTarget(target: Type<unknown>): SchemaMetadata | undefined {
return this.schemas.find((item) => item.target === target);
}
Expand All @@ -33,6 +39,10 @@ export class TypeMetadataStorageHost {
) {
return this.properties.filter(belongsToClass);
}

getVirtualsMetadataByTarget<TClass>(targetFilter: Type<TClass>) {
return this.virtuals.filter(({ target }) => target === targetFilter);
}
}

const globalRef = global as any;
Expand Down

0 comments on commit 6dc07b9

Please sign in to comment.