Now that we have our client configured, the next step is to create our records, using the client.items.create
method:
const categoryNameToRecord = {};for (let categoryName of uniqueCategories) {categoryNameToRecord[name] = await client.items.create({itemType: '552', // <- that's the ID of our category modelname});}
As you can see, we save the created records in a categoryNameToRecord
object so that it will be easier to access them during the creation of dog breeds, which is obviously the next thing we need to to do in our script:
for (let dogBreed of dogBreeds) {categoryNameToRecord[name] = await client.items.create({itemType: '730', // <- that's the ID of our dog_breed modelname: externalData.breed,category: categoryNameToRecord[dogBreed.category].id, // <- we pick the ID of our category recordbreed_for: externalData.breed_for,description: externalData.description,image: ???,});}
The last step is uploading the images. To do that, we can simply use the client.uploadFile
method, passing down additional data such as the default alternate text we want for each image:
for (let dogBreed of dogBreeds) {const image = await client.uploadFile(dogBreed.image_url,{defaultFieldMetadata: {en: {alt: `${dogBreed} dog`}},notes: `Imported from external source`,});categoryNameToRecord[name] = await client.items.create({// ...image: image,});}
And voilà! You've just successfully imported your external data to DatoCMS! Here's the complete script for reference:
const uniq = require('lodash.uniq');const fetch = require('node-fetch');const { SiteClient } = require('datocms-client');const client = new SiteClient('<YOUR-TOKEN-WITH-WRITE-ACCESS>');const data = [{"id": 1,"breed": "Alapaha Blue Blood Bulldog","bred_for": "Guarding","category": "Mixed","description": "The Alapaha Blue Blood Bulldog is a well-developed, exaggerated bulldog with a broad head and...","life_span": "12 - 13 years","image_url": "https://cdn2.thedogapi.com/images/kuvpGHCzm.jpg"},{"id": 2,"breed": "Alaskan Husky","bred_for": "Sled pulling","category": "Mixed","life_span": "10 - 13 years","image_url": "https://cdn2.thedogapi.com/images/uEPB98jBS.jpg"},{"id": 3,"breed": "Alaskan Malamute","bred_for": "Hauling heavy freight, Sled pulling","category": "Working","life_span": "12 - 15 years","image_url": "https://cdn2.thedogapi.com/images/aREFAmi5H.jpg"}];async function importDogBreeds() {const categories = data.map(dogBreed => dogBreed.category)const uniqueCategories = uniq(categories);const categoryNameToRecord = {};for (let categoryName of uniqueCategories) {categoryNameToRecord[name] = await client.items.create({itemType: '<CATEGORY-MODEL-ID>',name});}for (let dogBreed of dogBreeds) {const image = await client.uploadFile(dogBreed.image_url,{defaultFieldMetadata: {en: {alt: `${dogBreed} dog`}},notes: `Imported from external source`,});categoryNameToRecord[name] = await client.items.create({itemType: '<DOG-BREED-MODEL-ID>',name: externalData.breed,category: categoryNameToRecord[dogBreed.category].id,breed_for: externalData.breed_for,description: externalData.description,image,});}}importDogBreeds();