Fix table add
This commit is contained in:
parent
52afdbd08f
commit
8c21bd09b4
54
README.md
54
README.md
@ -53,7 +53,7 @@ Only one:
|
||||
|
||||
```python
|
||||
fiable_db.add({"name": "Miguel", "age": 41, "height": 189})
|
||||
# {"id": 1, "rev": 1, "data": {"name": "Miguel", "age": 41, "height": 189}}
|
||||
# {"id": 1, "rev": 1, "table": "default", "data": {"name": "Miguel", "age": 41, "height": 189}}
|
||||
```
|
||||
|
||||
Various:
|
||||
@ -67,9 +67,9 @@ fiable_db.add(
|
||||
]
|
||||
)
|
||||
# [
|
||||
# {"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}},
|
||||
# {"id": 2, "rev": 1, "table": "default", "data": {"name": "Noelia", "age": 34, "height": 165}},
|
||||
# {"id": 3, "rev": 1, "table": "default", "data": {"name": "Juan", "age": 41, "height": 187}},
|
||||
# {"id": 4, "rev": 1, "table": "default", "data": {"name": "Valentina", "age": 12, "height": 142}},
|
||||
# ]
|
||||
```
|
||||
|
||||
@ -79,28 +79,28 @@ Update a key:
|
||||
|
||||
```python
|
||||
fiable_db.update(4, {"age": 21})
|
||||
# {"id": 4, "rev": 2, "data": {"name": "Valentina", "age": 21, "height": 172}}
|
||||
# {"id": 4, "rev": 2, "table": "default", "data": {"name": "Valentina", "age": 21, "height": 172}}
|
||||
```
|
||||
|
||||
If the key does not exist, it will be added:
|
||||
|
||||
```python
|
||||
fiable_db.update(4, {"is_active": True})
|
||||
# {"id": 4, "rev": 3, "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
|
||||
# {"id": 4, "rev": 3, "table": "default", "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
|
||||
```
|
||||
|
||||
To delete a key you only have to give it a value `None`.
|
||||
|
||||
```python
|
||||
fiable_db.update(4, {"height": None})
|
||||
# {"id": 4, "rev": 4, "data": {"name": "Valentina", "age": 21, "is_active": True}}
|
||||
# {"id": 4, "rev": 4, "table": "default", "data": {"name": "Valentina", "age": 21, "is_active": True}}
|
||||
```
|
||||
|
||||
To overwrite the dictionary, use the `force=True`:
|
||||
|
||||
```python
|
||||
fiable_db.update(4, {"name": "Javier", "email": "foo@example.com"}, force=True)
|
||||
# {"id": 4, "rev": 5, "data": {"name": "Javier", "email": "foo@example.com"}}
|
||||
# {"id": 4, "rev": 5, "table": "default", "data": {"name": "Javier", "email": "foo@example.com"}}
|
||||
```
|
||||
|
||||
### Step 4: Delete
|
||||
@ -109,14 +109,14 @@ You can be specific by using the `id`.
|
||||
|
||||
```python
|
||||
fiable_db.delete(id=4)
|
||||
# {"id": 4, "rev": 6, "data": None}
|
||||
# {"id": 4, "rev": 6, "table": "default", "data": None}
|
||||
```
|
||||
|
||||
And you can delete by performing a search for their values:
|
||||
|
||||
```python
|
||||
fiable_db.delete(data={"name": "Javier"})
|
||||
# {"id": 4, "rev": 6, "data": None}
|
||||
# {"id": 4, "rev": 6, "table": "default", "data": None}
|
||||
```
|
||||
|
||||
### Step 5: Find one
|
||||
@ -125,21 +125,21 @@ Search by id.
|
||||
|
||||
```python
|
||||
fiable_db.find_one(id=2)
|
||||
# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}}
|
||||
# {"id": 2, "rev": 1, "table": "default", "data": {"name": "Noelia", "age": 34, "height": 165}}
|
||||
```
|
||||
|
||||
Search by value. It will give you the first match.
|
||||
|
||||
```python
|
||||
fiable_db.find_one(data={"name": "Noelia"})
|
||||
# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}}
|
||||
# {"id": 2, "rev": 1, "table": "default", "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}}
|
||||
# {"id": 2, "rev": 1, "table": "default", "data": {"name": "Noelia", "age": 34, "height": 165}}
|
||||
```
|
||||
|
||||
If there are no results it will return a None.
|
||||
@ -155,8 +155,8 @@ fiable_db.find_one(data={"name": "Noelia", "is_active": False})
|
||||
```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}},
|
||||
# {"id": 1, "rev": 1, "table": "default", "data": {"name": "Miguel", "age": 41, "height": 189}},
|
||||
# {"id": 3, "rev": 1, "table": "default", "data": {"name": "Juan", "age": 41, "height": 187}},
|
||||
# ]
|
||||
```
|
||||
|
||||
@ -175,17 +175,17 @@ Example: 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}}
|
||||
# {"id": 4, "rev": 3, "table": "default", "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
|
||||
```
|
||||
|
||||
For convenience, you can use negative numbers. `-1` will be the previous state, `-2` is 2 states back, etc.
|
||||
|
||||
```python
|
||||
fiable_db.find_one(id=4, rev=-1)
|
||||
# {"id": 4, "rev": 3, "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}}
|
||||
# {"id": 4, "rev": 3, "table": "default", "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}}
|
||||
# {"id": 4, "rev": 2, "table": "default", "data": {"name": "Valentina", "age": 21, "height": 172}}
|
||||
```
|
||||
|
||||
### Step 8: Working with tables or collections.
|
||||
@ -194,13 +194,25 @@ You can create as many tables as you want. The default table is called `default`
|
||||
|
||||
```python
|
||||
fiable_db.add({"name": "Luciano", "age": 54, "height": 165}, table="users")
|
||||
# {"id": 1, "rev": 1, "data": {"name": "Luciano", "age": 54, "height": 165}}
|
||||
# {"id": 1, "rev": 1, "table": "users", "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}}
|
||||
# {"id": 1, "rev": 1, "table": "users", "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}}
|
||||
# {"id": 1, "rev": 1, "table": "default", "data": {"name": "Miguel", "age": 41, "height": 189}}
|
||||
```
|
||||
|
||||
You can use the `table` parameter with any function.
|
||||
|
||||
```python
|
||||
fiable_db.update(1, {"age", 10}, table="users")
|
||||
# {"id": 1, "rev": 2, "table": "users", "data": {"name": "Luciano", "age": 10, "height": 165}}
|
||||
|
||||
fiable_db.delete(1, table="users")
|
||||
# {"id": 1, "rev": 3, "table": "users", "data": {"name": "Luciano", "age": 10, "height": 165}}
|
||||
|
||||
fiable_db.find_all(id=1, table="users")
|
||||
```
|
||||
|
||||
### Step 9: Save changes
|
||||
|
14
fiable_db.py
14
fiable_db.py
@ -1,4 +1,5 @@
|
||||
from typing import Dict, Tuple, Union, Sequence, TypedDict
|
||||
from functools import reduce
|
||||
import json
|
||||
from os import path
|
||||
|
||||
@ -27,9 +28,18 @@ Type_Find_All_Return = Tuple[Type_Find_One_Return]
|
||||
|
||||
|
||||
def get_next_id(table: str = "default") -> int:
|
||||
"""Get the next id for a table"""
|
||||
"""Get the next id for the table"""
|
||||
global database
|
||||
return database[-1]["id"] + 1 if database else 1
|
||||
|
||||
# Get the last id for the table
|
||||
def get_id(current_id, row: TypeData) -> int:
|
||||
if current_id == None and table == row["table"]:
|
||||
return row["id"]
|
||||
else:
|
||||
return current_id
|
||||
last_id = reduce(get_id, database[::-1], None)
|
||||
# Return the next id, or 1 if there is no last id
|
||||
return last_id + 1 if last_id else 1
|
||||
|
||||
|
||||
def start(file_name: str = "") -> str:
|
||||
|
@ -73,5 +73,28 @@ def test_add_in_table_foo():
|
||||
{"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
|
||||
{"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
|
||||
{"id": 6, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}},
|
||||
{"id": 7, "rev": 1, "table": "foo", "data": {"name": "John", "age": 42}},
|
||||
{"id": 1, "rev": 1, "table": "foo", "data": {"name": "John", "age": 42}},
|
||||
]
|
||||
add({"name": "Simone", "age": 33}, table="foo")
|
||||
assert get_database() == [
|
||||
{"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}},
|
||||
{"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
|
||||
{"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
|
||||
{"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
|
||||
{"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
|
||||
{"id": 6, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}},
|
||||
{"id": 1, "rev": 1, "table": "foo", "data": {"name": "John", "age": 42}},
|
||||
{"id": 2, "rev": 1, "table": "foo", "data": {"name": "Simone", "age": 33}},
|
||||
]
|
||||
add({"name": "Jose", "age": 25})
|
||||
assert get_database() == [
|
||||
{"id": 1, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}},
|
||||
{"id": 2, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
|
||||
{"id": 3, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
|
||||
{"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
|
||||
{"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
|
||||
{"id": 6, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}},
|
||||
{"id": 1, "rev": 1, "table": "foo", "data": {"name": "John", "age": 42}},
|
||||
{"id": 2, "rev": 1, "table": "foo", "data": {"name": "Simone", "age": 33}},
|
||||
{"id": 7, "rev": 1, "table": "default", "data": {"name": "Jose", "age": 25}},
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user