Skip to content

Commit

Permalink
feat(repository): helper function for getting Model metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Delisle committed Dec 20, 2017
1 parent 721e8ee commit c5d4114
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/repository/src/decorators/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {InspectionOptions, MetadataInspector} from '@loopback/context';
import {MODEL_KEY, MODEL_PROPERTIES_KEY} from '../';
import {ModelDefinition, PropertyDefinition} from '../../index';

export class ModelMetadataHelper {
/**
* A utility function to simplify retrieving metadata from a target model and
* its properties.
* @param target The class from which to retrieve metadata.
* @param options An options object for the MetadataInspector to customize
* the output of the metadata retrieval functions.
*/
static getModelMetadata(target: Function, options?: InspectionOptions) {
const classDef = MetadataInspector.getClassMetadata(
MODEL_KEY,
target,
options,
);
const meta = new ModelDefinition(
Object.assign(
{
name: target.name,
properties: {},
},
classDef,
),
);

meta.properties = Object.assign(
<PropertyDefinition>{},
MetadataInspector.getAllPropertyMetadata(
MODEL_PROPERTIES_KEY,
target.prototype,
options,
),
);
return meta;
}
}
1 change: 1 addition & 0 deletions packages/repository/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './common-types';
export * from './decorators/model';
export * from './decorators/repository';
export * from './decorators/relation';
export * from './decorators/metadata';
export * from './types';
export * from './model';
export * from './query';
Expand Down
55 changes: 55 additions & 0 deletions packages/repository/test/unit/decorator/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {ModelMetadataHelper} from '../../../src';
import {property, model, ModelDefinition} from '../../..';
import {expect} from '@loopback/testlab';

describe('Repository', () => {
describe('getAllClassMetadata', () => {
@model()
class Colour {
@property({})
rgb: string;
}
@model()
class Widget {
@property() id: number;
@property.array(Colour) colours: Colour[];
}

@model()
class Samoflange {
id: number;
name: string;
canRotate: boolean;
}

it('retrieves metadata for classes with @model', () => {
const meta = ModelMetadataHelper.getModelMetadata(Samoflange);
expect(meta).to.deepEqual(
new ModelDefinition({
name: 'Samoflange',
properties: {},
settings: new Map(),
}),
);
});

it('retrieves metadata for classes with @model and @property', () => {
const meta = ModelMetadataHelper.getModelMetadata(Widget);
expect(meta).to.deepEqual(
new ModelDefinition({
properties: {
id: {
type: Number,
},
colours: {
array: true,
type: Colour,
},
},
settings: new Map(),
name: 'Widget',
}),
);
});
});
});

0 comments on commit c5d4114

Please sign in to comment.