@wmfs/pg-delta-file
Advanced tools
Comparing version
@@ -0,1 +1,13 @@ | ||
# [1.4.0](https://github.com/wmfs/pg-delta-file/compare/v1.3.0...v1.4.0) (2018-08-14) | ||
### ✨ Features | ||
* Added $DATESTAMP, $TIMESTAMP, $DATETIMESTAMP output functions ([e4d2ed7](https://github.com/wmfs/pg-delta-file/commit/e4d2ed7)) | ||
### 📦 Code Refactoring | ||
* Rework transformer. Instead of dynamically generating function source code, use an array o ([3d26316](https://github.com/wmfs/pg-delta-file/commit/3d26316)) | ||
# [1.3.0](https://github.com/wmfs/pg-delta-file/compare/v1.2.0...v1.3.0) (2018-08-14) | ||
@@ -2,0 +14,0 @@ |
const Transform = require('stream').Transform | ||
const _ = require('lodash') | ||
const csvEncoder = require('./simple-csv-encoder') | ||
const DateTime = require('luxon').DateTime | ||
@@ -11,7 +11,4 @@ class Transformer extends Transform { | ||
// Let's dynamically create a function that will return an array of | ||
// output values, ready for stringification. | ||
const functionStatements = [ | ||
'const csvParts = []' | ||
] | ||
const transformers = [] | ||
options.csvExtracts[model].forEach( | ||
@@ -24,15 +21,32 @@ function (csvColumnSource) { | ||
case 'ROW_NUM': | ||
functionStatements.push(`csvParts.push(this.info.totalCount)`) | ||
transformers.push((row, info) => info.totalCount) | ||
break | ||
case 'ACTION': | ||
// TODO: handle deleted action | ||
const createdCol = options.createdColumnName || '_created' | ||
const modifiedCol = options.modifiedColumnName || '_modified' | ||
const created = `new Date(sourceRow['${createdCol}'])` | ||
const modified = `new Date(sourceRow['${modifiedCol}'])` | ||
const since = `new Date('${options.since}')` | ||
transformers.push(row => { | ||
// TODO: handle deleted action | ||
const createdCol = options.createdColumnName || '_created' | ||
const modifiedCol = options.modifiedColumnName || '_modified' | ||
const created = new Date(row[createdCol]) | ||
const modified = new Date(row[modifiedCol]) | ||
const since = new Date(options.since) | ||
functionStatements.push(`if (${modified} >= ${since} && ${created} >= ${since}) csvParts.push('${options.actionAliases.insert}')`) | ||
functionStatements.push(`if (${modified} >= ${since} && ${created} <= ${since}) csvParts.push('${options.actionAliases.update}')`) | ||
if (modified >= since && created >= since) { | ||
return options.actionAliases.insert | ||
} | ||
if (modified >= since && created <= since) { | ||
return options.actionAliases.update | ||
} | ||
}) | ||
break | ||
case 'TIMESTAMP': | ||
transformers.push(() => DateTime.local().toLocaleString(DateTime.TIME_24_WITH_SECONDS)) | ||
break | ||
case 'DATESTAMP': | ||
transformers.push(() => DateTime.local().toISODate()) | ||
break | ||
case 'DATETIMESTAMP': | ||
transformers.push(() => DateTime.local().toISO()) | ||
break | ||
default: | ||
transformers.push(() => `Unknown fn $${functionName}`) | ||
} | ||
@@ -42,13 +56,10 @@ break | ||
const columnName = csvColumnSource.slice(1) | ||
functionStatements.push(`csvParts.push(sourceRow['${columnName}'])`) | ||
transformers.push(row => row[columnName]) | ||
break | ||
default: | ||
functionStatements.push(`csvParts.push(${JSON.stringify(csvColumnSource)})`) | ||
transformers.push(() => csvColumnSource) | ||
} | ||
} | ||
) | ||
functionStatements.push('return csvParts') | ||
this.getOutputValues = new Function('sourceRow', functionStatements.join(';\n')) // eslint-disable-line | ||
this.getOutputValues = _.bind(this.getOutputValues, this) | ||
this.transformers = transformers | ||
} | ||
@@ -58,4 +69,6 @@ | ||
this.info.totalCount++ | ||
const outputValues = this.getOutputValues(sourceRow) | ||
const outputValues = this.transformers | ||
.map(fn => fn(sourceRow, this.info)) | ||
this.transformerFn( | ||
@@ -62,0 +75,0 @@ outputValues, |
{ | ||
"name": "@wmfs/pg-delta-file", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Outputs change-only-update CSV files (or “delta” files) that contain all the necessary actions required to re-synchronize rows in a cloned table.", | ||
@@ -25,2 +25,3 @@ "author": "West Midlands Fire Service", | ||
"lodash": "4.17.10", | ||
"luxon": "^1.3.3", | ||
"make-dir": "1.3.0", | ||
@@ -27,0 +28,0 @@ "pg": "7.4.3", |
@@ -22,3 +22,3 @@ # pg-delta-file | ||
generateDeltaFiles( | ||
await generateDeltaFiles( | ||
since: '2017-07-16T20:37:26.847Z', | ||
@@ -34,20 +34,18 @@ outputFilepath: '/some/temp/dir/people-delta.csv', | ||
transformFunction: (row, callback) => { ... } // optional data transformation | ||
tables: [ | ||
{ | ||
tableName: 'people', | ||
csvColumns: [ | ||
'PERSON', // Just output a literal | ||
'$ACTION', // Will output 'u' or 'd' | ||
'$ROW_NUM', // Row counter | ||
'@social_security_id', /// Column data | ||
'@first_name', | ||
'@last_name', | ||
'@age' | ||
] | ||
} | ||
], | ||
// Standard callback | ||
function (err, info) { | ||
// ... | ||
csvExtracts: { | ||
'[schema.]people': [ | ||
'PERSON', // Just output a literal | ||
'$ACTION', // Will output 'u' or 'd' | ||
'$ROW_NUM', // Row counter | ||
'@social_security_id', /// Column data | ||
'@first_name', | ||
'@last_name', | ||
'@age' | ||
'$DATESTAMP', | ||
'$TIMESTAMP', | ||
'$DATETIMESTAMP', | ||
], | ||
'[schema2.]address': [ | ||
... | ||
] | ||
} | ||
@@ -54,0 +52,0 @@ ) |
Sorry, the diff of this file is not supported yet
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
27785
4.69%368
3.37%0
-100%5
25%60
-3.23%1
-50%+ Added
+ Added