Skip to content

Commit

Permalink
fixup! introduce ModelDefinitionSyntax
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Aug 28, 2017
1 parent 878ec6b commit 33a087b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 35 deletions.
Expand Up @@ -27,9 +27,10 @@ const ds: juggler.DataSource = new DataSourceConstructor({
});

class Note extends Entity {
static definition = new ModelDefinition('note',
{title: 'string', content: 'string'},
{});
static definition = new ModelDefinition({
name: 'note',
properties: {title: 'string', content: 'string'},
});
}

class MyNoteRepository extends DefaultCrudRepository<Note, string> {
Expand Down
Expand Up @@ -41,9 +41,10 @@ const ds: juggler.DataSource = new DataSourceConstructor({
});

class Note extends Entity {
static definition = new ModelDefinition('note',
{title: 'string', content: 'string'},
{});
static definition = new ModelDefinition({
name: 'note',
properties: {title: 'string', content: 'string'},
});
}

async function main() {
Expand Down
14 changes: 3 additions & 11 deletions packages/repository/src/decorators/model.ts
Expand Up @@ -5,7 +5,7 @@

import {Class} from '../common-types';
import {Reflector} from '@loopback/context';
import {ModelDefinition, PropertyType} from '../model';
import {ModelDefinition, PropertyType, ModelDefinitionSyntax} from '../model';
import {PropertyDefinition} from '../index';

export const MODEL_KEY = 'loopback:model';
Expand All @@ -21,11 +21,7 @@ type PropertyMap = {[name: string]: PropertyDefinition};
* @param definition
* @returns {(target:any)}
*/
export function model(definition?: {
name: string;
properties?: {[name: string]: PropertyDefinition | PropertyType};
settings?: {[name: string]: any};
}) {
export function model(definition?: ModelDefinitionSyntax) {
return function(target: any) {
if (!definition) {
definition = {name: target.name};
Expand All @@ -35,11 +31,7 @@ export function model(definition?: {
Reflector.defineMetadata(MODEL_KEY, definition, target);

// Build "ModelDefinition" and store it on model constructor
const modelDef = new ModelDefinition(
definition.name,
definition.properties,
definition.settings,
);
const modelDef = new ModelDefinition(definition);

const propertyMap: PropertyMap = Reflector.getMetadata(
MODEL_PROPERTIES_KEY,
Expand Down
24 changes: 19 additions & 5 deletions packages/repository/src/model.ts
Expand Up @@ -36,6 +36,19 @@ export interface PropertyForm {
name?: string; // Custom name for this form
}

export interface PropertyDefinitionMap {
}

/**
* DSL for building a model definition.
*/
export interface ModelDefinitionSyntax {
name: string;
properties?: {[name: string]: PropertyDefinition | PropertyType};
settings?: {[name: string]: any};
}


/**
* Definition for a model
*/
Expand All @@ -46,11 +59,12 @@ export class ModelDefinition {
// indexes: Map<string, any>;
[attribute: string]: any; // Other attributes

constructor(
name: string,
properties?: {[name: string]: PropertyDefinition | PropertyType},
settings?: {[name: string]: any},
) {
constructor(nameOrDef: string | ModelDefinitionSyntax) {
if (typeof nameOrDef === 'string') {
nameOrDef = {name: nameOrDef};
}
const {name, properties, settings} = nameOrDef;

this.name = name;

this.properties = {};
Expand Down
11 changes: 8 additions & 3 deletions packages/repository/test/unit/decorator/repository-with-di.ts
Expand Up @@ -34,9 +34,14 @@ describe('repository class', () => {
});

class Note extends Entity {
static definition = new ModelDefinition('note',
{title: 'string', content: 'string', id: {type: 'number', id: true}},
{});
static definition = new ModelDefinition({
name: 'note',
properties: {
title: 'string',
content: 'string',
id: {type: 'number', id: true},
},
});

title: string;
content: string;
Expand Down
Expand Up @@ -48,9 +48,14 @@ describe('repository class', () => {
});

class Note extends Entity {
static definition = new ModelDefinition('note',
{title: 'string', content: 'string', id: {type: 'number', id: true}},
{});
static definition = new ModelDefinition({
name: 'note',
properties: {
title: 'string',
content: 'string',
id: {type: 'number', id: true},
},
});
}

ctx = new Context();
Expand Down
11 changes: 8 additions & 3 deletions packages/repository/test/unit/decorator/repository.ts
Expand Up @@ -32,9 +32,14 @@ describe('repository decorator', () => {
let ds: juggler.DataSource;

class Note extends Entity {
static definition = new ModelDefinition('note',
{title: 'string', content: 'string', id: {type: 'number', id: true}},
{});
static definition = new ModelDefinition({
name: 'note',
properties: {
title: 'string',
content: 'string',
id: {type: 'number', id: true},
},
});
}

before(function() {
Expand Down
Expand Up @@ -49,10 +49,13 @@ describe('DefaultCrudRepository', () => {
let ds: juggler.DataSource;

class Note extends Entity {
static definition = new ModelDefinition('note3', {
title: 'string',
content: 'string',
id: {name: 'id', type: 'number', id: true},
static definition = new ModelDefinition({
name: 'note3',
properties: {
title: 'string',
content: 'string',
id: {name: 'id', type: 'number', id: true},
},
});
}

Expand Down

0 comments on commit 33a087b

Please sign in to comment.