add typorm migration
This commit is contained in:
23
src/data-source.ts
Normal file
23
src/data-source.ts
Normal 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
16
src/entity/user.ts
Normal 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
|
||||
}
|
||||
15
src/migration/1761571888108-add_user_table.ts
Normal file
15
src/migration/1761571888108-add_user_table.ts
Normal 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"`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user