Issue with migrating from v2 to v3 schema.js file format

4 replies
Last updated: Mar 13, 2023
Hi, We're trying to migrate from v2 to v3. Originally in v2 we imported schemas into our schema.js file as below:

import * as theseDocuments from './documents'
import * as thesePlugs from './plugs'

const allDocuments = Object.values(theseDocuments).map(document => {
  return { ...document, fields: document.fields }
})

const allPlugs = Object.values(thesePlugs).map(plug => {
  return { ...plug, fields: plug.fields }
})

export default createSchema({
  name: 'default',
  types: schemaTypes
    .concat(allDocuments)
	 .concat(allPlugs)
})
This no longer appears to be working when upgrading to v3 and throws multiple errors regarding missing documents and plug types.

Switching to a standard as below fixes the issue:



import * as DocumentsA from './documents/docA'
import * as DocumentsA from './documents/docA'
import * as PlugsA from './plugs/plugA'
import * as PlugsB from './plugs/plugB'


export default [
	DocumentsA,
	DocumentsA,
	PlugA,
	PlugsB,
]
This isn't ideal as some of our studios are huge and we like to seperate our schemas as before, has something changed in v3 that no longer allows us to declare our schema.js file this way?
Mar 10, 2023, 4:28 PM
What implementation did you try in V3?
Mar 10, 2023, 7:12 PM
We originally tried to lift and shift directly into v3 but none of the schemas (documents, objects) we'd defined in our schema.js were being recognized as types when running the studio (this was declared as per the first code snippet in my original post) with all of the seperate schemas being declared in index.js for that folder as a list of exports like below:
export {default as banner} from './banner'
... another export
... another export

Those specific folders were then pulled together in the main schema.js file.

Now, It seems the only way to get Sanity to recognize the types is to declare everything inside the main schema js file (as per second code snippet) which isn't as ideal. Juist wondered if some syntax had changed that was causing the previous implementation to no longer work.
Mar 11, 2023, 12:45 PM
I realised my mistake, I was able to get it working with the following code:

import * as documents from './documents'
import * as objects from './objects'

const allDocuments = Object.values(documents).map(document => {
  return { ...document, fields: document.fields }
})

const allObjects = Object.values(objects).map(object => {
    return { ...object, fields: object.fields }
  })

export const schemaTypes = [allDocuments, allObjects].flat()
Mar 13, 2023, 9:49 AM
Glad you got it working!
Mar 13, 2023, 2:57 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?