🗃️ Initial database configuration and schema setup
Signed-off-by: Pau Costa <mico@micodev.es>CR
parent
fcced63c34
commit
4582f78afd
|
|
@ -1,17 +1,19 @@
|
|||
import "reflect-metadata"
|
||||
import { DataSource } from "typeorm"
|
||||
import { User } from "./entity/User"
|
||||
import {Comment} from "./entity/Comment";
|
||||
import {Post} from "./entity/Post";
|
||||
|
||||
export const AppDataSource = new DataSource({
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
host: "db",
|
||||
port: 3306,
|
||||
username: "test",
|
||||
password: "test",
|
||||
database: "test",
|
||||
username: process.env.MYSQL_USER,
|
||||
password: process.env.MYSQL_PASSWORD,
|
||||
database: process.env.MYSQL_DATABASE,
|
||||
synchronize: true,
|
||||
logging: false,
|
||||
entities: [User],
|
||||
entities: [User, Comment, Post],
|
||||
migrations: [],
|
||||
subscribers: [],
|
||||
})
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
import {Column, Entity, JoinTable, ManyToMany, ManyToOne, PrimaryGeneratedColumn} from "typeorm";
|
||||
import {User} from "./User";
|
||||
|
||||
@Entity()
|
||||
export class Comment {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number
|
||||
|
||||
@Column()
|
||||
content: string
|
||||
|
||||
@Column()
|
||||
createdAt: Date
|
||||
|
||||
@ManyToOne(() => User, user => user.posts)
|
||||
createdBy: User
|
||||
|
||||
@ManyToMany(()=> User)
|
||||
@JoinTable()
|
||||
likedBy: User[]
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
import {Column, Entity, JoinTable, ManyToMany, ManyToOne, PrimaryGeneratedColumn} from "typeorm";
|
||||
import {User} from "./User";
|
||||
|
||||
@Entity()
|
||||
export class Post {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number
|
||||
|
||||
@Column()
|
||||
title: string
|
||||
|
||||
@Column()
|
||||
content: string
|
||||
|
||||
@Column()
|
||||
createdAt: Date
|
||||
|
||||
@ManyToOne(() => User, user => user.posts)
|
||||
createdBy: User
|
||||
|
||||
@ManyToMany(()=> User)
|
||||
@JoinTable()
|
||||
likedBy: User[]
|
||||
|
||||
}
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
|
||||
import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, OneToMany, JoinTable} from "typeorm"
|
||||
import {Post} from "./Post";
|
||||
import {Comment} from "./Comment";
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
|
|
@ -13,6 +15,18 @@ export class User {
|
|||
lastName: string
|
||||
|
||||
@Column()
|
||||
age: number
|
||||
email: string
|
||||
|
||||
@Column()
|
||||
password: string
|
||||
|
||||
@ManyToMany(type => User)
|
||||
@JoinTable()
|
||||
followed: User[]
|
||||
|
||||
@OneToMany(type => Post, post=> post.createdBy)
|
||||
posts: Post[]
|
||||
|
||||
@OneToMany(type => Comment, comment=> comment.createdBy)
|
||||
comments: Comment[]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue