Skip to content

Commit

Permalink
refactor(repository): model decorator accepts options without name
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Delisle committed Jan 2, 2018
1 parent f83c502 commit 59bbfb6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/repository/src/decorators/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ type PropertyMap = MetadataMap<PropertyDefinition>;
* @param definition
* @returns {(target:any)}
*/
export function model(definition?: ModelDefinitionSyntax) {
export function model(definition?: Partial<ModelDefinitionSyntax>) {
return function(target: Function & {definition?: ModelDefinition}) {
if (!definition) {
definition = {name: target.name};
}

definition = definition || {};
const def: ModelDefinitionSyntax = Object.assign(definition, {
name: definition.name || target.name,
});
const decorator = ClassDecoratorFactory.createDecorator(
MODEL_KEY,
definition,
Expand All @@ -41,7 +41,7 @@ export function model(definition?: ModelDefinitionSyntax) {
decorator(target);

// Build "ModelDefinition" and store it on model constructor
const modelDef = new ModelDefinition(definition);
const modelDef = new ModelDefinition(def);

const propertyMap: PropertyMap =
MetadataInspector.getAllPropertyMetadata(
Expand Down
39 changes: 39 additions & 0 deletions packages/repository/test/unit/decorator/model-and-relation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ describe('model decorator', () => {
number: string;
}

@model({
properties: {
id: {
type: 'number',
required: true,
},
},
})
class Receipt extends Entity {
id: number;
customerId: number;
orderId: number;
subtotal: number;
tax: number;
}

@model()
class Account extends Entity {
id: string;
Expand Down Expand Up @@ -113,6 +129,29 @@ describe('model decorator', () => {
expect(meta).to.eql({name: 'order'});
});

it('adds model metadata without name', () => {
const meta = MetadataInspector.getClassMetadata(MODEL_KEY, Receipt);
expect(meta).to.eql({
name: 'Receipt',
properties: {
id: {
type: 'number',
required: true,
},
},
});
});

it('adds model metadata with custom name', () => {
@model({name: 'foo'})
class Doohickey {
name: string;
}

const meta = MetadataInspector.getClassMetadata(MODEL_KEY, Doohickey);
expect(meta).to.eql({name: 'foo'});
});

it('adds property metadata', () => {
const meta =
MetadataInspector.getAllPropertyMetadata(
Expand Down

0 comments on commit 59bbfb6

Please sign in to comment.