Skip to content

Commit 5d527f4

Browse files
committed
Storage API.
1 parent 84c45ac commit 5d527f4

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

docs/user-guide/builtins.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,80 @@ The following code demonstrates a `pyscript.WebSocket` in action.
372372
ws = WebSocket(url="ws://example.com/socket", onmessage=onmessage)
373373
```
374374

375+
### `pyscript.storage`
376+
377+
The `pyscript.storage` API wraps the browser's built-in
378+
[IndexDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)
379+
persistent storage in a synchronous Pythonic API.
380+
381+
```python
382+
from pyscript import storage
383+
384+
385+
# Each store must have a meaningful name.
386+
store = await storage("my-storage-name")
387+
388+
# store is a dictionary and can now be used as such.
389+
```
390+
391+
The returned dictionary automatically loads the current state of the referenced
392+
IndexDB. All changes are automatically queued in the background.
393+
394+
```python
395+
# This is a write operation.
396+
store["key"] = value
397+
398+
# This is also a write operation (it changes the stored data).
399+
del store["key"]
400+
```
401+
402+
Should you wish to be certain changes have been synchronized to the underlying
403+
IndexDB, just `await store.sync()`.
404+
405+
Common types of value can be stored via this API: `bool`, `float`, `int`, `str`
406+
and `None`. In addition, data structures like `list`, `dict` and `tuple` can
407+
be stored.
408+
409+
!!! warning
410+
411+
Because of the way the underlying data structure are stored in IndexDB,
412+
a Python `tuple` will always be returned as a Python `list`.
413+
414+
It is even possible to store arbitrary data via a `bytearray` or
415+
`memoryview` object. However, there is a limitation that **such values must be
416+
stored as a single key/value pair, and not as part of a nested data
417+
structure**.
418+
419+
Sometimes you may need to modify the behaviour of the `dict` like object
420+
returned by `pyscript.storage`. To do this, create a new class that inherits
421+
from `pyscript.Storage`, then pass in your class to `pyscript.storage` as the
422+
`storage_class` argument:
423+
424+
```python
425+
from pyscript import window, storage, Storage
426+
427+
428+
class MyStorage(Storage):
429+
430+
def __setitem__(self, key, value):
431+
super().__setitem__(key, value)
432+
window.console.log(key, value)
433+
...
434+
435+
436+
store = await storage("my-data-store", storage_class=MyStorage)
437+
438+
# The store object is now an instance of MyStorage.
439+
```
440+
441+
!!! warning
442+
443+
The storage API is persistent per user tab, page, or domain, in the same
444+
way IndexedDB persists.
445+
446+
This API **is not** saving files in the interpreter's virtual file system
447+
nor onto the user's hard drive.
448+
375449
### `pyscript.ffi.to_js`
376450

377451
A utility function to convert Python references into their JavaScript

0 commit comments

Comments
 (0)