# @sanity/groq-store

> Stream dataset to memory for in-memory querying

Latest version **4.1.3** (published 2023-10-12) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @sanity/groq-store
pnpm add @sanity/groq-store
yarn add @sanity/groq-store
bun add @sanity/groq-store
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 4.1.3 |
| Published | 2023-10-12 |
| First published | 2020-10-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 18 |
| Dependencies | 9 |
| Unpacked size | 256.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| Author | Sanity.io |
| Maintainers | rneatherway-sanity, ricokahler, pedro-sanity, jonabc, kenjonespizza, pauloborgesf, binoy14, simen.svale, svirs, josh_sanity_io, joneidejohnsen, nina.andal, rankers, snorreeb, mattcraig, vincentquigley, stipsan, michael-sanity, rubioz, tonina, ritasdias, simeonsanity, kmelve, bjoerge, rexxars, skogsmaskin, robinpyon, mariuslundgard, sanity-io, evenw, radhe_sanity, rbotten, judofyr, obliadp, dcilke, fredcarlsen, hermanw, mikolajsanity, sgulseth, atombender |
| Keywords | sanity, memory, query, groq |

## Links

- npm: https://www.npmjs.com/package/@sanity/groq-store
- Repository: https://github.com/sanity-io/groq-store
- Homepage: https://github.com/sanity-io/groq-store#readme
- Issues: https://github.com/sanity-io/groq-store/issues
- npm.io page: https://npm.io/package/@sanity/groq-store

## Dependencies (9)

- [groq](https://npm.io/package/groq.md) ^3.14.5
- [split2](https://npm.io/package/split2.md) 4.2.0
- [groq-js](https://npm.io/package/groq-js.md) 1.3.0
- [mendoza](https://npm.io/package/mendoza.md) 3.0.3
- [simple-get](https://npm.io/package/simple-get.md) 4.0.1
- [@sanity/types](https://npm.io/package/@sanity/types.md) ^3.14.5
- [fast-deep-equal](https://npm.io/package/fast-deep-equal.md) 3.1.3
- [throttle-debounce](https://npm.io/package/throttle-debounce.md) 5.0.0
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) ^5.0.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 4.1.3 (latest) — 2023-10-12
- 5.3.10 (experimental) — 2024-01-10
- 5.2.9-pink-lizard (pink-lizard) — 2023-11-13
- 4.0.5-canary.0 (canary) — 2023-08-23
- 2.2.3-0 (alpha) — 2023-06-28
- 1.1.0-add-type-allowlist.1 (add-type-allowlist) — 2022-11-14
- 5.3.9 — 2024-01-05
- 5.3.8 — 2024-01-04
- 5.3.7 — 2023-12-19
- 5.3.6 — 2023-12-14
- 5.3.5 — 2023-12-12
- 5.3.4 — 2023-12-08
- 5.3.3 — 2023-12-07
- 5.3.2 — 2023-12-06
- 5.3.1 — 2023-12-05
- … 84 more at https://npm.io/package/@sanity/groq-store/versions

## README

# @sanity/groq-store

[![npm stat](https://img.shields.io/npm/dm/@sanity/groq-store.svg?style=flat-square)](https://npm-stat.com/charts.html?package=@sanity/groq-store)
[![npm version](https://img.shields.io/npm/v/@sanity/groq-store.svg?style=flat-square)](https://www.npmjs.com/package/@sanity/groq-store)
[![gzip size][gzip-badge]][bundlephobia]
[![size][size-badge]][bundlephobia]

In-memory GROQ store. Streams all available documents from Sanity into an in-memory database and allows you to query them there.

## Targets

- Node.js >= 14
- Modern browsers (Edge >= 14, Chrome, Safari, Firefox etc)

## Caveats

- Streams _entire_ dataset to memory, so generally not recommended for large datasets
- Needs custom event source to work with tokens in browser

## Installation

```bash
npm i @sanity/groq-store
```

## Usage

```js
import {groqStore, groq} from '@sanity/groq-store'
// import SanityEventSource from '@sanity/eventsource'

const store = groqStore({
  projectId: 'abc123',
  dataset: 'blog',

  // Keep dataset up to date with remote changes. Default: false
  listen: true,

  // "Replaces" published documents with drafts, if available.
  // Note that document IDs will not reflect draft status, currently
  overlayDrafts: true,

  // Optional token, if you want to receive drafts, or read data from private datasets
  // NOTE: Needs custom EventSource to work in browsers
  token: 'someAuthToken',

  // Optional limit on number of documents, to prevent using too much memory unexpectedly
  // Throws on the first operation (query, retrieval, subscription) if reaching this limit.
  documentLimit: 10000,

  // Optional EventSource. Necessary to authorize using token in the browser, since
  // the native window.EventSource does not accept headers.
  // EventSource: SanityEventSource,

  // Optional allow list filter for document types. You can use this to limit the amount of documents by declaring the types you want to sync. Note that since you're fetching a subset of your dataset, queries that works against your Content Lake might not work against the local groq-store.
  // You can quickly list all your types using this query: `array::unique(*[]._type)`
  includeTypes: ['post', 'page', 'product', 'sanity.imageAsset'],
})

store.query(groq`*[_type == "author"]`).then((docs) => {
  console.log(docs)
})

store.getDocument('grrm').then((grrm) => {
  console.log(grrm)
})

store.getDocuments(['grrm', 'jrrt']).then(([grrm, jrrt]) => {
  console.log(grrm, jrrt)
})

const sub = store.subscribe(
  groq`*[_type == $type][] {name}`, // Query
  {type: 'author'}, // Params
  (err, result) => {
    if (err) {
      console.error('Oh no, an error:', err)
      return
    }

    console.log('Result:', result)
  },
)

// Later, to close subscription:
sub.unsubscribe()

// Later, to close listener:
store.close()
```

## License

MIT © [Sanity.io](https://www.sanity.io/)

## Release new version

Run ["CI & Release" workflow](https://github.com/sanity-io/groq-store/actions).
Make sure to select the main branch and check "Release new version".

Version will be automatically bumped based on [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) since the last release.

Semantic release will only release on configured branches, so it is safe to run release on any branch.

Note: commits with `chore:` will be ignored. If you want updated dependencies to trigger
a new version, use `fix(deps):` instead.

[gzip-badge]: https://img.shields.io/bundlephobia/minzip/@sanity/groq-store?label=gzip%20size&style=flat-square
[size-badge]: https://img.shields.io/bundlephobia/min/@sanity/groq-store?label=size&style=flat-square
[bundlephobia]: https://bundlephobia.com/package/@sanity/groq-store

---
_Source: https://npm.io/package/@sanity/groq-store · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
