dynamodb-onetable
Advanced tools
Comparing version 0.6.5 to 0.6.6
@@ -55,4 +55,5 @@ "use strict"; | ||
let op = this.op; | ||
let context = this.params.context || this.table.context; | ||
for (let [fieldName, field] of Object.entries(fields)) { | ||
let value = this.template(field, properties, this.table.context); | ||
let value = this.template(field, properties, context); | ||
if (value === undefined || value === null || value === '') { | ||
@@ -88,2 +89,5 @@ if (field.uuid && op == 'put') { | ||
if (op == 'scan') { | ||
/* | ||
Setup scan filters for properties outside the model | ||
*/ | ||
for (let [name, value] of Object.entries(properties)) { | ||
@@ -112,15 +116,14 @@ if (fields[name] || value == null) | ||
} | ||
else { | ||
if (op == 'find' || op == 'scan') { | ||
else if ((op == 'find' || op == 'scan')) { | ||
// schema.filter == false disables a field from being used in a filter | ||
if (this.properties[field.name] && field.filter !== false) { | ||
this.addFilter(field.attribute, value); | ||
} | ||
} | ||
if (op == 'delete' || op == 'put' || op == 'update') { | ||
if (op == 'put') { | ||
this.values[field.attribute] = value; | ||
} | ||
else if (op == 'update') { | ||
this.addUpdate(field, value); | ||
} | ||
if (op == 'put') { | ||
this.values[field.attribute] = value; | ||
} | ||
else if (op == 'update') { | ||
this.addUpdate(field, value); | ||
} | ||
this.fieldValues[field.attribute] = value; | ||
@@ -147,6 +150,4 @@ } | ||
} | ||
if (op == 'update') { | ||
if (params.add || params.remove || params.delete) { | ||
this.addUpdates(); | ||
} | ||
if (op == 'update' && (params.add || params.remove || params.delete)) { | ||
this.addUpdates(); | ||
} | ||
@@ -249,2 +250,9 @@ if (params.where && (op == 'delete' || op == 'update')) { | ||
} | ||
if (field.attribute != this.hash && field.attribute != this.sort && this.properties[field.name] === undefined) { | ||
// Only update values explicitly provided by caller in properties. | ||
// Unless params.updateContext requires context properties to be updated too. | ||
if (this.params.updateIndexes !== true) { | ||
return; | ||
} | ||
} | ||
updates.push(`#_${nindex} = :_${vindex}`); | ||
@@ -391,7 +399,7 @@ names[`#_${nindex++}`] = field.attribute; | ||
} | ||
/* | ||
Remaining template variable. If sort and doing find, then use sort key prefix, (provide no where clause). | ||
*/ | ||
if (s.indexOf('${') >= 0) { | ||
if (field.attribute == this.sort) { | ||
/* | ||
Special case for find without a sort key, where clause, and with a sort key field prefix. | ||
*/ | ||
if (this.op == 'find' && !this.params.where) { | ||
@@ -398,0 +406,0 @@ s = s.replace(/\${(.*?)}/g, ''); |
@@ -370,3 +370,2 @@ "use strict"; | ||
if (params.retry) { | ||
//MOB - consistency in error | ||
throw new Error('dynamo: Remove cannot retry'); | ||
@@ -373,0 +372,0 @@ } |
@@ -125,2 +125,5 @@ "use strict"; | ||
getModel(name) { | ||
if (typeof name != 'string') { | ||
throw new Error(`Bad argument type for model name ${name}`); | ||
} | ||
let model = this.models[name]; | ||
@@ -269,4 +272,2 @@ if (!model) { | ||
if (this.logger) { | ||
//MOB - temp | ||
type = 'info'; | ||
if (params && params.log) { | ||
@@ -273,0 +274,0 @@ this.logger('info', message, context); |
{ | ||
"name": "dynamodb-onetable", | ||
"version": "0.6.5", | ||
"version": "0.6.6", | ||
"description": "DynamoDB OneTable", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -22,2 +22,4 @@ # DynamoDB OneTable | ||
OneTable is used by the [SenseDeep Serverless Troubleshooter](https://www.sensedeep.com/) for all DynamoDB access. | ||
A big thank you to [Alex DeBrie](https://www.alexdebrie.com/about/) and his excellent [DynamoDB Book](https://www.dynamodbbook.com/). Highly recommended. And thanks also to [Jeremy Daly](https://www.jeremydaly.com/about/) for his [Off by None Blog](https://offbynone.io/) blog, posts and his [DynamoDB Toolbox](https://github.com/jeremydaly/dynamodb-toolbox) which pointed out a better way for us to do a number of things. | ||
@@ -187,3 +189,3 @@ | ||
The standard DynamoDB API requires a lot of boiler-plate syntax and expressions. This is tedius to use and can unfortunately can be error prone at times. I doubt that creating complex attribute type expressions, key, filter, condition and update expressions are anyones idea of a good time. | ||
The standard DynamoDB API requires a lot of boiler-plate syntax and expressions. This is tedious to use and can unfortunately can be error prone at times. I doubt that creating complex attribute type expressions, key, filter, condition and update expressions are anyones idea of a good time. | ||
@@ -378,4 +380,9 @@ Net/Net: it is not easy to write terse, clear, robust Dynamo code for one-table patterns. | ||
Each `Table` has a `context` of properties that are blended with `Model` properties before writing items to the database. The table `context` is useful to store global properties that apply to multiple models. A typical use case is for a central authorization module to add an `accountId` or `userId` to the context which is then used in the primary key for all items belonging to that account or user. | ||
Each `Table` has a `context` of properties that are blended with `Model` properties. The context is used to provide keys and attributes that apply to more than just one model. A typical use case is for a central authorization module to add an `accountId` or `userId` to the context which is then used in keys for items belonging to that account or user. | ||
When creating items, context properties are written to the database. When updating, context properties are not, only explicit attributes provided in the API `properties` parameter are written. | ||
Context properties take precedence over supplied `properties`. This is to prevent accidental updating of context keys. To force an update, provide the context properties either by updating the context via `Table.setContext` or supplying an explicit context via `params.context` | ||
Use the `Table.setContext` method to initialize the context and `Table.clear` to reset. | ||
@@ -840,2 +847,3 @@ | ||
| type | `string` | Add a `type` condition to the `create`, `delete` or `update` API call. Set `type` to the DynamoDB requried type.| | ||
| updateIndexes | `boolean` | Set to true to update index attributes. The default during updates is not to update index values which are defined during create. | | ||
| where | `string` | Define a filter or update conditional expression template. Use `${attribute}` for attribute names and `{value}` for values. OneTable will extract attributes and values into the relevant ExpressionAttributeNames and ExpressionAttributeValues.| | ||
@@ -842,0 +850,0 @@ |
111321
1629
904