npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dagens/sanity-orm

v1.10.1

Published

Typescript ORM wrapper over the Sanity API

Downloads

5,865

Readme

Sanity ORM

A Typescript based wrapper around the Sanity API

Install

Install the package and the reflect-metadata shim:

npm install @dagens/sanity-orm reflect-metadata

reflect-metadata is required by class-transformer and must be imported once before @dagens/sanity-orm

Transformer type

To tell class transformer which types to use, outside the simple types you should use the @Type decorator. Example

npm install class-transformer

Validation

If you want to enable validation install class-validator as well.

npm install class-validator

Usage

Initialization

Initialize with the initialize helper:

import { Document, initialize, getRepository } from '@dagens/sanity-orm';

// Create a Sanity javascript client instance
const sanityClient = require('@sanity/client');
const client = sanityClient({
  projectId: process.env.SANITY_PROJECT_ID,
  dataset: process.env.SANITY_DATASET,
  token: process.env.SANITY_TOKEN,
  apiVersion: '2021-12-30',
  useCdn: false,
});

// Initialize sanity-orm
initialize(client);

Note: initialize must only be run once

Example

This example uses dotenv, which is not required, just used as an example of externally importing secrets

import 'reflect-metadata'; // Must be required at the top level
import * as dotenv from 'dotenv';
import { Type } from 'class-transformer';

import { Document, initialize, getRepository } from '@dagens/sanity-orm';

dotenv.config();

// Create a Sanity javascript client instance
const sanityClient = require('@sanity/client');
const client = sanityClient({
  projectId: process.env.SANITY_PROJECT_ID,
  dataset: process.env.SANITY_DATASET,
  token: process.env.SANITY_TOKEN,
  apiVersion: '2021-12-30',
  useCdn: false,
});

// Initialize sanity-orm
initialize(client);

// Define a model
@Document('movie')
class Movie {
  _id!: string;

  _type!: string;

  @Type(() => Date)
  _createdAt!: Date;

  title!: string;
}

// Retrieve a repository for the model
const repo = getRepository(Movie);

// Query movis and print the result
repo.query().raw('*[_type == "movie"]').find().then((models: Movie[]) => {
  models.forEach((model: Movie) => {
    console.log(model._id, model._type, model.title, model._createdAt);
  })
});

Validation

Follow the validation section under installation.

import 'reflect-metadata'; // Must be required at the top level
import * as dotenv from 'dotenv';
import { Type } from 'class-transformer';
import {
  IsString,
  IsOptional,
  Equals,
} from 'class-validator';
import { Document, initialize, getRepository } from '@dagens/sanity-orm';

dotenv.config();

// Create a Sanity javascript client instance
const sanityClient = require('@sanity/client');
const client = sanityClient({
  projectId: process.env.SANITY_PROJECT_ID,
  dataset: process.env.SANITY_DATASET,
  token: process.env.SANITY_TOKEN,
  apiVersion: '2021-12-30',
  useCdn: false,
});

// Initialize sanity-orm
initialize(client, {
  validateQueryResult: true,
  validationOptions: {
    whitelist: true,
    forbidNonWhitelisted: true,
  },
});

// Define a model
@Document('movie')
class Movie {
  @IsString()
  _id!: string;

  @Equals('movie')
  _type!: string;

  @IsOptional()
  @Type(() => Date)
  _createdAt!: Date;

  @IsString()
  title!: string;
}

// Retrieve a repository for the model
const repo = getRepository(Movie);

// Query movis and print the result
repo.query().raw('*[_type == "movie"]').find().then((models: Movie[]) => {
  models.forEach((model: Movie) => {
    console.log(model._id, model._type, model.title, model._createdAt);
  })
});

The validation rules given in initialize are: | Name | Description | |------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------| | validateCreateUpdate | Validate on insertion and update. Recommended: true | | validateQueryResult | Validate on retrieval. Useful when moving to an ORM and not being quite sure of the integrity of the data already in the system | | validationOptions | Validation options given to class-validator, refer to class-transformers readme for decorators and options |

Development

Testing

Sanity has no in memory database that makes testing easy, so a live database is used instead. Go set one up on sanity.io. Then create a .env file in the root of the repo with the following contents:

SANITY_PROJECT_ID=<your-project-id>
SANITY_DATASET=<name-of-your-dataset>
SANITY_TOKEN=<your-access-token>

Running the tests should be as easy as:

npm run test