Skip to main content

mongoose-relay-paginate

Interfaces​

Type Aliases​

PagingCursor​

Ƭ PagingCursor<DocType>: { [P in keyof DocType]?: DocType[P] | null }

This is the default Cursor type for this project.

A cursor helps a server to find an item in a database.

If you're not exactly sure what that means here's an analogy. Its a lot like when you go to the library and have some information for a specific book. This information you have about the book can help you locate it.

A cursor (some information) can be turned into a database object (the book) by finding the first instance of it in the database (the library).

Below is a hypothetical example:

Cursor        -> Some query from a cursor -> DB Object
{name: "bob"} -> Some query from a cursor -> {name: "bob", job: "something", location: "some place 123rd street"}

An example of some query from a cursor, in MongoDB is:

const {name, job, location} = await Employee.findOne({name: "bob"});

A database object can be turned into a cursor with a transform of some sort in our case it will be provided by the user.

Below is a hypothetical example:

DB Object -> some transform to a cursor -> Cursor
{
name: "bob",
job: "something",
location: "some place 123rd street"
} -> some tranform to a cursor -> {name: "bob"}

Type parameters​

NameType
DocTypeunknown

Defined in​

src/index.ts:200


PagingInfo​

Ƭ PagingInfo<DocType>: Object

Info about how to page forward and backward

first and last are alot like limit in a typical skip and limit scheme. This is because first and last signify how many elements to return. You should never supply both first and last at the same time. You should either supply one or the other, but not both. Supplying both will lead to unpredicted behaviour.

after and before are more like the typical skip in skip and limit. This is because after and before signify where the collection starts and stops searching. You may supply both the after and before, but your before cursor must be later in your collection than your after cursor otherwise you will get 0 results.

Type parameters​

NameType
DocTypeunknown

Type declaration​

NameTypeDescription
after?PagingCursor<DocType> | nullfetch after the given record's cursor
before?PagingCursor<DocType> | nullfetch before the given record's cursor
first?numberfetch the first given number of records
last?numberfetch the last given number of records

Defined in​

src/index.ts:221

Functions​

aggregateRelayPaginate​

▸ aggregateRelayPaginate<T>(model, aggregate, «destructured»?): Object

This is an implementation of the relay pagination algorithm for mongoose. This algorithm and pagination format allows one to use cursor based pagination.

For more on cursors see PagingCursor

For more info on using cursor based pagination algorithms like relay see:

the documentation for relay's connection spec (look at this one for docs in more laymans terms),

the actual relay spec (look at this one for very exact and concise, but possibly confusing language),

Type parameters​

Name
T

Parameters​

NameType
modelModel<T, , , , IfAny<T, any, Document<unknown, , T> & Require_id<T>>, any>
aggregatePipelineStage[]
«destructured»MongooseRelayPaginateInfoOnModel<T>

Returns​

Object

NameType
thenAggregate<RelayResult<T[]>>["then"]
toNodesAggregate<AggregateResult>() => Aggregate<AggregateResult>

Defined in​

src/index.ts:704


alterNodeOnResult​

▸ alterNodeOnResult<Result, U>(doc, transform): TransformedRelayResult<U[], Result[]>

Alters a relay connection to have a different nodeType for each node in the nodes and edges property.

Type parameters​

Name
Result
U

Parameters​

NameTypeDescription
docRelayResult<U[]>the relay style connection document to alter
transform(doc: U, index?: number, arr?: U[], thisArg?: U[]) => Resulta mapping transform to turn one thing into another

Returns​

TransformedRelayResult<U[], Result[]>

An altered relay style connection with a new node type

Defined in​

src/index.ts:796


relayPaginate​

▸ relayPaginate<T>(query, paginationInfo?): QueryWithHelpers<Promise<RelayResult<MongooseRelayDocument<DefaultRelayQuery<T>>[]>>, QueryDocType<DefaultRelayQuery<T>>, QueryHelpers<DefaultRelayQuery<T>>, QueryRawDocType<DefaultRelayQuery<T>>>

This is an implementation of the relay pagination algorithm for mongoose. This algorithm and pagination format allows one to use cursor based pagination.

For more on cursors see PagingCursor

For more info on using cursor based pagination algorithms like relay see:

the documentation for relay's connection spec (look at this one for docs in more laymans terms),

the actual relay spec (look at this one for very exact and concise, but possibly confusing language),

Type parameters​

Name
T

Parameters​

NameTypeDescription
queryQuery<T[], T, unknown, T, "find">the query to add pagination to
paginationInfoMongooseRelayPaginateInfo<Query<T[], T, unknown, T, "find">>the information to help with the paging

Returns​

QueryWithHelpers<Promise<RelayResult<MongooseRelayDocument<DefaultRelayQuery<T>>[]>>, QueryDocType<DefaultRelayQuery<T>>, QueryHelpers<DefaultRelayQuery<T>>, QueryRawDocType<DefaultRelayQuery<T>>>

Defined in​

src/index.ts:488


relayPaginatePlugin​

▸ relayPaginatePlugin(«destructured»?): (schema: Schema<any, Model<any, any, any, any, any, any>, , , , , DefaultSchemaOptions, , Document<unknown, , FlatRecord<>> & FlatRecord<> & Required<{ _id: unknown }>>) => void

Creates the relay paginate plugin, so that you can use relayPaginate

Parameters​

NameType
«destructured»PluginOptions

Returns​

fn

▸ (schema): void

Parameters​
NameType
schemaSchema<any, Model<any, any, any, any, any, any>, , , , , DefaultSchemaOptions, , Document<unknown, , FlatRecord<>> & FlatRecord<> & Required<{ _id: unknown }>>
Returns​

void

Defined in​

src/index.ts:936


relayResultFromNodes​

▸ relayResultFromNodes<Node>(cursorKeys, pagingInfo, nodes): RelayResult<Node[]>

Creates a typed relay connection. This is what relay uses for it's cursor-based pagination algorithm. It can be constructed using three things: a set of documents called nodes, a transform that turns a document/node into a cursor, and finally some metadata about paging which is called the pagingInfo.

Type parameters​

Name
Node

Parameters​

NameTypeDescription
cursorKeyskeyof Node[]The way to turn a node into a cursor. Given certain props it will create a partial document node known as the cursor of the original document node with only those keys that are listed. For more info on cursors see PagingCursor
pagingInfoPick<{ endCursor?: null | PagingCursor<Node> ; hasNextPage: boolean ; hasPreviousPage: boolean ; startCursor?: null | PagingCursor<Node> }, "hasNextPage" | "hasPreviousPage">the metadata about paging information (such as cursors, number of documents returned, etc.) used by the client to gain some insight into the query and to more easily re-query and fetch the next and previous page.
nodesNode[]The nodes of the relay connection

Returns​

RelayResult<Node[]>

A RelayResult

Defined in​

src/index.ts:764


toCursorFromKeys​

▸ toCursorFromKeys<Node>(keys, doc): Partial<Node>

Type parameters​

Name
Node

Parameters​

NameType
keyskeyof Node[]
docNode

Returns​

Partial<Node>

Defined in​

src/index.ts:741