What is @sanity/export?
@sanity/export is a utility for exporting data from a Sanity.io dataset. It allows you to export documents, assets, and other data from your Sanity project into a JSON file, which can be useful for backups, migrations, or transferring data between projects.
What are @sanity/export's main functionalities?
Exporting all documents
This feature allows you to export all documents from a specified dataset in your Sanity project. The exported data is saved to a JSON file.
const sanityClient = require('@sanity/client');
const exportDataset = require('@sanity/export');
const client = sanityClient({
projectId: 'your-project-id',
dataset: 'your-dataset-name',
token: 'your-auth-token',
useCdn: false
});
const options = {
outputPath: './exported-data.json',
dataset: 'your-dataset-name',
client: client
};
exportDataset(options).then(() => {
console.log('Export complete!');
}).catch((err) => {
console.error('Export failed:', err);
});
Exporting specific document types
This feature allows you to export only specific types of documents from your dataset. In this example, only documents of type 'post' and 'author' are exported.
const sanityClient = require('@sanity/client');
const exportDataset = require('@sanity/export');
const client = sanityClient({
projectId: 'your-project-id',
dataset: 'your-dataset-name',
token: 'your-auth-token',
useCdn: false
});
const options = {
outputPath: './exported-data.json',
dataset: 'your-dataset-name',
client: client,
types: ['post', 'author']
};
exportDataset(options).then(() => {
console.log('Export complete!');
}).catch((err) => {
console.error('Export failed:', err);
});
Exporting with a query
This feature allows you to export documents based on a specific query. In this example, only published posts are exported.
const sanityClient = require('@sanity/client');
const exportDataset = require('@sanity/export');
const client = sanityClient({
projectId: 'your-project-id',
dataset: 'your-dataset-name',
token: 'your-auth-token',
useCdn: false
});
const options = {
outputPath: './exported-data.json',
dataset: 'your-dataset-name',
client: client,
query: '*[_type == "post" && published == true]'
};
exportDataset(options).then(() => {
console.log('Export complete!');
}).catch((err) => {
console.error('Export failed:', err);
});
Other packages similar to @sanity/export
contentful-export
contentful-export is a utility for exporting data from a Contentful space. It allows you to export content types, entries, assets, and locales into a JSON file. Similar to @sanity/export, it is used for backups, migrations, and data transfers. However, it is specific to Contentful, whereas @sanity/export is specific to Sanity.io.
strapi-plugin-import-export-content
strapi-plugin-import-export-content is a plugin for Strapi that allows you to import and export content types, entries, and assets. It provides similar functionality to @sanity/export but is designed for use with Strapi, a different headless CMS.
@sanity/export
Exports documents and assets from a Sanity dataset
Installing
npm install --save @sanity/export
Usage
const exportDataset = require('@sanity/export')
exportDataset({
client: someInstantiatedSanityClientInstance,
dataset: 'myDataset',
outputPath: '/home/your-user/myDataset.tar.gz',
assets: false,
raw: true,
drafts: true,
types: ['products', 'shops'],
assetConcurrency: 12,
})
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.