Skip to content

Store

Intro

Think of a store as a constelite wrapper around an API of a data provider.

Every store is represented by a [BaseStore][constelite.store.BaseStore] object. If we want to connect to a data provider through constelite we need to create a store of a corresponding type.

For example, we have MemoryStore, which stores records in memory, or NeofluxStore, which stores records in external Neo4j and InfluxDB databases.

Note

If you want to reach another kind of data provider, you need to write a new store class.

Why can't we use existing libraries to talk to data provider?

You are right, there are plenty of libraries to talk with Neo4j. And we use them inside constelite store classes.

Stores are just wrappers with a standard API so that constelite and constelite users don't need to worry about particular ways of reading and writing data associated with a particular data provider. All specific logic is implemented inside stores once and is then reused through a standard interface.

For example, to save a new record to a store, all you need to do is

store: Store

r_cat = ref(
    Cat(
        name="Snowball"
    )
)

store.put(ref=r_cat)

Where the record is created will depend on the store object.

Store methods

Each store implements a set of standard methods, which are:

get(ref:Ref[M]) -> Ref[M]

Returns the record referenced by ref.

put(ref: Ref[M]) -> Ref[M]

Creates a new record if ref.record is None or overwrites the existing record with properties from ref.state. Only fields that are set in the ref.state are updated.

For association and aggregation relationships, overwrite will keep the records that were previously related.

For composition relationships, overwrite will delete previously-related records.

patch(ref: Ref[M]) -> Ref[M]

Patches properties of the existing record with the state provided in ref.state.

For static properties, patch acts the same way as put. For dynamic properties, existing time points will be preserved and new time points, given in ref.state, will be added.

For association relationships, patch will act the same way as put.

For aggregation and composition relationships, patch will keep the records that were previously related and add new relationships from ref.state.

delete(ref:Ref) -> None

Deletes the record referenced by ref.

For composition relationships will also delete any related records.

query(query: Query, model_type: Type[StateModel], include_states: bool) -> List[Ref[M]]

Queries the store.

graphql(self, query: GraphQLQuery) -> Dict[str, Any]:

Runs a GraphQL query and return the data in the form of a GraphQL response dictionary.

graphql_models(self, query: GraphQLModelQuery) -> List[Ref]:

Runs a GraphQL query and return the data in the form of a list of Ref models.