https://github.com/bearlike/local-json-datastore
A file-based JSON data store exposed as a library.
https://github.com/bearlike/local-json-datastore
Last synced: 21 days ago
JSON representation
A file-based JSON data store exposed as a library.
- Host: GitHub
- URL: https://github.com/bearlike/local-json-datastore
- Owner: bearlike
- License: mit
- Created: 2020-12-01T14:10:44.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-09-17T11:54:11.000Z (almost 5 years ago)
- Last Synced: 2025-11-13T22:06:23.278Z (8 months ago)
- Language: Python
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Introduction
A file-based JSON data store exposed as a library that supports CRD operations with a lock-unlock mechanism to establish a thread-safe environment.
### Features
- A Python package for a user to import and instantiate the class to perform **CRD** operations.
- 85% Testing Coverage for the application using **PyTest** and deployed on [**Travis CI**](https://travis-ci.com/github/bearlike/Local-JSON-Datastore).
- **Read, Write and Delete** operations performed on a custom local data store in a user-specified location.
- Completely **Thread-Safe** between same-user and multi-user processes.
- A **lock-unlock mechanism** to make the data store thread-safe.
- Supports **Time-To-Live property** through argument **`life`** in **`datastore.datastore.Datastore.create()`**, defines the seconds the key must be retained in the data store. Once `life` for a key has expired, the key will no longer be available for Reading or Delete operations.
- **All Non-Functional Requirements satisfied**.
- **All functions inside the package and Unit Test modules have well-defined docstring in them.**
## Getting Started
### Requirements
This package required Python 3.x.x to run. Install python from [here](https://www.python.org/).
### Getting the package
To get the package, clone this repository into your local system and make it the current working directory using the following commands.
```bash
git clone https://github.com/bearlike/Local-JSON-Datastore.git
cd Local-JSON-Datastore
```
### Using the package
#### Importing the package
Import the package using the following command.
```python
import datastore
```
#### Creating an object
By default, an object is created with the storage file called `DB.json` in the root of the present working directory as follows.
```python
db = datastore.Datastore()
```
In case a file path is required it can be specified while instantiating the class.
```python
db = Datastore(path="test/file/path")
```
#### Inserting an object
An object can be inserted into the data store using the `add_obj()` method which takes in a user-created dictionary as an argument and returns a `True` if an object has been successfully inserted or false otherwise.
```python
test_object = {
'a':'apple',
'b':'ball',
'c':'cat',
'd':'dog'
}
is_object_inserted = db.create(key='alphabets', obj=test_object)
```
If an object has to be inserted with a specific **Time-To-Live** it can be passed as a separate argument (in seconds) to the add function.
```python
test_object = {
'a':'apple',
'b':'ball',
'c':'cat',
'd':'dog'
}
is_object_inserted = db.create(key='alphabets', obj=test_object, life=100)
```
#### Deleting an Object
To delete an object that has not crossed its **Time-To-Live** the key of that object can be passed as an argument to the `delete_object()` function. Return `true` if the object has been deleted successfully else a `False`.
```python
is_object_deleted = db.delete(key='alphabets')
```
#### Retrieve an Object
To retrieve an object provide the key of the object as an argument to the `get_object()` function.
```python
key = db.read(key='alphabet')
print(key)
```
```bash
{
'a':'apple',
'b':'ball',
'c':'cat',
'd':'dog'
}
```
Made with ❤️ by Krishna Alagiri
