From 6a0ae01cf2a7396c9e96e564d274ebcf1724c330 Mon Sep 17 00:00:00 2001 From: Albert Martin Date: Thu, 8 Nov 2018 21:13:59 -0600 Subject: [PATCH] feat: add purge option to sync --- index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 7e5d83e..495d9b0 100644 --- a/index.js +++ b/index.js @@ -118,9 +118,19 @@ class ContentfulCache { /** * Helper function to run a full sync against Contentful. All content will be * pulled down with paginated API requests and cached. + * @param {Object} config - Optional flags. + * @param {Boolean} config.purge - Whether the database should be flushed + * before a re-sync. Useful if the cache has become corrupted. Use with care + * since the purge will happen whether or not the Contentful API is reachable. * @return {Promise} - Fulfilled when the sync has finished. */ - async sync() { + async sync(config = {}) { + if (config.purge) { + return this.cache.client.flushdb((err) => { + if (err) console.error(err); + this.syncPaged(); + }); + } return this.syncPaged(); }