Last active
January 22, 2018 18:26
-
-
Save onefriendaday/a2ef18a6dd5a36def9653759107b93a4 to your computer and use it in GitHub Desktop.
Nodejs backup example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple backup example in node.js | |
// | |
// How to use: | |
// npm install storyblok-js-client | |
// | |
// Create the folder 'backup' | |
// | |
// Create the file 'backup.js' and execute | |
// node ./backup.js | |
let StoryblokClient = require('storyblok-js-client') | |
const fs = require('fs') | |
let client = new StoryblokClient({ | |
accessToken: 'WcdDcNgDm59K72EbsQg8Lgtt' | |
}) | |
let lastPage = 1 | |
let getStories = (page) => { | |
client.get('cdn/stories', { | |
version: 'draft', | |
per_page: 10, | |
page: page | |
}).then((res) => { | |
let stories = res.data.stories | |
stories.forEach((story) => { | |
fs.writeFile('./backup/' + story.full_slug.replace('/', '_'), JSON.stringify(story), (err) => { | |
if (err) throw err | |
console.log(story.full_slug + ' backed up') | |
}) | |
}) | |
let total = res.total | |
lastPage = Math.ceil((res.total / res.perPage)) | |
if (page <= lastPage) { | |
page++ | |
getStories(page) | |
} | |
}) | |
} | |
getStories(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment