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] Schema set function is not called

See original GitHub issue

Summary:

Type check is done before calling set function. Therefore, set function cannot transform value’s type.

Code sample:

Schema

const AlertSchema = new dynamoose.Schema({
	id: {
		type: String,
		hashKey: true,
		required: true
	},
	active: {
		type: Number,
		set: v => {
			console.log('set', v);
			return Number(v);
		},
		get: v => Boolean(v),
		index: {
			global: true
		},
		default: 1,
	}
}, {
	timestamps: {
		createdAt: 'createdAt',
		updatedAt: 'updatedAt'
	}
});

Model

const alert = await Alert.create({
	id: UUIDv4(),
	active: false
});

Current output and behavior (including stack trace):

TypeMismatch: Expected active to be of type number, instead found type boolean. at checkTypeFunction (./node_modules/dynamoose/lib/Document.ts:302:11) at Array.map (<anonymous>) at Function.<anonymous> (./node_modules/dynamoose/lib/Document.ts:321:111) at Generator.next (<anonymous>) at fulfilled (./node_modules/dynamoose/dist/Document.js:5:58)

The console.log is never triggered.

Expected output and behavior:

set function to be called.

Environment:

Operating System: macOS Operating System Version: 10.15 Node.js version (node -v): v13.10.1 NPM version: (npm -v): 6.13.7 Dynamoose version: 2.3.0-beta.1

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 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wdbdesigncommented, Aug 13, 2020

This really limits the usefulness of the set and get functions (for my case at least). In v1 these functions would allow the user to use non-DynamoDB (complex) types in schema objects, and transform them to DynamoDB-compatible types on get/put.

In v2 it seems I can only use the exact type for fields, reducing the get and put functions to transformations of the type representation.

0reactions
mrosackcommented, Dec 26, 2022

For me, I’m trying to convert my v1 codebase to v2, and if set was called before type checks this would help me a lot in refactoring my code to get data the way v2 wants it. That said, I’m sure that after I get everything working/was starting from scratch I wouldn’t need this. I’ll probably just have my conversion layer be outside of dynamoose.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mongoose schema setter overwrite function is not called
The problem is that you've got a typo in your strToDate function that's causing the function to throw a ReferenceError exception (which ...
Read more >
Documentation: 15: CREATE FUNCTION - PostgreSQL
If a schema name is included, then the function is created in the ... If the function is not supposed to return a...
Read more >
Mongoose v6.8.2: Schemas
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that...
Read more >
Node.js v19.3.0 Documentation
If the function has not been called exactly exact times when tracker.verify() is called, then tracker.verify() will throw an error. import assert from...
Read more >
Error handling - Apollo GraphQL Docs
The GraphQL operation is not valid against the server's schema. ... When Apollo Server formats an error in a response, it sets the...
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