From 43c3896c99676846f060a2b3f8382b945eaa7196 Mon Sep 17 00:00:00 2001 From: Sugat Bajracharya Date: Wed, 4 Dec 2024 13:33:37 +0545 Subject: [PATCH] add page limits and end of alphabet marker to separate constants file --- shared-libs/cht-datasource/src/constants.ts | 14 ++++++++++++++ shared-libs/cht-datasource/src/contact.ts | 3 ++- shared-libs/cht-datasource/src/index.ts | 14 ++++++++++---- shared-libs/cht-datasource/src/local/contact.ts | 6 +++--- shared-libs/cht-datasource/src/local/report.ts | 4 ++-- shared-libs/cht-datasource/src/person.ts | 3 ++- shared-libs/cht-datasource/src/place.ts | 3 ++- shared-libs/cht-datasource/src/report.ts | 3 ++- 8 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 shared-libs/cht-datasource/src/constants.ts diff --git a/shared-libs/cht-datasource/src/constants.ts b/shared-libs/cht-datasource/src/constants.ts new file mode 100644 index 0000000000..bcb9991576 --- /dev/null +++ b/shared-libs/cht-datasource/src/constants.ts @@ -0,0 +1,14 @@ +/** @ignore */ +export const DEFAULT_CONTACT_PAGE_LIMIT = 10000; + +/** @ignore */ +export const DEFAULT_PEOPLE_PAGE_LIMIT = 100; + +/** @ignore */ +export const DEFAULT_PLACE_PAGE_LIMIT = 100; + +/** @ignore */ +export const DEFAULT_REPORT_PAGE_LIMIT = 10000; + +/** @ignore */ +export const END_OF_ALPHABET_MARKER = '\ufff0'; diff --git a/shared-libs/cht-datasource/src/contact.ts b/shared-libs/cht-datasource/src/contact.ts index ca2992c10a..a75c4f6074 100644 --- a/shared-libs/cht-datasource/src/contact.ts +++ b/shared-libs/cht-datasource/src/contact.ts @@ -18,6 +18,7 @@ import { RemoteDataContext } from './remote/libs/data-context'; import * as Local from './local'; import * as Remote from './remote'; import * as ContactTypes from './contact-types'; +import { DEFAULT_CONTACT_PAGE_LIMIT } from './constants'; /** */ export namespace v1 { @@ -101,7 +102,7 @@ export namespace v1 { const curriedFn = async ( qualifier: ContactTypeQualifier | FreetextQualifier, cursor: Nullable = null, - limit = 10000 + limit = DEFAULT_CONTACT_PAGE_LIMIT ): Promise> => { assertCursor(cursor); assertLimit(limit); diff --git a/shared-libs/cht-datasource/src/index.ts b/shared-libs/cht-datasource/src/index.ts index f5dd662ed1..18aed3b93a 100644 --- a/shared-libs/cht-datasource/src/index.ts +++ b/shared-libs/cht-datasource/src/index.ts @@ -34,6 +34,12 @@ import * as Person from './person'; import * as Place from './place'; import * as Qualifier from './qualifier'; import * as Report from './report'; +import { + DEFAULT_CONTACT_PAGE_LIMIT, + DEFAULT_PEOPLE_PAGE_LIMIT, + DEFAULT_PLACE_PAGE_LIMIT, + DEFAULT_REPORT_PAGE_LIMIT +} from './constants'; export { Nullable, NonEmptyArray } from './libs/core'; export { DataContext } from './libs/data-context'; @@ -92,7 +98,7 @@ export const getDatasource = (ctx: DataContext) => { freetext: Nullable = null, type: Nullable = null, cursor: Nullable = null, - limit = 10000 + limit = DEFAULT_CONTACT_PAGE_LIMIT ) => ctx.bind(Contact.v1.getIdsPage)( Contact.v1.createQualifier(freetext, type), cursor, limit ), @@ -143,7 +149,7 @@ export const getDatasource = (ctx: DataContext) => { getPageByType: ( placeType: string, cursor: Nullable = null, - limit = 100 + limit = DEFAULT_PLACE_PAGE_LIMIT ) => ctx.bind(Place.v1.getPage)( Qualifier.byContactType(placeType), cursor, limit ), @@ -188,7 +194,7 @@ export const getDatasource = (ctx: DataContext) => { getPageByType: ( personType: string, cursor: Nullable = null, - limit = 100 + limit = DEFAULT_PEOPLE_PAGE_LIMIT ) => ctx.bind(Person.v1.getPage)( Qualifier.byContactType(personType), cursor, limit ), @@ -225,7 +231,7 @@ export const getDatasource = (ctx: DataContext) => { getIdsPage: ( qualifier: string, cursor: Nullable = null, - limit = 10000 + limit = DEFAULT_REPORT_PAGE_LIMIT // eslint-disable-next-line compat/compat ) => ctx.bind(Report.v1.getIdsPage)( Qualifier.byFreetext(qualifier), cursor, limit diff --git a/shared-libs/cht-datasource/src/local/contact.ts b/shared-libs/cht-datasource/src/local/contact.ts index 6734b4c319..04c4617b6d 100644 --- a/shared-libs/cht-datasource/src/local/contact.ts +++ b/shared-libs/cht-datasource/src/local/contact.ts @@ -9,10 +9,10 @@ import contactTypeUtils from '@medic/contact-types-utils'; import { getLineageDocsById, getPrimaryContactIds, hydrateLineage, hydratePrimaryContact } from './libs/lineage'; import { InvalidArgumentError } from '../libs/error'; import { validateCursor } from './libs/core'; +import { END_OF_ALPHABET_MARKER } from '../constants'; /** @internal */ export namespace v1 { - const END_OF_ALPHABET = '\ufff0'; const getContactTypes = (settings: SettingsService): string[] => { const contactTypesObjects = contactTypeUtils.getContactTypes(settings.getAll()); return contactTypesObjects.map((item) => item.id) as string[]; @@ -125,7 +125,7 @@ export namespace v1 { return (limit, skip) => getContactsByTypeFreetextRange( [qualifier.contactType, qualifier.freetext], - [qualifier.contactType, qualifier.freetext + END_OF_ALPHABET], + [qualifier.contactType, qualifier.freetext + END_OF_ALPHABET_MARKER], limit, skip ); @@ -146,7 +146,7 @@ export namespace v1 { } return (limit, skip) => getContactsByFreetextRange( [qualifier.freetext], - [qualifier.freetext + END_OF_ALPHABET], + [qualifier.freetext + END_OF_ALPHABET_MARKER], limit, skip ); diff --git a/shared-libs/cht-datasource/src/local/report.ts b/shared-libs/cht-datasource/src/local/report.ts index 49feaa16e9..cbb74977b8 100644 --- a/shared-libs/cht-datasource/src/local/report.ts +++ b/shared-libs/cht-datasource/src/local/report.ts @@ -6,10 +6,10 @@ import * as Report from '../report'; import { Doc } from '../libs/doc'; import logger from '@medic/logger'; import { validateCursor } from './libs/core'; +import { END_OF_ALPHABET_MARKER } from '../constants'; /** @internal */ export namespace v1 { - const END_OF_ALPHABET = '\ufff0'; const isReport = () => (doc: Nullable, uuid?: string): doc is Report.v1.Report => { if (!doc) { if (uuid) { @@ -50,7 +50,7 @@ export namespace v1 { } return (limit, skip) => getReportsByFreetextRange( [qualifier.freetext], - [qualifier.freetext + END_OF_ALPHABET], + [qualifier.freetext + END_OF_ALPHABET_MARKER], limit, skip ); diff --git a/shared-libs/cht-datasource/src/person.ts b/shared-libs/cht-datasource/src/person.ts index 174baf9ef8..ee47470704 100644 --- a/shared-libs/cht-datasource/src/person.ts +++ b/shared-libs/cht-datasource/src/person.ts @@ -8,6 +8,7 @@ import { LocalDataContext } from './local/libs/data-context'; import { RemoteDataContext } from './remote/libs/data-context'; import { InvalidArgumentError } from './libs/error'; import { assertCursor, assertLimit, assertTypeQualifier, getPagedGenerator, Nullable, Page } from './libs/core'; +import { DEFAULT_PEOPLE_PAGE_LIMIT } from "./constants"; /** */ export namespace v1 { @@ -88,7 +89,7 @@ export namespace v1 { const curriedFn = async ( personType: ContactTypeQualifier, cursor: Nullable = null, - limit = 100 + limit = DEFAULT_PEOPLE_PAGE_LIMIT ): Promise> => { assertTypeQualifier(personType); assertCursor(cursor); diff --git a/shared-libs/cht-datasource/src/place.ts b/shared-libs/cht-datasource/src/place.ts index ff599e17c4..129c326a01 100644 --- a/shared-libs/cht-datasource/src/place.ts +++ b/shared-libs/cht-datasource/src/place.ts @@ -8,6 +8,7 @@ import * as Local from './local'; import * as Remote from './remote'; import { assertCursor, assertLimit, assertTypeQualifier, getPagedGenerator, Nullable, Page } from './libs/core'; import { InvalidArgumentError } from './libs/error'; +import { DEFAULT_PLACE_PAGE_LIMIT } from "./constants"; /** */ export namespace v1 { @@ -88,7 +89,7 @@ export namespace v1 { const curriedFn = async ( placeType: ContactTypeQualifier, cursor: Nullable = null, - limit = 100 + limit = DEFAULT_PLACE_PAGE_LIMIT ): Promise> => { assertTypeQualifier(placeType); assertCursor(cursor); diff --git a/shared-libs/cht-datasource/src/report.ts b/shared-libs/cht-datasource/src/report.ts index 1e773d84ea..644e7a36b3 100644 --- a/shared-libs/cht-datasource/src/report.ts +++ b/shared-libs/cht-datasource/src/report.ts @@ -13,6 +13,7 @@ import { InvalidArgumentError } from './libs/error'; import * as Local from './local'; import { FreetextQualifier, isUuidQualifier, UuidQualifier } from './qualifier'; import * as Remote from './remote'; +import { DEFAULT_REPORT_PAGE_LIMIT } from "./constants"; /** */ export namespace v1 { @@ -81,7 +82,7 @@ export namespace v1 { const curriedFn = async ( qualifier: FreetextQualifier, cursor: Nullable = null, - limit = 10000 + limit = DEFAULT_REPORT_PAGE_LIMIT ): Promise> => { assertFreetextQualifier(qualifier); assertCursor(cursor);