Skip to content

Commit

Permalink
fix(): fix date and map support for defs factory
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jul 8, 2020
1 parent 663d31c commit 6d0a254
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/factories/definitions.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as mongoose from 'mongoose';
import { PropOptions } from '../decorators';
import { TypeMetadataStorage } from '../storages/type-metadata.storage';

const PRIMITIVE_TYPES: Function[] = [Boolean, Number, String];
const BUILT_IN_TYPES: Function[] = [Boolean, Number, String, Map, Date];

export class DefinitionsFactory {
static createForClass(target: Type<unknown>): mongoose.SchemaDefinition {
Expand Down Expand Up @@ -62,7 +62,7 @@ export class DefinitionsFactory {
}

private static isPrimitive(type: Function) {
return PRIMITIVE_TYPES.includes(type);
return BUILT_IN_TYPES.includes(type);
}

private static isMongooseSchemaType(type: Function) {
Expand Down
2 changes: 1 addition & 1 deletion lib/storages/type-metadata.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class TypeMetadataStorageHost {
}

getSchemaMetadataByTarget(target: Type<unknown>): SchemaMetadata | undefined {
return this.schemas.find(item => item.target === target);
return this.schemas.find((item) => item.target === target);
}

private compileClassMetadata(metadata: SchemaMetadata) {
Expand Down
18 changes: 18 additions & 0 deletions tests/e2e/schema-definitions.factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class ExampleClass {
@Prop()
mixed: mongoose.Schema.Types.Mixed;

@Prop(
raw({
expires: 0,
type: Date,
}),
)
expiresAt: Date;

@Prop()
map: Map<any, any>;

@Prop()
isEnabled: boolean;

Expand Down Expand Up @@ -90,9 +101,16 @@ describe('DefinitionsFactory', () => {
array: { type: [] },
customArray: [{ custom: 'literal', object: true }],
customObject: { custom: 'literal', object: true },
expiresAt: {
expires: 0,
type: Date,
},
isEnabled: {
type: Boolean,
},
map: {
type: Map,
},
mixed: { type: mongoose.Schema.Types.Mixed },
number: { type: Number },
});
Expand Down

0 comments on commit 6d0a254

Please sign in to comment.