|
| 1 | +# Utils |
| 2 | + |
| 3 | +This package contains some common utility functions that can be used by both users and implementors of the [Facade](./facade.md#facade). |
| 4 | + |
| 5 | +- [convertPropertyFilter](#convertPropertyFilter) |
| 6 | +- [createCursorFromEntity](#createCursorFromEntity) |
| 7 | +- [createPaginationFilter](#createPaginationFilter) |
| 8 | + |
| 9 | +### convertPropertyFilter |
| 10 | +Converts the filter value for a certain property name. For example, if you have a date property on your entity called `createdAt` storing the date at which an entity was created, then if you filter the property using a string (usually via [js-entity-repos/express](https://github.com/js-entity-repos/express)), you can use this util to ensure that the string is converted to a date before using the filter in the database. This is usually used by users of the js-entity-repos inside a `constructFilter` function in the factory config of [concrete implementations of the Facade](./facade.md#facade), the [Knex implementation allows this configuration function](https://github.com/js-entity-repos/knex#construct-the-facade) amongst others. |
| 11 | + |
| 12 | +```ts |
| 13 | +import convertPropertyFilter from '@js-entity-repos/core/dist/utils/convertPropertyFilter'; |
| 14 | + |
| 15 | +convertPropertyFilter({ |
| 16 | + converter: (value: any) => new Date(value), |
| 17 | + propertyName: 'createdAt', |
| 18 | + filter: { createdAt: '2018-01-01' }, |
| 19 | +}); |
| 20 | +// Returns the result of { createdAt: new Date('2018-01-01') } |
| 21 | +``` |
| 22 | + |
| 23 | +### createCursorFromEntity |
| 24 | +Exactly what it says on the tin, this creates a cursor from an entity. A cursor is constructed by creating a [filter](./options#filter) that will filter out entities not expected in the next set of paginated results. This filter is then stringified to JSON and base 64 encoded. This function is usually used by [concrete implementations of the Facade](./facade.md#facade) like [Knex's getEntities implementation](https://github.com/js-entity-repos/knex/blob/master/src/functions/getEntities.ts).. |
| 25 | + |
| 26 | +```ts |
| 27 | +import createCursorFromEntity from '@js-entity-repos/core/dist/utils/createCursorFromEntity'; |
| 28 | +import { asc } from '@js-entity-repos/core/dist/types/SortOrder'; |
| 29 | + |
| 30 | +createCursorFromEntity(undefined, { id: asc }); |
| 31 | +// Returns undefined |
| 32 | + |
| 33 | +createCursorFromEntity({ |
| 34 | + booleanProp: true, |
| 35 | + id: 'test_id_1', |
| 36 | + numberProp: 1, |
| 37 | + stringProp: 'test_string_prop', |
| 38 | +}, { |
| 39 | + id: asc, |
| 40 | +}); |
| 41 | +// Returns eyJpZCI6InRlc3RfaWQifQ== |
| 42 | +``` |
| 43 | + |
| 44 | +### createPaginationFilter |
| 45 | +Takes a [pagination option](./options#pagination) and a [sort option](./options#sort)) to produces a [filter](./options#filter) that can filter out entities not expected in the next set of paginated results. This function is usually used by [concrete implementations of the Facade](./facade.md#facade) like [Knex's getEntities implementation](https://github.com/js-entity-repos/knex/blob/master/src/functions/getEntities.ts). |
| 46 | + |
| 47 | +```ts |
| 48 | +import createPaginationFilter from '@js-entity-repos/core/dist/utils/createPaginationFilter'; |
| 49 | +import { asc, desc } from '@js-entity-repos/core/dist/types/SortOrder'; |
| 50 | +import { backward, forward } from '@js-entity-repos/core/dist/types/PaginationDirection'; |
| 51 | + |
| 52 | +createPaginationFilter( |
| 53 | + { cursor: undefined, direction: forward, limit: 1 }, |
| 54 | + { id: asc, numberProp: desc } |
| 55 | +); |
| 56 | +// Returns {} |
| 57 | + |
| 58 | +createPaginationFilter( |
| 59 | + { cursor: nextCursor, direction: forward, limit: 1 }, |
| 60 | + { id: asc, numberProp: desc } |
| 61 | +); |
| 62 | +// Returns the result of { id: { $gt: lastEntity.id }, numberProp: { $lte: lastEntity.numberProp } } |
| 63 | + |
| 64 | +createPaginationFilter( |
| 65 | + { cursor: prevCursor, direction: backward, limit: 1 }, |
| 66 | + { id: asc, numberProp: desc } |
| 67 | +); |
| 68 | +// Returns the result of { id: { $lt: firstEntity.id }, numberProp: { $gte: firstEntity.numberProp } } |
| 69 | +``` |
0 commit comments