-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Robot table #19
Robot table #19
Changes from all commits
63b796d
15e6148
7911ae2
7fc1620
92ad0ee
c3b2b76
48ddc20
bebe030
92a9e58
0638c97
0de6e16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
"""Defines CRUD interface for user API.""" | ||
|
||
import asyncio | ||
import uuid | ||
import warnings | ||
|
||
from boto3.dynamodb.conditions import Key | ||
|
||
from store.app.api.crud.base import BaseCrud | ||
from store.app.api.model import Token, User | ||
|
||
|
@@ -14,15 +17,15 @@ async def add_user(self, user: User) -> None: | |
|
||
async def get_user(self, email: str) -> User | None: | ||
table = await self.db.Table("Users") | ||
user_dict = await table.get_item(Key={"email": email}) | ||
if "Item" not in user_dict: | ||
user_dict = await table.query(IndexName="emailIndex", KeyConditionExpression=Key("email").eq(email)) | ||
if len(user_dict["Items"]) == 0: | ||
return None | ||
user = User.model_validate(user_dict["Item"]) | ||
user = User.model_validate(user_dict["Items"][0]) | ||
return user | ||
|
||
async def delete_user(self, user: User) -> None: | ||
table = await self.db.Table("Users") | ||
await table.delete_item(Key={"email": user.email}) | ||
await table.delete_item(Key={"id": user.id}) | ||
|
||
async def list_users(self) -> list[User]: | ||
warnings.warn("`list_users` probably shouldn't be called in production", ResourceWarning) | ||
|
@@ -40,16 +43,16 @@ async def add_token(self, token: Token) -> None: | |
|
||
async def get_token(self, email: str) -> Token | None: | ||
table = await self.db.Table("Tokens") | ||
token_dict = await table.get_item(Key={"email": email}) | ||
if "Item" not in token_dict: | ||
token_dict = await table.query(IndexName="emailIndex", KeyConditionExpression=Key("email").eq(email)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately DynamoDB does not allow you to Furthermore, even after we manually enforce uniqueness, DynamoDB still has no interest in just returning the first entry with the correct email it finds. There is no method to perform a singular query on a secondary index. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never mind we will only be using email for tokens There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually tokens and the entire auth structure require significant rearch. think it is worth considering using traditional cookie based auth since it is much simpler and easier to implement. (notably i believe this implementation is wrong, though how easy it is to fix is not known to me) |
||
if len(token_dict["Items"]) == 0: | ||
return None | ||
token = Token.model_validate(token_dict["Item"]) | ||
token = Token.model_validate(token_dict["Items"][0]) | ||
return token | ||
|
||
|
||
async def test_adhoc() -> None: | ||
async with UserCrud() as crud: | ||
await crud.add_user(User(email="ben@kscale.dev")) | ||
await crud.add_user(User(id=str(uuid.uuid4()), email="ben@kscale.dev")) | ||
# print(await crud.get_user("ben")) | ||
# print(await crud.get_user_count()) | ||
# await crud.get_token("ben") | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,19 +33,26 @@ async def create_tables(crud: Crud | None = None) -> None: | |||||||||
await asyncio.gather( | ||||||||||
crud._create_dynamodb_table( | ||||||||||
name="Users", | ||||||||||
columns=[ | ||||||||||
("email", "S", "HASH"), | ||||||||||
# ("banned", "B", "RANGE"), | ||||||||||
# ("deleted", "B", "RANGE"), | ||||||||||
keys=[ | ||||||||||
("id", "S", "HASH"), | ||||||||||
], | ||||||||||
gsis=[ | ||||||||||
("emailIndex", "email", "S", "HASH"), | ||||||||||
], | ||||||||||
), | ||||||||||
crud._create_dynamodb_table( | ||||||||||
name="Tokens", | ||||||||||
columns=[ | ||||||||||
("email", "S", "HASH"), | ||||||||||
# ("issued", "N", "RANGE"), | ||||||||||
# ("disabled", "B", "RANGE"), | ||||||||||
keys=[ | ||||||||||
("id", "S", "HASH"), | ||||||||||
], | ||||||||||
gsis=[("emailIndex", "email", "S", "HASH")], | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit
Suggested change
reason is that if we add more indexes it keeps the file blame easier to parse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially did that, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you put a comma inside the list, black will format it this way |
||||||||||
), | ||||||||||
crud._create_dynamodb_table( | ||||||||||
name="Robots", | ||||||||||
keys=[ | ||||||||||
("id", "S", "HASH"), | ||||||||||
], | ||||||||||
gsis=[("ownerIndex", "owner", "S", "HASH")], | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto here |
||||||||||
), | ||||||||||
) | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beautiful :)