Hi, thank you for writing this promising module! Right now, we are migrating from our existing sequelize model definitions to sequelize-typescript, but we are struggling with existing instance- and class-Methods. Is there a way to migrate them? If so, could you please provide an example? Thank you!
Greetings, Tim
Hey, thanks.
So you want to migrate classMethods and instanceMethods of a sequelize model definition to es6 class style?
This can be achieved with static and non-static methods like:
from
typescript
sequelize.define('User', {...}, {
classMethods: {
createSpecialUser() { ... }
},
instanceMethods: {
run() { ... }
}
});
to
````typescript
class User extends Model
static createSpecialUser() { ... }
run() { ... }
}
````
Thank you! I tryed this, but right now its not working. Can I still access the models via sequelize.models?
Here is a test code - if you run it, you get "TypeError: sequelize.models.Workstation.getDynamicRelations"
`import {Table, Model, PrimaryKey, Column, AutoIncrement, BelongsToMany,
DefaultScope, Scopes, DataType, Sequelize } from 'sequelize-typescript';
@Table
export class Workstation extends Model
@Column({ type: DataType.INTEGER })
position: number;
@Column({ type: DataType.STRING })
name: string;
@Column({ type: DataType.STRING })
redirect: string;
isSysModel() {
return false
}
static getDynamicRelations() {
}
}
const sequelize = new Sequelize({
name: 'some_db',
dialect: 'mysql',
username: 'root',
password: '',
storage: ':memory:'
});
sequelize.addModels([Workstation]);
sequelize.models.Workstation.getDynamicRelations();
`
Unfortunately accessing models via sequelize.models.* is not fully supported and not recommended with sequelize-typescript. You need to call them via its class reference Workstation.getDynamicRelations().
If you say, you cannot live without sequelize.models.* model access, I can investigate this one (?)
Ah, okay. Right now we are using the sequelize.models quite extensively... long term we could probably refactor a lot of it, but right now for migration it would ease the process fundamentally if there would be a way. Alternatively, I could use another function, like "getModels()" if that would make thinks easier...
@creffect I've published a new release (0.3.2) in which you can access the models dynamically with _ instead of models:
typescript
sequelize._['Workstation'].create(...)
sequelize._.Workstation.create(...)
I hope this helps :)
Thank you very much! :-)
@RobinBuschmann How is this working in 1.0? I want to create a Node relay resolver for GraphQL so I need to call the Sequelize model classes "dynamically" based on the Relay Node type.
import { nodeDefinitions } from 'graphql-relay-tools';
import { DB } from '../../system/database';
import { fromGlobalId } from 'graphql-relay';
const { nodeResolver } = nodeDefinitions((globalId: string) => {
const { type, id } = fromGlobalId(globalId);
const models = DB.getDB().sequelize.models;
const modelName = Object.keys(models).find(
(key: string) => key.toLowerCase() === (type && type.toLowerCase())
);
const Model = modelName ? models[modelName] : null;
if (!Model) {
return null;
}
return Model.findById(id);
});
export { nodeResolver };
but it's not working because it can't find findById and there's also no _
@lookapanda Your implementation looks correct to me. in 1.0 there is a models property on the sequelize instance - so exactly how you've done it. But I cannot reproduce your issue, it is working well for me. Can you provide more information here?
The 'this' context of type 'typeof Model' is not assignable to method's 'this' of type 'new () => Model<Model<any>>'.\n Cannot assign an abstract constructor type to a non-abstract constructor type.
Well, this is the error message I get on return Model.findById(id);
Most helpful comment
Hey, thanks.
So you want to migrate classMethods and instanceMethods of a sequelize model definition to es6 class style?
This can be achieved with static and non-static methods like:
from
{
typescript sequelize.define('User', {...}, { classMethods: { createSpecialUser() { ... } }, instanceMethods: { run() { ... } } });to
````typescript
class User extends Model
static createSpecialUser() { ... }
run() { ... }
}
````