Create timer function to initialise card database from filesystem #14

Merged
Vylpes merged 2 commits from feature/9-timer-function into develop 2023-09-03 17:26:47 +01:00
11 changed files with 98 additions and 6 deletions
Showing only changes of commit fb62be96b6 - Show all commits

View file

@ -22,3 +22,5 @@ DB_AUTH_USER=dev
DB_AUTH_PASS=dev
DB_SYNC=true
DB_LOGGING=true
DB_CARD_FILE=card.db

View file

@ -22,3 +22,5 @@ DB_AUTH_USER=prod
DB_AUTH_PASS=prod
DB_SYNC=false
DB_LOGGING=false
DB_CARD_FILE=card.db

View file

@ -22,3 +22,5 @@ DB_AUTH_USER=stage
DB_AUTH_PASS=stage
DB_SYNC=false
DB_LOGGING=false
DB_CARD_FILE=card.db

View file

@ -0,0 +1,6 @@
export enum CardRarity {
Bronze,
Silver,
Gold,
Legendary,
}

View file

@ -13,13 +13,13 @@ const AppDataSource = new DataSource({
synchronize: process.env.DB_SYNC == "true",
logging: process.env.DB_LOGGING == "true",
entities: [
"dist/database/entities/**/*.js",
"dist/database/entities/app/**/*.js",
],
migrations: [
"dist/database/migrations/**/*.js",
"dist/database/migrations/app/**/*.js",
],
subscribers: [
"dist/database/subscribers/**/*.js",
"dist/database/subscribers/app/**/*.js",
],
});

View file

@ -0,0 +1,22 @@
import { DataSource } from "typeorm";
import * as dotenv from "dotenv";
dotenv.config();
const CardDataSource = new DataSource({
type: "sqlite",
database: process.env.DB_CARD_FILE!,
synchronize: process.env.DB_SYNC == "true",
logging: process.env.DB_LOGGING == "true",
entities: [
"dist/database/entities/card/**/*.js",
],
migrations: [
"dist/database/migrations/card/**/*.js",
],
subscribers: [
"dist/database/subscribers/card/**/*.js",
],
});
export default CardDataSource;

View file

@ -0,0 +1,33 @@
import { Column, Entity, OneToMany } from "typeorm";
import BaseEntity from "../../../contracts/BaseEntity";
import { CardRarity } from "../../../constants/CardRarity";
import Series from "./Series";
import CardDataSource from "../../dataSources/cardDataSource";
@Entity()
export default class Card extends BaseEntity {
constructor(id: string, name: string, rarity: CardRarity) {
super();
this.Id = id;
this.Name = name;
this.Rarity = rarity;
}
@Column()
Name: string;
@Column()
Rarity: CardRarity;
@OneToMany(() => Series, x => x.Cards)
Series: Series;
public static async FetchAllByRarity(rarity: CardRarity): Promise<Card[]> {
const repository = CardDataSource.getRepository(Card);
const all = await repository.find({ where: { Rarity: rarity }});
return all;
}
}

View file

@ -0,0 +1,25 @@
import { Column, Entity, ManyToOne } from "typeorm";
import BaseEntity from "../../../contracts/BaseEntity";
import Card from "./Card";
@Entity()
export default class Series extends BaseEntity {
constructor(id: string, name: string) {
super();
this.Id = id;
this.Name = name;
}
@Column()
Name: string;
@ManyToOne(() => Card, x => x.Series)
Cards: Card[];
public async AddCard(card: Card) {
if (!this.Cards) return;
this.Cards.push(card);
}
}

View file