Skip to content

Commit 1c2cb2c

Browse files
committed
Sets proper name on record type for identification
Also supports a custom name on the `@Model()` decorator.
1 parent 7ccf1f5 commit 1c2cb2c

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

desktop/src/@batch-flask/core/record/decorators.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ export function ListProp<T>(type: any) {
5656
};
5757
}
5858

59-
export function Model() {
59+
export function Model(name?: string) {
6060
return <T extends new(...args: any[]) => {}>(ctr: T) => {
6161
if (!(ctr.prototype instanceof Record)) {
6262
throw new RecordMissingExtendsError(ctr);
6363
}
64-
65-
return (class extends ctr {
64+
const model = (class extends ctr {
6665
constructor(...args: any[]) {
6766
const [data] = args;
6867
if (data instanceof ctr) {
@@ -72,5 +71,7 @@ export function Model() {
7271
(this as any)._completeInitialization();
7372
}
7473
});
74+
Object.defineProperty(model, "name", { value: name || ctr.name });
75+
return model;
7576
};
7677
}

desktop/src/@batch-flask/core/record/record.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class InheritedTestRec extends SimpleTestRec {
4444
public d: number;
4545
}
4646

47+
@Model("CustomName")
48+
class CustomNameRec extends Record<any> {
49+
@Prop()
50+
public id: string = "default-id";
51+
}
52+
4753
describe("Record", () => {
4854
it("should throw an exeption when record doesn't extends Record class", () => {
4955
try {
@@ -176,4 +182,12 @@ describe("Record", () => {
176182
expect(SimpleTestRec.isStaticMethod).not.toBeFalsy();
177183
expect(SimpleTestRec.isStaticMethod()).toBe(true);
178184
});
185+
186+
it("should allow a custom record name to be set", () => {
187+
const rec1 = new TestRec();
188+
expect(rec1.constructor.name).toEqual("TestRec");
189+
190+
const rec2 = new CustomNameRec();
191+
expect(rec2.constructor.name).toEqual("CustomName");
192+
});
179193
});

0 commit comments

Comments
 (0)