From 5fe2bcdfc00b05359c5e14bd5cd32c28eb99c6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=E1=BA=A1m=20Th=C3=A0nh=20Phong?= <49814372+phamphong9981@users.noreply.github.com> Date: Tue, 15 Aug 2023 15:04:57 +0700 Subject: [PATCH] Refactor/cw20 index and relation ( staging ) (#320) * refactor: cw20 index and add relation * refactor: code --- .../20230809073622_index_cw20_models.ts | 27 +++++++++++++++++++ src/models/cw20_activity.ts | 22 +++++++++++++++ src/models/cw721_tx.ts | 13 +++++++++ 3 files changed, 62 insertions(+) create mode 100644 migrations/20230809073622_index_cw20_models.ts 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', + }, + }, + }, }; } }