dynamodb-onetable
Advanced tools
Comparing version 0.6.3 to 0.6.4
@@ -46,3 +46,3 @@ export default class Expression { | ||
template(field: any, properties: any, ...contexts: any[]): any; | ||
removeEmptyStrings(obj: any): any; | ||
removeEmptyStrings(field: any, obj: any): any; | ||
getFieldValues(): {}; | ||
@@ -49,0 +49,0 @@ and(terms: any): any; |
@@ -54,19 +54,15 @@ "use strict"; | ||
parseFields(fields, properties) { | ||
// MOB - what if properties that are not fields? | ||
let op = this.op; | ||
for (let [fieldName, field] of Object.entries(fields)) { | ||
let value = this.template(field, properties, this.table.context); | ||
if (value == null || value == '') { | ||
if (field.uuid && this.op == 'put') { | ||
// Allocate a UUID for this item | ||
if (value === undefined || value === null || value === '') { | ||
if (field.uuid && op == 'put') { | ||
value = this.table.uuid(); | ||
} | ||
else if (this.op != 'put' || this.model.nulls !== true) { | ||
if (field.attribute == this.hash && this.op != 'scan') { | ||
throw new Error(`dynamo: Empty primary hash key`); | ||
} | ||
if (field.name == this.sort && this.params.high) { | ||
// High level API without sort key. Fallback to find to select the items of interest | ||
this.fallback = true; | ||
return; | ||
} | ||
else if (field.name == this.sort && this.params.high) { | ||
// High level API without sort key. Fallback to find to select the items of interest | ||
this.fallback = true; | ||
return; | ||
} | ||
else if (value === undefined || (value === null && field.nulls !== true)) { | ||
continue; | ||
@@ -76,3 +72,3 @@ } | ||
else if (typeof value == 'object') { | ||
value = this.removeEmptyStrings(value); | ||
value = this.removeEmptyStrings(field, value); | ||
} | ||
@@ -83,3 +79,5 @@ this.add(field, value); | ||
} | ||
let op = this.op; | ||
if (op != 'scan' && this.fieldValues[this.hash] == null) { | ||
throw new Error(`dynamo: Empty primary hash key`); | ||
} | ||
if (op == 'delete' || op == 'put' || op == 'update') { | ||
@@ -369,6 +367,7 @@ this.addConditions(op); | ||
let s = field.value; | ||
contexts.push(properties); | ||
if (s == null) { | ||
return properties[field.name]; | ||
let context = contexts.find(context => context[field.name] !== undefined); | ||
return context ? context[field.name] : undefined; | ||
} | ||
contexts.push(properties); | ||
for (let context of contexts) { | ||
@@ -379,3 +378,3 @@ if (s.indexOf('${') < 0) { | ||
s = s.replace(/\${(.*?)}/g, (match, varName) => { | ||
if (context[varName] != null) { | ||
if (context[varName] !== undefined) { | ||
return context[varName]; | ||
@@ -402,7 +401,7 @@ } | ||
} | ||
return null; | ||
return undefined; | ||
} | ||
return s; | ||
} | ||
removeEmptyStrings(obj) { | ||
removeEmptyStrings(field, obj) { | ||
let result; | ||
@@ -413,5 +412,5 @@ if (obj !== null && typeof obj == 'object') { | ||
if (typeof value == 'object') { | ||
result[key] = this.removeEmptyStrings(value); | ||
result[key] = this.removeEmptyStrings(field, value); | ||
} | ||
else if (value === null && this.model.nulls !== true) { | ||
else if (value === null && field.nulls !== true) { | ||
continue; | ||
@@ -418,0 +417,0 @@ } |
@@ -43,4 +43,4 @@ export default class Model { | ||
mapWriteData(field: any, value: any): any; | ||
mapNestedFields(field: any, obj: any): any; | ||
validate(op: any, properties: any, params: any): {}; | ||
validateFields(obj: any): any; | ||
getIndexProperties(indexes: any): {}; | ||
@@ -47,0 +47,0 @@ encrypt(text: any, name?: string, inCode?: string, outCode?: string): any; |
@@ -116,2 +116,5 @@ "use strict"; | ||
field.attribute = this.map[name]; | ||
if (field.nulls !== true && field.nulls !== false) { | ||
field.nulls = this.nulls; | ||
} | ||
if (this.indexProperties[this.map[name]]) { | ||
@@ -505,3 +508,3 @@ field.isIndexed = true; | ||
if (this.migrate && ReadWriteMigrations[op] == 'read') { | ||
rec = this.migrate(this, op, rec); | ||
rec = this.migrate(this, op, rec, result); | ||
} | ||
@@ -538,3 +541,3 @@ for (let [name, field] of Object.entries(this.fields)) { | ||
if (value != null && typeof value == 'object') { | ||
value = this.validateFields(value); | ||
value = this.mapNestedFields(field, value); | ||
} | ||
@@ -550,2 +553,17 @@ // Invoke custom transformation before writing data | ||
} | ||
mapNestedFields(field, obj) { | ||
for (let [key, value] of Object.entries(obj)) { | ||
if (value instanceof Date) { | ||
obj[key] = value.getTime(); | ||
} | ||
else if (value == null && field.nulls !== true) { | ||
// Skip nulls | ||
continue; | ||
} | ||
else if (typeof value == 'object') { | ||
obj[key] = this.mapNestedFields(field, value); | ||
} | ||
} | ||
return obj; | ||
} | ||
/* | ||
@@ -567,3 +585,3 @@ Validate properties and map types before writing to the database. | ||
if (field.required && value == null && field.value == null) { | ||
if (context[fieldName]) { | ||
if (context[fieldName] !== undefined) { | ||
value = context[fieldName]; | ||
@@ -593,3 +611,3 @@ } | ||
if (field.required && field.value == null) { | ||
if (context[fieldName]) { | ||
if (context[fieldName] !== undefined) { | ||
value = context[fieldName]; | ||
@@ -639,17 +657,2 @@ } | ||
} | ||
validateFields(obj) { | ||
for (let [key, value] of Object.entries(obj)) { | ||
if (value instanceof Date) { | ||
obj[key] = value.getTime(); | ||
} | ||
else if (value == null && this.nulls !== true) { | ||
// Skip nulls | ||
continue; | ||
} | ||
else if (typeof value == 'object') { | ||
obj[key] = this.validateFields(value); | ||
} | ||
} | ||
return obj; | ||
} | ||
/* | ||
@@ -656,0 +659,0 @@ Get a hash of all the property names of the indexes |
{ | ||
"name": "dynamodb-onetable", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"description": "DynamoDB OneTable", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# DynamoDB OneTable | ||
[![npm](https://img.shields.io/npm/v/dynamodb-onetable.svg)](https://www.npmjs.com/package/dynamodb-onetable) | ||
[![npm](https://img.shields.io/npm/l/dynamodb-onetable.svg)](https://www.npmjs.com/package/dynamodb-onetable) | ||
![OneTable](https://www.sensedeep.com/images/ring.png) | ||
@@ -232,3 +235,3 @@ | ||
| name | `string` | yes | The name of your DynamoDB table | | ||
| nulls | `boolean` | Store nulls in database attributes. Default false. | ||
| nulls | `boolean` | Store nulls in database attributes. Default false. | | ||
| schema | `string` | Definition of your DynamoDB indexes and models | | ||
@@ -241,2 +244,4 @@ | timestamps | `boolean` | Make "created" and "updated" timestamps in items | | ||
By default, OneTable will not write `null` values to the database. If you set the `nulls` property to true, `null` values will be written via `create` or `update`. You can also define `nulls` on a per-attribute basis via the schema. | ||
#### Crypto | ||
@@ -334,2 +339,3 @@ | ||
| map | `string` | Map the field value to a different attribute when storing in the database. | | ||
| nulls | `boolean` | Set to true to store null values. Default to table.nulls value. | | ||
| required | `boolean` | Set to true if the attribute is required. | | ||
@@ -768,4 +774,8 @@ | transform | `function` | Hook function to be invoked to format and parse the data before reading and writing. | | ||
### Contact | ||
You can contact me (Michael O'Brien) on Twitter at: @SenseDeepCloud, or [email](mob-pub-18@sensedeep.com). | ||
### SenseDeep | ||
Please try our Serverless trouble shooter [SenseDeep](https://www.sensedeep.com/). |
100702
1577
778