The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { Injectable } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { isDefined } from 'twenty-shared/utils';
import { DataSource } from 'typeorm';
import { WorkspaceManyOrAllFlatEntityMapsCacheService } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.service';
import { type ObjectRecordCountDTO } from 'src/engine/metadata-modules/object-metadata/dtos/object-record-count.dto';
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';
@Injectable()
export class ObjectRecordCountService {
constructor(
@InjectDataSource()
private readonly coreDataSource: DataSource,
private readonly workspaceManyOrAllFlatEntityMapsCacheService: WorkspaceManyOrAllFlatEntityMapsCacheService,
) {}
// Never-analyzed tables report reltuples as -1
async getApproximateRecordCountByTableName(
workspaceId: string,
): Promise<Map<string, number>> {
const schemaName = getWorkspaceSchemaName(workspaceId);
const rows: { relname: string; approximate_count: number }[] =
await this.coreDataSource.query(
`SELECT relname, reltuples::bigint AS approximate_count
FROM pg_class c
JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = $1
AND c.relkind = 'r'`,
[schemaName],
);
const countByTableName = new Map<string, number>();
for (const row of rows) {
countByTableName.set(
row.relname,
Math.max(0, Number(row.approximate_count)),
);
}
return countByTableName;
}
async getRecordCounts(workspaceId: string): Promise<ObjectRecordCountDTO[]> {
const { flatObjectMetadataMaps } =
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
{
workspaceId,
flatMapsKeys: ['flatObjectMetadataMaps'],
},
);
const flatObjectMetadatas = Object.values(
flatObjectMetadataMaps.byUniversalIdentifier,
).filter(isDefined);
const countByTableName =
await this.getApproximateRecordCountByTableName(workspaceId);
return flatObjectMetadatas.map((flatObjectMetadata) => ({
objectNamePlural: flatObjectMetadata.namePlural,
totalCount:
countByTableName.get(computeObjectTargetTable(flatObjectMetadata)) ?? 0,
}));
}
}