add typorm migration

This commit is contained in:
Roland Schneider 2025-10-27 15:33:13 +01:00
parent bcbfd8eac5
commit f82202f973
5 changed files with 59 additions and 2 deletions

View File

@ -11,4 +11,4 @@ services:
adminer:
image: adminer
ports:
- 4302:8080
- "4302:8080"

View File

@ -17,7 +17,10 @@
"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"
"test:e2e": "jest --config ./test/jest-e2e.json",
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js",
"migration:generate": "npm run typeorm -- -d src/data-source.ts migration:generate src/migration/$npm_config_name",
"migration:run": "npm run typeorm -- -d src/data-source.ts migration:run"
},
"dependencies": {
"@nestjs/common": "^11.0.1",

23
src/data-source.ts Normal file
View File

@ -0,0 +1,23 @@
import 'reflect-metadata';
import { DataSource } from 'typeorm';
import { User } from './entity/user';
import * as dotenv from 'dotenv';
dotenv.config();
export const AppDataSource = new DataSource({
type: 'postgres',
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT as string, 10),
username: process.env.DATABASE_USER,
password: process.env.DATABASE_PASS,
database: process.env.DATABASE_NAME,
synchronize: false,
logging: false,
entities: [User],
migrations: [
'src/migration/**/*.ts'
],
subscribers: [],
});

16
src/entity/user.ts Normal file
View File

@ -0,0 +1,16 @@
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm"
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Column()
username: string
@Column()
email: string
@Column()
password: string
}

View File

@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddUserTable1761571888108 implements MigrationInterface {
name = 'AddUserTable1761571888108';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "user" ("id" SERIAL NOT NULL, "username" character varying NOT NULL, "email" character varying NOT NULL, "password" character varying NOT NULL, CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "user"`);
}
}