[BUG] `saveUnknown` doesn't work for nested properties of Arrays
See original GitHub issueSummary:
It appears that if you use the saveUnknown
property for a subfield of an Array
entry, undeclared fields are not actually persisted. In the example below, the following is saved and properties e
and f
are erased:
{
a: 2,
b: [{
c: 'foo',
d: {}
}]
}
The only way I could get around this was enabling saveUnknown: true
at the top-level schema (ie, the second argument to schema
in the example below). This doesn’t really seem ideal because you’re now opening up the schema to unexpected top-level fields that aren’t supposed to be saved.
Code sample:
Schema
const nested = new dynamoose.Schema({}, { saveUnknown: true })
const schema = new dynamoose.Schema({
a: Number,
b: {
type: Array,
schema: [{
type:Object,
schema: { c: String, d: nested }
}]
}
})
Model
const obj = {
a: 2,
b: [{
c: 'foo',
d: {
e: 1,
f: 'bar'
}
}]
}
General
schema.create(obj)
Current output and behavior (including stack trace):
Expected output and behavior:
Environment:
Operating System:
Operating System Version:
Node.js version (node -v
):
NPM version: (npm -v
):
Dynamoose version:
Other information (if applicable):
Other:
- I have read through the Dynamoose documentation before posting this issue
- I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
- I have searched the internet and Stack Overflow to ensure this issue hasn’t been raised or answered before
- I have tested the code provided and am confident it doesn’t work as intended
- I have filled out all fields above
- I am running the latest version of Dynamoose
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:8 (4 by maintainers)
Top Results From Across the Web
[BUG] Schema option `saveUnknown` is not working for an array
Summary: Setting the Schema option saveUnknown to true works as expected, but using an array does save empty objects into DynamoDB: const ...
Read more >Dynamoose Nested Map/Array in TypeScript Schema
Solution: Extract the nested schema into it's own schema and reference that in the original one. const appleSchema = new dynamoose.
Read more >Nested field type | Elasticsearch Guide [8.5] | Elastic
The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way...
Read more >Schema - Dynamoose
saveUnknown, array | boolean, false, This setting lets you specify if the schema should allow properties not defined in the schema. If you...
Read more >Property of object in a nested array doesn't update when ...
In the database there is one document with an array history and inside that array 3 nested documents (objects) with a property “name”...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the report. I was able to reproduce this. Will work on fixing as soon as I can.
For personal reference, MCVE below. The issue is that the
saveUnknown
variable is includingschema
properties, when it shouldn’t be.I got the same issue. I workaround by adding one more simple field to the document. Ex: The below document will not be saved.
but this will work, of course,
saveUnknown: true