From 0117f3bd54c534dcfb0745215ca05e43ea7a7ea0 Mon Sep 17 00:00:00 2001 From: Emanuel Souza Date: Fri, 31 Jul 2020 12:51:43 -0300 Subject: [PATCH] feat: create a separate type for DataSourceEntry and get the dimensions as well --- gatsby-node.js | 33 +++++++++++++++++++++++++++------ src/sync.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 00c47e460..a4a9206a4 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -17,10 +17,10 @@ exports.sourceNodes = async function({ boundActionCreators }, options) { const languages = space.language_codes.map((lang) => { return lang + '/*' }) languages.push('') - for (var spKey = 0; spKey < languages.length; spKey++) { + for (const language of languages) { await Sync.getAll('stories', { node: 'StoryblokEntry', - params: getStoryParams(languages[spKey], options), + params: getStoryParams(language, options), process: (item) => { for (var prop in item.content) { if (!item.content.hasOwnProperty(prop) || ['_editable', '_uid'].indexOf(prop) > -1) { @@ -59,13 +59,34 @@ exports.sourceNodes = async function({ boundActionCreators }, options) { node: 'StoryblokDatasource' }) - for (var dsKey = 0; dsKey < datasources.length; dsKey++) { + for (const datasource of datasources) { + const datasourceSlug = datasource.slug + await Sync.getAll('datasource_entries', { - node: 'StoryblokDatasource', - params: {datasource: datasources[dsKey].slug}, + node: 'StoryblokDatasourceEntry', + params: { + datasource: datasourceSlug + }, process: (item) => { - item.data_source = datasources[dsKey].slug + item.data_source_dimension = null + item.data_source = datasourceSlug } }) + + const datasourceDimensions = datasource.dimensions || [] + + for (const dimension of datasourceDimensions) { + await Sync.getAll('datasource_entries', { + node: 'StoryblokDatasourceEntry', + params: { + datasource: datasourceSlug, + dimension: dimension.entry_value + }, + process: (item) => { + item.data_source_dimension = dimension.entry_value + item.data_source = datasourceSlug + } + }) + } } } diff --git a/src/sync.js b/src/sync.js index ddef0ba2e..2c7d450d7 100644 --- a/src/sync.js +++ b/src/sync.js @@ -28,8 +28,23 @@ module.exports = { }, createNode(name, item) { + const nodeObject = this.builderNode(name, item) + + this.$createNode(nodeObject) + }, + + builderNode (name, item) { + if (name ==='StoryblokDatasourceEntry') { + return this.factoryDatasourceEntryNode(name, item) + } + + return this.factoryDefaultNode(name, item) + }, + + factoryDefaultNode (name, item) { const lang = item.lang || 'default' - const node = Object.assign({}, item, { + + return Object.assign({}, item, { id: `${name.toLowerCase()}-${item.id}-${lang}`, internalId: item.id, parent: null, @@ -40,8 +55,21 @@ module.exports = { contentDigest: crypto.createHash(`md5`).update(stringify(item)).digest(`hex`) } }) + }, - this.$createNode(node) + factoryDatasourceEntryNode (name, item) { + const dimension = item.data_source_dimension || 'default' + return Object.assign({}, item, { + id: `${name.toLowerCase()}-${item.id}-${dimension}`, + internalId: item.id, + parent: null, + children: [], + internal: { + mediaType: `application/json`, + type: name, + contentDigest: crypto.createHash(`md5`).update(stringify(item)).digest(`hex`) + } + }) }, async getOne(single, type, options) {