Querying offline Sanity datasets with GROQ

Did you know you can query your .ndjson files from Sanity dataset exports with GROQ? Check this out:

Initial motivation

I wanted to explore the idea of letting users query backup data in the Baccup application, and started by exploring what the ndjson format really was. Turns out, itā€™s really simple. Itā€™s just a giant JSON array, except objects in that array are separated by a newline. ndjson stands for newline-delimited JSON.

That got me thinking, it would be trivial to do a sort of ā€œobject visualizerā€, where each object (which would have an arbitrary schema) could be represented visually and queried. I was probably thinking about my experience working with MongoDB at that point. I could then possibly create a GraphQL-like query language to query these objects and create some sort of filter function to explore the dataset.

Arenā€™t you just reinventing GROQ?

If you got here in the previous paragraph, youā€™re smarter than me (low bar). After I made that connection, I looked up GROQ and turns out, thereā€™s a specification document. TLDR, I learned that itā€™s a query language to query any JSON document (read: not just Sanity ones) and best of all, just when I thought I wanted to work on my own GROQ implementation in TypeScript, I discovered groq-js already exists.

(I told you the bar was low. )

From here, itā€™s just about creating an interface where you could select any .ndjson file and run the groq-js library against it. Check it out. Or if you want to try running GROQ queries against any JSON object input, check out groq.dev.

Insights/lightbulb moments

  • GROQ works with any JSON input, not just Sanity dataset exports. This gave me the idea of creating a minimalist CMS that runs on just a .json file, and where the query language is still GROQ.
  • With an assets-included Sanity dataset export, asset entries are included simiar to this: image@file://./images/379904115c36fd84f7b1e996188b5bccc7e9662f-150x150.jpg. This maps neatly to the assets folder in the same export.
  • Looking at .ndjson files gave me the idea of portable websites that could run completely offline with a sort of lightweight GROQ client, but keeping Sanity as the CMS/content-editing experience.
  • we could plausibly create entire ā€˜point-in-timeā€™ website snapshots with a little engineering.

Credit where itā€™s due

Sanity has released the GROQ spec and tools to work with it, so it was really easy to connect the dots and put all this together. I can only imagine the powerful tools we could create for the Sanity community. Definitely excited to experiment more in this area.