Looking for a Built-in Data Structure Similar to EntityStore in TypeScript
Was wondering if there may be some existing built in data structure that approximates this? :
export class EntityStore<
T extends Record<string | symbol, unknown> = Record<string | symbol, unknown>,
Id extends string | symbol = string | symbol
> {
public readonly allIds: Id[] = [];
public readonly byId: Record<Id, T> = {} as Record<Id, T>;
static add<T extends EntityStore>(
store: T,
entity: T,
id: T["allIds"][number]
) {
return {
allIds: Array.append(store.allIds, id),
byId: Record.set(store.byId, id, entity),
};
}
static remove<T extends EntityStore>(store: T, id: T["allIds"][number]) {
return {
allIds: Array.filter(store.allIds, (i) => i !== id),
byId: Record.remove(store.byId, id),
};
}
static update<T extends EntityStore>(
store: T,
id: T["allIds"][number],
entity: Partial<T>
) {
return {
allIds: store.allIds,
byId: Record.modify(store.byId, id, (e) => ({ ...e, ...entity })),
};
}
}export class EntityStore<
T extends Record<string | symbol, unknown> = Record<string | symbol, unknown>,
Id extends string | symbol = string | symbol
> {
public readonly allIds: Id[] = [];
public readonly byId: Record<Id, T> = {} as Record<Id, T>;
static add<T extends EntityStore>(
store: T,
entity: T,
id: T["allIds"][number]
) {
return {
allIds: Array.append(store.allIds, id),
byId: Record.set(store.byId, id, entity),
};
}
static remove<T extends EntityStore>(store: T, id: T["allIds"][number]) {
return {
allIds: Array.filter(store.allIds, (i) => i !== id),
byId: Record.remove(store.byId, id),
};
}
static update<T extends EntityStore>(
store: T,
id: T["allIds"][number],
entity: Partial<T>
) {
return {
allIds: store.allIds,
byId: Record.modify(store.byId, id, (e) => ({ ...e, ...entity })),
};
}
}