Sequelize-typescript: Can't add 'sequelize' as a table define option

Created on 28 Apr 2019  路  6Comments  路  Source: RobinBuschmann/sequelize-typescript

Versions

  • sequelize: 5.5.0
  • sequelize-typescript: 1.0.0-beta.2
  • typescript: 3.4.5

I'm submitting a ...
[x] bug report
[ ] feature request

Actual behavior:
I'm trying to specify what sequelize instance my models should use by using:

import sequelize from '../sequelize'
@Table({
  modelName: "team",
  tableName: "teams",
  sequelize: sequelize
})

where ../sequelize.ts is:

import "dotenv/config";
import { Sequelize } from "sequelize-typescript";

// Connect sequelize to database
const sequelize = new Sequelize({
  database: process.env.DATABASE || "",
  username: process.env.DATABASE_USER || "",
  password: process.env.DATABASE_PASSWORD,
  dialect: "postgres",
  models: [__dirname + "/models"]
});
export default sequelize;

However I'm getting the error message:

Unable to resolve signature of class decorator when called as an expression.
  Cannot invoke an expression whose type lacks a call signature. Type 'void' has no compatible call signatures.

It seems that I can't use the sequelize define option on @Table.

If I can't specify here what instance of sequelize to use, then how do I do it? ../sequelize.ts isn't referenced anywhere else in my server. If it's not able to be included in @Table then it's just a standalone export that isn't imported anywhere or used anywhere in my app.

Most helpful comment

@TidyIQ You could import it in your main app file or your main index file - if exists:

// app.ts or index.ts
import './sequelize';

All 6 comments

Hey @TidyIQ this is because the property sequelize isn't part of the TableOptions interface:

@Table({
  modelName: "team",
  tableName: "teams",
  sequelize: sequelize // Omit this line and it should work
})

Hope this helps!

If I remove it then I get the error:

Model not initialized: Member \"findAll\" cannot be called. \"Team\" needs to be added to a Sequelize instance.

@TidyIQ You have to import your sequelize setup (./sequelize.ts) somewhere - otherwise sequelize-typescript cannot load you models. If you did and it still gives you this error, probably the path to your models is wrong (models: [__dirname + "/models"])

Sorry if I'm unclear. That's exactly what I'm asking. How do I import it? Previously I just specified it as a define option when I was using normal sequelize but like you said, that's not possible with sequelize-typescript.

I see you used sync in your example however I don't want to do that (and I can't anyway due to how I've setup my permissions).

@TidyIQ You could import it in your main app file or your main index file - if exists:

// app.ts or index.ts
import './sequelize';

OK cool, that was easy. Thanks, it's working.

Was this page helpful?
0 / 5 - 0 ratings