question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] `saveUnknown` doesn't work for nested properties of Arrays

See original GitHub issue

Summary:

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:open
  • Created a year ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
fishcharliecommented, May 20, 2022

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 including schema properties, when it shouldn’t be.

const dynamoose = require("dynamoose");

(async () => {
	dynamoose.aws.ddb.local();

	const nested = new dynamoose.Schema({"random": String}, { saveUnknown: true })
	const schema = new dynamoose.Schema({
		a: Number,
		b: {
			type: Array,
			schema: [{
				type: Object,
				schema: { c: String, d: nested }
			}]
		}
	});

	const Model = dynamoose.model(`Test-${Date.now()}`, schema);

	const obj = {
		a: 2,
		b: [{
			c: 'foo',
			d: {
				e: 1,
				f: 'bar'
			}
		}]
	}

	await Model.create(obj);

	console.log(JSON.stringify((await Model.scan().exec()).toJSON(), null, 4));
})();
1reaction
manh-vvcommented, May 20, 2022

I got the same issue. I workaround by adding one more simple field to the document. Ex: The below document will not be saved.

{
  "id": "d1",
  "tx": [{ "object1": "value1" }]
}

but this will work, of course, saveUnknown: true

{
  "id": "d1",
  "ttl": -1,
  "tx": [{ "object1": "value1" }]
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found