작성
·
665
·
수정됨
0
안녕하세요.
npm run seed를 실행하면 아래와 같은 내용만 나오고 기본 값 생성이 되지 않습니다. 에러 메세지가 Unknown arguments: d, seed 이것뿐인 것 같은데.. 알 수 없는 인수.. d, seed가 무슨 말인지 모르겠습니다.
a-nest@0.0.1 seed
> ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed -d ./dataSource.ts
Usage: typeorm-extension <command> [options]
Commands:
typeorm-extension db:create Create database.
typeorm-extension db:drop Drop database.
typeorm-extension seed:run Populate the database with an initial data set or generated data by a
factory.
typeorm-extension seed:create Create a seeder file.
Options:
-h, --help Show help [boolean] -v, --version Show version number [boolean]
Unknown arguments: d, seed
dataSource.ts
import { DataSource } from 'typeorm';
import dotenv from 'dotenv';
import { ChannelChats } from './src/entities/ChannelChats';
import { ChannelMembers } from './src/entities/ChannelMembers';
import { Channels } from './src/entities/Channels';
import { DMs } from './src/entities/DMs';
import { Mentions } from './src/entities/Mentions';
import { Users } from './src/entities/Users';
import { WorkspaceMembers } from './src/entities/WorkspaceMembers';
import { Workspaces } from './src/entities/Workspaces';
dotenv.config();
const dataSource = new DataSource({
type: 'mysql',
host: 'localhost',
port: 3306,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: [
ChannelChats,
ChannelMembers,
Channels,
DMs,
Mentions,
Users,
WorkspaceMembers,
Workspaces,
],
migrations: [__dirname + '/src/migrations/*.ts'],
charset: 'utf8mb4_general_ci',
synchronize: false,
logging: true,
});
export default dataSource;
create-initial-data.ts
import { Seeder, SeederFactoryManager } from 'typeorm-extension';
import { DataSource } from 'typeorm';
import { Workspaces } from '../../entities/Workspaces';
import { Channels } from '../../entities/Channels';
export default class UserSeeder implements Seeder {
public async run(
dataSource: DataSource,
factoryManager: SeederFactoryManager,
): Promise<any> {
const workspacesRepository = dataSource.getRepository(Workspaces);
await workspacesRepository.insert([
{
id: 1,
name: 'Sleact',
url: 'sleact',
},
]);
const channelsRepository = dataSource.getRepository(Channels);
await channelsRepository.insert([
{
id: 1,
name: '일반',
WorkspaceId: 1,
private: false,
},
]);
}
}
.env
SECRET=제로초강의
PORT=3095
DB_USERNAME=root
DB_PASSWORD=password
DB_DATABASE=sleact
package.json
{
"name": "a-nest",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev-backup": "nest start --watch",
"start:dev": "nest build --webpack --webpackPath webpack-hmr.config.js --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
"db:create": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs db:create -d ./dataSource.ts",
"db:drop": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs db:drop -d ./dataSource.ts",
"seed": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed -d ./dataSource.ts",
"schema:drop": "ts-node ./node_modules/typeorm/cli.js schema:drop",
"schema:sync": "ts-node ./node_modules/typeorm/cli.js schema:sync",
"db:migrate": "npm run typeorm migration:run -- -d ./dataSource.ts",
"db:migrate:revert": "npm run typeorm migration:revert -- -d ./dataSource.ts",
"db:create-migration": "npm run typeorm migration:create -- ./src/migrations/",
"db:generate-migration": "npm run typeorm migration:generate -- ./src/migrations -d ./dataSource.ts"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.2",
"@nestjs/typeorm": "^10.0.0",
"bcrypt": "^5.1.0",
"class-validator": "^0.14.0",
"mysql2": "^3.6.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"swagger-ui-express": "^5.0.0",
"typeorm-extension": "^3.0.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"eslint": "^8.42.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.5.0",
"prettier": "^2.8.8",
"run-script-webpack-plugin": "^0.2.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typeorm-model-generator": "^0.4.6",
"typescript": "^5.1.3",
"webpack": "^5.88.2",
"webpack-node-externals": "^3.0.0"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
혹시 경로를 못찾는 건가 싶어서 데이터소스 파일쪽에 seeds의 경로를 넣어주려고 했으나 seeds에 오류가 발생해서 다시 원복 시켰었습니다.
(src>database>seeds>create-initial-data.ts 파일은 제로초님 깃에서 복붙했습니다.)
강의에서 듣기로는 dataSource > create-initial-data 로 넘어가면서 기본 값이 생성된다고 들었는데요 어디 부분을 확인해봐야 할까요??
답변 1
1
pacakge.json 명령어를 ...cjs seed:run -d ...으로 수정해보세요. 이것도 바뀐 것 같네요. 에러 메시지 잘 읽어보시면 사실 다 나와있습니다.
"seed": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed:run -d ./dataSource.ts",
이렇게 했는데도
Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
가 뜹니다 근데 DB는 만들어졌고 start:dev는 되는데 npm run seed는 안됩니다
seed로 나두면 윗분에러가 뜨고
seed:run으로 바꾸면 첨부한에러가뜹니다
아하 넵 뒷부분인데 스크립트 명령어만 seed:run으로 수정하고 있었네요..답변 감사합니다!