Skip to content

Commit

Permalink
feat: Allow multiple indices
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-linnell authored and Haroenv committed May 30, 2018
1 parent c3af255 commit fd6d9e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const query = `{

const queries = [
{
indexName: 'index name to target',
query,
transformer: ({ data }) => data.allSitePage.edges.map(({ node }) => node),
},
Expand All @@ -51,7 +52,6 @@ module.exports = {
options: {
appId: 'your appId',
apiKey: 'your admin api key',
indexName: 'index name to target',
queries,
chunkSize: 10000, // default: 1000
},
Expand Down
26 changes: 14 additions & 12 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ const chunk = require('lodash.chunk');

exports.onPostBuild = function(
{ graphql },
{ appId, apiKey, indexName, queries, chunkSize = 1000 }
{ appId, apiKey, queries, chunkSize = 1000 }
) {
const client = algoliasearch(appId, apiKey);
const index = client.initIndex(indexName);

const jobs = queries.map(async function doQuery({ query, transformer }) {
const result = await graphql(query);
const objects = transformer(result);
const chunks = chunk(objects, chunkSize);
const jobs = queries.map(
async function doQuery({ indexName, query, transformer }) {
const index = client.initIndex(indexName);
const result = await graphql(query);
const objects = transformer(result);
const chunks = chunk(objects, chunkSize);

const chunkJobs = chunks.map(async function(chunked) {
const { taskID } = await index.addObjects(chunked);
return index.waitTask(taskID);
});
const chunkJobs = chunks.map(async function(chunked) {
const { taskID } = await index.addObjects(chunked);
return index.waitTask(taskID);
});

return Promise.all(chunkJobs);
});
return Promise.all(chunkJobs);
}
);

return Promise.all(jobs);
};

0 comments on commit fd6d9e5

Please sign in to comment.