Create timer function to initialise card database from filesystem #14
11 changed files with 98 additions and 6 deletions
2
.dev.env
2
.dev.env
|
@ -22,3 +22,5 @@ DB_AUTH_USER=dev
|
|||
DB_AUTH_PASS=dev
|
||||
DB_SYNC=true
|
||||
DB_LOGGING=true
|
||||
|
||||
DB_CARD_FILE=card.db
|
|
@ -22,3 +22,5 @@ DB_AUTH_USER=prod
|
|||
DB_AUTH_PASS=prod
|
||||
DB_SYNC=false
|
||||
DB_LOGGING=false
|
||||
|
||||
DB_CARD_FILE=card.db
|
|
@ -22,3 +22,5 @@ DB_AUTH_USER=stage
|
|||
DB_AUTH_PASS=stage
|
||||
DB_SYNC=false
|
||||
DB_LOGGING=false
|
||||
|
||||
DB_CARD_FILE=card.db
|
6
src/constants/CardRarity.ts
Normal file
6
src/constants/CardRarity.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
export enum CardRarity {
|
||||
Bronze,
|
||||
Silver,
|
||||
Gold,
|
||||
Legendary,
|
||||
}
|
|
@ -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",
|
||||
],
|
||||
});
|
||||
|
||||
|
|
22
src/database/dataSources/cardDataSource.ts
Normal file
22
src/database/dataSources/cardDataSource.ts
Normal 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;
|
33
src/database/entities/card/Card.ts
Normal file
33
src/database/entities/card/Card.ts
Normal 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;
|
||||
}
|
||||
}
|
25
src/database/entities/card/Series.ts
Normal file
25
src/database/entities/card/Series.ts
Normal 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);
|
||||
}
|
||||
}
|
0
src/database/migrations/card/.gitkeep
Normal file
0
src/database/migrations/card/.gitkeep
Normal file
Loading…
Reference in a new issue