# Store
This library provides Collection
, Store
, and StoreService
classes. Collection
incapsulates a loading, creation, updating and deletion of its models, controls a models lifecycle. Store
class provides a central storage of collections. Collections do not support relations yet, but models data may be a structured object.
DANGER
The library is experimental, API breaking changes may be occured.
# Installation
# Description
You may use the library via StoreService
as a part of your VueEnt
-based application, via Store
class without VueEnt
's services and controllers, as independent collections directly. You're free to choose your own way.
# Collections
At first, we should start from the collection level. Collection
is a base class which should be inherited by collections of your project. Any collection stores a set of cached items.
# constructor
Constructor receives an object with the model constructor and functions which provides CRUD operations.
# destroy
The destroy
method destroys a collection instance and all its local records.
# setStore
The setStore
method binds a store instance.
WARNING
The method should not be used directly, it is used by the Store
's constructor.
# create
The create
method creates a local model instance. It receives an initial model data and an options list.
# find
The find
method searches for a multiple records and receives search parameters defined by server API. It scans a local cache if the reload
option is set to false
. A local filter function may be applied.
# findOne
The findOne
method searches for a single record and receives record's primary key and search parameters defined by server API. It scans a local cache if the reload
option is set to false
. A local filter function may be applied.
# peek
The peek
method searches for some records in a local cache. A filter function may be applied.
# peekOne
The peekOne
method searches for a single record in a local cache by the primary key. A filter function may be applied.
# normalize
The normalize
method converts an encoded data to internal (see serializing and deserializing).
# denormalize
The denormalize
method converts an internal representation to the encoded (see serializing and deserializing).
# unload
The unload
method unloads a model instance (removes a model instance from the collection).
WARNING
This method does not removes a record from the server store.
# unloadAll
The unloadAll
method unloads all models (clears the collection cache).
WARNING
This method does not removes records from the server store.
# Store class
The store class receives a collections list and provides a collection access through the get
method using the collection constructor (class name). It automatically infers collection types.
# Store service
The store service working like a store class, but extends VueEnt's Service
and can be accessed through useService
/injectService
.
# Usage
Define available collections as an argument of generic type of StoreService
to support compile time types constrains, e.g.:
store.get(UnknownCollectionClass); // compile error: Argument of type 'typeof StorableCollection' is not assignable to parameter of type...