Hypertekt › Docs › Guides

Fill a collection with a server function

Bring a track list, a price list or other data from another service into a collection. Every item is checked against the collection's schema.

Templates can’t fetch anything, so data from outside the site comes in through a server function: TypeScript that fills a collection. A function’s changes are checked against the collection’s schema, like a change made by a person.

A function

Each function is a file in functions/, named for what it does. This one fills in an album’s tracks from last.fm when an album is added. functions/album-tracks.ts:

export default defineFunction({
  triggers: [{ created: "albums" }],
  secrets: ["LASTFM_API_KEY"],
  async run({ item, get, update, secrets, fetch }) {
    const artist = get("artists", item.artist);
    const url = new URL("https://ws.audioscrobbler.com/2.0/");
    url.search = new URLSearchParams({
      method: "album.getInfo",
      format: "json",
      api_key: secrets.LASTFM_API_KEY,
      artist: artist.name,
      album: item.title,
    }).toString();
    const { album } = await (await fetch(url)).json();
    update("albums", item.id, {
      tracks: album.tracks.track.map((track) => ({ name: track.name })),
    });
  },
});

defineFunction is always available: there’s nothing to import.

When it runs

triggers says when the function runs:

  • { created: "albums" } runs the function when an item is added to the collection. For now, that’s when a new file appears in content/albums/ while tekt start is running. The function is given the new item.
  • { form: "contact" } runs the function as the form’s filter, each time a visitor sends the form. See Add a form.

A function with no triggers only runs when you run it with tekt run. That suits data that’s brought in again now and then, like a price list. Items that a function creates don’t trigger other functions.

What a function is given

  • items(collection) and get(collection, id) read any collection’s items, as they’re written in their files: a reference is the other item’s id, a date is text, and an image is its path in assets/.
  • create, update and delete change items. update changes only the fields it’s given.
  • fetch reaches anything on the internet, secrets holds the secrets the function lists, and log writes a line to the function’s log.

All or nothing

A function’s changes are held back until the function finishes. Then every item the function changed is checked against its collection’s schema. The changes are saved together only if every item passes. If any item fails, or the function itself fails, nothing is saved, and the error names the item and the field. A function can’t leave a collection half updated.

Secrets

A function lists the secrets it needs by name. Secret values are never in the site’s files. On your own computer, they’re in the site’s .env, which tekt keeps out of git.

LASTFM_API_KEY=0123456789abcdef

See Analytics, other scripts and secrets for setting them on Hypertekt.

Running it

tekt run album-tracks --item albums/onda   # as if that album had just been added
tekt run album-tracks --dry-run            # says what it would change, and saves nothing

tekt run saves what it changes in content/, and says what changed. tekt start runs created triggers itself when a new file appears in content/, and builds again. Then tekt push sends the changes, like any other change.