Skip to content

Commit f33b2d1

Browse files
authored
Merge pull request #404 from opsmill/wvd-20250509-fix-client-store-docs
Rework SDK client store guide
2 parents d137a2b + 02d23a5 commit f33b2d1

File tree

1 file changed

+59
-52
lines changed

1 file changed

+59
-52
lines changed

docs/docs/python-sdk/guides/store.mdx

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,175 +6,182 @@ import TabItem from '@theme/TabItem';
66

77
# Using the client store
88

9-
The client in the SDK contains a store object that is used to store Nodes in a local cache.
9+
The client in the SDK contains a store that is used to store objects in a local cache.
1010

11-
The store is mainly used for the internal working of the SDK. It is used to create relations between nodes that might not yet exist in the database, or to store relations for nodes that were retrieved from the database, amongst other things.
11+
The store is mainly used for the internal working of the SDK. It is used to create relations between objects that might not yet exist in the database, or to store relations for objects that were retrieved from the database, amongst other things.
1212

13-
The store can also be used to store Nodes that we retrieve using the client query methods. This allows to not have to keep references to nodes we retrieved throughout scripts, or avoids situations where we have to redo queries to retrieve nodes.
13+
The store stores objects that we are retrieving from Infrahub using the different query methods. This allows to not have to keep references to objects throughout scripts, or avoids situations where we have to re-execute queries.
1414

15-
## Storing nodes in the store
15+
Objects are stored in the following scenario:
1616

17-
Nodes retrieved from Infrahub using a the SDK client's query methods, such as the `get` `filters` or `all` method, will be automatically stored in the internal store. Nodes stored in the store using this method can be retrieved using their `id` as the key in the store.
17+
- The resulting objects from using the SDK client's `get`, `filters` or `all` methods
1818

1919
<Tabs groupId="async-sync">
2020
<TabItem value="Async" default>
2121

2222
```python
2323
tag = await client.get(kind="BuiltinTag", name__value="RED")
24+
tag_in_store = client.store.get(key=tag.id)
2425
```
2526

2627
</TabItem>
2728
<TabItem value="Sync" default>
2829

2930
```python
3031
tag = client.get(kind="BuiltinTag", name__value="RED")
32+
tag_in_store = client.store.get(key=tag.id)
3133
```
3234

3335
</TabItem>
3436
</Tabs>
3537

36-
This behaviour may not be desirable in all scenarios, therefor you can explicitly disable this behaviour by setting the `populate_store` argument to `False` when calling the query methods.
38+
- The resulting related objects for objects retrieved using the SDK client's query methods, when we use the `prefetch_relationships` argument.
3739

3840
<Tabs groupId="async-sync">
3941
<TabItem value="Async" default>
4042

4143
```python
42-
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
44+
device = await client.get(kind="InfraDevice", name__value="atl1-edge1", prefetch_relationships=True)
45+
site = client.store.get(key=device.site.id)
4346
```
4447

4548
</TabItem>
4649
<TabItem value="Sync" default>
4750

4851
```python
49-
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
52+
device = client.get(kind="InfraDevice", name__value="atl1-edge1", prefetch_relationships=True)
53+
site = client.store.get(key=device.site.id)
5054
```
5155

5256
</TabItem>
5357
</Tabs>
5458

55-
## Manually storing objects in the store
56-
57-
You can store nodes in the object store manually using the `set` method. This has the advantage that you can choose which key you use to reference the node in the store. For example, we could use the name attribute value of the node as the key.
59+
- The related objects of a object's relationship when the `fetch` method is used
5860

5961
<Tabs groupId="async-sync">
6062
<TabItem value="Async" default>
6163

6264
```python
63-
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
64-
client.store.set(key=tag.name.value, node=tag)
65+
device = await client.get(kind="InfraDevice", name__value="atl1-edge1")
66+
await device.site.fetch()
67+
site = client.store.get(key=device.site.id)
6568
```
6669

6770
</TabItem>
6871
<TabItem value="Sync" default>
6972

7073
```python
71-
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
72-
client.store.set(key=tag.name.value, node=tag)
74+
device = client.get(kind="InfraDevice", name__value="atl1-edge1")
75+
device.site.fetch()
76+
site = client.store.get(key=device.site.id)
7377
```
7478

7579
</TabItem>
7680
</Tabs>
7781

78-
## Retrieving object from the store
79-
80-
Nodes can be retrieved from the internal store using the key that was used to store them.
81-
For nodes that are stored by the client's query methods, this will be their `id`.
82+
- Objects that get created using the SDK
8283

8384
<Tabs groupId="async-sync">
8485
<TabItem value="Async" default>
8586

8687
```python
87-
tag = await client.get(kind="BuiltinTag", name__value="RED")
88+
tag = await client.create("BuiltinTag", name="BLACK")
89+
await tag.save()
8890
tag_in_store = client.store.get(key=tag.id)
89-
assert tag == tag_in_store
9091
```
9192

9293
</TabItem>
9394
<TabItem value="Sync" default>
9495

9596
```python
96-
tag = client.get(kind="BuiltinTag", name__value="RED")
97+
tag = client.create("BuiltinTag", name="BLACK")
98+
tag.save()
9799
tag_in_store = client.store.get(key=tag.id)
98-
assert tag == tag_in_store
99100
```
100101

101102
</TabItem>
102103
</Tabs>
103104

104-
For nodes that have been added manually to the store, this will be the key that you specified when storing the node. For example, when you used the name attribute value of the node you can use that name to retrieve the node from the store.
105+
## Retrieving objects from the store
106+
107+
You can retrieve objects from the store using their `id` or `hfid`. When using the `hfid`, we also have to provide the `kind` of the object that we want to retrieve.
105108

106109
<Tabs groupId="async-sync">
107110
<TabItem value="Async" default>
108111

109112
```python
110-
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
111-
client.store.set(key=tag.name.value, node=tag)
112-
tag_in_store = client.store.get(key=tag.name.value)
113-
assert tag == tag_in_store
113+
tag = await client.get("BuiltinTag", name__value="BLACK")
114+
tag_in_store = client.store.get(key=tag.id)
115+
tag == tag_in_store
116+
117+
tag = await client.get("BuiltinTag", name__value="BLACK")
118+
tag_in_store = client.store.get(key=tag.hfid, kind="BuiltinTag")
119+
tag == tag_in_store
114120
```
115121

116122
</TabItem>
117123
<TabItem value="Sync" default>
118124

119125
```python
120-
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
121-
client.store.set(key=tag.name.value, node=tag)
122-
tag_in_store = client.store.get(key=tag.name.value)
123-
assert tag == tag_in_store
126+
tag = client.get("BuiltinTag", name__value="BLACK")
127+
tag_in_store = client.store.get(key=tag.id)
128+
tag == tag_in_store
129+
130+
tag = client.get("BuiltinTag", name__value="BLACK")
131+
tag_in_store = client.store.get(key=tag.hfid, kind="BuiltinTag")
132+
tag == tag_in_store
124133
```
125134

126135
</TabItem>
127136
</Tabs>
128137

129-
## Prefetch relationships
138+
## Manually storing objects in the store
130139

131-
When you are using the internal store, you can use the `prefect_relationships` argument in all the client query methods. All the related nodes, for the relationships that are in scope of the query will be retrieved using this option. This has the advantage that you don't have to `fetch` related node(s) for a relation manually, but it comes at the cost of a more complex query, which might have an impact on the performance of the query.
140+
You can store objects in the store manually using the `set` method. This has the advantage that you can choose a key that you want to use to reference the object in the store, besides the `id` or `hfid`. For example, we could use the name attribute value of the node as the key.
132141

133142
<Tabs groupId="async-sync">
134143
<TabItem value="Async" default>
135144

136145
```python
137-
device = await client.get(kind="TestDevice", name__value="atl1-edge1", populate_store=True, prefetch_relationships=True, include=["interfaces"])
138-
print(device.interfaces.peers[0].peer.name.value)
146+
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
147+
client.store.set(key=tag.name.value, node=tag)
148+
149+
tag_in_store = client.store.get(key=tag.name.value)
150+
tag_in_store = client.store.get(key=tag.id)
151+
tag_in_store = client.store.get(key=tag.hfid, kind="BuiltinTag")
139152
```
140153

141154
</TabItem>
142155
<TabItem value="Sync" default>
143156

144157
```python
145-
device = client.get(kind="TestDevice", name__value="atl1-edge1", populate_store=True, prefetch_relationships=True, include=["interfaces"])
146-
print(device.interfaces.peers[0].peer.name.value)
158+
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
159+
client.store.set(key=tag.name.value, node=tag)
160+
161+
tag_in_store = client.store.get(key=tag.name.value)
162+
tag_in_store = client.store.get(key=tag.id)
163+
tag_in_store = client.store.get(key=tag.hfid, kind="BuiltinTag")
147164
```
148165

149166
</TabItem>
150167
</Tabs>
151168

152-
## Using a custom store
169+
## Disable storing objects in the store using the different query methods
153170

154-
You can use a custom store, outside of the Infrahub SDK client. Storing or retrieving nodes works exactly the same as for the internal store. The advantage is that you are in full control of the contents of the store.
171+
In some scenarios it might not be desirable to automatically store the retrieved objects in the store, when using the SDK client's different query methods. In this case you can set the `populate_store` argument to `False`.
155172

156173
<Tabs groupId="async-sync">
157174
<TabItem value="Async" default>
158175

159176
```python
160-
from infrahub_sdk.store import NodeStore
161-
store = NodeStore()
162-
163-
device = await client.get(kind="TestDevice", name__value="atl1-edge1", populate_store=False)
164-
store.set(key=device.name.value, node=device)
165-
store.get(key=device.name.value)
177+
tag = await client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
166178
```
167179

168180
</TabItem>
169181
<TabItem value="Sync" default>
170182

171183
```python
172-
from infrahub_sdk.store import NodeStoreSync
173-
store = NodeStoreSync()
174-
175-
device = await client.get(kind="TestDevice", name__value="atl1-edge1", populate_store=False)
176-
store.set(key=device.name.value, node=device)
177-
store.get(key=device.name.value)
184+
tag = client.get(kind="BuiltinTag", name__value="RED", populate_store=False)
178185
```
179186

180187
</TabItem>

0 commit comments

Comments
 (0)