https://github.com/olekli/mrjsonstore
Simple, transparent on-disk JSON store
https://github.com/olekli/mrjsonstore
Last synced: 2 months ago
JSON representation
Simple, transparent on-disk JSON store
- Host: GitHub
- URL: https://github.com/olekli/mrjsonstore
- Owner: olekli
- License: apache-2.0
- Created: 2024-09-01T14:44:58.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-10-15T16:20:08.000Z (7 months ago)
- Last Synced: 2025-03-14T19:51:06.768Z (2 months ago)
- Language: Python
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mrjsonstore
Simple, transparent on-disk JSON store using `atomicwrites`.## Basic Usage
### Without context
```python
store = JsonStore('example.json')
assert isinstance(store.content, dict)
store.content['woohoo'] = 'I am just a Python dictionary'
result = store.commit()
if not result:
print(f'There was a problem when writing: {result})
```You can also use transactions...
```python
store = JsonStore('example.json')
t = store.transaction()
store.content['woohoo'] = 'I am just a Python dictionary'
result = t.commit()
if not result:
print(f'There was a problem when writing: {result})
```... and possibly roll them back:
```python
store = JsonStore('example.json')
store.content['woohoo'] = 'I am just a Python dictionary'
t = store.transaction()
store.content['woohoo'] = 'I am going to be rolled back!'
t.rollback()
assert store.content['woohoo'] == 'I am just a Python dictionary'
```### With context
```python
store = JsonStore('example.json')
with store.transaction() as t:
store.content['woohoo'] = 'I am just a Python dictionary'
```Changes will be committed on context exit, unless there is an exception:
```python
store = JsonStore('example.json')
store.content['woohoo'] = 'I am just a Python dictionary'
with store.transaction() as t:
store.content['woohoo'] = 'I am going to be rolled back!'
raise RuntimeError()
[...]
assert store.content['woohoo'] == 'I am just a Python dictionary'
```If you want to commit regardless of exceptions, you can choose not to rollback:
```python
store = JsonStore('example.json')
store.content['woohoo'] = 'I am just a Python dictionary'
with store.transaction(rollback=False) as t:
store.content['woohoo'] = 'I am not going to be rolled back!'
raise RuntimeError()
[...]
assert store.content['woohoo'] == 'I am not going to be rolled back!'
```Changes will be committed to disk then.
## TODO
* Add support for concurrency?