sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

Model query problem when using webpack

Open nofx3000 opened this issue 2 years ago • 0 comments

Issue

Hi, recently i built a koa2 app with sequelize-typscript, and bundle it using it using webpack v5, it worked perfectly fine until when i changed webpack mode from development to production. When it conducted some model query, findAll for example, there was an error said "TypeError: Cannot read properties of undefined (reading 'findAll')". I assume something wrong happened when minimize , but i'm not sure.( I added 8 models on the sequlize instance, but when it minimized, it turned out with only 3.) Can you help me? Thanks.

Versions

  • sequelize: 6.28.0
  • sequelize-typescript: 2.1.5
  • typescript: 4.9.4
  • webpack: 5.75.0

Issue type

  • [x] bug report
  • [ ] feature request

Actual behavior

When codes were compiled to js, the Error: <-- GET /api/people/ ----- TypeError: Cannot read properties of undefined (reading 'findAll') at DivisionService. (/Users/dh/Documents/vacation-system-server/dist/src/service/DivisionService.js:22:34) at Generator.next () at /Users/dh/Documents/vacation-system-server/dist/src/service/DivisionService.js:8:71 at new Promise () at __awaiter (/Users/dh/Documents/vacation-system-server/dist/src/service/DivisionService.js:4:12) at DivisionService.findPeopleByDivision (/Users/dh/Documents/vacation-system-server/dist/src/service/DivisionService.js:21:16) at PeopleController. (/Users/dh/Documents/vacation-system-server/dist/src/controller/PeopleController.js:22:78) at Generator.next () at /Users/dh/Documents/vacation-system-server/dist/src/controller/PeopleController.js:8:71 at new Promise ()

Related code

webpack.config.js:

const path = require("path");
const webpack = require("webpack");
const nodeExternals = require("webpack-node-externals");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

const webpackConfig = {
  mode: **"production",**
  target: "node", 
  entry: {
    server: path.join(__dirname, "./bin/www.ts"),
  },
  output: {
    filename: "[name].bundle.js",
    path: path.join(__dirname, "./dist"),
  },
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: [
          "babel-loader",
          {
            loader: "ts-loader",
          },
        ],
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: [".js", ".ts"],
  },
  externals: [nodeExternals()], 
  plugins: [
    new CleanWebpackPlugin(), 
  ],
};

module.exports = webpackConfig;

seq.ts:

import { Sequelize } from "sequelize-typescript";
import { MYSQL_CONF } from "../conf/db";
import Models from "./models/index";

const seq = new Sequelize(MYSQL_CONF);
const models: any[] = [];
for (const model in Models) {
  models.push((Models as any)[model]);
}
seq.addModels(models);

export default seq;

people.model.ts:

import {
  Table,
  Column,
  Model,
  BelongsTo,
  ForeignKey,
  HasMany,
} from "sequelize-typescript";
import Division from "./divison.model";
import Record from "./record.model";

@Table({
  timestamps: false,
})
export default class People extends Model {
  @Column({
    allowNull: false,
  })
  name!: string;

  @Column
  catagory!: number;

  @Column
  work_age!: number;

  @ForeignKey(() => Division)
  @Column
  division_id!: number;

  @BelongsTo(() => Division)
  division!: Division;

  @HasMany(() => Record)
  record!: Record[];
}

PeopleService.ts:

import seq from "../db/seq";
import { PersonInfoInter } from "../interface/PeopleInterface";
class PeopleService {
  static PeopleService: PeopleService = new PeopleService();
  private People = seq.models.People;
  async findAllPeopleInfo() {
    return await this.People.findAll();
  }
}

export default PeopleService.PeopleService;

tsconfig.json:

{
"compilerOptions": {
"target": "es2016",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}

nofx3000 avatar Jan 14 '23 11:01 nofx3000