2.2.8 • Published 10 months ago

@hv/datoloader v2.2.8

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

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", ...}].

2.2.8

10 months ago

2.2.5

10 months ago

2.2.7

10 months ago

2.2.6

10 months ago

2.2.4

11 months ago

2.2.1

11 months ago

2.1.2

11 months ago

2.2.0

11 months ago

2.1.1

11 months ago

2.2.3

11 months ago

2.2.2

11 months ago

2.1.3

11 months ago

2.1.0

11 months ago

2.0.0

11 months ago

1.1.0

1 year ago

1.0.4

1 year ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago