How to use the @loopback/context.MetadataInspector.defineMetadata 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 / metadata.unit.ts View on Github external
it('returns cached metadata instead of recreating it', () => {
      const classMeta = MetadataInspector.getClassMetadata(
        MODEL_KEY,
        Phlange,
      ) as ModelDefinition;
      classMeta.properties = {
        foo: {
          type: String,
        },
      };
      // Intentionally change the metadata to be different from the Phlange
      // class metadata
      MetadataInspector.defineMetadata(
        MODEL_WITH_PROPERTIES_KEY.key,
        classMeta,
        Phlange,
      );

      const meta = ModelMetadataHelper.getModelMetadata(
        Phlange,
      ) as ModelDefinition;
      expect(meta.properties).to.eql(classMeta.properties);
    });
  });
github strongloop / loopback-next / packages / repository-json-schema / src / build-schema.ts View on Github external
// In the near future the metadata will be an object with
  // different titles as keys
  const cached = MetadataInspector.getClassMetadata(JSON_SCHEMA_KEY, ctor);
  const key = buildModelCacheKey(options);
  let schema = cached?.[key];

  if (!schema) {
    // Create new json schema from model
    // if not found in cache for specific key
    schema = modelToJsonSchema(ctor, options);
    if (cached) {
      // Add a new key to the cached schema of the model
      cached[key] = schema;
    } else {
      // Define new metadata and set in cache
      MetadataInspector.defineMetadata(
        JSON_SCHEMA_KEY.key,
        {[key]: schema},
        ctor,
      );
    }
  }

  return schema;
}
github strongloop / loopback-next / packages / repository-json-schema / src / __tests__ / integration / build-schema.integration.ts View on Github external
it('gets cached JSON schema if one exists', () => {
      @model()
      class TestModel {
        @property()
        foo: number;
      }

      const cachedSchema: JsonSchema = {
        properties: {
          cachedProperty: {
            type: 'string',
          },
        },
      };
      MetadataInspector.defineMetadata(
        JSON_SCHEMA_KEY,
        {[MODEL_TYPE_KEYS.ModelOnly]: cachedSchema},
        TestModel,
      );
      const jsonSchema = getJsonSchema(TestModel);
      expect(jsonSchema).to.eql(cachedSchema);
    });
    it('creates JSON schema if one does not already exist', () => {
github strongloop / loopback-next / packages / repository-json-schema / src / __tests__ / integration / build-schema.integration.ts View on Github external
},
            additionalProperties: false,
          },
        },
        properties: {
          id: {type: 'number'},
          cachedProp: {type: 'string'},
          products: {
            type: 'array',
            items: {$ref: '#/definitions/ProductWithRelations'},
          },
        },
        additionalProperties: false,
        title: 'CategoryWithRelations',
      };
      MetadataInspector.defineMetadata(
        JSON_SCHEMA_KEY,
        {[MODEL_TYPE_KEYS.ModelWithRelations]: cachedSchema},
        Category,
      );
      const jsonSchema = getJsonSchema(Category, {includeRelations: true});
      expect(jsonSchema).to.eql(cachedSchema);
    });
github strongloop / loopback-next / packages / openapi-v2 / src / controller-spec.ts View on Github external
export function getControllerSpec(constructor: Function): ControllerSpec {
  let spec = MetadataInspector.getClassMetadata(
    OAI2Keys.CONTROLLER_SPEC_KEY,
    constructor,
    {ownMetadataOnly: true},
  );
  if (!spec) {
    spec = resolveControllerSpec(constructor);
    MetadataInspector.defineMetadata(
      OAI2Keys.CONTROLLER_SPEC_KEY,
      spec,
      constructor,
    );
  }
  return spec;
}