Commit 663dfd6e authored by LAVENIER's avatar LAVENIER
Browse files

Release v0.13.13

[fix] Base entity service: add a countALl function
parent 1a670a51
{
"name": "@sumaris-net/ngx-components",
"description": "SUMARiS Angular components",
"version": "0.13.12",
"version": "0.13.13",
"author": "contact@e-is.pro",
"license": "AGPL-3.0",
"readmeFilename": "README.md",
......
......@@ -24,6 +24,7 @@ export interface BaseEntityGraphqlQueries {
load?: any;
loadAll: any;
loadAllWithTotal?: any;
countAll?: any;
}
export interface BaseEntityGraphqlMutations {
save?: any;
......@@ -265,6 +266,29 @@ export abstract class BaseEntityService<
return res;
}
async countAll(filter?: Partial<F>, opts?: LO) {
// If no countAll query
if (!this.queries.countAll) {
// Fail if no loadAll (cannot count)
if (!this.queries.loadAllWithTotal) throw new Error('Not implemented');
// Use loadAll query, with size = 0;
return (await this.loadAll(0, 1, null, null, filter,
<LO & { withTotal: boolean; }>{withTotal: true}))?.total || 0;
}
filter = this.asFilter(filter);
const res = await this.graphql.query<{ total: number;}>({
query: this.queries.countAll,
variables: {
filter: filter && filter.asPodObject()
},
error: {code: ErrorCodes.LOAD_DATA_ERROR, message: 'ERROR.COUNT_DATA_ERROR'},
fetchPolicy: opts && opts.fetchPolicy || 'network-only'
});
return res.total;
}
async saveAll(entities: T[], opts?: EntitySaveOptions): Promise<T[]> {
if (isEmptyArray(entities)) return entities; // Nothing to save: skip
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment