Unable to get 'export default' to work
I have an issue with sequelize-typescript. I cannot duplicate the success in this example.
I create a table definition i.e.:
@Table({tableName: "security.security_user"}) export default class User extends Model<User> { ...
Then the 'default' doesn't really do anything, and it cannot resolve even the functions in the defined class at 'tsc' compile time. If I don't define 'default' I get the same thing. Your example does not use default and it works fine.
If I define:
@Table({tableName: "security.security_user"}) export class User extends Model<User> { ...
and don't set a default there, but define something like 'export default this.Model('User', User)' after I close the definition, then compile works, but at run time, it fails because 'this.Model' is nonsense.
Your example imports using import {Movie} from '../models/Movie', but My routes complain that I have no exported members. I can only import using import User from '../models/User', .. i.e. no braces.
I would appreciate any advice you can give.
Hey @dougreed, I don't fully understand your issue.
When using default exports like export default class User extends Model<User> {} you need to import them with import User from './path/to/user' or import {default as User} from './path/to/user'
For named exports like export class User extends Model<User> {} use import {User} from './path/to/user'
If this doesn't solve your problem, can you provide an example repo, so that I can have a look at it?
Robin,
Thanque for replying. I am sure the problem is my own lack of understanding in this new and strange language that is JavaScript turned into a real language. I am trying to swap out ‘Mongoose' with 'sequelize-typescript’.
I think the problem is that I have created a Class, but not an Instance. I am far more used to C++ or Java, and have yet to wrap my head around the fact that JavaScript is a real language now. I have since found a nodeJS guru to point me in the right direction, but we still had issues getting your code to work. We didn’t spend a long time on it, but essentially, in the code I was working with using Mongoose, the ‘User' table was defined with 'const UserSchema = new mongoose.Schema({‘, and after defining the Class, we add 'export default mongoose.model('User', UserSchema)’, and everything is resolved. In 'sequelize-typescript’, I copied your example, but the functions were not resolved at compile time. If I added some form of export default at the end of the 'User.ts' file outside the class definition, then the tsc compiler resolved everything, but I didn’t really know what to export, so it all blew up at run time because the instance was null. In your example, you use:
static scope(name: string = 'defaultScope'): typeof Actor { return super.scope.call(this, name); }
Which seems to be the magic incantation, but this did not work for me. When I added that, I got the error ‘TS2417 Class static side ‘typeof User’ incorrectly extends base class static side ’typeof Model’. When I tried to create a User object with 'new User()’, it changed nothing.
It would be nice if your examples and description contained examples of adding functions within a 'sequelize-typescript’ definition. The instructions say that it works like ‘sequelize’, but an example would be nice.
In the end, I ended up using knex because I could get that to work, and also my project requires multiple database connections, and your 'Recommendations and limitations’ state 'One Sequelize instance per model’.
I don’t really think there is anything fundamentally wrong with ‘sequelize’ or 'sequelize-typescript’, but I just could not figure out how to get it to work, and because of the caveat, there was no point in pursuing it.
Douglas Reed [email protected]
On Mar 4, 2018, at 2:16 PM, Robin Buschmann [email protected] wrote:
Hey @dougreed https://github.com/dougreed, I don't fully understand your issue.
When using default exports like export default class User extends Model<User> {} you need to import them with import User from './path/to/user' or import {default as User} from './path/to/user'
For named exports like export class User extends Model<User> {} use import {User} from './path/to/user'
If this doesn't solve your problem, can you provide an example repo, so that I can have a look at it?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RobinBuschmann/sequelize-typescript-example/issues/1#issuecomment-370255365, or mute the thread https://github.com/notifications/unsubscribe-auth/AHh8CAflH7S2kLpk9gl0OBpo3kHzR0Jxks5tbD11gaJpZM4SbLYT.