dynamodb-service
Advanced tools
Comparing version 1.0.0 to 1.5.0
{ | ||
"name": "dynamodb-service", | ||
"version": "1.0.0", | ||
"version": "1.5.0", | ||
"description": "Managing records saved in Dynamodb", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test/create.test.js" | ||
"test": "echo 'NOT implemented'" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -30,3 +30,3 @@ <!-- | ||
// profile = 'not-default' // => this is the aws credentials found in `.aws/credentials` file | ||
let dynamoDBService = new DynamoDBService(primaryKeyName, tableName, profile); | ||
let dynamoDBService = new DynamoDBService(tableName, primaryKeyName, profile); | ||
@@ -183,1 +183,3 @@ dynamoDBService.create(data) // => @return Promise | ||
* [aws - loading credentials](http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html) | ||
* [aws - query tokens](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html) | ||
* [ test for alpha numeric values](http://stackoverflow.com/questions/388996/regex-for-javascript-to-allow-only-alphanumeric) |
@@ -43,3 +43,3 @@ /** | ||
// pre-requisite: data[this.keyName] | ||
// pre-requisite: data[this.keyName] = alpha-numeric values | ||
if((typeof data !== 'object') || !data[this.keyName]) { | ||
@@ -49,2 +49,7 @@ return Promise.reject(`Data object must have ${this.keyName} attribute in it`); | ||
let pattern = new RegExp(/^[a-z0-9]+$/i); | ||
if(!pattern.test(data[this.keyName])) { | ||
return Promise.reject(`${this.keyName} must contain alphaNumeric values only, found = ${data[this.keyName]}`); | ||
} | ||
let params = { | ||
@@ -83,16 +88,115 @@ TableName: this.tableName, | ||
/** | ||
* Update | ||
* update | ||
* @param id { string } the record identifier | ||
* @param data { JSON } | ||
*/ | ||
update() { | ||
update(id, data) { | ||
// pre-requisite: id and data are defined | ||
if(!id || typeof data !== 'object') { | ||
return Promise.reject('Both id and data parameters are required'); | ||
} | ||
return this.read(id).then(response => { | ||
if(!!response && this.keyName in response) { | ||
// clean up the data object | ||
delete data[this.keyName]; | ||
let updatedObject = Object.assign({}, response, data); | ||
return this.create(updatedObject); | ||
} | ||
return Promise.reject('Record not found'); | ||
}); | ||
} | ||
/** | ||
* Delete | ||
* Deleted | ||
* @param id { string } The primaryKey that presents the saved records | ||
* @return { Promise<any> } resolve with the deleted object | ||
*/ | ||
delete() { | ||
delete(id) { | ||
return this.read(id).then(response => { | ||
if(!!response && this.keyName in response) { | ||
let Key = {}; | ||
Key[this.keyName] = response[this.keyName]; | ||
let params = { | ||
TableName: this.tableName, | ||
Key | ||
}; | ||
// return a completely different result when dynamo completes | ||
return this.dynamoDb.delete(params).promise() | ||
.then((/* success */) => response ); | ||
} | ||
return Promise.reject('Record not found'); | ||
}); | ||
} | ||
// START extra functions | ||
/** | ||
* list | ||
* @warning aws does not provide a way to get all records @TODO | ||
* @TODO find a way to set the limit | ||
* @NOTE aws limits data fetch to 1 MB | ||
* @source http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#API_Query_RequestSyntax | ||
*/ | ||
list() { | ||
let params = { | ||
TableName: this.tableName, | ||
FilterExpression: '#key >= :val', | ||
ExpressionAttributeNames: { | ||
'#key': this.keyName, | ||
}, | ||
ExpressionAttributeValues: { | ||
':val': '.' | ||
} | ||
}; | ||
return this.dynamoDb.scan(params).promise() | ||
.then(response => { | ||
return response.Items; | ||
}); | ||
} | ||
/** | ||
* @param optionParam { { key: value } } JSON with only one key-value attr | ||
*/ | ||
query(optionParam) { | ||
// pre-requisite: data[this.keyName] = alpha-numeric values | ||
if((typeof optionParam !== 'object') || Object.keys(optionParam).length !== 1) { | ||
return Promise.reject('optionParam must have one key value attribute'); | ||
} | ||
let key = Object.keys(optionParam)[0]; | ||
let value = optionParam[key]; | ||
let params = { | ||
TableName: this.tableName, | ||
FilterExpression: '#key >= :val', | ||
ExpressionAttributeNames: { | ||
'#key': key, | ||
}, | ||
ExpressionAttributeValues: { | ||
':val': value | ||
} | ||
}; | ||
return this.dynamoDb.scan(params).promise() | ||
.then(response => { | ||
return response.Items; | ||
}); | ||
} | ||
// END extra functions | ||
} | ||
module.exports = DynamoDBService; |
15015
12
278
183