@hv/datoloader
v2.2.12
Published
Dato Loader ===========
Downloads
111
Keywords
Readme
Dato Loader
Downloads all data from DatoCMS. Data will be simplified. Typescript can be generated.
Usage
import { writeFile } from 'fs/promises';
import { Loader, generateTypescript } from '@hv/datoloader';
const loader = new Loader(process.env.DATO_API_KEY, {
onUpdate,
languages: { de: ['en'] },
});
loader.start();
async function onUpdate(data) {
await writeFile('data.json', JSON.stringify(data, null, 2));
await writeFile('data.ts', generateTypescript(firstLang));
}
Options:
export interface Options {
onUpdate: (payload: Result) => void | null
watch: boolean // keep watching for changes, onUpdate will be called again
changeKey: (key: string) => string // how to change the keys of the data (default: camelcase)
languages?: string[] | { [language: string]: string[] } // langauge: fallbackLanguages
itemVersion: boolean // include item version?
itemCreatedAt: boolean // include item createdAt?
itemUpdatedAt: boolean // include item updatedAt?
environment?: string
draft?: boolean
headers?: Record<string, string>
}
Please note that the resulting data will be simplified to someting like this:
{
"de": {
"itemType": {
"anotherBlock": {
"fields": {
"title": {
"type": "string",
"localized": false,
"required": false
}
}
}
},
"site": {
"name": "Boilerplate",
...
},
"upload": {
"54053638": {
"_id": "54053638",
"size": 9207613,
"width": 3840,
"height": 2160,
"url": "https://www.datocms-assets.com/99255/1682668783-weitsicht-3840x2160.png",
...
}
},
"item": {
"146161091": {
"_id": "146161091",
"_type": "anotherBlock",
"title": "Just Another",
"_updatedAt": "2023-04-28T08:02:27.684Z",
"_createdAt": "2023-04-28T08:02:27.680Z",
"_version": 267617551
}
}
}
}
Later, you can use the generated data and typescript like this:
import { readFile } from 'fs/promises';
import { Result, resolve } from '@hv/datoloader';
export async function loadData() {
const data = JSON.parse(await readFile('data.json'));
return resolve(data) as Result;
}
Resolving will link actual items to their references. So anything like items: ["23434232", "23423423"]
will be resolved to items: [{_id: "23434232", ...}, {_id: "23423423", ...}]
.