-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repository): helper function for getting Model metadata
- Loading branch information
Kevin Delisle
committed
Jan 2, 2018
1 parent
59bbfb6
commit 4a293d3
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {InspectionOptions, MetadataInspector} from '@loopback/context'; | ||
import {MODEL_PROPERTIES_KEY, MODEL_WITH_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) { | ||
let classDef = MetadataInspector.getClassMetadata( | ||
MODEL_WITH_PROPERTIES_KEY, | ||
target, | ||
options, | ||
); | ||
// Return the cached value, if it exists. | ||
// XXX(kjdelisle): If we're going to support dynamic updates, then this | ||
// will be problematic in the future, since it will never update. | ||
if (classDef) { | ||
return classDef; | ||
} else { | ||
const meta = new ModelDefinition( | ||
Object.assign({name: target.name}, classDef), | ||
); | ||
meta.properties = Object.assign( | ||
<PropertyDefinition>{}, | ||
MetadataInspector.getAllPropertyMetadata( | ||
MODEL_PROPERTIES_KEY, | ||
target.prototype, | ||
options, | ||
), | ||
); | ||
MetadataInspector.defineMetadata(MODEL_WITH_PROPERTIES_KEY, meta, target); | ||
return meta; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import {ModelMetadataHelper} from '../../../src'; | ||
import { | ||
property, | ||
model, | ||
ModelDefinition, | ||
MODEL_KEY, | ||
MODEL_WITH_PROPERTIES_KEY, | ||
} from '../../..'; | ||
import {expect} from '@loopback/testlab'; | ||
import {MetadataInspector} from '@loopback/context'; | ||
|
||
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; | ||
} | ||
|
||
@model() | ||
class Phlange { | ||
@property() id: number; | ||
@property() canFlap: boolean; | ||
@property.array(Colour) colours: Colour[]; | ||
} | ||
|
||
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', | ||
}), | ||
); | ||
}); | ||
|
||
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, | ||
classMeta, | ||
Phlange, | ||
); | ||
|
||
const meta = ModelMetadataHelper.getModelMetadata( | ||
Phlange, | ||
) as ModelDefinition; | ||
expect(meta.properties).to.eql(classMeta.properties); | ||
}); | ||
}); | ||
}); |