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

Fix generated watcher query with bigint arguments. #2

Merged
merged 1 commit into from
Sep 24, 2021
Merged
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
11 changes: 11 additions & 0 deletions packages/codegen/src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class Entity {
assert(pgType);

const columnOptions = [];

if (param.type === 'address') {
columnOptions.push(
{
Expand All @@ -105,6 +106,16 @@ export class Entity {
);
}

// Use bigintTransformer for bigint types.
if (tsType === 'bigint') {
columnOptions.push(
{
option: 'transformer',
value: 'bigintTransformer'
}
);
}

return {
name,
pgType,
Expand Down
14 changes: 5 additions & 9 deletions packages/codegen/src/templates/database-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,13 @@ export class Database {
{{~#each query.params}}, {{this.name~}} {{/each}} }: { blockHash: string, contractAddress: string
{{~#each query.params}}, {{this.name~}}: {{this.type~}} {{/each}} }): Promise<{{capitalize query.name tillIndex=1}} | undefined> {
return this._conn.getRepository({{capitalize query.name tillIndex=1}})
.createQueryBuilder('{{query.name}}')
.where(`${this._propColMaps['{{capitalize query.name tillIndex=1}}'].get('blockHash')} = :blockHash AND ${this._propColMaps['{{capitalize query.name tillIndex=1}}'].get('contractAddress')} = :contractAddress
{{~#each query.params}} AND ${this._propColMaps['{{capitalize query.name tillIndex=1}}'].get('{{this.name}}')} = :{{this.name~}} {{/each}}`, {
.findOne({
blockHash,
contractAddress
{{~#each query.params}},
{{this.name}}
{{~/each}}

contractAddress{{#if query.params.length}},{{/if}}
{{#each query.params}}
{{this.name}}{{#unless @last}},{{/unless}}
{{/each}}
})
.getOne();
}

{{/each}}
Expand Down
4 changes: 3 additions & 1 deletion packages/codegen/src/templates/entity-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import { {{~#each import.toImport}} {{this}} {{~#unless @last}}, {{~/unless}} {{~/each}} } from '{{import.from}}';
{{/each}}

import { bigintTransformer } from '@vulcanize/util';

@Entity()
{{#each indexOn as | index |}}
{{#if index.columns}}
@Index([
{{~#each index.columns}}'{{this}}'
{{~#unless @last}}, {{/unless}}
{{~#unless @last}}, {{/unless}}
{{~/each}}]
{{~#if index.unique}}, { unique: true }{{/if}})
{{/if}}
Expand Down
22 changes: 21 additions & 1 deletion packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ValueTransformer } from 'typeorm';
export const wait = async (time: number): Promise<void> => new Promise(resolve => setTimeout(resolve, time));

/**
* Transformer used by typeorm entity for Decimal type fields
* Transformer used by typeorm entity for Decimal type fields.
*/
export const decimalTransformer: ValueTransformer = {
to: (value?: Decimal) => {
Expand All @@ -30,3 +30,23 @@ export const decimalTransformer: ValueTransformer = {
return value;
}
};

/**
* Transformer used by typeorm entity for bigint type fields.
*/
export const bigintTransformer: ValueTransformer = {
to: (value?: bigint) => {
if (value) {
return value.toString();
}

return value;
},
from: (value?: string) => {
if (value) {
return BigInt(value);
}

return value;
}
};