-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
686f62e
commit acbf36c
Showing
7 changed files
with
66 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { isUndefined } from '@nestjs/common/utils/shared.utils'; | ||
import * as mongoose from 'mongoose'; | ||
import { TypeMetadataStorage } from '../storages/type-metadata.storage'; | ||
|
||
export class DefinitionsFactory { | ||
static createForClass(target: Type<unknown>): mongoose.SchemaDefinition { | ||
if (!target) { | ||
throw new Error( | ||
`Target class "${target}" passed in to the "DefinitionsFactory#createForClass()" method is "undefined".`, | ||
); | ||
} | ||
let schemaDefinition: mongoose.SchemaDefinition = {}; | ||
let parent: Function = target; | ||
|
||
while (!isUndefined(parent.prototype)) { | ||
if (parent === Function.prototype) { | ||
break; | ||
} | ||
const schemaMetadata = TypeMetadataStorage.getSchemaMetadataByTarget( | ||
parent as Type<unknown>, | ||
); | ||
if (!schemaMetadata) { | ||
parent = Object.getPrototypeOf(parent); | ||
continue; | ||
} | ||
schemaMetadata.properties?.forEach(item => { | ||
schemaDefinition = { | ||
[item.propertyKey]: item.options, | ||
...schemaDefinition, | ||
}; | ||
}); | ||
parent = Object.getPrototypeOf(parent); | ||
} | ||
|
||
return schemaDefinition; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './definitions.factory'; | ||
export * from './schema.factory'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,17 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { isUndefined } from '@nestjs/common/utils/shared.utils'; | ||
import * as mongoose from 'mongoose'; | ||
import { TypeMetadataStorage } from '../storages/type-metadata.storage'; | ||
import { DefinitionsFactory } from './definitions.factory'; | ||
|
||
export class SchemaFactory { | ||
static createForClass(target: Type<unknown>) { | ||
if (!target) { | ||
throw new Error(''); | ||
} | ||
const schemaDefinition = DefinitionsFactory.createForClass(target); | ||
const schemaMetadata = TypeMetadataStorage.getSchemaMetadataByTarget( | ||
target, | ||
); | ||
if (!schemaMetadata) { | ||
throw new Error('no'); | ||
} | ||
|
||
let schemaDefinition: mongoose.SchemaDefinition = {}; | ||
let parent: Function = target; | ||
|
||
while (!isUndefined(parent.prototype)) { | ||
parent = Object.getPrototypeOf(parent); | ||
if (parent === Function.prototype) { | ||
break; | ||
} | ||
schemaMetadata.properties?.forEach(item => { | ||
schemaDefinition = { | ||
[item.propertyKey]: item.options, | ||
...schemaDefinition, | ||
}; | ||
}); | ||
} | ||
|
||
return new mongoose.Schema(schemaDefinition, schemaMetadata.options); | ||
return new mongoose.Schema( | ||
schemaDefinition, | ||
schemaMetadata && schemaMetadata.options, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters