當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript PropertyRegistry.getProperties方法代碼示例

本文整理匯總了TypeScript中@tsed/common.PropertyRegistry.getProperties方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript PropertyRegistry.getProperties方法的具體用法?TypeScript PropertyRegistry.getProperties怎麽用?TypeScript PropertyRegistry.getProperties使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在@tsed/common.PropertyRegistry的用法示例。


在下文中一共展示了PropertyRegistry.getProperties方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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;
}
開發者ID:Romakita,項目名稱:ts-express-decorators,代碼行數:58,代碼來源:buildMongooseSchema.ts


注:本文中的@tsed/common.PropertyRegistry.getProperties方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。