feat: pagination with total count (#3384)

* feat: add totalCount

* feat: add command for production to fix existing tables
This commit is contained in:
Jérémy M
2024-01-12 10:41:38 +01:00
committed by GitHub
parent 9ecc3bdbf2
commit d0ed9ee2e0
4 changed files with 62 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import { DataSeedDemoWorkspaceCommand } from 'src/database/commands/data-seed-de
import { WorkspaceDataSourceModule } from 'src/workspace/workspace-datasource/workspace-datasource.module';
import { WorkspaceSyncMetadataModule } from 'src/workspace/workspace-sync-metadata/workspace-sync-metadata.module';
import { ObjectMetadataModule } from 'src/metadata/object-metadata/object-metadata.module';
import { WorkspaceAddTotalCountCommand } from 'src/database/commands/workspace-add-total-count.command';
@Module({
imports: [
@ -24,6 +25,7 @@ import { ObjectMetadataModule } from 'src/metadata/object-metadata/object-metada
providers: [
DataSeedWorkspaceCommand,
DataSeedDemoWorkspaceCommand,
WorkspaceAddTotalCountCommand,
ConfirmationQuestion,
],
})

View File

@ -0,0 +1,49 @@
import { Command, CommandRunner } from 'nest-commander';
import chalk from 'chalk';
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
@Command({
name: 'workspace:add-total-count',
description: 'Add pg_graphql total count directive to all workspace tables',
})
export class WorkspaceAddTotalCountCommand extends CommandRunner {
constructor(private readonly typeORMService: TypeORMService) {
super();
}
async run(): Promise<void> {
const mainDataSource = this.typeORMService.getMainDataSource();
try {
await mainDataSource.query(`
DO $$
DECLARE
schema_cursor CURSOR FOR SELECT schema_name FROM information_schema.schemata WHERE schema_name LIKE 'workspace_%';
schema_name text;
table_rec record;
BEGIN
OPEN schema_cursor;
LOOP
FETCH schema_cursor INTO schema_name;
EXIT WHEN NOT FOUND;
FOR table_rec IN SELECT t.table_name FROM information_schema.tables t WHERE t.table_schema = schema_name
LOOP
EXECUTE 'COMMENT ON TABLE ' || quote_ident(schema_name) || '.' || quote_ident(table_rec.table_name) || ' IS e''@graphql({"totalCount": {"enabled": true}})'';';
END LOOP;
END LOOP;
CLOSE schema_cursor;
END $$;
`);
console.log(
chalk.green('Total count directive added to all workspace tables'),
);
} catch (error) {
console.log(
chalk.red('Error adding total count directive to all workspace tables'),
);
}
}
}

View File

@ -140,6 +140,11 @@ export class WorkspaceMigrationRunnerService {
}),
true,
);
// Enable totalCount for the table
await queryRunner.query(`
COMMENT ON TABLE "${schemaName}"."${tableName}" IS '@graphql({"totalCount": {"enabled": true}})';
`);
}
/**

View File

@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import { GraphQLFieldConfigMap, GraphQLObjectType } from 'graphql';
import { GraphQLFieldConfigMap, GraphQLInt, GraphQLObjectType } from 'graphql';
import { WorkspaceBuildSchemaOptions } from 'src/workspace/workspace-schema-builder/interfaces/workspace-build-schema-optionts.interface';
import { ObjectMetadataInterface } from 'src/metadata/field-metadata/interfaces/object-metadata.interface';
@ -71,6 +71,11 @@ export class ConnectionTypeDefinitionFactory {
),
};
fields.totalCount = {
type: GraphQLInt,
description: 'Total number of records in the connection',
};
return fields;
}
}