Skip to content

Commit

Permalink
add page limits and end of alphabet marker to separate constants file
Browse files Browse the repository at this point in the history
  • Loading branch information
sugat009 committed Dec 4, 2024
1 parent ef8b93f commit 43c3896
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
14 changes: 14 additions & 0 deletions shared-libs/cht-datasource/src/constants.ts
Original file line number Diff line number Diff line change
@@ -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';
3 changes: 2 additions & 1 deletion shared-libs/cht-datasource/src/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -101,7 +102,7 @@ export namespace v1 {
const curriedFn = async (
qualifier: ContactTypeQualifier | FreetextQualifier,
cursor: Nullable<string> = null,
limit = 10000
limit = DEFAULT_CONTACT_PAGE_LIMIT
): Promise<Page<string>> => {
assertCursor(cursor);
assertLimit(limit);
Expand Down
14 changes: 10 additions & 4 deletions shared-libs/cht-datasource/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -92,7 +98,7 @@ export const getDatasource = (ctx: DataContext) => {
freetext: Nullable<string> = null,
type: Nullable<string> = null,
cursor: Nullable<string> = null,
limit = 10000
limit = DEFAULT_CONTACT_PAGE_LIMIT
) => ctx.bind(Contact.v1.getIdsPage)(
Contact.v1.createQualifier(freetext, type), cursor, limit
),
Expand Down Expand Up @@ -143,7 +149,7 @@ export const getDatasource = (ctx: DataContext) => {
getPageByType: (
placeType: string,
cursor: Nullable<string> = null,
limit = 100
limit = DEFAULT_PLACE_PAGE_LIMIT
) => ctx.bind(Place.v1.getPage)(
Qualifier.byContactType(placeType), cursor, limit
),
Expand Down Expand Up @@ -188,7 +194,7 @@ export const getDatasource = (ctx: DataContext) => {
getPageByType: (
personType: string,
cursor: Nullable<string> = null,
limit = 100
limit = DEFAULT_PEOPLE_PAGE_LIMIT
) => ctx.bind(Person.v1.getPage)(
Qualifier.byContactType(personType), cursor, limit
),
Expand Down Expand Up @@ -225,7 +231,7 @@ export const getDatasource = (ctx: DataContext) => {
getIdsPage: (
qualifier: string,
cursor: Nullable<string> = null,
limit = 10000
limit = DEFAULT_REPORT_PAGE_LIMIT
// eslint-disable-next-line compat/compat
) => ctx.bind(Report.v1.getIdsPage)(
Qualifier.byFreetext(qualifier), cursor, limit
Expand Down
6 changes: 3 additions & 3 deletions shared-libs/cht-datasource/src/local/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand Down
4 changes: 2 additions & 2 deletions shared-libs/cht-datasource/src/local/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Doc>, uuid?: string): doc is Report.v1.Report => {
if (!doc) {
if (uuid) {
Expand Down Expand Up @@ -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
);
Expand Down
3 changes: 2 additions & 1 deletion shared-libs/cht-datasource/src/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -88,7 +89,7 @@ export namespace v1 {
const curriedFn = async (
personType: ContactTypeQualifier,
cursor: Nullable<string> = null,
limit = 100
limit = DEFAULT_PEOPLE_PAGE_LIMIT
): Promise<Page<Person>> => {
assertTypeQualifier(personType);
assertCursor(cursor);
Expand Down
3 changes: 2 additions & 1 deletion shared-libs/cht-datasource/src/place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -88,7 +89,7 @@ export namespace v1 {
const curriedFn = async (
placeType: ContactTypeQualifier,
cursor: Nullable<string> = null,
limit = 100
limit = DEFAULT_PLACE_PAGE_LIMIT
): Promise<Page<Place>> => {
assertTypeQualifier(placeType);
assertCursor(cursor);
Expand Down
3 changes: 2 additions & 1 deletion shared-libs/cht-datasource/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -81,7 +82,7 @@ export namespace v1 {
const curriedFn = async (
qualifier: FreetextQualifier,
cursor: Nullable<string> = null,
limit = 10000
limit = DEFAULT_REPORT_PAGE_LIMIT
): Promise<Page<string>> => {
assertFreetextQualifier(qualifier);
assertCursor(cursor);
Expand Down

0 comments on commit 43c3896

Please sign in to comment.