👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Using Sanity as middleware for running queries on a database during build time and loading results into Sanity for manual edits.

5 replies
Last updated: Mar 29, 2022
I'm wondering if anybody uses Sanity as middleware, run some queries on a database during build time and then load results into sanity, so that users can make some manual edits like removing a few records of the results
Mar 28, 2022, 4:08 PM
At present, it may make more sense to use the studio for more frequent user interactions. Calls to your external data can be built into studio forms. And it may be less admin intensive to manage users on the sanity side. Additional users and API requests can be added to all plans as well if needed.
Mar 28, 2022, 6:29 PM
Thanks User! I'm just getting started with learning how to create a schema. I am interested in learning how to pull in external content to studio forms, but can't find what to look for in the docs
Mar 28, 2022, 7:19 PM
This one may be easier than it seems. Try using initial value and nest the result from a custom component. In that component, put your external data query - then you can play with passing props into that function in your initial value.
If you want to calculate the value from external sources, try the default
slug type with a similar query component input - and then you can create a new document -> enter a couple fields -> and then use
doc.field
to calculate the external query prop from one of your document fields - all this without having to create a custom input from scratch.
Mar 28, 2022, 10:30 PM
I’m not sure if onChange would work nicely with the default inputs. If you want to try it could be worth a shot, but if you’d like the query to update as you edit a field this may require a custom input component. I’d still try the built-in onSubmit within the default slug type first as proof of concept and to test the external query in the studio.
Mar 28, 2022, 10:32 PM
Thanks for all the info User! This is good news. My use case is for things like pre populating lists of items and allow users to delete certain items in studio, or let users enter in an item id and the rest of the data could be fetched from the db
Mar 29, 2022, 1:32 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?