Skip to content

Commit

Permalink
Refactor/cw20 index and relation ( staging ) (#320)
Browse files Browse the repository at this point in the history
* refactor: cw20 index and add relation

* refactor: code
  • Loading branch information
phamphong9981 authored Aug 15, 2023
1 parent ed3740d commit 5fe2bcd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
27 changes: 27 additions & 0 deletions migrations/20230809073622_index_cw20_models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
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<void> {
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');
});
}
22 changes: 22 additions & 0 deletions src/models/cw20_activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
},
},
},
};
}
}
13 changes: 13 additions & 0 deletions src/models/cw721_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
},
},
},
};
}
}

0 comments on commit 5fe2bcd

Please sign in to comment.