Skip to content

Commit

Permalink
add startFrom method for easier paging - fixes #53
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jul 18, 2017
1 parent 2449d4a commit 0b5195c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
41 changes: 41 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,47 @@ db.listTables().exec().then(tables => {
If you passed in a `prefix` property in the connection object, only the tables with that prefix will
be returned.

### Paging

You can implement paging by using the `startFrom()` method together with the `LastEvaluatedKey` property returned when using the `raw()` method.

```js
const result = Employee.find({Organisation: 'Amazon'}).where({Salary: {$gt: 3000}}).limit(1).raw().exec()
.then(result => {
/**
* {
* "Items": [
* { UserId: '1', FirstName: 'Foo', Name: 'Bar' }
* ],
* "Count": 1,
* "ScannedCount": 1,
* "LastEvaluatedKey": {
* Organisation: 'Amazon',
* UserId: '1'
* }
* }
*/

// Retrieve the next page
return Employee.find({Organisation: 'Amazon'}).where({Salary: {$gt: 3000}}).startFrom(result.LastEvaluatedKey).limit(1).raw().exec()

This comment has been minimized.

Copy link
@pratiks

pratiks Jul 25, 2017

With an object set as my LastEvaluatedKey (if logic to ensure the line does not execute ), when the statement executes I receive the following error:
Cannot convert undefined or null to object

})
.then(result => {
/**
* {
* "Items": [
* { UserId: '2', FirstName: 'Unicorn', Name: 'Rainbow' }
* ],
* "Count": 1,
* "ScannedCount": 1,
* "LastEvaluatedKey": {
* Organisation: 'Amazon',
* UserId: '2'
* }
* }
*/
});
```

### Create a table

A table can be created by either calling `create()` on a table instance or by calling `createTable` on the database instance.
Expand Down
11 changes: 11 additions & 0 deletions src/lib/methods/base-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ export abstract class BaseQuery extends Method {
return this;
}

/**
* Start querying from the provided key. Ideally for paging.
*
* @param lastEvaluatedKey The primary key of the first item that this operation will evaluate. Use the value that was returned for L`astEvaluatedKey` in the previous operation.
*/
startFrom(lastEvaluatedKey: any) {
this.params.ExclusiveStartKey = lastEvaluatedKey;

return this;
}

/**
* Limit the number of items returned. If the limit is set to 1, the exec method
* will return the first object instead of an array with one object.
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Params {
ExpressionAttributeValues?: Map<any>;
ProjectionExpression?: string;
ScanIndexForward?: boolean;
ExclusiveStartKey?: any;
Limit?: number;
ReturnValues?: 'ALL_NEW' | 'ALL_OLD';
Select?: 'COUNT';
Expand Down
31 changes: 31 additions & 0 deletions src/test/methods/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ test.serial('limit', async t => {
});
});

test.serial('exclusive start key', async t => {
await Table.find({id: '5'}).startFrom({id: '10', foo: 'bar'}).exec();

t.deepEqual(queryStub.lastCall.args[0], {
TableName: 'Table',
KeyConditionExpression: '#k_id=:v_id',
ExclusiveStartKey: {
id: '10',
foo: 'bar'
},
ExpressionAttributeNames: {
'#k_id': 'id'
},
ExpressionAttributeValues: {
':v_id': '5'
}
});
});

test.serial('sort ascending', async t => {
await (Table.find({id: '5'}) as any).sort(1).exec();

Expand Down Expand Up @@ -226,6 +245,18 @@ test.serial('find all where', async t => {
});
});

test.serial('scan::exclusive start key', async t => {
await Table.find().startFrom({id: '10', foo: 'bar'}).exec();

t.deepEqual(scanStub.lastCall.args[0], {
TableName: 'Table',
ExclusiveStartKey: {
id: '10',
foo: 'bar'
}
});
});

test.serial('scan::result', async t => {
t.deepEqual(await Table.find().exec(), ['foo', 'bar', 'baz']);
});
Expand Down

0 comments on commit 0b5195c

Please sign in to comment.