Skip to content

Commit 0c4ccef

Browse files
committed
add removeNullAttributes feature
1 parent 048459a commit 0c4ccef

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ const MyTable = new Table({
256256
| indexes | `object` | no | Complex type that optionally specifies the name keys of your secondary indexes (see below) |
257257
| autoExecute | `boolean` | no | Enables automatic execution of the DocumentClient method (default: `true`) |
258258
| autoParse | `boolean` | no | Enables automatic parsing of returned data when `autoExecute` is `true` (default: `true`) |
259+
| removeNullAttributes | `boolean` | no | Removes null attributes instead of setting them to `null` (default: `true`) |
259260
| DocumentClient | `DocumentClient` | * | A valid instance of the AWS [DocumentClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) |
260261

261262
\* *A Table can be instantiated without a DocumentClient, but most methods require it before execution*

classes/Entity.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,9 @@ class Entity {
488488
const names = {}
489489
const values = {}
490490

491+
491492
// Loop through valid fields and add appropriate action
492-
Object.keys(data).forEach(function(field) {
493+
Object.keys(data).forEach((field) => {
493494
const mapping = schema.attributes[field]
494495

495496
// Remove attributes
@@ -507,6 +508,9 @@ class Entity {
507508
REMOVE.push(`#${attr}`)
508509
names[`#${attr}`] = attr
509510
} // end for
511+
} else if (this._table._removeNulls === true && (data[field] === null || String(data[field]).trim() === '') && (!mapping.link || mapping.save)) {
512+
REMOVE.push(`#${field}`)
513+
names[`#${field}`] = field
510514
} else if (
511515
!mapping.partitionKey
512516
&& !mapping.sortKey
@@ -781,6 +785,7 @@ class Entity {
781785
return value !== undefined
782786
&& (mapping.save === undefined || mapping.save === true)
783787
&& (!mapping.link || (mapping.link && mapping.save === true))
788+
&& (!_table._removeNulls || (_table._removeNulls && value !== null))
784789
? Object.assign(acc, {
785790
[field]: value
786791
}) : acc

classes/Table.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ class Table {
5151
// Gets the current auto execute mode
5252
get autoParse() { return this._parse }
5353

54+
// Sets the auto execute mode (default to true)
55+
set removeNullAttributes(val) { this._removeNulls = typeof val === 'boolean' ? val : true }
56+
57+
// Gets the current auto execute mode
58+
get removeNullAttributes() { return this._removeNulls }
59+
5460
// Retrieves the document client
5561
get DocumentClient() { return this._docClient }
5662

lib/normalizeData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { error } = require('./utils')
1111

1212
// Normalize Data
1313
module.exports = (DocumentClient) => (schema,linked,data,filter=false) => {
14-
14+
1515
// Intialize validate type
1616
const validateType = validateTypes(DocumentClient)
1717

lib/parseTable.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = table => {
2222
indexes,
2323
autoExecute,
2424
autoParse,
25+
removeNullAttributes,
2526
entities,
2627
DocumentClient,
2728
...args // extraneous config
@@ -87,6 +88,7 @@ module.exports = table => {
8788
},
8889
autoExecute,
8990
autoParse,
91+
removeNullAttributes,
9092
_entities: [] // init Entity tracker
9193
},
9294
DocumentClient ? { DocumentClient } : {}, // end DocumentClient

0 commit comments

Comments
 (0)