From af95ca587b51593665d032186554fd04edea5615 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Sun, 1 Jun 2025 16:25:14 +0100 Subject: [PATCH] Fix datastore types --- packages/lib/src/datastore.ts | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/lib/src/datastore.ts b/packages/lib/src/datastore.ts index 1b84e7f8..266bad8b 100644 --- a/packages/lib/src/datastore.ts +++ b/packages/lib/src/datastore.ts @@ -30,7 +30,7 @@ export class MemoryDatastoreProvider< V extends DatastoreValue, > extends DatastoreProvider { constructor( - private value: Map = new Map() + private value = new Map() ) { super(); } @@ -39,34 +39,33 @@ export class MemoryDatastoreProvider< this.value = new Map(data); } - async load() { + async load(): Promise> { return new Map(this.value); } } // Store all data within a json object file and validate the result against a schema export class JsonDatastoreProvider< - K extends DatastoreKey, V extends DatastoreValue, J, // Intermediate type returned from migrations -> extends DatastoreProvider { +> extends DatastoreProvider { constructor( private filePath: string, - private fromJson: (json: J) => V = v => v as any, - private migrations: (rawJson: Record) => Record = v => v as any, + private fromJson: (json: J) => V = v => v as any as V, + private migrations: (rawJson: Record) => Record = v => v as Record, private finalise: (obj: V) => V = v => v, ) { super(); } // Save the data to the json file - async save(data: Map) { + async save(data: Map) { logger.verbose(`Saving ${this.filePath}`); await safeOutputFile(this.filePath, JSON.stringify(Object.fromEntries(data), null, "\t")); } // Load the data from the json file - async load() { + async load(): Promise> { // Read the raw json from the file let rawJson; try { @@ -89,27 +88,26 @@ export class JsonDatastoreProvider< // Store all data within a json array file where each element has a unique id export class JsonIdDatastoreProvider< - K extends DatastoreKey, - V extends DatastoreValue & { id: K }, + V extends DatastoreValue & { id: DatastoreKey }, J, // Intermediate type returned from migrations -> extends DatastoreProvider { +> extends DatastoreProvider { constructor( private filePath: string, - private fromJson: (json: J) => V = v => v as any, - private migrations: (rawJson: Array) => Array = v => v as any, + private fromJson: (json: J) => V = v => v as any as V, + private migrations: (rawJson: Array) => Array = v => v as Array, private finalise: (obj: V) => V = v => v, ) { super(); } // Save the data to the json file - async save(data: Map) { + async save(data: Map) { logger.verbose(`Saving ${this.filePath}`); await safeOutputFile(this.filePath, JSON.stringify([...data.values()], null, "\t")); } // Load the data from the json file - async load() { + async load(): Promise> { // Read the raw json from the file let rawJson; try { @@ -217,7 +215,7 @@ export abstract class Datastore< export class KeyValueDatastore< K extends DatastoreKey, V extends DatastoreValue, -> extends Datastore { +> extends Datastore { // Set the value in the datastore, be careful of race conditions if you await any functions before calling set set(key: K, value: V) { this.data.set(key, value);