Skip to content

Commit 5a493cf

Browse files
committed
feat(repository): change Model.modelName to a computed property
Implement `modelName` getter that returns model's `definition.name` or model class name if no definition was provided.
1 parent d2cece2 commit 5a493cf

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

packages/repository/src/model.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ function asObject(value: any, options?: Options): any {
167167
* Base class for models
168168
*/
169169
export abstract class Model {
170-
static modelName: string;
170+
static get modelName(): string {
171+
return (this.definition && this.definition.name) || this.name;
172+
}
173+
171174
static definition: ModelDefinition;
172175

173176
/**

packages/repository/test/unit/decorator/model-and-relation.decorator.unit.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ describe('model decorator', () => {
164164
expect(meta).to.eql({name: 'foo'});
165165
});
166166

167+
it('updates static property "modelName"', () => {
168+
@model()
169+
class Category extends Entity {}
170+
expect(Category.modelName).to.equal('Category');
171+
});
172+
167173
it('adds model metadata with arbitrary properties', () => {
168174
@model({arbitrary: 'property'})
169175
class Arbitrary {

packages/repository/test/unit/model/model.unit.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,13 @@ describe('model', () => {
293293
const instance = new NoId();
294294
expect(() => instance.getId()).to.throw(/missing.*id/);
295295
});
296+
297+
it('reads model name from the definition', () => {
298+
expect(Customer.modelName).to.equal('Customer');
299+
});
300+
301+
it('reads model name from the class name', () => {
302+
class MyModel extends Entity {}
303+
expect(MyModel.modelName).to.equal('MyModel');
304+
});
296305
});

0 commit comments

Comments
 (0)