How to create a typescript model with instance methods ?
See original GitHub issueHello everybody,
In first, thank you for this amazing package.
I’m currently working on typescript projet an we want to use Sequelize as PG ORM, but we’re totally confused on the model creation (with instance method) since v4:
// db/index.ts)
import * as Sequelize from "sequelize";
import {config} from "../config";
import UserModel from "./models/users"
const sequelize = new Sequelize(config.database.schema, config.database.username, config.database.password, {
host: config.database.host,
dialect: "postgres",
});
// sequelize.sync({force: true}).then(() => console.log("Database synchronized."));
const Users = sequelize.import("./models/users");
Users.create({
first_name: "Test",
last_name: "TEST",
email: "example@example.com",
password: "123456",
}).then((user) => {
console.log("================> verifyPassword: " + user.verifyPassword("123456"));
});
export {sequelize};
// db/models/users.js
import * as Sequelize from "sequelize";
import {createHash} from "crypto";
import {v4} from "uuid";
import {config} from "../../config";
interface IUserModelAttribute {
id?: string;
email?: string;
first_name?: string;
last_name?: string;
gender?: string;
phone_number?: string;
confirmation_token?: string;
confirmation_date?: string;
password?: string;
status?: string;
password_reset_token?: string;
password_reset_date?: string;
lang?: string;
created_at?: string;
updated_at?: string;
deleted_at?: string;
}
interface IUserModelInstance extends Sequelize.Instance<IUserModelAttribute>, IUserModelAttribute {
verifyPassword(password: string): boolean;
}
const UserModel = (sequelize: Sequelize.Sequelize, dataTypes: Sequelize.DataTypes) => {
const User = sequelize.define<IUserModelInstance, IUserModelAttribute>(
"users",
{
id: {
type: dataTypes.INTEGER(11),
primaryKey: true,
autoIncrement: true,
},
email: {
type: dataTypes.STRING(255),
allowNull: false,
},
password: {
type: dataTypes.STRING(255),
allowNull: false,
},
},
{
tableName: "users",
underscored: true,
freezeTableName: true,
paranoid: true,
},
);
User.prototype.verifyPassword = function(password: string) {
const hash = createHash(config.password.type)
.update(password + config.password.salt)
.digest("hex");
return this.password === hash;
};
return User;
};
export {UserModel as default};
When I try to build, typescript say me
Property 'prototype' does not exist on type 'Model<IUserModelInstance, IUserModelAttribute>'.
Package version:
"sequelize": "^4.38.0",
"@types/sequelize": "^4.27.24",
"typescript": "^3.0.1"
Do you know how can I solve it ?
Thank you ! Regards,
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:18 (3 by maintainers)
Top Results From Across the Web
Creating instance methods in a Sequelize Model using ...
node. js - Creating instance methods in a Sequelize Model using Typescript - Stack Overflow. Stack Overflow for Teams – Start collaborating and ......
Read more >Statics and Methods in TypeScript - Mongoose
You can define instance methods and static functions on Mongoose models. ... To define an instance method in TypeScript, create a new interface...
Read more >Handbook - Classes - TypeScript
When you declare a class in TypeScript, you are actually creating multiple declarations at the same time. The first is the type of...
Read more >How To Use Classes in TypeScript | DigitalOcean
A constructor is a method that runs every time a new instance of the class is created. This can be used to initialize...
Read more >TypeScript - Class or Interface for Model?
On top of being capable of defining a structure, Class lets us create an object instance of it. With that, we can include...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Any idea of how to do this now that
Sequelize.Instance
no longer exist in v5?Done @papb , here it is https://github.com/sequelize/sequelize/issues/11215