diff --git a/src/models/ctype.ts b/src/models/ctype.ts index 9e0c09d2..cc69dccd 100644 --- a/src/models/ctype.ts +++ b/src/models/ctype.ts @@ -1,12 +1,6 @@ import type { DidUri, ICType } from '@kiltprotocol/sdk-js'; -import { - DataTypes, - HasManyGetAssociationsMixin, - Model, - ModelAttributes, - Sequelize, -} from 'sequelize'; +import { DataTypes, Model, ModelAttributes, Sequelize } from 'sequelize'; import { Attestation } from './attestation'; import { Tag } from './tag'; @@ -26,9 +20,7 @@ export interface CTypeData extends CTypeDataInput { tags?: Pick[]; } -export class CType extends Model { - declare getTags: HasManyGetAssociationsMixin; -} +export class CType extends Model {} export const CTypeModelDefinition: ModelAttributes = { id: { @@ -83,25 +75,26 @@ CTypeModelDefinition.search = { }, }; -// Cannot be provided as a part of the scope, include it in queries manually -export const groupForAttestationsCount = ['CType.id', 'Attestations.cTypeId']; - export function initCType(sequelize: Sequelize) { CType.init(CTypeModelDefinition, { sequelize, scopes: { stats: { - subQuery: false, - // Attestation’s attributes array must be empty - include: [{ model: Attestation, attributes: [] }], attributes: [ // Unfortunately all the CType model’s fields have to be listed explicitly ...Object.keys(CTypeModelDefinition).filter( (key) => key !== 'search', ), [ - Sequelize.fn('count', Sequelize.col('Attestations.claimHash')), + Sequelize.literal( + `coalesce( + (select count(*) + from "Attestations" + where "Attestations"."cTypeId" = "CType"."id" + group by "CType"."id"), + 0)`, + ), 'attestationsCount', ], ], diff --git a/src/pages/ctype/[id].astro b/src/pages/ctype/[id].astro index 35e077d3..81ab33d1 100644 --- a/src/pages/ctype/[id].astro +++ b/src/pages/ctype/[id].astro @@ -2,21 +2,17 @@ import Layout from '../../layouts/Layout.astro'; import Header from '../../components/Header/Header.astro'; import CTypeDetails from '../../components/CTypeDetails/CTypeDetails.astro'; -import { CType, groupForAttestationsCount as group } from '../../models/ctype'; +import { CType } from '../../models/ctype'; import { NotFoundResponse } from '../../utilities/NotFoundResponse'; const { id } = Astro.params; const success = Astro.url.searchParams.has('success'); -const cType = await CType.scope('stats').findByPk(id, { - group, -}); +const cType = await CType.scope('stats').findByPk(id, { include: 'tags' }); if (!cType) { return new NotFoundResponse(); } - -cType.dataValues.tags = await cType.getTags(); --- diff --git a/src/utilities/paginate.ts b/src/utilities/paginate.ts index 22762f67..341d5969 100644 --- a/src/utilities/paginate.ts +++ b/src/utilities/paginate.ts @@ -1,7 +1,7 @@ import type { Page } from 'astro'; import type { Attributes, WhereOptions } from 'sequelize'; -import { CType, groupForAttestationsCount as group } from '../models/ctype'; +import { CType } from '../models/ctype'; export async function paginate( url: URL, @@ -31,7 +31,6 @@ export async function paginate( limit: size, order: [['createdAt', 'DESC']], where, - group, }); const current = url.toString();