Skip to content

Commit

Permalink
[core] Skip system documents when exporting a dataset (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Aug 22, 2017
1 parent cfb38c6 commit 1080e75
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from 'path'
import fsp from 'fs-promise'
import split from 'split2'
import prettyMs from 'pretty-ms'
import streamDataset from '../../actions/dataset/streamDataset'
import skipSystemDocuments from '../../util/skipSystemDocuments'

export default {
name: 'export',
Expand Down Expand Up @@ -44,6 +46,8 @@ export default {
const startTime = Date.now()

streamDataset(client, dataset)
.pipe(split())
.pipe(skipSystemDocuments)
.pipe(outputPath ? fsp.createWriteStream(outputPath) : process.stdout)
.on('error', err => output.error(err))
.on('close', () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/@sanity/core/src/util/skipSystemDocuments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const through2 = require('through2')

const parseJson = json => {
try {
return JSON.parse(json)
} catch (err) {
return null
}
}

const isSystemDocument = doc => doc && doc._id && doc._id.indexOf('_.') === 0

module.exports = through2((line, enc, callback) => {
const doc = parseJson(line)

if (isSystemDocument(doc)) {
return callback()
}

return callback(null, `${line}\n`)
})

0 comments on commit 1080e75

Please sign in to comment.