Skip to content

Commit eadb268

Browse files
committed
Rename beforeCursor/afterCursor to before/after
1 parent c07263e commit eadb268

File tree

5 files changed

+146
-158
lines changed

5 files changed

+146
-158
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Are you running a service, using an SQL database, and want to support cursor sty
44

55
## How it works
66

7-
1. When a request comes in you call the library with a `query` object containing how many items to fetch (`first`/`last`), where to fetch from (`beforeCursor`/`afterCursor`) and the sort config (`sortFields`), along with a `setup` object.
7+
1. When a request comes in you call the library with a `query` object containing how many items to fetch (`first`/`last`), where to fetch from (`before`/`after`) and the sort config (`sortFields`), along with a `setup` object.
88
2. The `runQuery` function you provided in `setup` is invoked, and provided with a `limit`, `whereFragmentBuilder` and `orderByFragmentBuilder`. You integrate these into your query, run it, and then return the results.
99
3. The library takes the results, and for each one it generates a unique `cursor`, which it then returns alongside each row. It also returns `hasNextPage`/`hasPreviousPage`/`startCursor`/`endCursor` properties.
1010

@@ -16,9 +16,9 @@ Cursor pagination was made popular by GraphQL, and this library conforms to the
1616
- First you specify the sort config. This contains a list of field names with their orders. It must contain a unique key.
1717
- Then you request how many items you would like to fetch with `first`.
1818
- Each item you get back also contains an opaque string cursor. The cursor is an encrypted string that contains the names of the fields in the sort config, alongside their values.
19-
- To fetch the next set of items you make a new request with `first` and `afterCursor` being the cursor of the last item you received.
19+
- To fetch the next set of items you make a new request with `first` and `after` being the cursor of the last item you received.
2020

21-
If you want to fetch items in reverse order you can use `last` and `beforeCursor` instead.
21+
If you want to fetch items in reverse order you can use `last` and `before` instead.
2222

2323
The use of cursors means if items are added/removed between requests, the user will never see the same item twice.
2424

@@ -59,8 +59,8 @@ async function fetchUsers(userInput: {
5959
admins: boolean;
6060
first?: number;
6161
last?: number;
62-
beforeCursor?: string;
63-
afterCursor?: string;
62+
before?: string;
63+
after?: string;
6464
}) {
6565
const query = db('users').where('admin', userInput.admins);
6666

@@ -73,8 +73,8 @@ async function fetchUsers(userInput: {
7373
],
7474
first: userInput.first,
7575
last: userInput.last,
76-
beforeCursor: userInput.beforeCursor,
77-
afterCursor: userInput.afterCursor,
76+
before: userInput.before,
77+
after: userInput.after,
7878
},
7979
setup: {
8080
// generate one with `npx -p sql-cursor-pagination generate-secret`
@@ -139,13 +139,13 @@ E.g.
139139

140140
### Query
141141

142-
| Property | Type | Required | Description |
143-
| -------------- | --------------------------------------------- | ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
144-
| `first` | `number` | If `last` isn't present. | The number of rows to fetch from the start of the window. |
145-
| `last` | `number` | If `first` isn't present. | The number of rows to fetch from the end of the window. |
146-
| `sortFields` | `{ field: string, order: 'asc' \| 'desc' }[]` | Yes | This takes an array of objects which have `field` and `order` properties. There must be at least one entry and you must include an entry that maps to a unique key, otherwise it's possible for there to be cursor collisions, which will result in an exception. |
147-
| `afterCursor` | `string` | No | The window will cover the row after the provided cursor, and later rows. This takes the string `cursor` from a previous result`. |
148-
| `beforeCursor` | `string` | No | The window will cover the row before the provided cursor, and earlier rows. This takes the string `cursor` from a previous result. |
142+
| Property | Type | Required | Description |
143+
| ------------ | --------------------------------------------- | ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
144+
| `first` | `number` | If `last` isn't present. | The number of rows to fetch from the start of the window. |
145+
| `last` | `number` | If `first` isn't present. | The number of rows to fetch from the end of the window. |
146+
| `sortFields` | `{ field: string, order: 'asc' \| 'desc' }[]` | Yes | This takes an array of objects which have `field` and `order` properties. There must be at least one entry and you must include an entry that maps to a unique key, otherwise it's possible for there to be cursor collisions, which will result in an exception. |
147+
| `after` | `string` | No | The window will cover the row after the provided cursor, and later rows. This takes the string `cursor` from a previous result`. |
148+
| `before` | `string` | No | The window will cover the row before the provided cursor, and earlier rows. This takes the string `cursor` from a previous result. |
149149

150150
### Setup
151151

@@ -166,9 +166,9 @@ The `whereFragmentBuilder`/`orderByFragmentBuilder` objects provide the followin
166166

167167
## Errors
168168

169-
This library exports various error objects. `SqlCursorPaginationQueryError` will be thrown if the `first`/`last`/`beforeCursor`/`afterCursor` properties are the correct javascript type, but the contents is not valid.
169+
This library exports various error objects. `SqlCursorPaginationQueryError` will be thrown if the `first`/`last`/`before`/`after` properties are the correct javascript type, but the contents is not valid.
170170

171-
E.g. `ErrFirstNotInteger` is thrown if `first` was a `number`, but not an integer. `ErrBeforeCursorWrongQuery` is thrown if the provided `beforeCursor` was a valid cursor, but for a different query. You may want to map these errors to HTTP 400 responses.
171+
E.g. `ErrFirstNotInteger` is thrown if `first` was a `number`, but not an integer. `ErrBeforeCursorWrongQuery` is thrown if the provided `before` was a valid cursor, but for a different query. You may want to map these errors to HTTP 400 responses.
172172

173173
## I want the raw cursor
174174

@@ -179,6 +179,6 @@ const edgesWithRawCursor = res[edgesWithRawCursorSymbol];
179179
console.log(edgesWithRawCursor[0].rawCursor);
180180
```
181181

182-
This can then be provided to `beforeCursor`/`afterCursor` by wrapping the object with `rawCursor(object)`.
182+
This can then be provided to `before`/`after` by wrapping the object with `rawCursor(object)`.
183183

184184
You can also omit the `cursorSecret` and `cursor` will not be generated.

0 commit comments

Comments
 (0)