forked from kulak-at/gatsby-plugin-algolia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
executable file
·501 lines (431 loc) · 13.1 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
const algoliasearch = require('algoliasearch');
const chunk = require('lodash.chunk');
const deepEqual = require('deep-equal');
/**
* Fetches all records for the current index from Algolia
*
* @param {AlgoliaIndex} index eg. client.initIndex('your_index_name');
* @param {Array<String>} attributesToRetrieve eg. ['modified', 'slug']
*/
function fetchAlgoliaObjects(
index,
attributesToRetrieve = ['modified'],
reporter
) {
const hits = {};
return index
.browseObjects({
batch: batch => {
if (Array.isArray(batch)) {
batch.forEach(hit => {
hits[hit.objectID] = hit;
});
}
},
attributesToRetrieve,
})
.then(() => hits)
.catch(err =>
reporter.panicOnBuild('failed while getting indexed objects', err)
);
}
exports.onPostBuild = async function ({ graphql, reporter }, config) {
const {
appId,
apiKey,
queries,
concurrentQueries = true,
skipIndexing = false,
dryRun = false,
continueOnFailure = false,
} = config;
const activity = reporter.activityTimer(`index to Algolia`);
activity.start();
if (skipIndexing === true) {
activity.setStatus(`options.skipIndexing is true; skipping indexing`);
activity.end();
return;
}
if (dryRun === true) {
console.log(
'\x1b[33m%s\x1b[0m',
'==== THIS IS A DRY RUN ====================\n' +
'- No records will be pushed to your index\n' +
'- No settings will be updated on your index'
);
}
const client = algoliasearch(appId, apiKey, {
timeouts: {
connect: 1,
read: 30,
write: 30,
},
});
activity.setStatus(`${queries.length} queries to index`);
try {
// combine queries with the same index to prevent overwriting data
const groupedQueries = groupQueriesByIndex(queries, config);
const jobs = [];
for (const [indexName, indexQueries] of Object.entries(groupedQueries)) {
const queryPromise = runIndexQueries(indexName, indexQueries, {
client,
activity,
graphql,
config,
reporter,
dryRun,
});
if (concurrentQueries) {
jobs.push(queryPromise);
} else {
// await each individual query rather than batching them
const res = await queryPromise;
jobs.push(res);
}
}
await Promise.all(jobs);
} catch (err) {
if (continueOnFailure) {
reporter.warn('failed to index to Algolia');
console.error(err);
} else {
activity.panicOnBuild('failed to index to Algolia', err);
}
}
activity.end();
};
function groupQueriesByIndex(queries = [], config) {
const { indexName: mainIndexName } = config;
return queries.reduce((groupedQueries, queryOptions) => {
const { indexName = mainIndexName } = queryOptions;
return {
...groupedQueries,
[indexName]: [
...(groupedQueries.hasOwnProperty(indexName)
? groupedQueries[indexName]
: []),
queryOptions,
],
};
}, {});
}
/**
* Run all queries for a given index, then make any updates / removals necessary
* @param {string} indexName
* @param {string[]} queries
* @param {object} options
* @param {import('algoliasearch').SearchClient} options.client
* @param {any} options.activity
* @param {any} options.graphql
* @param {any} options.reporter
* @param {any} options.config
* @param {boolean=} options.dryRun
*/
async function runIndexQueries(
indexName,
queries = [],
{ client, activity, graphql, reporter, config, dryRun }
) {
const {
settings: mainSettings,
chunkSize = 1000,
enablePartialUpdates = false,
matchFields: mainMatchFields = ['modified'],
useEnvironment = false,
environmentKey = 'environment'
} = config;
activity.setStatus(
`Running ${queries.length} ${
queries.length === 1 ? 'query' : 'queries'
} for index ${indexName}...`
);
const objectMapsByQuery = await Promise.all(
queries.map(query => getObjectsMapByQuery(query, graphql, reporter))
);
const allObjectsMap = objectMapsByQuery.reduce((acc, objectsMap = {}) => {
return {
...acc,
...objectsMap,
};
}, {});
activity.setStatus(
`${queries.length === 1 ? 'Query' : 'Queries'} resulted in a total of ${
Object.keys(allObjectsMap).length
} results`
);
const index = client.initIndex(indexName);
const tempIndex = client.initIndex(`${indexName}_tmp`);
const indexToUse = await getIndexToUse({
index,
tempIndex,
enablePartialUpdates,
});
let toIndex = {}; // used to track objects that should be added / updated
const toRemove = {}; // used to track objects that are stale and should be removed
if (enablePartialUpdates !== true) {
// enablePartialUpdates isn't true, so index all objects
toIndex = { ...allObjectsMap };
} else {
// iterate over each query to determine which data are fresh
activity.setStatus(`Starting Partial updates...`);
// get all match fields for all queries to minimize calls to the api
const allMatchFields = getAllMatchFields(queries, mainMatchFields);
// get all indexed objects matching all matched fields
const indexedObjects = await fetchAlgoliaObjects(
indexToUse,
allMatchFields,
reporter
);
// iterate over each query
for (const [i, { matchFields = mainMatchFields }] of queries.entries()) {
const queryResultsMap = objectMapsByQuery[i] || {};
// 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];
if (
matchFields.every(field => newObj.hasOwnProperty(field) === false)
) {
reporter.panicOnBuild(
'when enablePartialUpdates is true, the objects must have at least one of the match fields. Current object:\n' +
JSON.stringify(newObj, null, 2) +
'\n' +
'expected one of these fields:\n' +
matchFields.join('\n')
);
}
if (
matchFields.some(
field =>
!deepEqual(existingObj[field], newObj[field], { strict: true })
)
) {
// one or more fields differ, so index new object
toIndex[id] = newObj;
} else {
// objects are the same, so skip
}
// remove from queryResultsMap, since it is already accounted for
delete queryResultsMap[id];
} else {
// check if existing object exists in any new query
if (!allObjectsMap.hasOwnProperty(id)) {
// existing object not in new queries; remove
toRemove[id] = true;
}
}
}
if (Object.values(queryResultsMap).length) {
// stale objects have been removed, remaining query objects should be indexed
toIndex = {
...toIndex,
...queryResultsMap,
};
}
}
}
const objectsToIndex = Object.values(toIndex);
const objectsToRemove = Object.keys(toRemove);
if (objectsToIndex.length) {
const chunks = chunk(objectsToIndex, chunkSize);
activity.setStatus(
`Found ${objectsToIndex.length} new / updated records...`
);
if (chunks.length > 1) {
activity.setStatus(`Splitting in ${chunks.length} jobs`);
}
/* Add changed / new objects */
const chunkJobs = chunks.map(async function (chunked) {
if (dryRun === true) {
reporter.info(`Records to add: ${objectsToIndex.length}`);
} else {
await indexToUse.saveObjects(chunked);
}
});
await Promise.all(chunkJobs);
} else {
activity.setStatus(`No updates necessary; skipping!`);
}
if (objectsToRemove.length) {
activity.setStatus(
`Found ${objectsToRemove.length} stale objects; removing...`
);
if (dryRun === true) {
reporter.info(`Records to delete: ${objectsToRemove.length}`);
} else {
await indexToUse.deleteObjects(objectsToRemove).wait();
}
}
// defer to first query for index settings
// todo: maybe iterate over all settings and throw if they differ
const { settings = mainSettings, forwardToReplicas } = queries[0] || {};
const settingsToApply = await getSettingsToApply({
settings,
index,
tempIndex,
indexToUse,
reporter,
});
if (dryRun) {
console.log('[dry run]: settings', settingsToApply);
} else {
await indexToUse
.setSettings(settingsToApply, {
forwardToReplicas,
})
.wait();
}
if (dryRun) {
console.log('[dry run]: settings', settingsToApply);
} else {
await indexToUse
.setSettings(settingsToApply, {
forwardToReplicas,
})
.wait();
}
if (indexToUse === tempIndex && dryRun === false) {
await moveIndex(client, indexToUse, index);
}
activity.setStatus('Done!');
}
/**
* moves the source index to the target index
* @param client
* @param sourceIndex
* @param targetIndex
* @return {Promise}
*/
async function moveIndex(client, sourceIndex, targetIndex) {
// first copy the rules and synonyms to the temporary index, as we don't want
// to touch the original index, and there's no way to only move objects and
// settings, leaving rules and synonyms in place.
await client.copyIndex(targetIndex.indexName, sourceIndex.indexName, {
scope: ['rules', 'synonyms'],
});
return client.moveIndex(sourceIndex.indexName, targetIndex.indexName).wait();
}
/**
* Does an Algolia index exist already
*
* @param index
*/
function indexExists(index) {
return index
.getSettings()
.then(() => true)
.catch(error => {
if (error.statusCode !== 404) {
throw error;
}
return false;
});
}
/**
* @param {object} options
* @param {import('algoliasearch').SearchIndex} options.index
* @param {import('algoliasearch').SearchIndex} options.tempIndex
* @param {boolean=} options.enablePartialUpdates
* @returns {Promise<import('algoliasearch').SearchIndex>}
*/
async function getIndexToUse({ index, tempIndex, enablePartialUpdates }) {
const mainIndexExists = await indexExists(index);
if (enablePartialUpdates && !mainIndexExists) {
return createIndex(index);
}
if (!enablePartialUpdates && mainIndexExists) {
return tempIndex;
}
return index;
}
/**
* @param {object} options
* @param {import('@algolia/client-search').Settings} options.settings
* @param {import('algoliasearch').SearchIndex} options.index
* @param {import('algoliasearch').SearchIndex} options.tempIndex
* @param {import('algoliasearch').SearchIndex} options.indexToUse
* @param {any} options.reporter
* @returns {import('@algolia/client-search').Settings}
*/
async function getSettingsToApply({
settings,
index,
tempIndex,
indexToUse,
reporter,
}) {
const existingSettings = await index.getSettings().catch(e => {
reporter.panicOnBuild(`${e.toString()} ${index.indexName}`);
});
if (!settings) {
return existingSettings;
}
const replicasToSet = getReplicasToSet(
settings.replicas,
existingSettings.replicas,
settings.replicaUpdateMode
);
const { replicaUpdateMode, ...requestedSettings } = {
...settings,
replicas: replicasToSet,
};
// If we're building replicas, we don't want to add them to temporary indices
if (indexToUse === tempIndex) {
const { replicas, ...adjustedSettings } = requestedSettings;
return adjustedSettings;
}
return requestedSettings;
}
function getReplicasToSet(
givenReplicas = [],
existingReplicas = [],
replicaUpdateMode = 'merge'
) {
if (replicaUpdateMode == 'replace') {
return givenReplicas;
}
if (replicaUpdateMode === 'merge') {
const replicas = new Set();
existingReplicas.forEach(replica => replicas.add(replica));
givenReplicas.forEach(replica => replicas.add(replica));
return [...replicas];
}
}
async function getObjectsMapByQuery({ query, transformer }, graphql, reporter) {
const result = await graphql(query);
if (result.errors) {
reporter.panicOnBuild(
`failed to index to Algolia, errors:\n ${JSON.stringify(result.errors)}`,
result.errors
);
}
const objects = (await transformer(result)).map(object => ({
objectID: object.objectID || object.id,
...object,
}));
if (objects.length > 0 && !objects[0].objectID) {
reporter.panicOnBuild(
`failed to index to Algolia. Query results do not have 'objectID' or 'id' key`
);
}
// return a map by id for later use
return Object.fromEntries(objects.map(object => [object.objectID, object]));
}
// get all match fields for all queries to minimize calls to the api
function getAllMatchFields(queries, mainMatchFields = []) {
const allMatchFields = new Set(mainMatchFields);
queries.forEach(({ matchFields = [] }) => {
matchFields.forEach(field => {
allMatchFields.add(field);
});
});
return [...allMatchFields];
}
async function createIndex(index) {
await index.setSettings({}).wait();
return index;
}