csv-to-pouch
Advanced tools
Comparing version 2.0.3 to 3.0.0
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
export default function parseCSVFile<T>( | ||
function parseCSVFile<T>( | ||
db: PouchDB.Database<T>, | ||
@@ -11,1 +11,4 @@ input: NodeJS.ReadableStream, | ||
): Promise<void> | ||
export default parseCSVFile | ||
export { parseCSVFile }; |
@@ -217,4 +217,5 @@ import parse from 'csv-parse'; | ||
* Creates a Writable stream that saves data into a PouchDB database. | ||
* @param {PouchDB} db | ||
* @param {Object} [pouchOpts] | ||
* @param {PouchDB.Database} db - database to save to | ||
* @param {object} [pouchOpts] - options passed to PouchDB's put or bulkDocs | ||
* function | ||
* @returns {stream.Writable} | ||
@@ -227,2 +228,4 @@ */ | ||
async write(chunk) { | ||
// Data should be in object format. Convert buffers and strings | ||
// by parsing the JSON they should represent | ||
let data = chunk; | ||
@@ -233,10 +236,18 @@ if (Buffer.isBuffer(data)) data = data.toString(); | ||
if (Array.isArray(data)) { | ||
const noRevs = data.filter(doc => !doc._rev) | ||
// Find all objects in the array without a `_rev` field | ||
const noRevs = data | ||
.filter(doc => !doc._rev) | ||
.reduce((map, doc) => map.set(doc._id, doc), new Map()); | ||
if (noRevs.size > 0) { | ||
// If there are objects without a `_rev` key, | ||
// check if they already exist in the database. | ||
// If so, add the existing rev property to the doc. | ||
const existing = await db.allDocs({ keys: [...noRevs.keys()] }); | ||
existing.rows.filter(row => !row.error) | ||
existing.rows | ||
.filter(row => !row.error) | ||
.forEach((row) => { noRevs.get(row.id)._rev = row.value.rev; }); | ||
} | ||
// Save the results in the database | ||
return db.bulkDocs(data, pouchOpts); | ||
@@ -246,5 +257,8 @@ } else { | ||
try { | ||
// If there is no `_rev` key, check if the doc already exists | ||
const doc = await db.get(data._id, { latest: true }); | ||
// Save the rev from the database onto the object if the doc exists | ||
data._rev = doc._rev; | ||
} catch (err) { | ||
// If the doc doesn't already exist, just keep going | ||
if (err.status !== 404) throw err; | ||
@@ -260,5 +274,12 @@ } | ||
/** | ||
* @param {PouchDB} db | ||
* @param {stream.Readable} input | ||
* @param {function} [transformer] | ||
* Parse CSV data from the input stream and save it to the PouchDB database. | ||
* Each row is saved as a seperate PouchDB document. If the `_id` prexists in | ||
* the database, the existing document will be updated with the new version | ||
* and the revision version will change. | ||
* @param {PouchDB.Database} db Database to save results to. | ||
* @param {stream.Readable} input Stream representing the CSV file, | ||
* such as `fs.createReadableStream('data.csv')` | ||
* @param {function} [transformer] Optional function to transform CSV rows. | ||
* Input `row` represents the CSV data, and the returned object will be used as | ||
* a PouchDB document. | ||
* @returns {Promise<void>} resolves when db has been written to | ||
@@ -284,3 +305,3 @@ */ | ||
export default parseCSVFile; | ||
export { parseCSVFile };export default parseCSVFile; | ||
//# sourceMappingURL=index.es.js.map |
40
index.js
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
@@ -222,4 +224,5 @@ | ||
* Creates a Writable stream that saves data into a PouchDB database. | ||
* @param {PouchDB} db | ||
* @param {Object} [pouchOpts] | ||
* @param {PouchDB.Database} db - database to save to | ||
* @param {object} [pouchOpts] - options passed to PouchDB's put or bulkDocs | ||
* function | ||
* @returns {stream.Writable} | ||
@@ -232,2 +235,4 @@ */ | ||
async write(chunk) { | ||
// Data should be in object format. Convert buffers and strings | ||
// by parsing the JSON they should represent | ||
let data = chunk; | ||
@@ -238,10 +243,18 @@ if (Buffer.isBuffer(data)) data = data.toString(); | ||
if (Array.isArray(data)) { | ||
const noRevs = data.filter(doc => !doc._rev) | ||
// Find all objects in the array without a `_rev` field | ||
const noRevs = data | ||
.filter(doc => !doc._rev) | ||
.reduce((map, doc) => map.set(doc._id, doc), new Map()); | ||
if (noRevs.size > 0) { | ||
// If there are objects without a `_rev` key, | ||
// check if they already exist in the database. | ||
// If so, add the existing rev property to the doc. | ||
const existing = await db.allDocs({ keys: [...noRevs.keys()] }); | ||
existing.rows.filter(row => !row.error) | ||
existing.rows | ||
.filter(row => !row.error) | ||
.forEach((row) => { noRevs.get(row.id)._rev = row.value.rev; }); | ||
} | ||
// Save the results in the database | ||
return db.bulkDocs(data, pouchOpts); | ||
@@ -251,5 +264,8 @@ } else { | ||
try { | ||
// If there is no `_rev` key, check if the doc already exists | ||
const doc = await db.get(data._id, { latest: true }); | ||
// Save the rev from the database onto the object if the doc exists | ||
data._rev = doc._rev; | ||
} catch (err) { | ||
// If the doc doesn't already exist, just keep going | ||
if (err.status !== 404) throw err; | ||
@@ -265,5 +281,12 @@ } | ||
/** | ||
* @param {PouchDB} db | ||
* @param {stream.Readable} input | ||
* @param {function} [transformer] | ||
* Parse CSV data from the input stream and save it to the PouchDB database. | ||
* Each row is saved as a seperate PouchDB document. If the `_id` prexists in | ||
* the database, the existing document will be updated with the new version | ||
* and the revision version will change. | ||
* @param {PouchDB.Database} db Database to save results to. | ||
* @param {stream.Readable} input Stream representing the CSV file, | ||
* such as `fs.createReadableStream('data.csv')` | ||
* @param {function} [transformer] Optional function to transform CSV rows. | ||
* Input `row` represents the CSV data, and the returned object will be used as | ||
* a PouchDB document. | ||
* @returns {Promise<void>} resolves when db has been written to | ||
@@ -289,3 +312,4 @@ */ | ||
module.exports = parseCSVFile; | ||
exports['default'] = parseCSVFile; | ||
exports.parseCSVFile = parseCSVFile; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "csv-to-pouch", | ||
"version": "2.0.3", | ||
"version": "3.0.0", | ||
"description": "Parse CSV files and save their data to a PouchDB database.", | ||
"main": "index.js", | ||
"module": "index.es.js", | ||
"license": "MIT", | ||
"repository": "NotWoods/csv-to-pouch", | ||
"bin": { | ||
@@ -7,0 +10,0 @@ "csv-to-pouch": "./cmd.js" |
@@ -12,2 +12,6 @@ # csv-to-pouch | ||
``` | ||
Parse CSV data from the input stream and save it to the PouchDB database. | ||
Each row is saved as a seperate PouchDB document. If the `_id` prexists in | ||
the database, the existing document will be updated with the new version | ||
and the revision version will change. | ||
@@ -14,0 +18,0 @@ - **db**: Database to save results to. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
63634
9
0
645
41