Skip to content

Commit

Permalink
feat: create a separate type for DataSourceEntry and get the dimensio…
Browse files Browse the repository at this point in the history
…ns as well
  • Loading branch information
emanuelgsouza committed Jul 31, 2020
1 parent 3c142a7 commit 0117f3b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
33 changes: 27 additions & 6 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
})
}
}
}
32 changes: 30 additions & 2 deletions src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit 0117f3b

Please sign in to comment.