설명!
Gather 와 비슷한 메타버스 프로젝트 진행중이고
간단하게 전체챗, 공간을 DB에 구현하고
주요기능은 소켓으로 처리할 예정이다.
npm i --save typeorm @nestjs/typeorm mysql2 typeorm-extension
export class Users {
@PrimaryGeneratedColumn({ type: 'int', name: 'id' })
id: number;
...
@Column('varchar', { name: 'email', length: 255, unique: true })
email: string;
...
@OneToMany(() => Invites, (Invites) => Invites.TargetId, {
cascade: true,
})
TargetUserId: Invites[];
}
...
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: +process.env.MYSQL_PORT,
username: process.env.MYSQL_USERNAME,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
entities: [Users, Spaces, Chats, Invites, UserInSpace],
synchronize: false,
keepConnectionAlive: true,
logging: true,
charset: 'utf8mb4',
// 이모티콘 쓰기 위해
}),
typeorm-extension은 app.module에 있는 typeOrm설정을 읽지 못한다.
import { DataSource } from 'typeorm';
require('dotenv').config();
import { Users } from './src/entities/Users';
import { Chats } from './src/entities/Chats';
import { Spaces } from './src/entities/Spaces';
import { UserInSpace } from './src/entities/UserInSpace';
import { Invites } from './src/entities/Invites';
// dotenv.config();
const dataSource = new DataSource({
type: 'mysql',
host: 'localhost',
port: +process.env.MYSQL_USERNAME,
username: process.env.MYSQL_USERNAME,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
entities: [Users, Spaces, Chats, Invites, UserInSpace],
migrations: [__dirname + '/src/migrations/*.ts'],
charset: 'utf8mb4_general_ci',
synchronize: false,
logging: true,
});
export default dataSource;
"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:run -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 --dataSource ./dataSource.ts",
"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"
// package.json
extension 으로 쿼리문 대신 javascript로 db를 조작할 수 있다.