API Reference

BaseStore module.

class diffsync.store.BaseStore(*args, diffsync: Optional[DiffSync] = None, name: str = '', **kwargs)

Bases: object

Reference store to be implemented in different backends.

__init__(*args, diffsync: Optional[DiffSync] = None, name: str = '', **kwargs) None

Init method for BaseStore.

add(*, obj: DiffSyncModel)

Add a DiffSyncModel object to the store.

Parameters

obj (DiffSyncModel) – Object to store

Raises

ObjectAlreadyExists – if a different object with the same uid is already present.

count(*, model: Optional[Union[str, DiffSyncModel, Type[DiffSyncModel]]] = None) int

Returns the number of elements of a specific model, or all elements in the store if not specified.

get(*, model: Union[str, DiffSyncModel, Type[DiffSyncModel]], identifier: Union[str, Mapping]) DiffSyncModel

Get one object from the data store based on its unique id.

Parameters
  • model – DiffSyncModel class or instance, or modelname string, that defines the type of the object to retrieve

  • identifier – Unique ID of the object to retrieve, or dict of unique identifier keys/values

Raises
  • ValueError – if obj is a str and identifier is a dict (can’t convert dict into a uid str without a model class)

  • ObjectNotFound – if the requested object is not present

get_all(*, model: Union[str, DiffSyncModel, Type[DiffSyncModel]]) List[DiffSyncModel]

Get all objects of a given type.

Parameters

model – DiffSyncModel class or instance, or modelname string, that defines the type of the objects to retrieve

Returns

List of Object

Return type

List[DiffSyncModel]

get_all_model_names() Set[str]

Get all the model names stored.

Returns

Set of all the model names.

Return type

Set[str]

get_by_uids(*, uids: List[str], model: Union[str, DiffSyncModel, Type[DiffSyncModel]]) List[DiffSyncModel]

Get multiple objects from the store by their unique IDs/Keys and type.

Parameters
  • uids – List of unique id / key identifying object in the database.

  • model – DiffSyncModel class or instance, or modelname string, that defines the type of the objects to retrieve

Raises

ObjectNotFound – if any of the requested UIDs are not found in the store

get_or_add_model_instance(obj: DiffSyncModel) Tuple[DiffSyncModel, bool]

Attempt to get the object with provided obj identifiers or instantiate obj.

Parameters

obj – An obj of the DiffSyncModel to get or add.

Returns

Provides the existing or new object and whether it was added or not.

get_or_instantiate(*, model: Type[DiffSyncModel], ids: Dict, attrs: Optional[Dict] = None) Tuple[DiffSyncModel, bool]

Attempt to get the object with provided identifiers or instantiate it with provided identifiers and attrs.

Parameters
  • model (DiffSyncModel) – The DiffSyncModel to get or create.

  • ids (Mapping) – Identifiers for the DiffSyncModel to get or create with.

  • attrs (Mapping, optional) – Attributes when creating an object if it doesn’t exist. Defaults to None.

Returns

Provides the existing or new object and whether it was created or not.

Return type

Tuple[DiffSyncModel, bool]

remove(*, obj: DiffSyncModel, remove_children: bool = False)

Remove a DiffSyncModel object from the store.

Parameters
  • obj (DiffSyncModel) – object to remove

  • remove_children (bool) – If True, also recursively remove any children of this object

Raises

ObjectNotFound – if the object is not present

remove_item(modelname: str, uid: str)

Remove one item from store.

update(*, obj: DiffSyncModel)

Update a DiffSyncModel object to the store.

Parameters

obj (DiffSyncModel) – Object to update

update_or_add_model_instance(obj: DiffSyncModel) Tuple[DiffSyncModel, bool]

Attempt to update an existing object with provided ids/attrs or instantiate obj.

Parameters

instance – An instance of the DiffSyncModel to update or create.

Returns

Provides the existing or new object and whether it was added or not.

update_or_instantiate(*, model: Type[DiffSyncModel], ids: Dict, attrs: Dict) Tuple[DiffSyncModel, bool]

Attempt to update an existing object with provided ids/attrs or instantiate it with provided identifiers and attrs.

Parameters
  • model (DiffSyncModel) – The DiffSyncModel to get or create.

  • ids (Dict) – Identifiers for the DiffSyncModel to get or create with.

  • attrs (Dict) – Attributes when creating/updating an object if it doesn’t exist. Pass in empty dict, if no specific attrs.

Returns

Provides the existing or new object and whether it was created or not.

Return type

Tuple[DiffSyncModel, bool]