fiableDB/README.md

240 lines
6.0 KiB
Markdown
Raw Normal View History

2022-11-09 12:50:23 +01:00
2022-11-09 11:56:38 +01:00
<p align="center">
<img src="assets/logo.png" alt="fiableDB logo">
</p>
2022-11-08 07:14:29 +01:00
## Features
2022-11-08 22:14:27 +01:00
- **Information is never lost**. Even if you make updates or deletions, you will be able to recover any information at any time.
- There are **no restrictions on the data structure or columns**, since dictionaries are used without limitations on nesting. Similar to MongoDB Documents.
- All the information is **stored in a JSON file**.
2022-11-08 23:33:30 +01:00
- **Extremely fast** since it has no queue or locking limitations.
2022-11-08 22:14:27 +01:00
- **Minimalistic** to implement and use.
2022-11-08 07:14:29 +01:00
2022-11-08 23:33:30 +01:00
## Why use fiableDB instead of other relational databases?
2022-11-08 07:18:14 +01:00
2022-11-08 22:14:27 +01:00
- **High level of consistency and accuracy of data**, such as a hospital patient's chronology or banking data. It cannot be modified once it has been aggregated.
- They **simplify the process of backing up and restoring data**, because you can always **revert to the original version** of the data if necessary.
- **Very secure**, modifying existing data will be detected and rejected.
2022-11-08 07:18:14 +01:00
## Install
2022-11-21 16:37:43 +01:00
Python version: >=3.8
2022-11-08 22:14:27 +01:00
```python
2022-11-08 23:27:59 +01:00
pip3 install --user fiable_db
2022-11-08 22:14:27 +01:00
```
2022-11-08 23:11:20 +01:00
## Docs
2022-11-08 22:49:43 +01:00
All documentation can be read as a sequential tutorial.
2022-11-08 23:11:20 +01:00
### Step1: Start
2022-11-09 12:13:22 +01:00
To load the database you must import `fiable_db` and start it.
2022-11-08 22:49:43 +01:00
```python
import fiable_db
2022-11-08 23:11:20 +01:00
fiable_db.start()
```
It will create a file named `fiable_db.json` in the current directory. If you want to change the name of the file, you can do it by passing the name as a parameter.
```python
2022-11-09 12:13:22 +01:00
fiable_db.start(file="my_db.json")
2022-11-08 22:49:43 +01:00
```
2022-11-08 07:18:14 +01:00
2022-11-08 23:11:20 +01:00
If the file already exists, it will be loaded. Nothing is deleted here!
### Step 2: Agregation
2022-11-08 07:18:14 +01:00
2022-11-08 22:49:43 +01:00
Only one:
```python
2022-11-08 23:11:20 +01:00
fiable_db.add({"name": "Miguel", "age": 41, "height": 189})
2022-11-08 23:29:57 +01:00
# {"id": 1, "rev": 1, "data": {"name": "Miguel", "age": 41, "height": 189}}
2022-11-08 22:49:43 +01:00
```
Various:
```python
2022-11-08 23:11:20 +01:00
fiable_db.add(
2022-11-08 22:49:43 +01:00
[
{"name": "Noelia", "age": 34, "height": 165},
{"name": "Juan", "age": 41, "height": 187},
{"name": "Valentina", "age": 12, "height": 142},
]
)
2022-11-08 23:34:21 +01:00
# [
2022-11-20 22:15:20 +01:00
# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}},
# {"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}},
# {"id": 4, "rev": 1, "data": {"name": "Valentina", "age": 12, "height": 142}},
2022-11-08 23:34:21 +01:00
# ]
2022-11-08 22:49:43 +01:00
```
2022-11-08 23:11:20 +01:00
### Step 3: Update
2022-11-08 07:18:14 +01:00
2022-11-08 22:49:43 +01:00
Update a key:
```python
2022-11-08 23:11:20 +01:00
fiable_db.update(4, {"age": 21})
2022-11-20 22:15:20 +01:00
# {"id": 4, "rev": 2, "data": {"name": "Valentina", "age": 21, "height": 172}}
2022-11-08 22:49:43 +01:00
```
2022-11-09 12:13:22 +01:00
If the key does not exist, it will be added:
2022-11-08 22:49:43 +01:00
```python
2022-11-08 23:11:20 +01:00
fiable_db.update(4, {"is_active": True})
2022-11-20 22:15:20 +01:00
# {"id": 4, "rev": 3, "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
2022-11-08 22:49:43 +01:00
```
2022-11-09 12:13:22 +01:00
To delete a key you only have to give it a value `None`.
2022-11-08 22:49:43 +01:00
```python
2022-11-08 23:11:20 +01:00
fiable_db.update(4, {"height": None})
2022-11-20 22:15:20 +01:00
# {"id": 4, "rev": 4, "data": {"name": "Valentina", "age": 21, "is_active": True}}
2022-11-08 22:49:43 +01:00
```
2022-11-09 12:13:22 +01:00
To overwrite the dictionary, use the `force=True`:
2022-11-08 22:49:43 +01:00
```python
2022-11-08 23:11:20 +01:00
fiable_db.update(4, {"name": "Javier", "email": "foo@example.com"}, force=True)
2022-11-20 22:15:20 +01:00
# {"id": 4, "rev": 5, "data": {"name": "Javier", "email": "foo@example.com"}}
2022-11-08 22:49:43 +01:00
```
2022-11-08 23:11:20 +01:00
### Step 4: Delete
2022-11-09 12:13:22 +01:00
You can be specific by using the `id`.
```python
fiable_db.delete(id=4)
# {"id": 4, "rev": 6, "data": None}
```
And you can delete by performing a search for their values:
2022-11-08 23:11:20 +01:00
```python
2022-11-09 12:13:22 +01:00
fiable_db.delete(data={"name": "Javier"})
2022-11-08 23:29:57 +01:00
# {"id": 4, "rev": 6, "data": None}
2022-11-08 23:11:20 +01:00
```
### Step 5: Find one
Search by id.
```python
fiable_db.find_one(id=2)
2022-11-20 22:15:20 +01:00
# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}}
2022-11-08 23:11:20 +01:00
```
2022-11-09 12:13:22 +01:00
Search by value. It will give you the first match.
2022-11-08 23:11:20 +01:00
```python
fiable_db.find_one(data={"name": "Noelia"})
2022-11-20 22:15:20 +01:00
# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}}
2022-11-08 23:11:20 +01:00
```
Search by several values.
```python
fiable_db.find_one(data={"name": "Noelia", "age": 34})
2022-11-20 22:15:20 +01:00
# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}}
2022-11-08 23:11:20 +01:00
```
2022-11-09 12:13:22 +01:00
If there are no results it will return a None.
2022-11-08 23:11:20 +01:00
```python
fiable_db.find_one(data={"name": "Noelia", "is_active": False})
2022-11-08 23:29:57 +01:00
# None
2022-11-08 23:11:20 +01:00
```
### Step 6: Find all
```python
fiable_db.find_all(data={"age": 41})
2022-11-08 23:29:57 +01:00
# [
2022-11-20 22:15:20 +01:00
# {"id": 1, "rev": 1, "data": {"name": "Miguel", "age": 41, "height": 189}},
# {"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}},
2022-11-08 23:29:57 +01:00
# ]
2022-11-08 23:11:20 +01:00
```
2022-11-09 12:13:22 +01:00
If no results are found it will return an empty list.
```python
fiable_db.find_all(data={"age": 88})
# []
```
2022-11-08 23:11:20 +01:00
### Step 7: See previous revisions
2022-11-08 07:18:14 +01:00
2022-11-09 12:13:22 +01:00
At any time you can view the previous information of any row using the `rev` parameter.
Example: Previous version to be deleted.
2022-11-08 23:11:20 +01:00
```python
fiable_db.find_one(id=4, rev=3)
2022-11-20 22:15:20 +01:00
# {"id": 4, "rev": 3, "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
2022-11-08 23:11:20 +01:00
```
2022-11-09 12:13:22 +01:00
For convenience, you can use negative numbers. `-1` will be the previous state, `-2` is 2 states back, etc.
2022-11-08 23:11:20 +01:00
```python
fiable_db.find_one(id=4, rev=-1)
2022-11-20 22:15:20 +01:00
# {"id": 4, "rev": 3, "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
2022-11-08 23:11:20 +01:00
fiable_db.find_one(id=4, rev=-2)
2022-11-20 22:15:20 +01:00
# {"id": 4, "rev": 2, "data": {"name": "Valentina", "age": 21, "height": 172}}
2022-11-08 23:11:20 +01:00
```
2022-11-08 07:18:14 +01:00
2022-11-08 23:27:59 +01:00
### Step 8: Working with tables or collections.
2022-11-09 12:13:22 +01:00
You can create as many tables as you want. The default table is called `default`. If you want to work in another table, just use the `table` attribute in any of the above functions.
2022-11-08 23:27:59 +01:00
```python
fiable_db.add({"name": "Luciano", "age": 54, "height": 165}, table="users")
2022-11-08 23:29:57 +01:00
# {"id": 1, "rev": 1, "data": {"name": "Luciano", "age": 54, "height": 165}}
2022-11-08 23:27:59 +01:00
2022-11-08 23:34:21 +01:00
fiable_db.find_one(id=1, table="users") # "users" table
2022-11-08 23:29:57 +01:00
# {"id": 1, "rev": 1, "data": {"name": "Luciano", "age": 54, "height": 165}}
2022-11-08 23:27:59 +01:00
2022-11-08 23:34:21 +01:00
fiable_db.find_one(id=1) # Default table
2022-11-08 23:29:57 +01:00
# {"id": 1, "rev": 1, "data": {"name": "Miguel", "age": 41, "height": 189}}
2022-11-08 23:27:59 +01:00
```
2022-11-28 16:10:49 +01:00
### Step 9: Save changes
Save the database to a file.
```python
fiable_db.save(filename, data)
```
2022-11-21 16:37:43 +01:00
### Other help functions
#### Get all data
Get all data from the database.
```python
2022-11-28 16:10:49 +01:00
fiable_db.get_database()
2022-11-21 16:37:43 +01:00
```
### Load file
Load a file into the database.
```python
2022-11-28 16:10:49 +01:00
fiable_db.load(filename)
2022-11-21 16:37:43 +01:00
```
2022-11-13 17:05:21 +01:00
## Implementations in other languages
- [Clojure](https://github.com/Toni-zgz/db_inmutable).
- [Haskell](https://github.com/FabianVegaA/SafeDB).
2022-11-08 23:11:20 +01:00
---
2022-11-08 22:14:27 +01:00
2022-11-08 23:11:20 +01:00
Thanks to the power of 🐍 Python 🐍