Skip to content

Commit

Permalink
hello there
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispenGari committed Feb 1, 2024
1 parent d62fae8 commit b01e63c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 15 additions & 1 deletion orm/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,22 @@ def find_many(self, instance: Model, filters: dict = {}):
data.append(instance(**res))
return data

def find_by_pk(self, instance: Model, pk):
def find_by_pk(self, instance: Model, pk, options: dict = {}):
# what is the name of the primary key column?
"""
SELECT
posts.post_id,
posts.content,
posts.created_at,
users.user_id,
users.username
FROM
posts
JOIN
users ON posts.user_id = users.user_id
WHERE
posts.post_id = 1; -- Replace 1 with the specific post_id you are interested in
"""
pk_name = "id"
fields = list()
for name, field in inspect.getmembers(instance):
Expand Down
4 changes: 4 additions & 0 deletions playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@ def to_dict(self):
user = User(name="Crispen", username="heyy")
userId = db.create(user)
posts = db.create_bulk([Post(userId=userId, title=f"Post {i}") for i in range(2)])

post = db.find_by_pk(Post, 1, options={"include": [User]})

print(post.to_dict())
if __name__ == "__main__":
conn.close()

0 comments on commit b01e63c

Please sign in to comment.