我想在我的express api上使用sequelize seeder和migrations,目前所有模型都是使用sequelize-typescript以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 }),
]);
}),
down: (queryInterface: QueryInterface): Promise<object | object> =>
queryInterface.sequelize.transaction(async (transaction) => {
// here go all migration undo changes
return Promise.all([
queryInterface.bulkDelete('feed_types', null, { transaction }),
queryInterface.bulkDelete('feeds', null, { transaction }),
]);
}),
};
我在package.json文件中添加了2个命令以进行种子生成
"apply-seeders": "sequelize-cli db:seed:all",
"revert-seeders": "sequelize-cli db:seed:undo:all",
当我执行'npm run apply-seeders'时,它给出了以下错误
Sequelize CLI [Node: 16.17.0, CLI: 6.5.1, ORM: 6.23.2]
ERROR: Cannot find "/Users/vr/Desktop/code/ch/api/src/config/index.js". Have you run "sequelize init"?
ERROR: Cannot read properties of undefined (reading 'detail')
sequelize-cli db:seed:all
Run every seeder
Options:
--version Show version number [boolean]
--help Show help [boolean]
--env The environment to run the command in [string] [default: "development"]
--config The path to the config file [string]
--options-path The path to a JSON file with additional options [string]
--migrations-path The path to the migrations folder [string] [default: "migrations"]
--seeders-path The path to the seeders folder [string] [default: "seeders"]
--models-path The path to the models folder [string] [default: "models"]
--url The database connection string to use. Alternative to using --config files [string]
--debug When available show various debug information [boolean] [default: false]
TypeError: Cannot read properties of undefined (reading 'detail')
at Object.error (/Users/vr/Desktop/code/ch/api/node_modules/sequelize-cli/lib/helpers/view-helper.js:43:24)
at /Users/vr/Desktop/code/ch/api/node_modules/sequelize-cli/lib/commands/seed.js:48:39
at async Object.exports.handler (/Users/vr/Desktop/code/ch/api/node_modules/sequelize-cli/lib/commands/seed.js:24:7)
vr@vivz api %
我做了一些深入研究,结果表明,您不能直接运行带有sequelize的typescript文件,如这里的THIS ANSWER
我修改了我的.sequelizerc文件以运行dist文件夹中的内容,而不是src文件夹中的内容
.续集文件
require("@babel/register");
const path = require('path');
module.exports = {
config: path.resolve('dist', 'config', 'index.js'),
'migrations-path': path.resolve('dist', 'data', 'migrations'),
'models-path': path.resolve('dist', 'data', 'models'),
'seeders-path': path.resolve('dist', 'data', 'seeders'),
};
现在运行此命令会出现不同类型的错误
Sequelize CLI [Node: 16.17.0, CLI: 6.5.1, ORM: 6.23.2]
ERROR: Error reading "dist/config/index.js". Error: Error: Cannot find module 'babel-plugin-module-resolver'
Require stack:
- /Users/vr/Desktop/code/ch/api/node_modules/@babel/core/lib/config/files/plugins.js
- /Users/vr/Desktop/code/ch/api/node_modules/@babel/core/lib/config/files/index.js
- /Users/vr/Desktop/code/ch/api/node_modules/@babel/core/lib/index.js
- /Users/vr/Desktop/code/ch/api/node_modules/@babel/register/lib/worker/babel-core.js
- /Users/vr/Desktop/code/ch/api/node_modules/@babel/register/lib/worker/handle-message.js
- /Users/vr/Desktop/code/ch/api/node_modules/@babel/register/lib/worker-client.js
- /Users/vr/Desktop/code/ch/api/node_modules/@babel/register/lib/node.js
- /Users/vr/Desktop/code/ch/api/node_modules/@babel/register/lib/nodeWrapper.js
- /Users/vr/Desktop/code/ch/api/node_modules/@babel/register/lib/index.js
- /Users/vr/Desktop/code/ch/api/.sequelizerc
- /Users/vr/Desktop/code/ch/api/node_modules/sequelize-cli/lib/core/yargs.js
- /Users/vr/Desktop/code/ch/api/node_modules/sequelize-cli/lib/sequelize
ERROR: Cannot read properties of undefined (reading 'detail')
sequelize-cli db:seed:all
Run every seeder
Options:
--version Show version number [boolean]
--help Show help [boolean]
--env The environment to run the command in [string] [default: "development"]
--config The path to the config file [string]
--options-path The path to a JSON file with additional options [string]
--migrations-path The path to the migrations folder [string] [default: "migrations"]
--seeders-path The path to the seeders folder [string] [default: "seeders"]
--models-path The path to the models folder [string] [default: "models"]
--url The database connection string to use. Alternative to using --config files [string]
--debug When available show various debug information [boolean] [default: false]
TypeError: Cannot read properties of undefined (reading 'detail')
at Object.error (/Users/vr/Desktop/code/ch/api/node_modules/sequelize-cli/lib/helpers/view-helper.js:43:24)
at /Users/vr/Desktop/code/ch/api/node_modules/sequelize-cli/lib/commands/seed.js:48:39
at async Object.exports.handler (/Users/vr/Desktop/code/ch/api/node_modules/sequelize-cli/lib/commands/seed.js:24:7)
这将是我的tsconfig.json文件
{
"compilerOptions": {
"lib": ["es2020"],
"module": "commonjs",
"moduleResolution": "node",
"target": "es2020",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
"outDir": "dist",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"baseUrl": ".",
"paths": {
"server/*": ["src/server/*"],
"tests/*": ["src/tests/*"],
"data/*": ["src/data/*"],
"config": ["src/config"],
}
}
}
有人能告诉我如何使用typescript运行播种程序和迁移文件吗
更新1
我安装了babel-plugin-module-resolver。现在它给了我一个新的错误。如果你正常运行ts文件,这个错误不会出现。当我运行console.log时,我可以看到所有的值,但是当程序运行时,那个方言根本无法加载,它似乎是从env文件中加载的
Loaded configuration file "dist/config/index.js".
ERROR: Dialect needs to be explicitly supplied as of v4.0.0
ERROR: Cannot read properties of undefined (reading 'detail')
更新2
我将postgres方言硬编码到配置文件中,但它仍然给我错误。
2条答案
按热度按时间xbp102n01#
我在运行全局安装的sequelize命令时也遇到了同样的问题。这是一个奇怪的问题,因为在其他项目上它们都很好(不是TypeScript项目)。
这一次我尝试在本地安装sequelize并使用“npx”运行它们。它再次运行得很好。例如:
我的设置是节点18.10,续集6.25.5,第8.8.0页。
rjee0c152#
我设法让事情顺利进行
你的.sequelizerc和config/index.ts绝对不能有导入导出,看来
这是我的**/.续集**
必需(“@巴别塔/寄存器”);
const path =需要('path');
下面是我的**/源代码/配置/索引.ts**
我这里包括一个播种机
这是我的博客,我的博客是我的博客,我的博客是我的博客,我的博客是我的博客
操作的顺序在这里非常重要
1.创建所需种子
1.将每个生成的js文件的扩展名更改为ts
1.修改**
.sequelizerc
以从dist
**读取传输的js文件1.安装标签插件模块解析器
所有种子文件都使用导入/导出,而使用require和module.exports的文件只有.sequelizerc和config文件
如果有人知道如何让这些与导入/导出语法一起工作,请随时提出建议。
这是我在package.json中的seed命令的样子