I want to use sequelize seeders and migrations on my express api and currently all the models are written in typescript using sequelize-typescript
I tried adding my first seeder file using typescript and I get an error when running it
20221028050116-feeds.ts seeder file
'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 }),
]);
}),
};
I added 2 commands in my package.json file to seed
"apply-seeders": "sequelize-cli db:seed:all",
"revert-seeders": "sequelize-cli db:seed:undo:all",
When I execute 'npm run apply-seeders', it gives me the following error
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 %
I did some digging into it and it turns out that you cannot directly run typescript files with sequelize as per THIS ANSWER here
I modified my .sequelizerc file to run stuff from dist folder instead of src
.sequelizerc file
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'),
};
Running this now gives me a different type of error
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)
This would be my tsconfig.json file
{
"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"],
}
}
}
Can someone kindly tell me how I can run my seeder and migration files using typescript
UPDATE 1
I installed the babel-plugin-module-resolver. Now it gives me a new error. This error doesnt show up if you run the ts files normally. When I console.log I can see all the values but when the program is run, that dialect simply doesnt load it seems from the env file
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')
UPDATE 2
I hardcoded the dialect postgres into the config file and it still gives me the error. I even verified that the transpiled js file has the postgres dialect specified