Sanity Pioneers: Early access. Extra AI credits. A direct line to engineering

Using Sanity as middleware to edit database query results

5 repliesLast updated: Nov 29, 2025

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
AI Update

Yes, using Sanity as a "middleware" or curation layer is definitely a valid pattern! This workflow is exactly what you're describing: import data from an external database during build time, then let editors manually curate, filter, or enhance it in Sanity Studio.

How This Works

The pattern you're describing aligns with what Sanity calls a sync plugin approach. Here's the typical flow:

Implementation Options

1. Sanity Connect Pattern (Automated Sync)

Sanity Connect is the official implementation of this pattern, currently available for Shopify. It automatically syncs external data and keeps it updated. The benefits are:

2. Custom Import Scripts (Build-Time)

For your specific use case with a custom database, you'd write your own import script:

Using sanity exec - Run scripts with authentication:

sanity exec scripts/import-from-db.js --with-user-token

Your script would fetch from your database and create/update documents in Sanity using the client API.

Using NDJSON import - Generate an NDJSON file from your database query results:

sanity dataset import data.ndjson production

The CLI migration tooling is ideal for this because it provides:

Create a migration:

npx sanity@latest migration create

Editorial Workflow Benefits

This approach gives you the best of both worlds:

Keeping Data Fresh

You can trigger re-imports on:

Practical Example

Many e-commerce teams do this with product catalogs: import products from their PIM/database, then let merchandisers curate which products appear on the site, add lifestyle images, write better descriptions, or remove discontinued items—all without touching the source database.

This pattern works great when you want your database to remain the system of record for certain data (like inventory or technical specs) while Sanity becomes the editorial/presentation layer.

Show original thread
5 replies

Was this answer helpful?

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.

Related contributions