sequelize-typescript
sequelize-typescript copied to clipboard
Cannot read properties of undefined (reading 'Model')
Issue
I am unable to use this package as it seems to fail at Model declaration
Versions
- sequelize: 6.28.0
- sequelize-typescript: 2.1.5
- typescript: 4.9.5
- vite: 4.1.1
Issue type
- [x ] bug report
- [ ] feature request
Actual behavior
Trying to add/use model fails.
Expected behavior
Model loading should be working normally.
Steps to reproduce
- create Mail.ts
import type {Optional} from "sequelize"
import {DataTypes} from "sequelize"
import {Column, Model, Table} from "sequelize-typescript"
interface MailAttributes
{
id: number
uid: number
subject: string
createdAt: Date
}
@Table
export default class Mail extends Model<MailAttributes, Optional<MailAttributes, "id">>
{
@Column
uid!: number
@Column(DataTypes.TEXT)
subject!: string
@Column(DataTypes.DATE)
createdAt!: Date
}
- add it to sequelize
import {Sequelize} from "sequelize"
import Mail from "./model/Mail"
const sequelize = new Sequelize("postgres://dns", {
logging: false,
pool: {
max: 10,
min: 3,
idle: 30000,
acquire: 60000,
},
})
sequelize.authenticate()
sequelize.modelManager.addModel(Mail)
export {sequelize, Mail}
- try to use it
import {Mail, sequelize} from "../db/adapter.server"
const mail = Mail.build({
uid: 0,
createdAt: new Date(),
subject: "test",
})
mail.save()
- get error bellow
Internal server error: Cannot read properties of undefined (reading 'Model')
at eval (/src/lib/db/model/Mail.ts:18:48)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async instantiateModule (file:///project/node_modules/vite/dist/node/chunks/dep-3007b26d.js:52400:9)
TypeError: Cannot read properties of undefined (reading 'Model')
at /project/src/lib/db/model/Mail.ts:14:34
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async instantiateModule (file:///projectnode_modules/vite/dist/node/chunks/dep-3007b26d.js:52400:9)
Related code
Sample code can be found above tsconfig.json
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"useDefineForClassFields": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
}
}
the included config
{
"compilerOptions": {
"paths": {
"$lib": [
"../src/lib"
],
"$lib/*": [
"../src/lib/*"
]
},
"rootDirs": [
"..",
"./types"
],
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"preserveValueImports": true,
"lib": [
"esnext",
"DOM",
"DOM.Iterable"
],
"moduleResolution": "node",
"module": "esnext",
"target": "esnext"
},
"include": [
"ambient.d.ts",
"./types/**/$types.d.ts",
"../vite.config.ts",
"../src/**/*.js",
"../src/**/*.ts",
"../src/**/*.svelte",
"../src/**/*.js",
"../src/**/*.ts",
"../src/**/*.svelte",
"../tests/**/*.js",
"../tests/**/*.ts",
"../tests/**/*.svelte"
],
"exclude": [
"../node_modules/**",
"./[!ambient.d.ts]**",
"../src/service-worker.js",
"../src/service-worker.ts",
"../src/service-worker.d.ts"
]
}
modelManager is never mentioned in the documentation.
import { Sequelize } from 'sequelize-typescript';
const sequelize = new Sequelize("postgres://dns", {
logging: false,
pool: {
max: 10,
min: 3,
idle: 30000,
acquire: 60000,
},
models:[Mail]
})
maybe better.