Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@sanity/export

sanity-io478.1kMIT3.42.2

Export Sanity documents and assets

sanity, cms, headless, realtime, content, export, ndjson

readme

@sanity/export

Exports documents and assets from a Sanity dataset

Installing

npm install --save @sanity/export

Usage

const exportDataset = require('@sanity/export')

exportDataset({
  // Instance of @sanity/client configured to correct project ID and dataset
  client: someInstantiatedSanityClientInstance,

  // Name of dataset to export
  dataset: 'myDataset',

  // Path to write tar.gz-archive file to, or `-` for stdout
  outputPath: '/home/your-user/myDataset.tar.gz',

  // Whether or not to export assets. Note that this operation is currently slightly lossy;
  // metadata stored on the asset document itself (original filename, for instance) might be lost
  // Default: `true`
  assets: false,

  // Exports documents only, without downloading or rewriting asset references
  // Default: `false`
  raw: true,

  // Whether or not to export drafts
  // Default: `true`
  drafts: true,

  // Export only given document types (`_type`)
  // Optional, default: all types
  types: ['products', 'shops'],

  // Run 12 concurrent asset downloads
  assetConcurrency: 12,

  // What mode to use when exporting documents, can be eiter `stream`(default) or `cursor`.
  // Cursor mode might help when dealing with large datasets, but might yield inconsistent results if the dataset is mutated during export.
  // Default: 'stream'
  mode: 'stream',
})

Future improvements

  • Restore original filenames, keep track of duplicates, increase counter (filename (<num>).ext)
  • Skip archiving on raw/no-asset mode?

CLI-tool

This functionality is built in to the @sanity/cli package as sanity dataset export

License

MIT-licensed. See LICENSE.

changelog

📓 Changelog

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

3.42.2 (2025-01-03)

Bug Fixes

  • retain falsy string and int array elements during export (#20) (ffb04f2)

3.42.1 (2025-01-02)

Bug Fixes

3.42.0 (2024-12-19)

Features

  • also ignore 401 and 403 when downloading assets. (8f5df3d)
  • debug logging of archiver events. (f226bea)
  • ignore 404 errors when downloading assets. (5f2790c)

3.41.2 (2024-12-19)

Bug Fixes

  • fixes stringifying of large asset maps by streaming the JSON to file. (f8e24c9)

3.41.1 (2024-12-11)

Bug Fixes

3.41.0 (2024-07-22)

Features

  • add more debug logs to cursor stream (c4ab854)
  • add status code logging to export with cursor (0d1f341)

Bug Fixes

  • cursor: dont parse empty strings (dd20c28)
  • handle multiple json docs inside one chunk when streaming with cursor (b17e562)
  • log failed chunk (11e78c7)

3.40.0 (2024-07-02)

Features

  • add support for exporting documents with "inconsistent" cursor (4294be0)

3.39.0 (2024-06-24)

Features

  • add option for tweaking readTimeout (03db723)

3.38.2 (2024-06-21)

Bug Fixes

  • check for document attributes to prevent throwing on error like document (d7ffa00)

3.38.1 (2024-05-22)

Bug Fixes

3.38.0 (2024-05-08)

Features

  • make max retries configurable (e02fa3a)
  • resolve with result object (9ba6d27)

Bug Fixes

  • asset-handler: do not clear pending task queue on a transient failure, today this is causing missing assets in final export when a transient failure is encountered (593e496)
  • catch errors during entire lifecycle (e981a92)
  • correct error message for asset stream errors (927c958)
  • do not retry client errors on asset downloading (6e8cd12)
  • export: remove promise rejection handling since we are handling it in pipeline (9b49a32)
  • export: throw when export pipeline encounters an error, today it is silently failing and hanging if any of the pipeline step fails (173a672)
  • improve error messages from API errors (3b601fe)
  • improve error messages from request errors (6aeeb9e)
  • improved debug message on errors (253229e)
  • tests: remove dependancy on canvas to create images and depend on static image (1ba1a36)
  • validate number flags (878b945)

3.37.4 (2024-04-23)

Bug Fixes

  • relay error messages for better visibility (#1) (fdac3ab)