Skip to content

Commit

Permalink
Fix generated watcher query with bigint arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikugogoi committed Sep 24, 2021
1 parent 4e4fe65 commit 591c6f7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
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;
}
};

0 comments on commit 591c6f7

Please sign in to comment.