fiableDB/README.md

186 lines
4.9 KiB
Markdown
Raw Normal View History

2022-11-08 07:18:14 +01:00
Immutable NoSQL database that works on a single plain text file
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**.
- Extremely fastb since it has no queue or locking limitations.
- **Minimalistic** to implement and use.
2022-11-08 07:14:29 +01:00
## Advantages of using an immutable database
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-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
To load the database you must import reliable_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
fiable_db.start("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})
// {"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},
]
)
// [
// {"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": 83, "height": 172}},
// ]
```
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-08 22:49:43 +01:00
// {"id": 4, "rev": 2, "data": {{"name": "Valentina", "age": 21, "height": 172}}
```
Add new key:
```python
2022-11-08 23:11:20 +01:00
fiable_db.update(4, {"is_active": True})
2022-11-08 22:49:43 +01:00
// {"id": 4, "rev": 3, "data": {{"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
```
Delete key:
```python
2022-11-08 23:11:20 +01:00
fiable_db.update(4, {"height": None})
2022-11-08 22:49:43 +01:00
// {"id": 4, "rev": 4, "data": {{"name": "Valentina", "age": 21, "is_active": True}}
```
Forcing new structure.
```python
2022-11-08 23:11:20 +01:00
fiable_db.update(4, {"name": "Javier", "email": "foo@example.com"}, force=True)
2022-11-08 22:49:43 +01:00
// {"id": 4, "rev": 5, "data": {{"name": "Javier", "email": "foo@example.com"}}
```
2022-11-08 23:11:20 +01:00
### Step 4: Delete
```python
fiable_db.delete(4)
// {"id": 4, "rev": 6, "data": None}
```
### Step 5: Find one
Search by id.
```python
fiable_db.find_one(id=2)
// {"id": 2, "rev": 1, "data": {{"name": "Noelia", "age": 34, "height": 165}}
```
Search by value.
```python
fiable_db.find_one(data={"name": "Noelia"})
// {"id": 2, "rev": 1, "data": {{"name": "Noelia", "age": 34, "height": 165}}
```
Search by several values.
```python
fiable_db.find_one(data={"name": "Noelia", "age": 34})
// {"id": 2, "rev": 1, "data": {{"name": "Noelia", "age": 34, "height": 165}}
```
No results.
```python
fiable_db.find_one(data={"name": "Noelia", "is_active": False})
// None
```
### Step 6: Find all
```python
fiable_db.find_all(data={"age": 41})
// [
// {"id": 1, "rev": 1, "data": {{"name": "Miguel", "age": 41, "height": 189}},
// {"id": 3, "rev": 1, "data": {{"name": "Juan", "age": 41, "height": 187}},
// ]
```
### Step 7: See previous revisions
2022-11-08 07:18:14 +01:00
2022-11-08 23:11:20 +01:00
Previous version to be deleted.
```python
fiable_db.find_one(id=4, rev=3)
// {"id": 4, "rev": 3, "data": {{"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
```
You can also use negative numbers.
```python
fiable_db.find_one(id=4, rev=-1)
// {"id": 4, "rev": 3, "data": {{"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
fiable_db.find_one(id=4, rev=-2)
// {"id": 4, "rev": 2, "data": {{"name": "Valentina", "age": 21, "height": 172}}
```
2022-11-08 07:18:14 +01:00
2022-11-08 23:27:59 +01:00
### Step 8: Working with tables or collections.
You can create as many tables as you want. The default table is called `default`. If you want to work in another area, just use the `table` attribute in any of the above functions.
```python
fiable_db.add({"name": "Luciano", "age": 54, "height": 165}, table="users")
// {"id": 1, "rev": 1, "data": {"name": "Luciano", "age": 54, "height": 165}}
fiable_db.find_one(id=1, table="users") // "users" table
// {"id": 1, "rev": 1, "data": {"name": "Luciano", "age": 54, "height": 165}}
fiable_db.find_one(id=1) // Default table
// {"id": 1, "rev": 1, "data": {"name": "Miguel", "age": 41, "height": 189}}
```
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 🐍