Sequelize with TypeScript - class fields undefined
See original GitHub issueWhat are you doing?
I’am using Babel for code compilation + TypeScript for type-checking. Two scenarios:
- Using define, which is “really wouldn’t recommend this” (according types/test/define.ts comments)
interface User extends Model {
id: number;
login: string;
}
type UserModel = {
new (): User;
} & typeof Model;
const User = sequelize.define(
'User',
{ login: DataTypes.STRING },
{ tableName: 'user' }
) as UserModel;
- Using new approach (based on code, found in the types/models/User.ts)
export class User extends Model {
public id: number;
public login: string;
}
User = User.init(
{ login: DataTypes.STRING },
{ sequelize, tableName: 'user' }
);
In DB (Postgres) I have one row with login ‘admin’.
For the test, I use this function.
async function test() {
const user = (await User.findOne()) as User;
console.log('login', user.login, user.get('login'));
}
test();
What do you expect to happen?
It is logical that in both scenarios I should get
login admin admin
What is actually happening?
However, if in “modern”, second approach I see
login undefined admin
And if I print console.log(user) I see
user1 User {
dataValues:
{ id: 1,
login: 'admin',
createdAt: 2019-03-18T23:00:33.516Z,
updatedAt: 2019-03-18T23:00:33.516Z },
_previousDataValues:
{ id: 1,
login: 'admin',
createdAt: 2019-03-18T23:00:33.516Z,
updatedAt: 2019-03-18T23:00:33.516Z },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: false,
underscored: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: {},
indexes: [],
name: { plural: 'Users', singular: 'User' },
omitNull: false,
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [PostgresDialect],
queryInterface: [QueryInterface],
models: [Object],
modelManager: [ModelManager],
connectionManager: [ConnectionManager],
importCache: {},
test: [Object] },
tableName: 'user',
hooks: {} },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [ 'id', 'login', 'createdAt', 'updatedAt' ] },
isNewRecord: false,
id: undefined,
login: undefined }
Dialect: postgres “sequelize”: “^5.1.0”,
upd: Just tested with ts-node - everything works as it should. Here is my .babelrc
{
"sourceMaps": "inline",
"retainLines": true,
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": true
}
}
],
"@babel/preset-typescript"
],
"plugins": [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-object-rest-spread",
"@babel/proposal-class-properties"
]
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:41 (6 by maintainers)
Top Results From Across the Web
Configuring babel-typescript for Sequelize ORM causes ...
Columns should be retrieved through the Object.defineProperty() but when an existing property already exists it is skipped see source here. When ...
Read more >TypeScript - Sequelize
As Sequelize heavily relies on runtime property assignments, TypeScript won't be very useful out of the box. A decent amount of manual type ......
Read more >cannot read properties of undefined (reading 'findall') - You.com
when you use the sequelize model you need to use the defined model class name, which is "user" instead of "User" in line...
Read more >sequelize-typescript - npm
A primary key ( id ) will be inherited from base class Model . This primary key is by default an INTEGER and...
Read more >Understanding the exclamation mark in TypeScript
The non-null assertion operator tells the TypeScript compiler that a value typed as optional cannot be null or undefined .
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
Please reopen
Closed huh? Reopen this.