Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Fix typescript error avoid ID not matching when use @dbType #2658

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/cli/src/template/model.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class <%= props.className %> implements CompatEntity {
}
}
<% } else { %>static async getBy<%=helper.upperFirst(field.name) %>(<%=field.name %>: <%=field.type %>, options: GetOptions<Compat<%=props.className %>Props>): Promise<<%=props.className %>[]> {
// @ts-ignore
const records = await store.getByField<Compat<%=props.className %>Props>('<%=props.entityName %>', '<%=field.name %>', <%=field.name %>, options);
return records.map(record => this.create(record as unknown as <%= props.className %>Props));
}
Expand All @@ -80,6 +81,7 @@ export class <%= props.className %> implements CompatEntity {
* ⚠️ This function will first search cache data followed by DB data. Please consider this when using order and offset options.⚠️
* */
static async getByFields(filter: FieldsExpression<<%= props.className %>Props>[], options: GetOptions<<%= props.className %>Props>): Promise<<%=props.className %>[]> {
// @ts-ignore
const records = await store.getByFields<<%=props.className %>Props>('<%=props.entityName %>', filter, options);
return records.map(record => this.create(record as unknown as <%= props.className %>Props));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export class PlainModel<T extends BaseEntity = BaseEntity> implements IModel<T>
}

async set(id: string, data: T, blockHeight: number, tx?: Transaction): Promise<void> {
if (id !== data.id) {
// NOTE: this is to fix the error when use @dbType('BigInt') or maybe Int, Float as parameters on the graphql schema
const dataId = data.id as unknown as string | bigint | number;
if (typeof dataId !== 'string' && typeof dataId.toString === 'function' && dataId.toString() !== id) {
throw new Error(`Id doesnt match with data ${typeof id} !== ${typeof data.id}`);
} else if (typeof dataId === 'string' && dataId !== id) {
throw new Error(`Id doesnt match with data`);
}

Expand Down