-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mongoose): Schema decorator can be used to decorate a class
- Loading branch information
Showing
15 changed files
with
242 additions
and
147 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
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 |
---|---|---|
@@ -1,17 +1,53 @@ | ||
import {getDecoratorType} from "@tsed/core"; | ||
import {PropertyMetadata, PropertyRegistry} from "@tsed/common"; | ||
import {SchemaTypeOpts} from "mongoose"; | ||
import {MONGOOSE_SCHEMA} from "../constants"; | ||
import {MongooseSchemaOptions} from "../interfaces"; | ||
import {createSchema} from "../utils"; | ||
|
||
/** | ||
* Define a class as a Mongoose Schema ready to be used to compose other schemes and models. | ||
* | ||
* ### Example | ||
* | ||
* ```typescript | ||
* @Schema() | ||
* export class EventSchema { | ||
* @Property() | ||
* field: string; | ||
* } | ||
* ``` | ||
* | ||
* ### Options | ||
* | ||
* - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior. | ||
* | ||
* @param {MongooseSchemaOptions | undefined} options | ||
* @returns {(target: any) => void} | ||
* @decorator | ||
* @mongoose | ||
*/ | ||
export function Schema(options?: MongooseSchemaOptions): (target: any) => void; | ||
/** | ||
* Attach a schema on property class. | ||
* | ||
* @param definition A mongoose schema | ||
* @param {SchemaTypeOpts<any>} definition | ||
* @returns {Function} | ||
* @decorator | ||
* @mongoose | ||
*/ | ||
export function Schema(definition: SchemaTypeOpts<any>) { | ||
return PropertyRegistry.decorate((propertyMetadata: PropertyMetadata) => { | ||
propertyMetadata.store.merge(MONGOOSE_SCHEMA, definition); | ||
}); | ||
export function Schema(definition: SchemaTypeOpts<any>): Function; | ||
export function Schema(options: MongooseSchemaOptions | SchemaTypeOpts<any> = {}) { | ||
return (...parameters: any[]) => { | ||
switch (getDecoratorType(parameters)) { | ||
case "property": | ||
return PropertyRegistry.decorate((propertyMetadata: PropertyMetadata) => { | ||
propertyMetadata.store.merge(MONGOOSE_SCHEMA, options as SchemaTypeOpts<any>); | ||
})(...parameters); | ||
case "class": | ||
const [target] = parameters; | ||
createSchema(target, options as MongooseSchemaOptions); | ||
break; | ||
} | ||
}; | ||
} |
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,75 +1,7 @@ | ||
import {HookDoneFunction, HookErrorCallback, HookNextFunction, NativeError, Schema, SchemaOptions} from "mongoose"; | ||
import {MongooseDocument} from "./MongooseDocument"; | ||
import {MongooseSchemaOptions} from "./MongooseSchemaOptions"; | ||
|
||
/** | ||
* | ||
*/ | ||
export interface MongoosePreHookAsyncCB<T> { | ||
(doc: MongooseDocument<T>, next: HookNextFunction, done: HookDoneFunction): any; | ||
} | ||
|
||
export interface MongoosePreHookSyncCB<T> { | ||
(doc: MongooseDocument<T>, next: HookNextFunction): any; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
export interface MongoosePostErrorHookCB<T> { | ||
(error: any, doc: MongooseDocument<T>, next: (err?: NativeError) => void): void; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
export interface MongoosePostHookCB<T> { | ||
(doc: MongooseDocument<T>, next: (err?: NativeError) => void): void; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
export interface MongoosePreHook { | ||
method: string; | ||
fn: MongoosePreHookSyncCB<any> | MongoosePreHookAsyncCB<any>; | ||
parallel?: boolean; | ||
errorCb?: HookErrorCallback; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
export interface MongoosePostHook { | ||
method: string; | ||
fn: MongoosePostHookCB<any> | MongoosePostErrorHookCB<any>; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
export interface MongoosePluginOptions { | ||
plugin: (schema: Schema, options?: any) => void; | ||
options?: any; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
export interface MongooseIndexOptions { | ||
fields: object; | ||
options?: any; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
export interface MongooseModelOptions { | ||
schemaOptions?: SchemaOptions; | ||
export interface MongooseModelOptions extends MongooseSchemaOptions { | ||
name?: string; | ||
collection?: string; | ||
skipInit?: boolean; | ||
plugins?: MongoosePluginOptions[]; | ||
indexes?: MongooseIndexOptions[]; | ||
pre?: MongoosePreHook[]; | ||
post?: MongoosePostHook[]; | ||
} |
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,48 @@ | ||
import {HookDoneFunction, HookErrorCallback, HookNextFunction, NativeError, Schema, SchemaOptions} from "mongoose"; | ||
import {MongooseDocument} from "./MongooseDocument"; | ||
|
||
export interface MongoosePreHookAsyncCB<T> { | ||
(doc: MongooseDocument<T>, next: HookNextFunction, done: HookDoneFunction): any; | ||
} | ||
|
||
export interface MongoosePreHookSyncCB<T> { | ||
(doc: MongooseDocument<T>, next: HookNextFunction): any; | ||
} | ||
|
||
export interface MongoosePostErrorHookCB<T> { | ||
(error: any, doc: MongooseDocument<T>, next: (err?: NativeError) => void): void; | ||
} | ||
|
||
export interface MongoosePostHookCB<T> { | ||
(doc: MongooseDocument<T>, next: (err?: NativeError) => void): void; | ||
} | ||
|
||
export interface MongoosePreHook { | ||
method: string; | ||
fn: MongoosePreHookSyncCB<any> | MongoosePreHookAsyncCB<any>; | ||
parallel?: boolean; | ||
errorCb?: HookErrorCallback; | ||
} | ||
|
||
export interface MongoosePostHook { | ||
method: string; | ||
fn: MongoosePostHookCB<any> | MongoosePostErrorHookCB<any>; | ||
} | ||
|
||
export interface MongoosePluginOptions { | ||
plugin: (schema: Schema, options?: any) => void; | ||
options?: any; | ||
} | ||
|
||
export interface MongooseIndexOptions { | ||
fields: object; | ||
options?: any; | ||
} | ||
|
||
export interface MongooseSchemaOptions { | ||
schemaOptions?: SchemaOptions; | ||
plugins?: MongoosePluginOptions[]; | ||
indexes?: MongooseIndexOptions[]; | ||
pre?: MongoosePreHook[]; | ||
post?: MongoosePostHook[]; | ||
} |
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
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
Oops, something went wrong.