Skip to content

Instantly share code, notes, and snippets.

@Haroenv
Created February 20, 2020 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Haroenv/75d6d762fdb2ff062f18b909d7cda0b7 to your computer and use it in GitHub Desktop.
Save Haroenv/75d6d762fdb2ff062f18b909d7cda0b7 to your computer and use it in GitHub Desktop.
import algoliasearch from 'algoliasearch';
import { encode, addMethods } from '@algolia/client-common';
import { MethodEnum } from '@algolia/requester-common';
import {
createBrowsablePromise,
browseObjects,
BrowseResponse,
SearchResponse,
ObjectWithObjectID,
} from '@algolia/client-search';
const customBrowse: typeof browseObjects = base => {
return ({
limit,
...requestOptions
}: ReturnType<typeof browseObjects> & { limit: number }) => {
return createBrowsablePromise({
...requestOptions,
shouldStop: (
response: SearchResponse & BrowseResponse<ObjectWithObjectID>
) => {
return (
response.cursor === undefined ||
(response.page + 1) * response.hitsPerPage >= limit
);
},
request: data =>
base.transporter.read(
{
method: MethodEnum.Post,
path: encode('1/indexes/%s/browse', base.indexName),
data,
},
requestOptions
),
});
};
};
// then add this to your client
const index = addMethods(
algoliasearch('latency', 'f4500fb6027c0b192d095289344542ba').initIndex(
'instant_search'
),
{ customBrowse }
);
function getEverything() {
let hits = [];
return index
.customBrowse({
limit: '10000',
batch(batch) {
hits.push(...batch);
},
})
.then(() => hits);
}
getEverything().then(everything => console.log(everything.length));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment