Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

implement #138 since option for collection.getTotalRecords #142

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/collection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { v4 as uuid } from "uuid";

import { capable, toDataBody, isObject } from "./utils";
import { capable, toDataBody, isObject, qsify } from "./utils";
import * as requests from "./requests";
import endpoint from "./endpoint";

Expand Down Expand Up @@ -79,13 +79,19 @@ export default class Collection {
*
* @param {Object} [options={}] The options object.
* @param {Object} [options.headers] The headers object option.
* @param {String} [options.since] The since ETag option.
* @return {Promise<Number, Error>}
*/
getTotalRecords(options={}) {
const { headers } = this._collOptions(options);
const { since, ...otherOptions } = options;
const { headers } = this._collOptions(otherOptions);
let path = endpoint("record", this.bucket.name, this.name);
if (since) {
path += "?" + qsify({since});
}
return this.client.execute({
method: "HEAD",
path: endpoint("record", this.bucket.name, this.name),
path,
headers
}, {raw: true})
.then(({headers}) => parseInt(headers.get("Total-Records"), 10));
Expand Down
12 changes: 12 additions & 0 deletions test/collection_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ describe("Collection", () => {
return getBlogPostsCollection().getTotalRecords()
.should.become(42);
});

it("should pass the since parameter", () => {
sandbox.stub(client, "execute").returns(Promise.resolve());

getBlogPostsCollection().getTotalRecords({since: "ETAG"});

sinon.assert.calledWithMatch(client.execute, {
method: "HEAD",
path: "/buckets/blog/collections/posts/records?since=ETAG",
}, {raw: true});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


});

/** @test {Collection#getData} */
Expand Down