diff --git a/migrations/20230809073622_index_cw20_models.ts b/migrations/20230809073622_index_cw20_models.ts new file mode 100644 index 000000000..76f46a0ed --- /dev/null +++ b/migrations/20230809073622_index_cw20_models.ts @@ -0,0 +1,27 @@ +import { Knex } from 'knex'; + +export async function up(knex: Knex): Promise { + await knex.schema.alterTable('cw20_activity', (table) => { + table.index('from'); + table.index('to'); + table.index('action'); + }); + await knex.schema.alterTable('cw721_activity', (table) => { + table.index('from'); + table.index('to'); + table.index('action'); + }); +} + +export async function down(knex: Knex): Promise { + await knex.schema.alterTable('cw20_activity', (table) => { + table.dropIndex('from'); + table.dropIndex('to'); + table.dropIndex('action'); + }); + await knex.schema.alterTable('cw721_activity', (table) => { + table.dropIndex('from'); + table.dropIndex('to'); + table.dropIndex('action'); + }); +} diff --git a/src/models/cw20_activity.ts b/src/models/cw20_activity.ts index ac0646b53..9eb327b2c 100644 --- a/src/models/cw20_activity.ts +++ b/src/models/cw20_activity.ts @@ -3,6 +3,8 @@ import BaseModel from './base'; // eslint-disable-next-line import/no-cycle import { Cw20Contract } from './cw20_contract'; import { SmartContract } from './smart_contract'; +import { SmartContractEvent } from './smart_contract_event'; +import { Event } from './event'; export class Cw20Event extends BaseModel { [relation: string]: any; @@ -68,6 +70,26 @@ export class Cw20Event extends BaseModel { }, }, }, + smart_contract_event: { + relation: Model.BelongsToOneRelation, + modelClass: SmartContractEvent, + join: { + from: 'cw20_activity.smart_contract_event_id', + to: 'smart_contract_event.id', + }, + }, + event: { + relation: Model.HasOneThroughRelation, + modelClass: Event, + join: { + from: 'cw20_activity.smart_contract_event_id', + to: 'event.id', + through: { + from: 'smart_contract_event.id', + to: 'smart_contract_event.event_id', + }, + }, + }, }; } } diff --git a/src/models/cw721_tx.ts b/src/models/cw721_tx.ts index 2db679513..096a20864 100644 --- a/src/models/cw721_tx.ts +++ b/src/models/cw721_tx.ts @@ -4,6 +4,7 @@ import BaseModel from './base'; import CW721Contract from './cw721_contract'; import CW721Token from './cw721_token'; import { SmartContractEvent } from './smart_contract_event'; +import { Event } from './event'; export default class CW721Activity extends BaseModel { static softDelete = false; @@ -79,6 +80,18 @@ export default class CW721Activity extends BaseModel { to: 'smart_contract_event.id', }, }, + event: { + relation: Model.HasOneThroughRelation, + modelClass: Event, + join: { + from: 'cw721_activity.smart_contract_event_id', + to: 'event.id', + through: { + from: 'smart_contract_event.id', + to: 'smart_contract_event.event_id', + }, + }, + }, }; } }