Fix table add

This commit is contained in:
Andros Fenollosa 2022-11-30 11:05:31 +01:00
parent 52afdbd08f
commit 8c21bd09b4
3 changed files with 69 additions and 24 deletions

View File

@ -53,7 +53,7 @@ Only one:
```python ```python
fiable_db.add({"name": "Miguel", "age": 41, "height": 189}) 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: Various:
@ -67,9 +67,9 @@ fiable_db.add(
] ]
) )
# [ # [
# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}}, # {"id": 2, "rev": 1, "table": "default", "data": {"name": "Noelia", "age": 34, "height": 165}},
# {"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}}, # {"id": 3, "rev": 1, "table": "default", "data": {"name": "Juan", "age": 41, "height": 187}},
# {"id": 4, "rev": 1, "data": {"name": "Valentina", "age": 12, "height": 142}}, # {"id": 4, "rev": 1, "table": "default", "data": {"name": "Valentina", "age": 12, "height": 142}},
# ] # ]
``` ```
@ -79,28 +79,28 @@ Update a key:
```python ```python
fiable_db.update(4, {"age": 21}) 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: If the key does not exist, it will be added:
```python ```python
fiable_db.update(4, {"is_active": True}) 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`. To delete a key you only have to give it a value `None`.
```python ```python
fiable_db.update(4, {"height": None}) 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`: To overwrite the dictionary, use the `force=True`:
```python ```python
fiable_db.update(4, {"name": "Javier", "email": "foo@example.com"}, force=True) 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 ### Step 4: Delete
@ -109,14 +109,14 @@ You can be specific by using the `id`.
```python ```python
fiable_db.delete(id=4) 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: And you can delete by performing a search for their values:
```python ```python
fiable_db.delete(data={"name": "Javier"}) fiable_db.delete(data={"name": "Javier"})
# {"id": 4, "rev": 6, "data": None} # {"id": 4, "rev": 6, "table": "default", "data": None}
``` ```
### Step 5: Find one ### Step 5: Find one
@ -125,21 +125,21 @@ Search by id.
```python ```python
fiable_db.find_one(id=2) 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. Search by value. It will give you the first match.
```python ```python
fiable_db.find_one(data={"name": "Noelia"}) 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. Search by several values.
```python ```python
fiable_db.find_one(data={"name": "Noelia", "age": 34}) 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. 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 ```python
fiable_db.find_all(data={"age": 41}) fiable_db.find_all(data={"age": 41})
# [ # [
# {"id": 1, "rev": 1, "data": {"name": "Miguel", "age": 41, "height": 189}}, # {"id": 1, "rev": 1, "table": "default", "data": {"name": "Miguel", "age": 41, "height": 189}},
# {"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}}, # {"id": 3, "rev": 1, "table": "default", "data": {"name": "Juan", "age": 41, "height": 187}},
# ] # ]
``` ```
@ -175,17 +175,17 @@ Example: Previous version to be deleted.
```python ```python
fiable_db.find_one(id=4, rev=3) 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. For convenience, you can use negative numbers. `-1` will be the previous state, `-2` is 2 states back, etc.
```python ```python
fiable_db.find_one(id=4, rev=-1) 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) 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. ### 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 ```python
fiable_db.add({"name": "Luciano", "age": 54, "height": 165}, table="users") 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 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 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 ### Step 9: Save changes

View File

@ -1,4 +1,5 @@
from typing import Dict, Tuple, Union, Sequence, TypedDict from typing import Dict, Tuple, Union, Sequence, TypedDict
from functools import reduce
import json import json
from os import path 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: def get_next_id(table: str = "default") -> int:
"""Get the next id for a table""" """Get the next id for the table"""
global database 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: def start(file_name: str = "") -> str:

View File

@ -73,5 +73,28 @@ def test_add_in_table_foo():
{"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}}, {"id": 4, "rev": 1, "table": "default", "data": {"name": "John", "age": 12}},
{"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}}, {"id": 5, "rev": 1, "table": "default", "data": {"name": "Jane", "age": 34}},
{"id": 6, "rev": 1, "table": "default", "data": {"name": "John", "age": 42}}, {"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}},
] ]