Comparing version 0.8.0 to 2.0.0-alpha.1
'use strict'; | ||
const { promisify } = require('util'); | ||
const csvParse = require('csv-parse'); | ||
@@ -9,3 +11,3 @@ const { ImplementationError } = require('flora-errors'); | ||
return (row) => { | ||
return row => { | ||
let matches = false; | ||
@@ -17,8 +19,8 @@ for (let i = 0; i < requestFilter.length; i++) { | ||
switch (filter.operator) { | ||
case 'equal': | ||
// eslint-disable-next-line eqeqeq | ||
if (row[filter.attribute] != filter.value) matches = false; | ||
break; | ||
default: | ||
throw new ImplementationError('DataSource "flora-csv" supports only "equal" filter-operator'); | ||
case 'equal': | ||
// eslint-disable-next-line eqeqeq | ||
if (row[filter.attribute] != filter.value) matches = false; | ||
break; | ||
default: | ||
throw new ImplementationError('DataSource "flora-csv" supports only "equal" filter-operator'); | ||
} | ||
@@ -32,2 +34,4 @@ } | ||
const csvParsePromise = promisify(csvParse); | ||
class DataSource { | ||
@@ -54,10 +58,9 @@ /** | ||
/** | ||
* @param {Object} dsConfig DataSource config object | ||
* @param {Object} [dsConfig] DataSource config object | ||
*/ | ||
prepare(dsConfig) { | ||
dsConfig = dsConfig || {}; | ||
prepare(dsConfig = {}) { | ||
this._queries[++this._queryIndex] = dsConfig.data; | ||
dsConfig._queryIndex = this._queryIndex; | ||
['delimiter', 'quote', 'escape', 'comment'].forEach((opt) => { | ||
['delimiter', 'quote', 'escape', 'comment'].forEach(opt => { | ||
if (dsConfig[opt]) this._parserOptions[opt] = dsConfig[opt]; | ||
@@ -69,63 +72,46 @@ }); | ||
* @param {Object} request | ||
* @param {Function} callback | ||
* @return {Promise<Object>} | ||
*/ | ||
process(request, callback) { | ||
async process(request) { | ||
if (request.order) { | ||
return callback(new ImplementationError('DataSource "flora-csv" does not support "order"')); | ||
throw new ImplementationError('DataSource "flora-csv" does not support "order"'); | ||
} | ||
const proceed = () => { | ||
let result; | ||
if (!this._parsed[request._queryIndex]) { | ||
this._log.trace('Parsing CSV'); | ||
// request.page | ||
const offset = (request.page && request.limit) | ||
? ((request.page - 1) * request.limit) | ||
: 0; | ||
const limit = request.limit || null; | ||
this._parsed[request._queryIndex] = await csvParsePromise(this._queries[request._queryIndex], this._parserOptions); | ||
} | ||
try { | ||
result = this._parsed[request._queryIndex] | ||
// request.filter | ||
.filter(filterFn(request.filter)) | ||
// request.page | ||
const offset = request.page && request.limit ? (request.page - 1) * request.limit : 0; | ||
const limit = request.limit || null; | ||
// request.attributes | ||
.map((row) => { | ||
const newRow = {}; | ||
request.attributes.forEach((attribute) => { | ||
newRow[attribute] = row[attribute]; | ||
}); | ||
return newRow; | ||
}); | ||
let result = this._parsed[request._queryIndex] | ||
// request.filter | ||
.filter(filterFn(request.filter)) | ||
// request.limit | ||
if (limit) result = result.slice(offset, limit + offset); | ||
} catch (e) { | ||
return callback(e); | ||
} | ||
return callback(null, { | ||
totalCount: null, | ||
data: result | ||
// request.attributes | ||
.map(row => { | ||
const newRow = {}; | ||
request.attributes.forEach(attribute => { | ||
newRow[attribute] = row[attribute]; | ||
}); | ||
return newRow; | ||
}); | ||
}; | ||
if (!this._parsed[request._queryIndex]) { | ||
this._log.trace('Parsing CSV'); | ||
return csvParse(this._queries[request._queryIndex], this._parserOptions, | ||
(err, output) => { | ||
if (err) return callback(err); | ||
this._parsed[request._queryIndex] = output; | ||
return proceed(); | ||
}); | ||
} | ||
// request.limit | ||
if (limit) result = result.slice(offset, limit + offset); | ||
return proceed(); | ||
return { | ||
totalCount: null, | ||
data: result | ||
}; | ||
} | ||
/** | ||
* @param {Function} callback | ||
* @return {Promise} | ||
*/ | ||
close(callback) { | ||
async close() { | ||
this._log.trace('Close'); | ||
callback(); | ||
} | ||
@@ -132,0 +118,0 @@ } |
{ | ||
"name": "flora-csv", | ||
"version": "0.8.0", | ||
"version": "2.0.0-alpha.1", | ||
"description": "CSV data source for Flora", | ||
"main": "index.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"bamboo": "mocha -R mocha-bamboo-reporter", | ||
"test": "eslint . && mocha", | ||
@@ -34,7 +33,7 @@ "lint": "eslint ." | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
"dependencies": { | ||
"csv-parse": "^3.1.3", | ||
"flora-errors": "^0.9.0" | ||
"csv-parse": "^4.0.1", | ||
"flora-errors": "^0.9.1" | ||
}, | ||
@@ -44,9 +43,9 @@ "devDependencies": { | ||
"chai": "^4.2.0", | ||
"eslint": "^5.8.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint": "^5.9.0", | ||
"eslint-config-prettier": "^3.3.0", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"mocha": "^5.2.0", | ||
"mocha-bamboo-reporter": "^1.1.1", | ||
"pre-commit": "^1.2.2" | ||
"pre-commit": "^1.2.2", | ||
"prettier": "^1.15.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
6612
7
96
+ Addedcsv-parse@4.16.3(transitive)
- Removedcsv-parse@3.2.0(transitive)
Updatedcsv-parse@^4.0.1
Updatedflora-errors@^0.9.1