All Projects → danielroe → Sanity Typed Queries

danielroe / Sanity Typed Queries

Licence: mit
A typed, zero-dependency schema generator and query builder for Sanity.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Sanity Typed Queries

Anglesharp
👼 The ultimate angle brackets parser library parsing HTML5, MathML, SVG and CSS to construct a DOM based on the official W3C specifications.
Stars: ✭ 4,018 (+7340.74%)
Mutual labels:  hacktoberfest, library
Ngx Ui
🚀 Style and Component Library for Angular
Stars: ✭ 534 (+888.89%)
Mutual labels:  hacktoberfest, library
Virtualenv
Virtual Python Environment builder
Stars: ✭ 4,017 (+7338.89%)
Mutual labels:  hacktoberfest, library
Gerador Validador Cpf
Biblioteca JS open-source para gerar e validar CPF.
Stars: ✭ 312 (+477.78%)
Mutual labels:  hacktoberfest, library
Whatpulse
WhatPulse reverse engineered
Stars: ✭ 30 (-44.44%)
Mutual labels:  hacktoberfest, library
Grabana
User-friendly Go library for building Grafana dashboards
Stars: ✭ 313 (+479.63%)
Mutual labels:  hacktoberfest, library
Telethon
Pure Python 3 MTProto API Telegram client library, for bots too!
Stars: ✭ 5,805 (+10650%)
Mutual labels:  hacktoberfest, library
Rot.js
ROguelike Toolkit in JavaScript. Cool dungeon-related stuff, interactive manual, documentation, tests!
Stars: ✭ 2,002 (+3607.41%)
Mutual labels:  hacktoberfest, library
Xtoolkit.whitelabel
Modular MVVM framework for fast creating powerful cross-platform applications with Xamarin.
Stars: ✭ 22 (-59.26%)
Mutual labels:  hacktoberfest, library
Lambdacd
a library to define a continuous delivery pipeline in code
Stars: ✭ 655 (+1112.96%)
Mutual labels:  hacktoberfest, library
Ngx Smart Modal
Modal/Dialog component crafted for Angular
Stars: ✭ 256 (+374.07%)
Mutual labels:  hacktoberfest, library
Siler
⚡ Flat-files and plain-old PHP functions rockin'on as a set of general purpose high-level abstractions.
Stars: ✭ 1,056 (+1855.56%)
Mutual labels:  hacktoberfest, library
Cosmos
Hacktoberfest 2021 | World's largest Contributor driven code dataset | Algorithms that run our universe | Your personal library of every algorithm and data structure code that you will ever encounter |
Stars: ✭ 12,936 (+23855.56%)
Mutual labels:  hacktoberfest, library
Kiimagepager
The KIImagePager is inspired by foursquare's ImageSlideshow, the user may scroll through images loaded from the Web
Stars: ✭ 324 (+500%)
Mutual labels:  hacktoberfest, library
Json Api
Implementation of JSON API in PHP 7
Stars: ✭ 171 (+216.67%)
Mutual labels:  hacktoberfest, library
Ferret
Declarative web scraping
Stars: ✭ 4,837 (+8857.41%)
Mutual labels:  hacktoberfest, library
Hledger
A reliable, user-friendly Plain Text Accounting tool with command line, terminal and web interfaces.
Stars: ✭ 1,887 (+3394.44%)
Mutual labels:  hacktoberfest, library
Pipedrive
Complete Pipedrive API client for PHP
Stars: ✭ 138 (+155.56%)
Mutual labels:  hacktoberfest, library
Leku
🌍 Map location picker component for Android. Based on Google Maps. An alternative to Google Place Picker.
Stars: ✭ 612 (+1033.33%)
Mutual labels:  hacktoberfest, library
Mod Pbxproj
A python module to manipulate XCode projects
Stars: ✭ 959 (+1675.93%)
Mutual labels:  hacktoberfest, library

🔤 sanity-typed-queries

A typed query generator for Sanity

A zero-dependency schema generator and query builder that is fully-typed and works in JavaScript and TypeScript.

Features

  • 📚 Documentation: Sanity documentation appears as you type.
  • 💪 TypeScript: Written in TypeScript.

Progress

  • [x] Fully typed schema builder
  • [x] Query builder (working with string, boolean, number), ordering, projections
  • [x] Inferred type of arrays
  • [x] Support for object types with subfields
  • [x] Custom mappings ("prop": my.prop)
  • [x] Resolving image and file types
  • [x] Resolving custom object/document types
  • [ ] Distinguish object/document types within valid field types
  • [ ] Additional query filters
  • [ ] Querying multiple types of document

Help and contributions are welcome.

Quick Start

First install sanity-typed-queries:

yarn add sanity-typed-queries

# or npm

npm install sanity-typed-queries --save

Schema definition

Now you will need to generate your Sanity schema documents using the schema builder. You will get documentation as you type, and enforced compliance with Sanity's schema builder, such as being able to see validation rules applicable to the type of field you are creating, and so on.

schema/author.js:

import { defineDocument } from 'sanity-typed-queries'

const { document } = defineDocument('author', {
  name: {
    type: 'string',
    validation: Rule => Rule.required(),
  },
  biography: {
    type: 'text',
    rows: 4,
  },
  yearOfBirth: {
    type: 'number',
  },
})

export default document

This is equivalent to defining the following schema:

export default {
  name: 'author',
  title: 'Author',
  type: 'document',
  fields: [
    {
      name: 'name',
      title: 'Name',
      type: 'string',
      validation: Rule => Rule.required(),
    },
    {
      name: 'biography',
      title: 'Biography',
      type: 'text',
      rows: 4,
    },
    {
      name: 'yearOfBirth',
      title: 'Year Of Birth',
      type: 'number',
    },
  ],
}

Query builder

For more documentation, see this GROQ/query builder cheat sheet.

You can also export a query builder from the same file.

import { defineDocument } from 'sanity-typed-queries'

const { document, builder } = defineDocument('author', {
  ...
})

// Export your query builder for use elsewhere
export { builder }
export default document

You can use this builder elsewhere to generate the appropriate types and GROQ queries. For example:

import { builder as authorBuilder } from './cms/schema/author.js'

const [query, type] = authorBuilder.pick('name').first().use()

// *[_type == 'author'][0].name
const queryString = query

// string
type AuthorName = typeof type

If you're using the Sanity client, you might use it like this:

import sanityClient from '@sanity/client'
import { author } from './cms/schema/author.js'

const [query, type] = author.pick('name').first().use()

const client = sanityClient(config)
// Promise<string>
const result = client.fetch<typeof type>(query)

Custom types

You can export utility objects or documents for reference within other schemas.

schema/tag.js:

import { defineObject } from 'sanity-typed-queries'

const { tag, object } = defineObject('tag', {
  ...
})

export { tag }
export default object

Then you can pass that when defining documents that reference it.

schema/author.js:

import { defineObject } from 'sanity-typed-queries'
import { tag } from './tag'

const { builder, document } = defineDocument(
  'author',
  {
    tag: {
      type: 'tag',
    },
  },
  [tag]
)

export default document

Inspirations

Projects I've found helpful are:

Contributors

This has been developed to suit my needs but additional use cases and contributions are very welcome.

License

MIT License - Copyright © Daniel Roe

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].