Skip to content

Commit

Permalink
Merge branch 'update-models'
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispenGari committed Jan 31, 2024
2 parents a7d5c7e + 8ed9626 commit daa7c18
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ When a column is marked as `UpdatedAtColumn` it's value will automatically get g
### Syncing Tables

This is the process of creating tables from models and save them to a database.
After defining your tables you will need to `sync` your database tables. To Sync a database you call the method called `sync`. This method allows you to create and commit tables into the database. Let's say we have two models `User` and `Post` and you want to create commit them to the database you can do it as follows:
After defining your tables you will need to `sync` your database tables. To Sync a database you call the method called `sync`. This method allows you to create and save tables into the database. Let's say we have two models `User` and `Post` and you want to them to the database you can do it as follows:

```py
tables = db.sync([User, Post], drop=True, force=True)
Expand Down Expand Up @@ -281,26 +281,26 @@ Returns a `conn` and `tablenames` that are in the database. The method accepts t

1. Creating a Record

The `commit` method let you create save a single row in a particular table. When you save this will return the `id` of the inserted document
The `create` method let you create save a single row in a particular table. When you save this will return the `id` of the inserted document

```py
user = User(name="Crispen", username="heyy")
userId = db.commit(user)
userId = db.create(user)
print(userId)
```

Using the `commit_bulk` you will be able to save in bulk as the method explains itself. The following is an example showing how we can add `3` post to the database table at the same time.
Using the `create_bulk` you will be able to save in bulk as the method explains itself. The following is an example showing how we can add `3` post to the database table at the same time.

```py
posts = [
Post(userId=userId, title="What are you thinking"),
Post(userId=userId, title="What are you doing?"),
Post(userId=userId, title="What are we?"),
]
row_count = db.commit_bulk(posts)
row_count = db.create_bulk(posts)
```

> Unlike the `commit`, `commit_bulk` method returns the row count of the inserted documents rather that individual `id` of those document.
> Unlike the `create`, `create_bulk` method returns the row count of the inserted documents rather that individual `id` of those document.
2. Getting records

Expand Down Expand Up @@ -411,14 +411,14 @@ So to insert a single post you first need to have a user that will create a post

```py
user = User(name="Crispen", username="heyy")
userId = db.commit(user)
userId = db.create(user)

post = Post(userId=userId, title="What are you thinking")
db.commit(post)
db.create(post)
post = Post(userId=userId, title="What are you thinking")
db.commit(post)
db.create(post)
post = Post(userId=userId, title="What are we?")
db.commit(post)
db.create(post)
```

> We have created `3` posts that belongs to `Crispen`.
Expand Down
4 changes: 2 additions & 2 deletions orm/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ def sync(self, models: list[Model], drop=False, force=False, alter=False):
except Exception as e:
raise Exception(e)

def commit(self, instance: Model):
def create(self, instance: Model):
sql, values = instance._get_insert_one_stm()
row = self._execute_sql(sql, args=tuple(values), fetchone=True)
return row[0]

def commit_bulk(self, instances: list[Model]):
def create_bulk(self, instances: list[Model]):
columns = None
placeholders = None
data = list()
Expand Down
7 changes: 6 additions & 1 deletion orm/model/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from orm.model.column import (
Column,
CreatedAtColumn,
Expand All @@ -13,7 +14,7 @@

class Model:
def __init__(self, **args) -> None:
self._data = {id: None}
self._data = {}
for k, v in args.items():
self._data[k] = v

Expand All @@ -23,6 +24,10 @@ def __getattribute__(self, key: str):
return _data[key]
return object.__getattribute__(self, key)

# def __setattr__(self, __name: str, __value: Any) -> None:
# if __name in self._data:
# self._data[__name] = __value

@classmethod
def _get_name(cls):
__tablename__ = None
Expand Down
10 changes: 5 additions & 5 deletions orm/tests/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Users(Model):
db.sync([Users], drop=True, force=True)

user = Users(name="Crispen", username="heyy")
userId = db.commit(user)
userId = db.create(user)
assert userId == 1
conn.close()

Expand Down Expand Up @@ -53,13 +53,13 @@ class Post(Model):

conn, _ = db.connect_and_sync([User, Post], drop=True, force=True)
user = User(name="Crispen", username="heyy")
userId = db.commit(user)
userId = db.create(user)
posts = [
Post(userId=userId, title="What are you thinking"),
Post(userId=userId, title="What are you doing?"),
Post(userId=userId, title="What are we?"),
]
row_count = db.commit_bulk(posts)
row_count = db.create_bulk(posts)

assert row_count == 3
conn.close()
Expand Down Expand Up @@ -98,8 +98,8 @@ class Post(Model):
db = Database("hi", password="root", user="postgres")
conn, _ = db.connect_and_sync([User, Post], drop=True, force=True)
user = User(name="Crispen", username="heyy")
userId = db.commit(user)
postId = db.commit(
userId = db.create(user)
postId = db.create(
Post(userId=userId, title="What are you thinking"),
)
now = db.find_by_pk(Post, postId)
Expand Down
2 changes: 1 addition & 1 deletion orm/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def to_dict(self):
db.sync([User], drop=True, force=True)

user = User(name="Crispen", username="heyy")
db.commit(user)
db.create(user)
users = db.find_all(User)
me = db.find_by_pk(User, 1).to_dict()
her = db.find_by_pk(User, 2)
Expand Down
23 changes: 5 additions & 18 deletions playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,11 @@ def to_dict(self):

db = Database("hi", password="root", user="postgres")
conn, tables = db.connect_and_sync([User, Post], drop=True, force=True)
user = [
User(name="Crispen", username="heyy"),
User(name="Crispen", username="heyy"),
User(name="Crispen", username="heyy"),
]
userId = db.commit_bulk(user)
# postId = db.commit(
# Post(userId=1, title="What are you thinking"),
# )

now = db.delete_one(User, {"name": "Crispen"})
print(now)


# print(f"now: {now}")
# now = db.delete_one(Post, {"id": 8})
# print(now.userId)
# print(postId)
user = User(name="Crispen", username="heyy")
userId = db.commit(user)
me = db.find_by_pk(User, 1)
# me.name = "Gari"
print(me.name)


# post = Post(userId=userId, title="What are you thinking")
Expand Down

0 comments on commit daa7c18

Please sign in to comment.