Using Sanity as middleware to edit database query results
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:
- Import external data from your database into Sanity as documents
- Editors review and curate the imported content in Studio (removing records, adding metadata, etc.)
- Your frontend queries the curated content from Sanity's Content Lake
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:
- Single query to fetch all data from Sanity's API
- Complete visibility of synced items in Studio
- Editors can add custom fields, remove items, or enhance content
- Two-way sync potential using GROQ webhooks
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-tokenYour 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 production3. Migration Scripts (Recommended for Structured Workflows)
The CLI migration tooling is ideal for this because it provides:
- Automatic batching to avoid rate limits
- Dry-run mode with visual feedback
- Document validation against your schemas
- Idempotent scripts you can run multiple times
Create a migration:
npx sanity@latest migration createEditorial Workflow Benefits
This approach gives you the best of both worlds:
- Structured data from your database
- Human curation - editors can delete unwanted records, add editorial notes, attach images, or enhance descriptions
- Single source of truth for your frontend - just query Sanity
- Content modeling flexibility - add custom fields beyond what your database provides
Keeping Data Fresh
You can trigger re-imports on:
- Build time - Run import script as part of your build process
- Cron schedule - Periodic syncs using serverless functions
- Webhooks - Trigger imports when source data changes (using Sanity Functions for event handling)
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 thread5 replies
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.