Skip to content

Instantly share code, notes, and snippets.

@timjonesdev
Last active August 21, 2019 20:58
Show Gist options
  • Save timjonesdev/bb8bc88f36f7a6e583eb57c30778166a to your computer and use it in GitHub Desktop.
Save timjonesdev/bb8bc88f36f7a6e583eb57c30778166a to your computer and use it in GitHub Desktop.
Typescript Model Classes for Reactive Mongo Example
/**
* Enforces a deserialize method to ensure a model class
* can construct itself from a JSON string
*/
export interface Deserializable {
deserialize(input: any): this;
}
import {Deserializable} from "./deserializable";
export class PlayerModel implements Deserializable {
name: string;
score: number;
deserialize(input: any): this {
Object.assign(this, input);
return this;
}
}
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