Added generic interfaces for StoredObjectCollection and StoredObject, with proper specification and implementation in the Mongo- versions.
This commit is contained in:
@@ -6,7 +6,7 @@ import {
|
||||
} from "./stats";
|
||||
import {
|
||||
getMongoObjectCollection,
|
||||
MongoStoredObject, MongoStoredObjectCollection,
|
||||
MongoStoredObject, MongoStoredObjectCollection, StoredObject, StoredObjectCollection,
|
||||
} from "./utils";
|
||||
import {CellValue} from "../controllers/statsController";
|
||||
import mongo from "mongodb";
|
||||
@@ -23,12 +23,12 @@ export interface StoredPlayerData {
|
||||
stats: PlayerStats;
|
||||
}
|
||||
|
||||
interface StoredPlayerCollection {
|
||||
findPlayerById(id: string): Promise<StoredPlayer>;
|
||||
interface StoredPlayerCollection extends StoredObjectCollection<StoredPlayer> {
|
||||
findById(id: string): Promise<StoredPlayer>;
|
||||
}
|
||||
|
||||
class MongoStoredPlayerCollection
|
||||
extends MongoStoredObjectCollection<MongoStoredPlayer>
|
||||
extends MongoStoredObjectCollection<MongoStoredPlayer, StoredPlayer>
|
||||
implements StoredPlayerCollection {
|
||||
|
||||
private updater: PlayerStatsUpdater;
|
||||
@@ -37,19 +37,19 @@ class MongoStoredPlayerCollection
|
||||
this.updater = new PlayerStatsUpdater();
|
||||
}
|
||||
|
||||
async findPlayerById(id: string): Promise<StoredPlayer> {
|
||||
async findById(id: string): Promise<StoredPlayer> {
|
||||
const data = await this.findObjectById(id);
|
||||
return new MongoStoredPlayer(data);
|
||||
}
|
||||
}
|
||||
|
||||
export interface StoredPlayer {
|
||||
export interface StoredPlayer extends StoredObject {
|
||||
nick(): string;
|
||||
setNick(newNick: string): Promise<void>;
|
||||
updateStats(results: PlayerGameResults & {outcome: OutcomeType}, ruleset: Ruleset): Promise<void>;
|
||||
}
|
||||
|
||||
class MongoStoredPlayer extends MongoStoredObject implements StoredPlayer {
|
||||
class MongoStoredPlayer extends MongoStoredObject<StoredPlayerData> implements StoredPlayer {
|
||||
constructor(data: StoredPlayerData) {
|
||||
super(data);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,11 @@ export function getMongoObjectCollection(collectionName: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class MongoStoredObjectCollection<T extends MongoStoredObject<any>> {
|
||||
export interface StoredObjectCollection<K> {
|
||||
findById(id: string): Promise<K>;
|
||||
}
|
||||
|
||||
export abstract class MongoStoredObjectCollection<T extends MongoStoredObject<any>, K extends StoredObject> implements StoredObjectCollection<K> {
|
||||
protected mongoDbClientCollection: mongo.Collection;
|
||||
protected constructor(collectionClient: mongo.Collection) {
|
||||
this.mongoDbClientCollection = collectionClient;
|
||||
@@ -28,6 +32,8 @@ export abstract class MongoStoredObjectCollection<T extends MongoStoredObject<an
|
||||
return this.mongoDbClientCollection!.findOne({_id: id});
|
||||
}
|
||||
|
||||
abstract async findById(id: string): Promise<K>;
|
||||
|
||||
async save(...objects: T[]): Promise<void> {
|
||||
for (const object of objects) {
|
||||
await this.mongoDbClientCollection.findOneAndUpdate({_id: object.id()}, {...object.rawData()});
|
||||
@@ -35,7 +41,12 @@ export abstract class MongoStoredObjectCollection<T extends MongoStoredObject<an
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class MongoStoredObject<T> {
|
||||
export interface StoredObject {
|
||||
id(): string;
|
||||
rawData(): any;
|
||||
}
|
||||
|
||||
export abstract class MongoStoredObject<T> implements StoredObject {
|
||||
protected constructor(protected data: {_id: string} & T) {}
|
||||
|
||||
id(): string {
|
||||
|
||||
Reference in New Issue
Block a user