0.2.18 • Published 1 year ago

resub-entity v0.2.18

Weekly downloads
46
License
MIT
Repository
github
Last release
1 year ago

resub-entity

npm npm

This project eases the use of ReSub by providing a simple function to create a Store for an entity

different stores

EntityStore: A simple store, that holds one type of objects.

Creating a store works with the method createEntityStore<EntityType, idType>. Creation looks as follows:

class SampleObject {
    id: number;
    value: string;
}

const SampleStore = createEntityStore<SampleObject, number>({selectIdFunction: (so: SampleObject) => so.id});

You now can add objects via SampleStore.setOne({id: 1, value: 'Hello World!'}). And you can get an Object via SampleStore.getOne(1).

SelectEntityStore: The same as the EntityStore, but you can select one item per ID with SampleStore.setSelected(ID).

DynamicLoadingStore: The DynamicLoadingStore is a SelectEntityStore, but with the option to load an object asynchonously, if it does not exist in the store. This is usefull for example for loading values from a service.

Creation of the DynamicLoadingStore looks as follows:

class SampleObject {
    id: number;
    value: string;
}

const sampleObjects: Map<number, SampleObject> = new Map<string, string>([[1, {id: 1, value: 'Hello'}], [2, {id: 1, value: 'Hello'}]]);

const SampleStore = createEntityStore<SampleObject, number>({
    selectIdFunction: (so: SampleObject) => so.id,
    loadFunction: (id: number) => sampleObjects[id] 
});

SampleStore.getOne(1) now loads the Object {id: 1, value: 'Hello'} from the sampleObjects map.

Carefull: As it loads values asynchronously, the returnvalue of the getOne function will be undefined on the first call!

Until further documentation, just have a look at the tests.

0.2.18

1 year ago

0.2.17

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

3 years ago

0.2.13

3 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.2.10

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0-alpha7

4 years ago

0.1.0-alpha6

4 years ago

0.1.0-alpha5

4 years ago

0.1.0-alpha4

4 years ago

0.1.0-alpha2

4 years ago

0.1.0-alpha3

4 years ago

0.1.0-alpha1

4 years ago