Last active
July 26, 2020 21:40
-
-
Save manchuwook/57581af4545da861e8b84bb5a131c549 to your computer and use it in GitHub Desktop.
Creating a TypeORM entity and using it in a module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tslint:disable: quotemark | |
// modules | |
import { Module } from '@nestjs/common'; | |
import { CharacterModule } from './character/character.module'; | |
import { CampaignModule } from './campaign/campaign.module'; | |
import { PlayerModule } from './player/player.module'; | |
import { SageModule } from './sage/sage.module'; | |
import { TypeOrmModule } from "@nestjs/typeorm"; | |
import { Connection } from 'typeorm'; | |
// controllers | |
import { AppController } from './app.controller'; | |
import { PlayerController } from './player/player.controller'; | |
import { AuthController } from './auth/auth.controller'; | |
// services | |
import { AppService } from './app.service'; | |
// constants for everything else | |
import { SAGE_DB, SAGE_DB_HOST, SAGE_DB_PORT, ENTITIES_PATH } from './constants'; | |
import { databaseProviders } from './providers/database.providers'; | |
import { CharacterEntity } from './entities/character.entity'; | |
@Module({ | |
imports: [ | |
TypeOrmModule.forRoot({ | |
"type": "mongodb", | |
"host": SAGE_DB_HOST, | |
"port": SAGE_DB_PORT, | |
"database": SAGE_DB, | |
"keepConnectionAlive": true, | |
"synchronize": true, | |
"autoLoadEntities": true | |
}), | |
CharacterModule, | |
SageModule, | |
CampaignModule, | |
PlayerModule, | |
], | |
controllers: [ | |
AppController, | |
PlayerController, | |
AuthController | |
], | |
providers: [AppService], | |
}) | |
export class AppModule { | |
constructor(private connection: Connection) { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tslint:disable: quotemark | |
import { Controller, HttpStatus, Get, Post, Put, Delete, Req, Param, Body, Res } from '@nestjs/common'; | |
import { MessagePattern } from '@nestjs/microservices'; | |
import { Character } from "../model/character"; | |
import { CharacterService } from './character.service'; | |
import { Request as ExpressRequest, Response as ExpressResponse } from "express"; | |
import { CHARACTER_PATH } from '../constants'; | |
import { NotFoundError, ServerError } from '../model'; | |
@Controller('character') | |
export class CharacterController { | |
constructor( | |
private readonly characterService: CharacterService | |
) { | |
} | |
// typeguard for 404 - Not found | |
is404(object: any): object is NotFoundError { return true; }; | |
// typeguard for 500 - Server error | |
is500(object: any): object is ServerError { return true; } | |
@MessagePattern({ cmd: 'sage.character' }) | |
async characterById( | |
@Param() characterId: string | |
) { | |
const r = this.characterService.findCharacterById(characterId); | |
if (this.is404(r)) { | |
// res.status(HttpStatus.NOT_FOUND).send(r); | |
} | |
} | |
@Post(CHARACTER_PATH) | |
async createCharacter( | |
@Req() request: ExpressRequest, | |
@Res() res: ExpressResponse | |
) { | |
res.status(HttpStatus.OK).send(this.characterService.createCharacter()); | |
} | |
@Get(CHARACTER_PATH) | |
async readCharacter( | |
@Param('characterId') characterId: string, | |
@Res() res: ExpressResponse | |
) { | |
const r = this.characterService.findCharacterById(characterId); | |
console.log(r); | |
( | |
this.is404(r) ? res.status(HttpStatus.NOT_FOUND) : | |
this.is500(r) ? res.status(HttpStatus.INTERNAL_SERVER_ERROR) : | |
res.status(HttpStatus.OK) | |
).send(r); | |
} | |
@Put(CHARACTER_PATH) | |
async updateCharacter( | |
@Param('characterId') characterId: string, | |
@Body() character: Character, | |
@Res() res: ExpressResponse | |
) { | |
res.status(HttpStatus.OK).send(this.characterService.updateCharacter(characterId, character)); | |
} | |
@Delete(CHARACTER_PATH) | |
async deleteCharacter( | |
@Param('characterId') characterId: string, | |
@Res() res: ExpressResponse | |
) { | |
res.status(HttpStatus.OK).send(this.characterService.deleteCharacter(characterId)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tslint:disable: quotemark | |
import { | |
Column, | |
Entity, | |
ObjectID, | |
ObjectIdColumn | |
} from 'typeorm'; | |
import { | |
Character, Profile, Disciplines, | |
CharacterMagic, Social, | |
Beacon, Pool, Defense, Statistics, | |
Derivatives, CharacterAccrued, | |
Demonology, Skills, Attributes | |
} from '../model'; | |
@Entity() | |
export class CharacterEntity implements Character { | |
@ObjectIdColumn() Character_Id: string; | |
@Column() Token: string; | |
@Column() Created?: string; | |
@Column() Updated?: string; | |
@Column() Owner?: string; | |
@Column() Profile?: Profile; | |
@Column() Abilities?: Disciplines; | |
@Column() Spells?: import("../model").Spell[]; | |
@Column() Fluency?: string[]; | |
@Column() Culture?: string; | |
@Column() Magic?: CharacterMagic; | |
@Column() Social?: Social; | |
@Column() Beacon?: Beacon; | |
@Column() Equipment?: ( | |
import("../model").Equipment | | |
import("../model").Armor | | |
import("../model").Weapon | |
)[]; | |
@Column() Pools?: Pool; | |
@Column() Defenses?: Defense; | |
@Column() Statistics?: Statistics; | |
@Column() Derivatives?: Derivatives; | |
@Column() Contacts?: import("../model").Contact[]; | |
@Column() Wounds?: import("../model").Wound[]; | |
@Column() Active_Conditions?: string[]; | |
@Column() Accrued?: CharacterAccrued; | |
@Column() Demonology?: Demonology; | |
@Column() Tags?: import("../model").Tag[]; | |
@Column() Skills?: Skills; | |
@Column() Attributes?: Attributes; | |
@Column() Afflictions?: import("../model").Affliction[]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tslint:disable: quotemark | |
import { Module, Inject } from '@nestjs/common'; | |
import { TypeOrmModule } from "@nestjs/typeorm"; | |
import { Connection } from 'typeorm'; | |
import { ServeStaticModule } from '@nestjs/serve-static'; | |
import { join } from 'path'; | |
import { CharacterEntity } from "../entities/character.entity"; | |
import { CharacterService } from './character.service'; | |
import { CharacterController } from './character.controller'; | |
@Module({ | |
imports: [ | |
TypeOrmModule | |
], | |
controllers: [ | |
CharacterController | |
], | |
providers: [ | |
CharacterService | |
], | |
exports: [ | |
CharacterService, | |
] | |
}) | |
export class CharacterModule { | |
constructor() { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tslint:disable: quotemark | |
import { Injectable } from '@nestjs/common'; | |
import { InjectRepository } from '@nestjs/typeorm'; | |
import { Repository } from 'typeorm'; | |
import { Response as ExpressResponse } from 'express'; | |
import { | |
NotFoundError, | |
Player, | |
Character, | |
UpdatedResponse, | |
CreatedResponse, | |
Disciplines, | |
Campaign, | |
Armor, | |
Defense, | |
Pool, | |
Statistics, | |
Beacon, | |
Weapon, | |
Equipment, | |
Profile, | |
Attributes, | |
Wound, | |
Demonology, | |
Affliction, | |
Contact, | |
Social, | |
Tag, | |
Spell, | |
Skills | |
} from 'src/model'; | |
import { CharacterEntity } from '../entities/character.entity'; | |
@Injectable() | |
export class CharacterService { | |
characterNotFound: NotFoundError = { | |
errorType: 'Not Found', | |
Event_Code: '404', | |
Event_Id: '', | |
Event_Message: 'The character was not found', | |
Event_Subject: 'Character not found', | |
Event_Timestamp: new Date().toString() | |
} as NotFoundError; | |
constructor( | |
@InjectRepository(CharacterEntity) | |
private characterRepo: Repository<CharacterEntity> | |
) { } | |
updateCharacter(characterId: string, character: Character): UpdatedResponse { | |
return new Object() as UpdatedResponse; | |
} | |
createCharacter(): CreatedResponse { | |
return new Object() as CreatedResponse; | |
} | |
async findCharacterById(characterId: string): Promise<CharacterEntity> { | |
return this.characterRepo | |
.findOne({ Character_Id: characterId }); | |
} | |
async findCharacterByToken(token: string): Promise<CharacterEntity> { | |
return this.characterRepo | |
.findOne({ Token: token }); | |
} | |
deleteCharacter(characterId: string): UpdatedResponse { | |
return new Object() as UpdatedResponse; | |
} | |
async readCharacter(characterId: string): Promise<CharacterEntity> { | |
return this.characterRepo | |
.findOne({ Character_Id: characterId }); | |
} | |
//#region Not implemented yet //#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment