Giter VIP home page Giter VIP logo

groq-store's Introduction

@sanity/groq-store

npm stat npm version gzip size size

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

npm i @sanity/groq-store

Usage

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

Release new version

Run "CI & Release" workflow. Make sure to select the main branch and check "Release new version".

Version will be automatically bumped based on conventional commits 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.

groq-store's People

Contributors

chris-erickson avatar jfulse avatar judofyr avatar kmelve avatar renovate[bot] avatar rexxars avatar runeb avatar semantic-release-bot avatar snorrees avatar stipsan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

groq-store's Issues

Suggestion: store.subscribe should throw if syntax is invalid

Referencing sanity-io/groq-js#29

Was fighting a bug for a few hours that was caused by groq-store swallowing an error caused by passing an invalid query to groq-js's parse() function. This invalid query is likely a bug in groq-js because the query works in every other sanity-io tool.

When groq-store is consumed by another library, the error thrown by groq-js is not logged in the console, so it was very difficult to trace it to this.

Therefore, I think the store.subscribe() function should check to see if the query is valid, because otherwise the subscription would appear as though it is broken / not receiving any updates.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.


Using a curated preset maintained by


Sanity: The Composable Content Cloud

Pending Approval

These branches will be created by Renovate only once you click their checkbox below.

  • chore(lockfile): Update dependency @sanity/eventsource to v5.0.2
  • chore(deps): update dependency @sanity/client to ^6.18.2
  • chore(deps): update dependency @sanity/pkg-utils to ^3.3.8
  • chore(deps): update dependency prettier-plugin-packagejson to ^2.5.0
  • chore(deps): update dependency typescript to ^5.4.5
  • chore(lockfile): Update sanity monorepo to v3.42.1 (@sanity/types, groq)
  • fix(deps): Update dependency groq-js to v1.9.0
  • chore(deps): update dependency @sanity/pkg-utils to v4
  • chore(deps): update dependency @sanity/pkg-utils to v6
  • chore(deps): lock file maintenance
  • ๐Ÿ” Create all pending approval PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/main.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
npm
example/package.json
  • parcel-bundler ^1.12.5
package.json
  • @sanity/eventsource ^5.0.0
  • @sanity/types ^3.14.5
  • fast-deep-equal 3.1.3
  • groq ^3.14.5
  • groq-js 1.4.3
  • mendoza 3.0.5
  • simple-get 4.0.1
  • throttle-debounce 5.0.0
  • @commitlint/cli ^19.2.0
  • @commitlint/config-conventional ^19.1.0
  • @sanity/client ^6.15.5
  • @sanity/pkg-utils ^3.0.0
  • @sanity/semantic-release-preset ^4.1.7
  • @types/split2 ^4.2.3
  • @types/throttle-debounce ^5.0.2
  • @typescript-eslint/eslint-plugin ^6.7.5
  • @typescript-eslint/parser ^6.7.5
  • @vitest/coverage-v8 ^0.34.6
  • eslint ^8.51.0
  • eslint-config-prettier ^9.0.0
  • eslint-config-sanity ^7.0.1
  • eslint-plugin-prettier ^5.0.1
  • eslint-plugin-react ^7.33.2
  • eslint-plugin-simple-import-sort ^12.0.0
  • ls-engines ^0.9.1
  • prettier ^3.0.3
  • prettier-plugin-packagejson ^2.4.12
  • rimraf ^5.0.0
  • typescript ^5.2.2
  • vitest ^0.34.6
  • vitest-github-actions-reporter ^0.11.1
  • node >= 18

  • Check this box to trigger a request for Renovate to run again on this repository

Crashes when listen config is `false`

Repro

const { groqStore } = require('@sanity/groq-store')

const store = groqStore({
    projectId: '27lgnhnz',
    dataset: 'workflow',
    listen: false, // true does not produce error
    overlayDrafts: true
})

const run = async () => {
    const docs = await store.query(`*[].title`)
    console.log(docs)
    store.close()
}

run()

Running the above

โžœ  repro npm ls
[email protected] /Users/runeb/tmp/repro
โ””โ”€โ”€ @sanity/[email protected]
โžœ  repro node -v
v16.13.1
โžœ  repro node query.js
/Users/runeb/tmp/repro/node_modules/@sanity/groq-store/dist/node/groq-store.js:219
    stagedDocs = undefined;
               ^

ReferenceError: Cannot access 'stagedDocs' before initialization
    at onUpdate (/Users/runeb/tmp/repro/node_modules/@sanity/groq-store/dist/node/groq-store.js:219:16)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async loadDataset (/Users/runeb/tmp/repro/node_modules/@sanity/groq-store/dist/node/groq-store.js:333:5)
    at async Object.query (/Users/runeb/tmp/repro/node_modules/@sanity/groq-store/dist/node/groq-store.js:337:5)
    at async run (/Users/runeb/tmp/repro/query.js:11:18)

changing the listen config to true avoid the error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.