Sequelize 文档声称它可以与 Typescript 一起使用,但为了使其在生产中有用,需要使用数据库迁移脚本。这些只能使用 Sequelize CLI 执行,但这个 CLI 似乎根本不考虑 Typescript。它只生成(并且显然运行)JS 文件。NPM 上有一个“sequelize-cli-typescript”项目,但已经有 4 年历史了!Stack Overflow 上对相关问题的唯一答案实际上有 6 个字长,但没有一个有用:Using Sequelize cli with TypeScript
我的设置是这样的:
我使用 Typescript 创建了一个基本的 Node 应用程序,tsconfig.js如下所示:
{
  "compilerOptions": {
    "allowJs": true,
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*"
      ]
    }
  },
  "include": [
    "*"
  ]
}
我还将 ESLint 与以下内容一起使用.eslintrc:
{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint"
  ],
  "extends": …我想在我的express api上使用sequelize播种机和迁移,目前所有模型都是使用sequelize-typescript以typescript编写的
我尝试使用打字稿添加我的第一个种子文件,但运行时出现错误
20221028050116-feeds.ts播种文件
'use strict';
import { QueryInterface } from 'sequelize';
const feedTypes = [
  { id: 'b871a455-fddb-414c-ac02-2cdee07fa671', name: 'crypto' },
  { id: '68b15f90-19ca-4971-a2c6-67e66dc88f77', name: 'general' },
];
const feeds = [
  {
    id: 1,
    name: 'cointelegraph',
    url: 'https://cointelegraph.com/rss',
    feed_type_id: 'b871a455-fddb-414c-ac02-2cdee07fa671',
  },
];
module.exports = {
  up: (queryInterface: QueryInterface): Promise<number | object> =>
    queryInterface.sequelize.transaction(async (transaction) => {
      // here go all migration changes
      return Promise.all([
        queryInterface.bulkInsert('feed_types', feedTypes, { transaction }),
        queryInterface.bulkInsert('feeds', feeds, { transaction }),
      ]);
    }), …database-migration node.js sequelize.js typescript sequelize-cli