Last active
August 21, 2019 20:58
-
-
Save timjonesdev/bb8bc88f36f7a6e583eb57c30778166a to your computer and use it in GitHub Desktop.
Typescript Model Classes for Reactive Mongo Example
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
/** | |
* Enforces a deserialize method to ensure a model class | |
* can construct itself from a JSON string | |
*/ | |
export interface Deserializable { | |
deserialize(input: any): this; | |
} |
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
import {Deserializable} from "./deserializable"; | |
export class PlayerModel implements Deserializable { | |
name: string; | |
score: number; | |
deserialize(input: any): this { | |
Object.assign(this, input); | |
return this; | |
} | |
} |
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
import {PlayerModel} from './player.model'; | |
import {Deserializable} from "./deserializable"; | |
export class TeamModel implements Deserializable { | |
id: string; | |
name: string; | |
players: PlayerModel[] = []; | |
totalScore: number; | |
deserialize(input: any): this { | |
Object.assign(this, input); | |
this.players = input.players | |
.map((player: PlayerModel) => new PlayerModel().deserialize(player)); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment