-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
70 lines (58 loc) · 2.42 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const { buildClient } = require('@datocms/cma-client-node')
const { HTML2DatoCMS } = require('../html2datocms.js')
DATO_ITEM_TYPE_ID = 'YOUR_ITEM_TYPE_ID'
DATO_IMG_BLOCK_ID = 'YOUR_IMG_BLOCK_ID'
DATO_API_TOKEN = 'YOUR_API_TOKEN'
async function example () {
const exampleData = {
title: 'Example Title',
active: 'true',
image: 'https://picsum.photos/200/300',
// text with translations
description: '<p><strong>Bold Text</strong>, <i>Italic Text</i> and <u>Underlined Text</u></p>',
translations: {
'de-CH': {
description: '<p><strong>Fett Text</strong>, <i>Kursiv Text</i> und <u>Unterstrichener Text</u></p>'
}
},
// text with images
description_img: '<p><strong>Bold Text</strong>, <i>Italic Text</i> und <u>Underlined Text</u></p><img src="https://picsum.photos/200/300" alt="Image Alt Text" />',
categories: ['Events', 'News']
}
const client = buildClient({ apiToken: DATO_API_TOKEN })
const h2d = new HTML2DatoCMS(client, DATO_IMG_BLOCK_ID)
// check if item already exists (assuming title is unique)
const records = await h2d.fetchRecords('my_item', 'title', exampleData.title)
if (records.length > 0) {
console.log('Item already exists!')
return
}
datoCategories = []
for (let i = 0; i < exampleData.categories.length; i++) {
const category = exampleData.categories[i]
const categoryRecords = await h2d.fetchRecords('category', 'name', category, client)
if (categoryRecords.length > 0) datoCategories.push(categoryRecords[0].id.toString())
else console.log(`Category ${category} not found!`)
}
const datoItem = {
item_type: { type: 'item_type', id: DATO_ITEM_TYPE_ID },
title: exampleData.title,
description: {
en: await h2d.html2block(exampleData.description),
'de-CH': await h2d.html2block(exampleData.translations['de-CH'].description)
},
// client is needed for uploading images
description_img: await h2d.html2block(exampleData.description_img),
active: h2d.boolToDatoCMS(exampleData.active),
image: await h2d.uploadToDatoCMS(exampleData.image),
categories: datoCategories
}
await client.items.create(datoItem).catch((e) => {
console.log(e)
// To inspect the item if something is wrong
const fs = require('fs')
fs.writeFile('example_dato_item.json', JSON.stringify(datoItem), () => { })
console.log('Error while creating item. Check example_dato_item.json')
})
}
example()