Skip to content

Commit

Permalink
[export] Add support for filtering on drafts and types
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Feb 16, 2018
1 parent dbc0bba commit 1d452dd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
11 changes: 10 additions & 1 deletion packages/@sanity/export/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ exportDataset({
assets: false,

// Exports documents only, without downloading or rewriting asset references
raw: true
// 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']
})
```

Expand Down
8 changes: 6 additions & 2 deletions packages/@sanity/export/src/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const stringifyStream = require('./stringifyStream')
const validateOptions = require('./validateOptions')
const rejectOnApiError = require('./rejectOnApiError')
const getDocumentsStream = require('./getDocumentsStream')
const skipSystemDocuments = require('./skipSystemDocuments')
const filterSystemDocuments = require('./filterSystemDocuments')
const filterDocumentTypes = require('./filterDocumentTypes')
const filterDrafts = require('./filterDrafts')

const noop = () => null

Expand Down Expand Up @@ -57,8 +59,10 @@ function exportDataset(opts) {
inputStream,
split(JSON.parse),
rejectOnApiError,
skipSystemDocuments,
filterSystemDocuments,
assetStreamHandler,
filterDocumentTypes(options.types),
options.drafts ? miss.through.obj() : filterDrafts,
stringifyStream
)

Expand Down
14 changes: 14 additions & 0 deletions packages/@sanity/export/src/filterDocumentTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const miss = require('mississippi')

module.exports = allowedTypes =>
allowedTypes
? miss.through.obj((doc, enc, callback) => {
const type = doc && doc._type
if (allowedTypes.includes(type)) {
callback(null, doc)
return
}

callback()
})
: miss.through.obj()
11 changes: 11 additions & 0 deletions packages/@sanity/export/src/filterDrafts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const miss = require('mississippi')

const isDraft = doc => doc && doc._id && doc._id.indexOf('drafts.') === 0

module.exports = miss.through.obj((doc, enc, callback) => {
if (isDraft(doc)) {
return callback()
}

return callback(null, doc)
})
3 changes: 2 additions & 1 deletion packages/@sanity/export/src/validateOptions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const defaults = require('lodash/defaults')

const clientMethods = ['getUrl', 'config']
const booleanFlags = ['assets', 'raw', 'compress']
const booleanFlags = ['assets', 'raw', 'compress', 'drafts']
const exportDefaults = {
compress: true,
drafts: true,
assets: true,
raw: false
}
Expand Down

0 comments on commit 1d452dd

Please sign in to comment.