How to create multiple objects from a single file upload in Sanity?

6 replies
Last updated: Jul 28, 2022
Hi! β˜•
I would like to load a file and generate several new objects based on its contents (depending on the size of the loaded file). I can already do this for one output object (based on
the tutorial ), but I don't know how to make it more flexible so that I can load any number of objects.
I will be very grateful for your tips
πŸ€—
AI Update

Based on your question, it sounds like you want to create multiple documents in Sanity programmatically based on the contents of a file. Here's how you can approach this flexibly:

Using the Sanity Client for Bulk Document Creation

The Sanity Client is the official JavaScript/TypeScript SDK that you'll need for creating multiple documents. Here's a flexible approach that can handle any number of documents:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  apiVersion: '2024-01-01',
  token: 'your-write-token', // Required for creating documents
  useCdn: false
})

// Example: Load and parse your file, then create documents
async function createDocumentsFromFile(fileData) {
  // Parse your file data into an array of objects
  const items = parseYourFile(fileData) // Your parsing logic here
  
  // Create a transaction for multiple documents
  const transaction = client.transaction()
  
  items.forEach(item => {
    transaction.create({
      _type: 'yourDocumentType',
      // Map your file data to document fields
      title: item.title,
      description: item.description,
      // ... other fields from your file
    })
  })
  
  // Commit all documents at once (atomic operation)
  try {
    const result = await transaction.commit()
    console.log(`Created ${result.results.length} documents`)
    return result
  } catch (error) {
    console.error('Transaction failed:', error)
    throw error
  }
}

For Large Files: Batch Processing

If your file contains many items, process them in batches to avoid rate limits:

async function bulkCreateDocuments(items) {
  const batchSize = 100 // Adjust based on your needs
  
  for (let i = 0; i < items.length; i += batchSize) {
    const batch = items.slice(i, i + batchSize)
    const transaction = client.transaction()
    
    batch.forEach(item => {
      transaction.create({
        _type: 'yourDocumentType',
        ...item
      })
    })
    
    await transaction.commit()
    console.log(`Processed batch ${Math.floor(i / batchSize) + 1}`)
  }
}

If you want this to happen server-side (which is the modern, recommended approach), use Sanity Functions:

// sanity.blueprint.ts
import {defineBlueprint} from 'sanity'

export default defineBlueprint({
  functions: [
    {
      name: 'process-file',
      handler: async (event, context) => {
        const {fileData} = event.body
        const client = context.getClient()
        
        // Parse your file
        const items = parseFile(fileData)
        
        // Create documents in batches
        const batchSize = 100
        for (let i = 0; i < items.length; i += batchSize) {
          const batch = items.slice(i, i + batchSize)
          const transaction = client.transaction()
          
          batch.forEach(item => {
            transaction.create({
              _type: 'yourType',
              ...item
            })
          })
          
          await transaction.commit()
        }
        
        return {success: true, count: items.length}
      }
    }
  ]
})

Key Points for Flexibility

  1. Transactions are atomic - all documents are created or none are (good for data integrity)
  2. Dynamic document count - the number of documents created depends entirely on your file contents
  3. Batch processing - handles large files by processing in chunks (recommended: 100-1000 per batch)
  4. Error handling - wrap in try/catch to handle failures gracefully
  5. Authentication - requires a token with write permissions

The flexibility comes from how you parse your file and map the data to your schema. Just iterate through your parsed data and add each item to the transaction. The number of output objects will automatically match the size of your input file!

Would you like help with a specific file format (CSV, JSON, Excel) or guidance on mapping your data to a particular schema?

Show original thread
6 replies
Hello
user N
can you be more specific please?
I have a text file
test.txt
that has
x
lines, for example, for
x=5
it would be:

line 1
line 2
line 3
line 4
line 5
I want to load the
test.txt
file into Sanity in my custom component, and then I want to cut it into five
string
objects that will be dropped into an
array
.
I can load in the component, read the entire file and perform operations on its contents (already done). The problem occurs when I want to write those five lines into five different new objects in the
array
. So far, I have managed to save the entire contents of the
test.txt
file to a single
string
I defined earlier.
In summary, I would like to implement a component that has two fields:
-
file
-
array

When the
test.txt
is loaded into file, the contents are split into
x
objects (
strings
), each of which is added (and displayed in Sanity) to the
array
.
If you can do it with JS (or TS) you can do it with Sanity. You can us normal .toString() and spread methods for getting your lined into and array, each of your txt lines must have some kind of line break marker (
\r\n
as a encodeURIComponent I think), which you can use as a stop when converting them into a string each.And then you should follow setting up the tutorial till the end (patches into the other field, which would be the array).
Hope that helps (The person who wrote the tutorial is on
🌴 and so I can only tell you as much), but I am sure you will get it to work! πŸ™‚
Thank you for your reply!
This
test.txt
content I can already download and processed. This problem is a step further: I don't know how to create new objects for the array in my custom component.
Uf, I managed to do it! πŸ₯³ I think you gave me a lot of motivation with the words "_If you can do it with JS (or TS) you can do it with Sanity._" For which I am very grateful to you
user J

I will share my solution later
πŸ”₯
Very happy I could help and especially motivate you!!! πŸ™‚ Yes please share your code (we could think about adding it to the exchange on our website, so others can also find help through it!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?