Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
language: node_js
node_js:
- 4.2
- 6.0
- 6.10
- 8.10
jdk:
- oraclejdk8
install:
- mkdir /tmp/dynamodb
- wget -O - http://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest | tar xz --directory /tmp/dynamodb
- wget -O - https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz | tar xz --directory /tmp/dynamodb
- npm install
script: make test-cov
before_script:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ DynamoDB processor operates a process by simple JSON expression.
## Features

* If it have failed to set child objects to Map Type field, auto trying to update with initial fields again. futhermore, If it have failed by the conflict, auto trying the updating process at first once more.
* Node.js 4.2 or later
* Node.js 6.10 or later

## Install

Expand Down
22 changes: 9 additions & 13 deletions lib/doc_client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = function(docClient) {
module.exports = (docClient) => {

function get(params) {
return docClient.get(params).promise();
Expand All @@ -16,23 +16,19 @@ module.exports = function(docClient) {

function update(params) {
return docClient.update(params).promise()
.then(data => {
return data.Attributes;
});
.then(data => data.Attributes);
}

function batchWrite(params) {
return docClient.batchWrite(params).promise()
.then(data => {
return data.UnprocessedItems;
});
.then(data => data.UnprocessedItems);
}

return {
get: get,
put: put,
update: update,
batchGet: batchGet,
batchWrite: batchWrite,
};
get,
put,
update,
batchGet,
batchWrite
}
}
61 changes: 36 additions & 25 deletions lib/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();


function Expression(initFields) {
this._initFields = initFields || {};
}
Expression.prototype = {
generate: function(table, key, ops, withInit) {
class Expression {
constructor(initFields) {
this._initFields = initFields || {};
}

generate(table, key, ops, withInit) {
this._reset();

const init = withInit || false;
Expand Down Expand Up @@ -62,8 +63,9 @@ Expression.prototype = {
}

return result;
},
_reset: function() {
}

_reset() {
this._inits = _.cloneDeep(this._initFields);
this._sets = [];
this._adds = [];
Expand All @@ -73,35 +75,40 @@ Expression.prototype = {
this._fldIdx = 0;
this._valIdx = 0;
this._conds = [];
},
_addSet: function(name, val, init) {
}

_addSet(name, val, init) {
if (!init || !this._addInitSet(name, val)) {
const path = this._registerName(name);
const i = this._regVal(val);
this._sets.push(`${path} = :v${i}`);
}
},
_addAdd: function(name, val, init) {
}

_addAdd(name, val, init) {
if (!init || !this._addInitSet(name, val)) {
const path = this._registerName(name);
const i = this._regVal(val);
this._adds.push(`${path} :v${i}`);
}
},
_addPushSet: function(name, val, init) {
}

_addPushSet(name, val, init) {
const valSet = docClient.createSet(val);

if (!init || !this._addInitSet(name, valSet)) {
const path = this._registerName(name);
const i = this._regVal(valSet);
this._adds.push(`${path} :v${i}`);
}
},
_addRemove: function(name) {
}

_addRemove(name) {
const path = this._registerName(name);
this._removes.push(path);
},
_addInitSet: function(name, val) {
}

_addInitSet(name, val) {
const paths = name.split(' ');
const rootPath = paths[0];
const initVal = this._inits[rootPath];
Expand All @@ -114,20 +121,23 @@ Expression.prototype = {
initVal.push(val);
}
return true;
},
_registerName: function(name) {
}

_registerName(name) {
return name.split(' ').map((nm) => {
const fn = '#f' + ++this._fldIdx;
this._attrNames[fn] = nm;
return fn;
}).join('.');
},
_regVal: function(val) {
}

_regVal(val) {
const vi = ++this._valIdx;
this._attrValues[`:v${vi}`] = val;
return vi;
},
_geneInits: function(fields) {
}

_geneInits(fields) {
for (let field in this._inits) {
const val = this._inits[field];
const i = this._regVal(val);
Expand All @@ -144,8 +154,9 @@ Expression.prototype = {
this._conds.push(`attribute_not_exists(${fn})`);
}
}
},
_getExpression: function() {
}

_getExpression() {
const exp = [];

if (this._sets.length) {
Expand Down
10 changes: 3 additions & 7 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class Processor {
this.logger.info('Updated %s on %s table', itemKey, table);
return data;
})
.catch((err) => {
.catch(err => {
if (init && err.code === 'ValidationException'
&& err.message === MSG_INVALID_EXPRESSION) {

Expand Down Expand Up @@ -211,9 +211,7 @@ class Processor {
if (useBatch) {
return this.batchWrite(table, data.items);
} else {
return data.items.map((item) => {
return this.put(table, item);
});
return data.items.map((item) => this.put(table, item));
}
}
} else if (data.action === 'update' || data.set
Expand All @@ -233,9 +231,7 @@ class Processor {
if (useBatch) {
return this.batchGet(table, data.keys);
} else {
return data.keys.map((key) => {
return this.get(table, key);
});
return data.keys.map((key) => this.get(table, key));
}
}
}
Expand Down