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


TypeScript common.PropertyRegistry類代碼示例

本文整理匯總了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;
}
開發者ID:Romakita,項目名稱:ts-express-decorators,代碼行數:58,代碼來源:buildMongooseSchema.ts

示例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
    });
  });
}
開發者ID:Romakita,項目名稱:ts-express-decorators,代碼行數:9,代碼來源:virtualRef.ts

示例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)
    });
  });
}
開發者ID:Romakita,項目名稱:ts-express-decorators,代碼行數:11,代碼來源:ref.ts

示例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;
   }
 };
開發者ID:Romakita,項目名稱:ts-express-decorators,代碼行數:12,代碼來源:schema.ts


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