mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 15:43:13 +09:00
Fix datastore types
This commit is contained in:
1 file changed
+14
-16
@@ -30,7 +30,7 @@ export class MemoryDatastoreProvider<
|
|||||||
V extends DatastoreValue,
|
V extends DatastoreValue,
|
||||||
> extends DatastoreProvider<K, V> {
|
> extends DatastoreProvider<K, V> {
|
||||||
constructor(
|
constructor(
|
||||||
private value: Map<K, V> = new Map()
|
private value = new Map<K, V>()
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
@@ -39,34 +39,33 @@ export class MemoryDatastoreProvider<
|
|||||||
this.value = new Map(data);
|
this.value = new Map(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load(): Promise<Map<K, V>> {
|
||||||
return new Map(this.value);
|
return new Map(this.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store all data within a json object file and validate the result against a schema
|
// Store all data within a json object file and validate the result against a schema
|
||||||
export class JsonDatastoreProvider<
|
export class JsonDatastoreProvider<
|
||||||
K extends DatastoreKey,
|
|
||||||
V extends DatastoreValue,
|
V extends DatastoreValue,
|
||||||
J, // Intermediate type returned from migrations
|
J, // Intermediate type returned from migrations
|
||||||
> extends DatastoreProvider<K, V> {
|
> extends DatastoreProvider<string, V> {
|
||||||
constructor(
|
constructor(
|
||||||
private filePath: string,
|
private filePath: string,
|
||||||
private fromJson: (json: J) => V = v => v as any,
|
private fromJson: (json: J) => V = v => v as any as V,
|
||||||
private migrations: (rawJson: Record<DatastoreKey, unknown>) => Record<DatastoreKey, J> = v => v as any,
|
private migrations: (rawJson: Record<string, unknown>) => Record<string, J> = v => v as Record<string, J>,
|
||||||
private finalise: (obj: V) => V = v => v,
|
private finalise: (obj: V) => V = v => v,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the data to the json file
|
// Save the data to the json file
|
||||||
async save(data: Map<K, V>) {
|
async save(data: Map<string, V>) {
|
||||||
logger.verbose(`Saving ${this.filePath}`);
|
logger.verbose(`Saving ${this.filePath}`);
|
||||||
await safeOutputFile(this.filePath, JSON.stringify(Object.fromEntries(data), null, "\t"));
|
await safeOutputFile(this.filePath, JSON.stringify(Object.fromEntries(data), null, "\t"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the data from the json file
|
// Load the data from the json file
|
||||||
async load() {
|
async load(): Promise<Map<string, V>> {
|
||||||
// Read the raw json from the file
|
// Read the raw json from the file
|
||||||
let rawJson;
|
let rawJson;
|
||||||
try {
|
try {
|
||||||
@@ -89,27 +88,26 @@ export class JsonDatastoreProvider<
|
|||||||
|
|
||||||
// Store all data within a json array file where each element has a unique id
|
// Store all data within a json array file where each element has a unique id
|
||||||
export class JsonIdDatastoreProvider<
|
export class JsonIdDatastoreProvider<
|
||||||
K extends DatastoreKey,
|
V extends DatastoreValue & { id: DatastoreKey },
|
||||||
V extends DatastoreValue & { id: K },
|
|
||||||
J, // Intermediate type returned from migrations
|
J, // Intermediate type returned from migrations
|
||||||
> extends DatastoreProvider<K, V> {
|
> extends DatastoreProvider<V["id"], V> {
|
||||||
constructor(
|
constructor(
|
||||||
private filePath: string,
|
private filePath: string,
|
||||||
private fromJson: (json: J) => V = v => v as any,
|
private fromJson: (json: J) => V = v => v as any as V,
|
||||||
private migrations: (rawJson: Array<unknown>) => Array<J> = v => v as any,
|
private migrations: (rawJson: Array<unknown>) => Array<J> = v => v as Array<J>,
|
||||||
private finalise: (obj: V) => V = v => v,
|
private finalise: (obj: V) => V = v => v,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the data to the json file
|
// Save the data to the json file
|
||||||
async save(data: Map<K, V>) {
|
async save(data: Map<V["id"], V>) {
|
||||||
logger.verbose(`Saving ${this.filePath}`);
|
logger.verbose(`Saving ${this.filePath}`);
|
||||||
await safeOutputFile(this.filePath, JSON.stringify([...data.values()], null, "\t"));
|
await safeOutputFile(this.filePath, JSON.stringify([...data.values()], null, "\t"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the data from the json file
|
// Load the data from the json file
|
||||||
async load() {
|
async load(): Promise<Map<V["id"], V>> {
|
||||||
// Read the raw json from the file
|
// Read the raw json from the file
|
||||||
let rawJson;
|
let rawJson;
|
||||||
try {
|
try {
|
||||||
@@ -217,7 +215,7 @@ export abstract class Datastore<
|
|||||||
export class KeyValueDatastore<
|
export class KeyValueDatastore<
|
||||||
K extends DatastoreKey,
|
K extends DatastoreKey,
|
||||||
V extends DatastoreValue,
|
V extends DatastoreValue,
|
||||||
> extends Datastore<K, V, [K, V, isDeleted?: boolean]> {
|
> extends Datastore<K, V, [key: K, value: V, isDeleted?: boolean]> {
|
||||||
// Set the value in the datastore, be careful of race conditions if you await any functions before calling set
|
// Set the value in the datastore, be careful of race conditions if you await any functions before calling set
|
||||||
set(key: K, value: V) {
|
set(key: K, value: V) {
|
||||||
this.data.set(key, value);
|
this.data.set(key, value);
|
||||||
|
|||||||
Reference in new issue
Block a user