You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement asynchronicity in getters / setters #965
Allow for async execution of virtual getters / setters.
I will implement this feature as it's a show-stopper for me (hashing algo for passwords is async).
Since there is no established pattern for async callbacks in Sequelize (apart from chained promises) i'll take the initiative to use the this.async() pattern.
I'm not choosing detecting function arity (ala express.js) as this has side-effects in combination with promises.
Just a note, decision about the async pattern will also have to be applied to other parts where async will be required (e.g. custom instance / class methods for models)
@mickhansen it's a subtle and very real issue. The resume is that the arity detection pattern assumes that any argument passed to the callback function is considered to be an error:
functionsomeAsync(done){// this is considered an error and execution should stop after thatdone('any value');}
Promises on the other hand may have arguments on callbacks which conflicts with this pattern:
functionsomeAsync(done){anotherAsync.then(done);}functionanotherAsync(){vardef=defer();// do stuff...// the 'a result' value will create an error as it will be passed as arg to the 'done' cbreturndef.resolve('a result');};
You could solve the problem by being more verbose on the done statement:
Mind you the value an error occurred is hard-coded as a promise that is rejected, may or may not provide an argument...
There are more than meets the eye here and personally i had several issues i had to tackle while writing tests. You can dive in, in one of the many related discussions kriskowal/q#86
@Inconsistency: i guess we can ignore it at this point as, because it (imho) makes sense that the done argument takes the result of the getter as first parameter, whereas it seems kinda senseless, to demonstrate the error case?! i mean, if you'd use a normal synchronous getter, you wouldn't demonstrate the error case explicitly as well?!
In my opinion we should maintain node-style callbacks with error first, also for done callbacks for setter/getters.
It might seem silly for setters/getters to have errors, but ultimately it will keep the api more sane i think.
We need to discuss what happens if one does callback with an error though - We don't really have any way to support that - The error that is, where would it be reported? :) If we want to report it all, which i assume we do if its there
@diosney It's simply too complicated to get right, and would change pretty much everything.
You could add a afterFind that checks if the VIRTUAL attribute has been requested and then inject the value.
@mickhansen or @diosney, can you give an example of how to use an afterFind hook in that way, with an async bit of code? In particular, checking if the virtual attribute has been requested, and then fulfilling a promise of a result for that field.
In my experimentation, the hook is called both after findById and findAll, but the first argument is a single instance in the first case and a list of instances in the second. I can handle this. But the second argument options doesn't have the attribute I added to the options ({attributes: {include: ['field']}}).
I added an afterFind hook which just logs out the second argument.
When I try the following
console.log"find by id"models.User.findById1,
attributes:include:'position'
.then->console.log"findall"models.User.findAllattributes:include:'position'
I'm told Unknown column 'position' in 'field list'. This is legit, because that column doesn't exist. But it is a virtual field so I expected it not to really add it to the query.
What I want is to request the position attribute (which is defined as VIRTUAL), and have the afterFind know about it, as you suggested was possible above. Am I going about this totally wrongly?
You're going about this correctly but it appears there's a bug in our code. You're getting the cleaned and normalized options rather than the original options.
Thanks to @iamjochem and others for making what I believe are good points regarding the problem with asynchronous calls in getters and setters. May I suggest, in this case, that the example code in the VIRTUAL datatype be changed to something else besides password hashing as a setter method, since it might mislead folks to the problem discussed here?
@thomascgray - what does it mean to change a virtual field value? (given that is it niether stored in the instance nor represented in the underlying storage). When you say you tried "everything" it is always helpful to show actual code. I'll hazard a guess that your virtual-field is retrieving a value from the instance using this.getDataValue('some_real_attribute') and then possibly doing something with it then returing a value based on it ... in your afterFind hook you'll want to manipulate the some_real_attribute somehow (e.g. this.some_real_attribute = "my fudged after-find value")
nice thread hijack btw ;-) maybe consider opening a new issue instead?
Hi, is there any alternate for an async getter? I'm saving the file key in DB and every time I want a file, I need to fetch its signed URL using its key. Writing the code in one place i.e getter would have solved the issue.
Activity
thanpolas commentedon Oct 7, 2013
// @durango
thanpolas commentedon Oct 7, 2013
Just a note, decision about the async pattern will also have to be applied to other parts where async will be required (e.g. custom instance / class methods for models)
mickhansen commentedon Oct 7, 2013
I'm mostly for the function arity detection. What are the possible side effects of that?
thanpolas commentedon Oct 7, 2013
@mickhansen it's a subtle and very real issue. The resume is that the arity detection pattern assumes that any argument passed to the callback function is considered to be an error:
Promises on the other hand may have arguments on callbacks which conflicts with this pattern:
You could solve the problem by being more verbose on the done statement:
Mind you the value
an error occurredis hard-coded as a promise that is rejected, may or may not provide an argument...There are more than meets the eye here and personally i had several issues i had to tackle while writing tests. You can dive in, in one of the many related discussions kriskowal/q#86
sdepold commentedon Oct 7, 2013
i'd would vote for the callback parameter.
sdepold commentedon Oct 8, 2013
Hmm we actually already have some kind of inconsistency in the project :(
https://github.com/sequelize/sequelize-doc/pull/81/files => custom validators use the first argument as an error.
sdepold commentedon Oct 8, 2013
oops. sorry.
sdepold commentedon Oct 8, 2013
@Inconsistency: i guess we can ignore it at this point as, because it (imho) makes sense that the done argument takes the result of the getter as first parameter, whereas it seems kinda senseless, to demonstrate the error case?! i mean, if you'd use a normal synchronous getter, you wouldn't demonstrate the error case explicitly as well?!
mickhansen commentedon Oct 8, 2013
In my opinion we should maintain node-style callbacks with error first, also for done callbacks for setter/getters.
It might seem silly for setters/getters to have errors, but ultimately it will keep the api more sane i think.
sdepold commentedon Oct 8, 2013
+1 for that
sdepold commentedon Oct 8, 2013
so we will go for:
mickhansen commentedon Oct 8, 2013
We need to discuss what happens if one does callback with an error though - We don't really have any way to support that - The error that is, where would it be reported? :) If we want to report it all, which i assume we do if its there
sdepold commentedon Oct 8, 2013
we don't have to handle that. the user will use it like this:
12 remaining items
mickhansen commentedon Dec 16, 2015
@diosney It's simply too complicated to get right, and would change pretty much everything.
You could add a afterFind that checks if the VIRTUAL attribute has been requested and then inject the value.
diosney commentedon Dec 16, 2015
@mickhansen Thanks! As always, you give me another ray of hope 😄
I didn't know about the
afterFindhook, that definitively will work for me.Thanks!
tremby commentedon Mar 8, 2016
@mickhansen or @diosney, can you give an example of how to use an
afterFindhook in that way, with an async bit of code? In particular, checking if the virtual attribute has been requested, and then fulfilling a promise of a result for that field.In my experimentation, the hook is called both after
findByIdandfindAll, but the first argument is a single instance in the first case and a list of instances in the second. I can handle this. But the second argumentoptionsdoesn't have the attribute I added to the options ({attributes: {include: ['field']}}).mickhansen commentedon Mar 9, 2016
@tremby Hmm that's odd, the options should be options passed, so your attribute should be in there.
tremby commentedon Mar 9, 2016
I added an afterFind hook which just logs out the second argument.
When I try the following
I get back this:
If I instead have, as the options,
I get the same output.
If I have
I'm told
posdoesn't exist (that's correct).If I have
I'm told
Unknown column 'position' in 'field list'. This is legit, because that column doesn't exist. But it is a virtual field so I expected it not to really add it to the query.If I have
I get
What I want is to request the
positionattribute (which is defined asVIRTUAL), and have the afterFind know about it, as you suggested was possible above. Am I going about this totally wrongly?mickhansen commentedon Mar 9, 2016
You're going about this correctly but it appears there's a bug in our code. You're getting the cleaned and normalized options rather than the original options.
tremby commentedon Mar 9, 2016
Want me to open a new ticket?
mickhansen commentedon Mar 9, 2016
Please do @tremby
twelve17 commentedon Sep 30, 2016
Thanks to @iamjochem and others for making what I believe are good points regarding the problem with asynchronous calls in getters and setters. May I suggest, in this case, that the example code in the VIRTUAL datatype be changed to something else besides password hashing as a setter method, since it might mislead folks to the problem discussed here?
felixfbecker commentedon Oct 1, 2016
@twelve17 sure, feel free to do a PR. I prefer the firstName / lastName / fullName example
twelve17 commentedon Oct 21, 2016
@felixfbecker Sounds good, I'll take a look.
thomascgray commentedon Oct 12, 2017
How do you change a virtual field in
afterFind? Tried everything with getters and setters, but it still comes out with original values.iamjochem commentedon Oct 12, 2017
@thomascgray - what does it mean to change a virtual field value? (given that is it niether stored in the instance nor represented in the underlying storage). When you say you tried "everything" it is always helpful to show actual code. I'll hazard a guess that your virtual-field is retrieving a value from the instance using
this.getDataValue('some_real_attribute')and then possibly doing something with it then returing a value based on it ... in yourafterFindhook you'll want to manipulate thesome_real_attributesomehow (e.g.this.some_real_attribute = "my fudged after-find value")pforprince commentedon Sep 27, 2022
Hi, is there any alternate for an async getter? I'm saving the file key in DB and every time I want a file, I need to fetch its signed URL using its key. Writing the code in one place i.e getter would have solved the issue.
BuistvoPloti commentedon Mar 12, 2024
though it is closed i'm leaving my answer on the opened issue
aws/aws-sdk-js-v3#5509 (comment)