From c88dd392fe0f9fccd9f8c5193893d4ba02cb45eb Mon Sep 17 00:00:00 2001 From: Kacper Kula Date: Thu, 18 Feb 2021 13:42:43 +0100 Subject: [PATCH] feat(environment): Added functionality to perist entries from different instance. --- README.md | 5 +++++ gatsby-node.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 961bb3e..f8cb575 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,8 @@ module.exports = { matchFields: ['slug', 'modified'], // Array default: ['modified'] concurrentQueries: false, // default: true skipIndexing: true, // default: false, useful for e.g. preview deploys or local development + useEnvironment: 'blog', // default: false, used in conjunction with enablePartialUpdates let you persist entries from different environment + environmentKey: 'product', // default: 'environment', key used when useEnvironment is set. }, }, ], @@ -127,6 +129,9 @@ If you pass `replicaUpdateMode: 'replace'` in the index settings, you can choose If you pass `replicaUpdateMode: 'merge'` in the index settings, the replica settings will combine the replicas set on your dashboard with the additional ones you set via index settings here. +### Persisting entries +If you want to persist entries that are added to the index (for example added by the different gatsby instance if your index is combining several websites) you can set `useEnvironment` to unique string that represents given instance. All entries with different `environment` value will be ignored. You can change the key accordingly using `environmentKey`. + ## Concurrent Queries Sometimes, on limited platforms like Netlify, concurrent queries to the same index can lead to unexpected results or hanging builds. Setting `concurrentQueries` to `false` makes it such that queries are run sequentially rather than concurrently, which may solve some concurrent access issues. Be aware that this option may make indexing take longer than it would otherwise. diff --git a/gatsby-node.js b/gatsby-node.js index cc8ba23..b817ab2 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -108,6 +108,8 @@ async function runIndexQueries( chunkSize = 1000, enablePartialUpdates = false, matchFields: mainMatchFields = ['modified'], + useEnvironment = false, + environmentKey = 'environment' } = config; setStatus( @@ -168,6 +170,9 @@ async function runIndexQueries( // iterate over existing objects and compare to fresh data for (const [id, existingObj] of Object.entries(indexedObjects)) { + if (useEnvironment && existingObj[environmentKey] !== useEnvironment) { + continue; + } if (queryResultsMap.hasOwnProperty(id)) { // key matches fresh objects, so compare match fields const newObj = queryResultsMap[id];