@@ -13,7 +13,13 @@ import {<% props.importEnums.forEach(function(e){ %>
13
13
14
14
export type <%= props .className %> Props = Omit<<%= props .className %> , NonNullable<FunctionPropertyNames <<% =props.className % >>> | '_name'>;
15
15
16
- export class <%= props .className %> implements Entity {
16
+ /*
17
+ * Compat types allows for support of alternative `id` types without refactoring the node
18
+ */
19
+ type Compat<%= props .className %> Props = Omit<<%= props .className %> Props, 'id'> & { id: string; };
20
+ type CompatEntity = Omit<Entity , ' id' > & { id: <%= props .idType %> ; };
21
+
22
+ export class <%= props .className %> implements CompatEntity {
17
23
18
24
constructor(
19
25
<% props .fields .forEach (function (field ) { if (field .required ) { % >
@@ -31,21 +37,21 @@ export class <%= props.className %> implements Entity {
31
37
}
32
38
33
39
async save(): Promise<void > {
34
- let id = this.id;
40
+ const id = this.id;
35
41
assert(id !== null, "Cannot save <%= props .className %> entity without an ID");
36
- await store.set('<%= props .entityName %> ', id.toString(), this);
42
+ await store.set('<%= props .entityName %> ', id.toString(), this as unknown as Compat <%= props . className %> Props );
37
43
}
38
44
39
- static async remove(id: string ): Promise<void > {
45
+ static async remove(id: <%= props . idType %> ): Promise<void > {
40
46
assert(id !== null, "Cannot remove <%= props .className %> entity without an ID");
41
47
await store.remove('<%= props .entityName %> ', id.toString());
42
48
}
43
49
44
- static async get(id: string ): Promise<<%= props .className %> | undefined> {
50
+ static async get(id: <%= props . idType %> ): Promise<<%= props .className %> | undefined> {
45
51
assert((id !== null && id !== undefined), "Cannot get <%= props .className %> entity without an ID");
46
52
const record = await store.get('<%= props .entityName %> ', id.toString());
47
53
if (record) {
48
- return this.create(record as <%= props .className %> Props);
54
+ return this.create(record as unknown as <%= props .className %> Props);
49
55
} else {
50
56
return;
51
57
}
@@ -56,14 +62,14 @@ export class <%= props.className %> implements Entity {
56
62
static async getBy< %= helper .upperFirst (field .name ) % > (< %= field .name % > : < %= field .type % > ): Promise << %= props .className % > | undefined > {
57
63
const record = await store .getOneByField (' <%=props.entityName %>' , ' <%=field.name %>' , < %= field .name % > );
58
64
if (record) {
59
- return this .create (record as < %= props .className % > Props);
65
+ return this .create (record as unknown as < %= props .className % > Props);
60
66
} else {
61
67
return ;
62
68
}
63
69
}
64
- < % } else { % > static async getBy< %= helper .upperFirst (field .name ) % > (< %= field .name % > : < %= field .type % > , options: GetOptions<< %= props .className % >> ): Promise << %= props .className % > []> {
65
- const records = await store .getByField << %= props .className % >> (' <%=props.entityName %>' , ' <%=field.name %>' , < %= field .name % > , options);
66
- return records .map (record => this .create (record as < %= props .className % > Props));
70
+ < % } else { % > static async getBy< %= helper .upperFirst (field .name ) % > (< %= field .name % > : < %= field .type % > , options: GetOptions< Compat < %= props .className % > Props > ): Promise << %= props .className % > []> {
71
+ const records = await store .getByField < Compat < %= props .className % > Props > (' <%=props.entityName %>' , ' <%=field.name %>' , < %= field .name % > , options);
72
+ return records .map (record => this .create (record as unknown as < %= props .className % > Props));
67
73
}
68
74
< % }% >
69
75
< % }); %>
@@ -73,14 +79,14 @@ export class <%= props.className %> implements Entity {
73
79
*
74
80
* ⚠️ This function will first search cache data followed by DB data. Please consider this when using order and offset options.⚠️
75
81
* */
76
- static async getByFields(filter: FieldsExpression<<%= props .className %> Props>[], options: GetOptions<<%= props .className %> Props>): Promise<<%= props .className %> []> {
77
- const records = await store.getByFields<<%= props .className %> >('<%= props .entityName %> ', filter, options);
78
- return records.map(record => this.create(record as <%= props .className %> Props));
82
+ static async getByFields(filter: FieldsExpression<<%= props .className %> Props>[], options: GetOptions<Compat <% = props.className % >Props>): Promise<<%= props .className %> []> {
83
+ const records = await store.getByFields<Compat <% =props.className % >Props >('<%= props .entityName %> ', filter, options);
84
+ return records.map(record => this.create(record as unknown as <%= props .className %> Props));
79
85
}
80
86
81
87
static create(record: <%= props .className %> Props): <%= props .className %> {
82
- assert(typeof record.id === 'string' , "id must be provided");
83
- let entity = new this(
88
+ assert(record.id !== undefined && record.id !== null , "id must be provided");
89
+ const entity = new this(
84
90
<% props .fields .filter (function (field ) {return field .required === true ;}).forEach (function (requiredField ) { % > record.< %= requiredField .name % > ,
85
91
< % }) %> );
86
92
Object.assign(entity,record);
0 commit comments