Pir*_*App 3 database-migration node.js sequelize.js typescript sequelize-cli
我想在我的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 }),
]);
}),
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 }),
]);
}),
};
Run Code Online (Sandbox Code Playgroud)
我在 package.json 文件中添加了 2 个命令作为种子
"apply-seeders": "sequelize-cli db:seed:all",
"revert-seeders": "sequelize-cli db:seed:undo:all",
Run Code Online (Sandbox Code Playgroud)
当我执行“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 %
Run Code Online (Sandbox Code Playgroud)
我做了一些深入研究,结果发现你不能按照这里的这个答案直接运行带有续集的打字稿文件
我修改了 .sequelizerc 文件以运行 dist 文件夹而不是 src 中的内容
.sequelizerc文件
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'),
};
Run Code Online (Sandbox Code Playgroud)
现在运行它会出现不同类型的错误
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)
Run Code Online (Sandbox Code Playgroud)
这将是我的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"],
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何使用打字稿运行我的播种机和迁移文件吗
更新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')
Run Code Online (Sandbox Code Playgroud)
更新2
我将方言 postgres 硬编码到配置文件中,但它仍然给我错误。我什至验证了转译的 js 文件已指定 postgres 方言
我设法让事情顺利进行
您的 .sequelizerc 和 config/index.ts 似乎绝对不能进行导入导出
这是我的/.sequelizerc
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'),
};
Run Code Online (Sandbox Code Playgroud)
这是我的/src/config/index.ts
const config: any = {
// if we are running tests we use an in memory db with sqlite
dialect: process.env.DB_DIALECT,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
host: process.env.POSTGRES_HOST,
port: Number(process.env.POSTGRES_PORT),
define: {
underscored: true,
},
logging: false,
};
module.exports = config;
Run Code Online (Sandbox Code Playgroud)
我在这里包括了一个播种者
这是我的/src/data/seeders/20221116042655-feeds.ts
import { QueryInterface } from 'sequelize';
import { feedTypes, feeds } from './fixtures';
const 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 }),
]);
});
const 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,
// @ts-ignore
ignoreDuplicates: true,
}),
queryInterface.bulkInsert('feeds', feeds, {
transaction,
// @ts-ignore
ignoreDuplicates: true,
}),
queryInterface.sequelize.query(
`SELECT setval('feeds_id_seq', (SELECT MAX(id) FROM feeds))`,
{ transaction },
),
]);
});
export { down, up };
Run Code Online (Sandbox Code Playgroud)
这里的操作顺序非常重要
创建所需的播种机
npx sequelize-cli seed:generate --name feeds
npx sequelize-cli seed:generate --name tag_rules
npx sequelize-cli seed:generate --name users
Run Code Online (Sandbox Code Playgroud)
将生成的每个js文件的扩展名改为ts
修改.sequelizerc
以读取转译的js文件dist
安装 babel-plugin-module-resolver
npm i --save-dev babel-plugin-module-resolver
Run Code Online (Sandbox Code Playgroud)
所有种子文件都使用导入/导出,唯一使用 require 和 module.exports 的文件是 .sequelizerc 和配置文件
如果有人知道如何让它们与导入/导出语法一起使用,请随时提出建议。
这就是我在package.json中的种子命令的样子
"apply-seeders": "node -r dotenv-flow/config ./node_modules/.bin/sequelize db:seed:all",
"revert-seeders": "node -r dotenv-flow/config ./node_modules/.bin/sequelize db:seed:undo:all",
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3195 次 |
最近记录: |