本文整理匯總了TypeScript中@tsed/common.PropertyRegistry類的典型用法代碼示例。如果您正苦於以下問題:TypeScript PropertyRegistry類的具體用法?TypeScript PropertyRegistry怎麽用?TypeScript PropertyRegistry使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了PropertyRegistry類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: buildMongooseSchema
export function buildMongooseSchema(target: any): MongooseSchema {
const properties = PropertyRegistry.getProperties(target);
const schema: MongooseSchema = {schema: {}, virtuals: new Map()};
if (properties) {
const jsonSchema: any = JsonSchemesRegistry.getSchemaDefinition(target) || {properties: {}};
for (const [key, metadata] of properties.entries()) {
if (MONGOOSE_RESERVED_KEYS.includes(key as string)) {
continue;
}
// Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.
const mongooseSchema = metadata.store.get(MONGOOSE_SCHEMA) || {};
if (mongooseSchema.ref && mongooseSchema.localField && mongooseSchema.foreignField) {
mongooseSchema.justOnce = !metadata.isArray;
schema.virtuals.set(key as string, mongooseSchema);
continue;
}
let definition: any = {
required: metadata.required
? function() {
return metadata.isRequired(this[key]);
}
: false
};
if (!metadata.isClass) {
definition = Object.assign(definition, {type: metadata.type}, mapProps(jsonSchema.properties[key]));
} else if (!mongooseSchema.ref) {
// References are handled by the final merge
definition = Object.assign(definition, {type: Store.from(metadata.type).get(MONGOOSE_SCHEMA)});
}
definition = clean(Object.assign(definition, mongooseSchema));
if (metadata.isCollection) {
if (metadata.isArray) {
definition = [definition];
} else {
// Can be a Map or a Set;
// Mongoose implements only Map;
if (metadata.collectionType !== Map) {
throw new Error(`Invalid collection type. ${metadata.collectionName} is not supported.`);
}
definition = {type: Map, of: definition};
}
}
schema.schema[key as string] = definition;
}
}
return schema;
}
示例2: VirtualRef
export function VirtualRef(type: string, foreignField: string) {
return PropertyRegistry.decorate((propertyMetadata: PropertyMetadata) => {
propertyMetadata.store.merge(MONGOOSE_SCHEMA, {
ref: type,
localField: propertyMetadata.name,
foreignField
});
});
}
示例3: Ref
export function Ref(type: string | any) {
return PropertyRegistry.decorate((propertyMetadata: PropertyMetadata) => {
if (typeof type !== "string") {
propertyMetadata.type = type;
}
propertyMetadata.store.merge(MONGOOSE_SCHEMA, {
type: Schema.Types.ObjectId,
ref: typeof type === "string" ? type : Store.from(type).get(MONGOOSE_MODEL_NAME)
});
});
}
示例4: return
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;
}
};