@sanity/import
Advanced tools
Comparing version 0.116.0-alpha.5bbff73d to 0.116.0-alpha.617e9488
'use strict'; | ||
module.exports = function documentHasErrors(doc) { | ||
function documentHasErrors(doc) { | ||
if (typeof doc._id !== 'undefined' && typeof doc._id !== 'string') { | ||
@@ -13,2 +13,11 @@ return `Document contained an invalid "_id" property - must be a string`; | ||
return null; | ||
}; | ||
} | ||
documentHasErrors.validate = function (doc, index) { | ||
var err = documentHasErrors(doc); | ||
if (err) { | ||
throw new Error(`Failed to parse document at index #${index}: ${err}`); | ||
} | ||
}; | ||
module.exports = documentHasErrors; |
'use strict'; | ||
var importFromStream = function () { | ||
var _ref = _asyncToGenerator(function* (stream, opts) { | ||
var options = validateOptions(stream, opts); | ||
var importDocuments = function () { | ||
var _ref = _asyncToGenerator(function* (input, opts) { | ||
var options = validateOptions(input, opts); | ||
// Get raw documents from the stream | ||
debug('Streaming input source to array of documents'); | ||
options.onProgress({ step: 'Reading/validating data file' }); | ||
var raw = yield streamToArray(stream); | ||
var isStream = typeof input.pipe === 'function'; | ||
var documents = input; | ||
if (isStream) { | ||
debug('Streaming input source to array of documents'); | ||
documents = yield streamToArray(input); | ||
} else { | ||
documents.some(documentHasErrors.validate); | ||
} | ||
// User might not have applied `_key` on array elements which are objects; | ||
// if this is the case, generate random keys to help realtime engine | ||
var keyed = raw.map(function (doc) { | ||
var keyed = documents.map(function (doc) { | ||
return assignArrayKeys(doc); | ||
@@ -58,3 +63,3 @@ }); | ||
return function importFromStream(_x, _x2) { | ||
return function importDocuments(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
@@ -77,2 +82,3 @@ }; | ||
var uploadAssets = require('./uploadAssets'); | ||
var documentHasErrors = require('./documentHasErrors'); | ||
var batchDocuments = require('./batchDocuments'); | ||
@@ -87,2 +93,2 @@ var importBatches = require('./importBatches'); | ||
module.exports = importFromStream; | ||
module.exports = importDocuments; |
@@ -9,3 +9,3 @@ 'use strict'; | ||
function validateOptions(stream, opts) { | ||
function validateOptions(input, opts) { | ||
var options = defaults({}, opts, { | ||
@@ -16,4 +16,4 @@ operation: defaultOperation, | ||
if (typeof stream.pipe !== 'function') { | ||
throw new Error('Stream does not seem to be a readable stream - no "pipe" method found'); | ||
if (!input || typeof input.pipe !== 'function' && !Array.isArray(input)) { | ||
throw new Error('Stream does not seem to be a readable stream or an array'); | ||
} | ||
@@ -20,0 +20,0 @@ |
{ | ||
"name": "@sanity/import", | ||
"version": "0.116.0-alpha.5bbff73d", | ||
"version": "0.116.0-alpha.617e9488", | ||
"description": "Import documents to a Sanity dataset", | ||
@@ -23,3 +23,3 @@ "main": "lib/import.js", | ||
"dependencies": { | ||
"@sanity/mutator": "0.116.0-alpha.5bbff73d", | ||
"@sanity/mutator": "0.116.0-alpha.617e9488", | ||
"debug": "^2.6.3", | ||
@@ -35,3 +35,3 @@ "get-uri": "^2.0.1", | ||
"devDependencies": { | ||
"@sanity/client": "0.116.0-alpha.5bbff73d", | ||
"@sanity/client": "0.116.0-alpha.617e9488", | ||
"babel-preset-env": "^1.6.0", | ||
@@ -38,0 +38,0 @@ "eslint": "^4.6.1", |
@@ -25,2 +25,3 @@ # @sanity/import | ||
// Input can either be a stream or an array of documents | ||
const input = fs.createReadStream('my-documents.ndjson') | ||
@@ -27,0 +28,0 @@ sanityImport(input, { |
@@ -1,2 +0,2 @@ | ||
module.exports = function documentHasErrors(doc) { | ||
function documentHasErrors(doc) { | ||
if (typeof doc._id !== 'undefined' && typeof doc._id !== 'string') { | ||
@@ -12,1 +12,10 @@ return `Document contained an invalid "_id" property - must be a string` | ||
} | ||
documentHasErrors.validate = (doc, index) => { | ||
const err = documentHasErrors(doc) | ||
if (err) { | ||
throw new Error(`Failed to parse document at index #${index}: ${err}`) | ||
} | ||
} | ||
module.exports = documentHasErrors |
@@ -8,2 +8,3 @@ const debug = require('debug')('sanity:import') | ||
const uploadAssets = require('./uploadAssets') | ||
const documentHasErrors = require('./documentHasErrors') | ||
const batchDocuments = require('./batchDocuments') | ||
@@ -18,13 +19,18 @@ const importBatches = require('./importBatches') | ||
async function importFromStream(stream, opts) { | ||
const options = validateOptions(stream, opts) | ||
async function importDocuments(input, opts) { | ||
const options = validateOptions(input, opts) | ||
// Get raw documents from the stream | ||
debug('Streaming input source to array of documents') | ||
options.onProgress({step: 'Reading/validating data file'}) | ||
const raw = await streamToArray(stream) | ||
const isStream = typeof input.pipe === 'function' | ||
let documents = input | ||
if (isStream) { | ||
debug('Streaming input source to array of documents') | ||
documents = await streamToArray(input) | ||
} else { | ||
documents.some(documentHasErrors.validate) | ||
} | ||
// User might not have applied `_key` on array elements which are objects; | ||
// if this is the case, generate random keys to help realtime engine | ||
const keyed = raw.map(doc => assignArrayKeys(doc)) | ||
const keyed = documents.map(doc => assignArrayKeys(doc)) | ||
@@ -67,2 +73,2 @@ // Sanity prefers to have a `_type` on every object. Make sure references | ||
module.exports = importFromStream | ||
module.exports = importDocuments |
@@ -7,3 +7,3 @@ const noop = require('lodash/noop') | ||
function validateOptions(stream, opts) { | ||
function validateOptions(input, opts) { | ||
const options = defaults({}, opts, { | ||
@@ -14,17 +14,11 @@ operation: defaultOperation, | ||
if (typeof stream.pipe !== 'function') { | ||
throw new Error( | ||
'Stream does not seem to be a readable stream - no "pipe" method found' | ||
) | ||
if (!input || (typeof input.pipe !== 'function' && !Array.isArray(input))) { | ||
throw new Error('Stream does not seem to be a readable stream or an array') | ||
} | ||
if (!options.client) { | ||
throw new Error( | ||
'`options.client` must be set to an instance of @sanity/client' | ||
) | ||
throw new Error('`options.client` must be set to an instance of @sanity/client') | ||
} | ||
const missing = clientMethods.find( | ||
key => typeof options.client[key] !== 'function' | ||
) | ||
const missing = clientMethods.find(key => typeof options.client[key] !== 'function') | ||
@@ -31,0 +25,0 @@ if (missing) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
170185
1095
62
+ Added@sanity/mutator@0.116.0-alpha.617e9488(transitive)
- Removed@sanity/mutator@0.116.0-alpha.5bbff73d(transitive)