Sequelize-typescript: Raw queries

Created on 22 Jan 2018  路  14Comments  路  Source: RobinBuschmann/sequelize-typescript

There's a typing issue when trying to use raw queries with Sequelize-Typescript:

sequelize.query(query, {
            replacements: [this.user.id],
            model: MyModel
        });

The above generates the following TypeScript error:

Error:(25, 32) TS2345: Argument of type '{ replacements: any[]; model: typeof MyModel; }' is not assignable to parameter of type 'QueryOptions'.
  Types of property 'model' are incompatible.
    Type 'typeof MyModel' is not assignable to type 'Model<any, any>'.
      Property 'Instance' is missing in type 'typeof MyModel'.
bug

Most helpful comment

@TalissonJunior This will be part of [email protected] - I'm working on it

All 14 comments

Hey @ClueGuy, thanks for reporting

Any updates ? or solution?

@TalissonJunior This will be part of [email protected] - I'm working on it

@RobinBuschmann Awesome, i麓m excited to see that new version.

how are you even able to access the sequelize object? Why am I not able to do this? I can't run raw queries, change the default timeout from 15 seconds to longer, or even called stored procedures. How do I do that?

@Romstar you need to get the instance of your sequelize connection, then you can run raw queries here is an example:

var mySequelize = new Sequelize({
            host: 'host',
            database: 'database',
            dialect: 'dialect',
            username: 'username',
            password: 'password',
            modelPaths: [__dirname + '/Models/**/**']
        });

// Here you can you use your sequelize instance to run raw queries.
mySequelize.query(query, {
            replacements: [this.user.id],
            model: MyModel // <-- this is the bug that we are talking about
        });

Hope it helps.

@TalissonJunior doesn't work for me.

image

this is what is being exported

image

I also can't change the timeout on it. Here are all of the packages I have installed.

"devDependencies": {
"types/body-parser": "^1.17.0",
"types/chai": "^4.1.3",
"types/express": "^4.11.1",
"types/mocha": "^5.2.0",
"types/node": "^9.6.6",
"types/sinon": "^4.3.3",
"types/sinon-chai": "^2.7.31",
"types/supertest": "^2.0.4",
"chai": "^4.1.2",
"del": "^3.0.0",
"gulp": "^4.0.0",
"gulp-clean": "^0.4.0",
"gulp-zip": "^4.1.0",
"minimist": "^1.2.0",
"mocha": "^5.1.1",
"nodemon": "^1.17.3",
"sinon": "^5.0.7",
"sinon-chai": "^3.0.0",
"supertest": "^3.1.0",
"ts-node": "^6.0.0",
"tslint": "^5.9.1",
"typescript": "^2.8.3"
},
"dependencies": {
"types/hashmap": "^2.0.29",
"types/helmet": "0.0.37",
"applicationinsights": "^1.0.2",
"express": "^4.16.3",
"gulp-typescript": "^4.0.2",
"hashmap": "^2.3.0",
"helmet": "^3.12.0",
"inversify": "^4.13.0",
"inversify-express-utils": "^6.0.0",
"pg": "^7.4.1",
"pg-hstore": "^2.3.2",
"reflect-metadata": "^0.1.12",
"sequelize": "^4.37.6",
"sequelize-typescript": "^0.6.4",
"swagger-express-ts": "^1.0.0-rc.3",
"swagger-ui-dist": "^3.15.0",
"tedious": "^2.3.1"
},

I am unable to change the timeout, to run manual queries etc. ts-node won't build the application with a .query call and when I debug I don't even see that function as part of the sequelize instance.

@Romstar can you provide a link contain a minimal code or a link to your repository that i can access to try to figure out what麓s wrong?

@TalissonJunior I can, I'll just create a new repository with the same package json and a single index.ts file creating a new sequelize instance and failing to have a query accessible as well as the config being limited. I'll link you here when I do it.

This works for me.

var mySequelize = new Sequelize({
  host: 'host',
  database: 'database',
  dialect: 'dialect',
  username: 'username',
  password: 'password',
  modelPaths: [__dirname + '/Models/**/**']
});

mySequelize.query(query, {
  replacements: [this.user.id],
  model: mySequelize.models.MyModel // <-- This works for me.
});

Nice workaround @Rodrigo54, waiting for the v1.0.0 fix.

Fixed on branch 1.0.0 and is released with [email protected]

How should i do in my repo

@vipulpatel1994, if you use [email protected], this should do the trick:

sequelize.query('SELECT ...', {model: YourModel });
Was this page helpful?
0 / 5 - 0 ratings