Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Migrating schema file to v3 and updating slugify example.

4 replies
Last updated: Jun 13, 2023
Hi folks... just migrating my v2 studio to v3. Just a litte question here. How do i migrate this schema file regarding the new sanity client retrival?

import {FaMap} from "react-icons/fa";
import slugify from "slugify";
import sanityClient from 'part:@sanity/base/client'

function myAsyncSlugifier(input) {
    //const slug = slugify(input);
    /// The type of the reference field to get the value from
    const referenceNameType = 'city'
    /// Query for the referenced "category" and return the "name" field of that referenced document
    const query = `*[_type == "${referenceNameType}" && _id == $nameRef][0] {name}`
    /// Start with the "category: reference id in the draft
    let referenceName = input.cityRef
    /// Fetch the category referenced in this document
    return sanityClient.fetch(query, {nameRef: referenceName}).then(city => {
        /// Set the referenceName to the city.name field
        referenceName = !!city ? city.name : referenceName;
        return slugify(`${referenceName} - ${input.name}`, {lower: true});
    })
}

export default {
    name: 'neighbourhood',
    title: 'Stadtteile',
    type: 'document',
    icon: FaMap,
    fields: [
        {
            name: 'slug',
            title: 'Slug',
            type: 'slug',
            options: {
                source: (doc, options) => ({name: doc.name, cityRef: doc.city._ref}),
                maxLength: 100,
                slugify: myAsyncSlugifier
            },
        },
    ],
    preview: {
        select: {
            title: 'name',
            media: 'image',
            city: 'city.name'
        },
        prepare(selection) {
            const {title, city} = selection
            return {
                title: `${city} - ${title}`,
                //subtitle: date.split('-')[0] // YYYY-MM-DD --> YYYY
            }
        }
    },
}
Jun 13, 2023, 10:29 AM
oh. Looks like you fine people have updated your slugify example. This should work then:

async function myAsyncSlugifier(input, schemaType, context) {
  const slug = slugify(input)
  const {getClient} = context
  const client = getClient({apiVersion: '2022-12-07'})
  ...
}
Jun 13, 2023, 10:52 AM
Let’s use greetings like “Hey Everyone,” “Hi, Y’all," or “Hello, Folks” to make sure everyone in the community is included. Please read the Sanity Community Code of Conduct to stay updated on expected communication & behavior in our spaces: https://www.sanity.io/docs/community-code-of-conduct
Jun 13, 2023, 10:52 AM
kudos to the writers of the migration guide... THIS IS AWESOME!
Jun 13, 2023, 10:56 AM
Glad the guide was helpful!
Jun 13, 2023, 7:52 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?