🗃️ Initial database configuration and schema setup

Signed-off-by: Pau Costa <mico@micodev.es>
CR
Pau Costa Ferrer 2024-02-01 17:59:18 +01:00
parent fcced63c34
commit 4582f78afd
4 changed files with 70 additions and 7 deletions

View File

@ -1,17 +1,19 @@
import "reflect-metadata" import "reflect-metadata"
import { DataSource } from "typeorm" import { DataSource } from "typeorm"
import { User } from "./entity/User" import { User } from "./entity/User"
import {Comment} from "./entity/Comment";
import {Post} from "./entity/Post";
export const AppDataSource = new DataSource({ export const AppDataSource = new DataSource({
type: "mysql", type: "mysql",
host: "localhost", host: "db",
port: 3306, port: 3306,
username: "test", username: process.env.MYSQL_USER,
password: "test", password: process.env.MYSQL_PASSWORD,
database: "test", database: process.env.MYSQL_DATABASE,
synchronize: true, synchronize: true,
logging: false, logging: false,
entities: [User], entities: [User, Comment, Post],
migrations: [], migrations: [],
subscribers: [], subscribers: [],
}) })

View File

@ -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[]
}

25
server/src/entity/Post.ts Normal file
View File

@ -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[]
}

View File

@ -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() @Entity()
export class User { export class User {
@ -13,6 +15,18 @@ export class User {
lastName: string lastName: string
@Column() @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[]
} }