New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript queries with raw: true
should not return model instances
#12575
Comments
So, there's no solution for this, any idea? |
I realized I never made an official PR, so I added that at least. #12921 My workaround is in some model definition file write: import Seq from 'sequelize';
declare module 'sequelize' {
export abstract class ModelRaw<T, T2> extends Seq.Model<T, T2> {
/**
* @override
*
* @see {@link https://github.com/sequelize/sequelize/issues/12575}
*/
public static findAll<M extends Seq.Model>(
this: ModelStatic<M>,
options?: FindOptions<M['_attributes']> & { raw?: false }): Promise<M[]>;
public static findAll<M extends Seq.Model, R extends any = M['_attributes']>(
this: ModelStatic<M>,
options?: FindOptions<M['_attributes']> & { raw: true }): Promise<R[]>;
/**
* @override
*
* @see {@link https://github.com/sequelize/sequelize/issues/12575}
*/
public static findOne<M extends Seq.Model>(
this: ModelStatic<M>,
options: NonNullFindOptions<M['_attributes']> & { raw?: false }
): Promise<M>;
public static findOne<M extends Seq.Model, R extends any = M['_attributes']>(
this: ModelStatic<M>,
options: NonNullFindOptions<M['_attributes']> & { raw: true }
): Promise<R>;
public static findOne<M extends Seq.Model>(
this: ModelStatic<M>,
options?: FindOptions<M['_attributes']> & { raw?: false }
): Promise<M | null>;
public static findOne<M extends Seq.Model, R extends any = M['_attributes']>(
this: ModelStatic<M>,
options?: FindOptions<M['_attributes']> & { raw: true }
): Promise<R | null>;
}
}
class Foo extends Seq['Model' as 'ModelRaw'] { ... } Its a hack, but it has worked well enough for me. |
Any update on this issue? |
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
Yes this is still an issue. |
🎉 This issue has been resolved in version 7.0.0-alpha.15 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Issue Description
Currently when running a query with a model in Typescript, the result(s) are instances of the model.
e.g.
This is expected and ideal behavior.
However when running a query with
raw: true
(https://sequelize.org/master/manual/raw-queries.html) the response is very much not an instance of the model (what it is is up for discussion in this issue).The typing for queries that support raw should support returning non-model responses.
What was unclear/insufficient/not covered in the documentation
Presently, all Model query methods that support
{raw: true}
still declare they return the model instance, when raw queries are explicitly not instances.So regular model attributes like
get
are not even available (which are used as a workaround for custom attributes on non-raw results).If possible: Provide some suggestion on how we can enhance the docs
Allow type parameters on methods that support
raw: true
to return a custom type, defaulting to the models raw attributes.As an example, below is the existing definition for findAll: https://github.com/sequelize/sequelize/blob/v6/types/lib/model.d.ts#L1826
If we added another method definition that explicitly declared when
raw: true
to return a type parameter, it could correctly handle typing raw queries:The example query above could now be supported as:
Additional context
If there is another known method (or suggestions) for typing raw queries I'd appreciate the advice.
I know it is possible to just run the query as
raw: false
and use getters to load the custom attributes, but I believe raw queries serve their purpose when the extra noise of model instances is unnecessary. Not to mention the current typing is objectively wrong in this case.Issue Template Checklist
Is this issue dialect-specific?
Would you be willing to resolve this issue by submitting a Pull Request?
Happy to make an actual PR with my suggested
findAll()
as well as a couple others (possiblyfindOne
+findByPk
?) if the solution sounds correctThe text was updated successfully, but these errors were encountered: