How to use the @loopback/context.MetadataInspector.getAllPropertyMetadata function in @loopback/context

To help you get started, we’ve selected a few @loopback/context examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / model-and-relation.decorator.unit.ts View on Github external
it('adds belongsTo metadata', () => {
    const meta =
      MetadataInspector.getAllPropertyMetadata(
        RELATIONS_KEY,
        Order.prototype,
      ) ?? /* istanbul ignore next */ {};
    const relationDef = meta.customerId;
    expect(relationDef).to.containEql({
      type: RelationType.belongsTo,
      name: 'customer',
      target: () => Customer,
      keyFrom: 'customerId',
    });
    expect(relationDef.source).to.be.exactly(Order);
    expect(relationDef.target()).to.be.exactly(Customer);
  });
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / model-and-relation.decorator.unit.ts View on Github external
it('"@property.array" adds array metadata', () => {
        @model()
        class TestModel {
          @property.array(Product)
          items: Product[];
        }

        const meta =
          MetadataInspector.getAllPropertyMetadata(
            MODEL_PROPERTIES_KEY,
            TestModel.prototype,
          ) ?? /* istanbul ignore next */ {};
        expect(meta.items).to.eql({type: Array, itemType: Product});
      });
github strongloop / loopback-next / packages / repository / src / decorators / model.decorator.ts View on Github external
) {
  // Check if the definition for this class has been built (not from the super
  // class)
  const baseClass = Object.getPrototypeOf(target);
  if (
    !def &&
    target.definition &&
    baseClass &&
    target.definition !== baseClass.definition
  ) {
    return target.definition;
  }
  const modelDef = new ModelDefinition(def ?? {name: target.name});
  const prototype = target.prototype;
  const propertyMap: PropertyMap =
    MetadataInspector.getAllPropertyMetadata(MODEL_PROPERTIES_KEY, prototype) ??
    {};
  for (const p in propertyMap) {
    const propertyDef = propertyMap[p];
    const designType = MetadataInspector.getDesignTypeForProperty(prototype, p);
    if (!propertyDef.type) {
      propertyDef.type = designType;
    }
    modelDef.addProperty(p, propertyDef);
  }
  target.definition = modelDef;
  const relationMeta: RelationDefinitionMap =
    MetadataInspector.getAllPropertyMetadata(RELATIONS_KEY, prototype) ?? {};
  const relations: RelationDefinitionMap = {};
  // Build an object keyed by relation names
  Object.values(relationMeta).forEach(r => {
    relations[r.name] = r;
github strongloop / loopback-next / packages / repository / src / decorators / model.decorator.ts View on Github external
const modelDef = new ModelDefinition(def ?? {name: target.name});
  const prototype = target.prototype;
  const propertyMap: PropertyMap =
    MetadataInspector.getAllPropertyMetadata(MODEL_PROPERTIES_KEY, prototype) ??
    {};
  for (const p in propertyMap) {
    const propertyDef = propertyMap[p];
    const designType = MetadataInspector.getDesignTypeForProperty(prototype, p);
    if (!propertyDef.type) {
      propertyDef.type = designType;
    }
    modelDef.addProperty(p, propertyDef);
  }
  target.definition = modelDef;
  const relationMeta: RelationDefinitionMap =
    MetadataInspector.getAllPropertyMetadata(RELATIONS_KEY, prototype) ?? {};
  const relations: RelationDefinitionMap = {};
  // Build an object keyed by relation names
  Object.values(relationMeta).forEach(r => {
    relations[r.name] = r;
  });
  target.definition.relations = relations;
  return modelDef;
}
github strongloop / loopback-next / packages / repository / src / __tests__ / unit / decorator / relation.decorator.unit.ts View on Github external
it('creates juggler property metadata', () => {
      @model()
      class AddressBook extends Entity {
        @property({id: true})
        id: number;
      }
      @model()
      class Address extends Entity {
        @belongsTo(() => AddressBook)
        addressBookId: number;
      }
      const jugglerMeta = MetadataInspector.getAllPropertyMetadata(
        MODEL_PROPERTIES_KEY,
        Address.prototype,
      );
      expect(jugglerMeta).to.eql({
        addressBookId: {
          type: Number,
        },
      });
      expect(Address.definition.relations).to.containDeep({
        addressBook: {
          keyFrom: 'addressBookId',
          name: 'addressBook',
          type: 'belongsTo',
        },
      });
    });