Created
November 16, 2019 16:56
-
-
Save dev-tim/1d06645d75a93b6b6e4fadc0c32022a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findAll(entityType: string, query: QueryObject): Promise<Array<any>> { | |
const table: Dexie.Table<any, any> = db.table(entityType); | |
// if there is filter query try to apply filters | |
const maybeFiltered = applyFilteringObject(table, query.filter); | |
const maybePaginated = applyPaginationObject(maybeFiltered, query.pagination) | |
const promise = maybePaginated.toArray(); | |
return promise; | |
} | |
function applyPaginationObject(collection: Dexie.Collection<any, any>, pagination: PaginationObject): Dexie.Collection<any, any>{ | |
if (pagination){ | |
return collection.limit(pagination.limit).offset(pagination.limit) | |
} | |
return collection; | |
} | |
function applyFilteringObject(table: Dexie.Table<any, any>, filterObject: FilterObject): Dexie.Collection<any, any> { | |
if (filterObject){ | |
if (_.isObject(filterObject.equals)) { | |
return table.where(filterObject.equals) | |
} else if (_.isObject(filterObject.isAnyOf)) { | |
const anyOf = filterObject.isAnyOf; | |
return table.where(anyOf.key).anyOf(anyOf.value); | |
} | |
} | |
return table.toCollection(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment