Skip to content

Commit

Permalink
documentation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispenGari committed Feb 4, 2024
1 parent d11f722 commit de73c2d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
- [`CreatedAtColumn` Class](#createdatcolumn-class)
- [`UpdatedAtColumn` Class](#updatedatcolumn-class)
- [Syncing Tables](#syncing-tables)
- [1. The `sync` method.](#1-the-sync-method)
- [2. The `connect_and_sync` method.](#2-the-connect_and_sync-method)
- [CRUD Operations with Dataloom](#crud-operations-with-dataloom)
- [1. Creating a Record](#1-creating-a-record)
- [2. Getting records](#2-getting-records)
- [3. Getting a single record](#3-getting-a-single-record)
- [4. Deleting a record](#4-deleting-a-record)
- [5. Updating a record](#5-updating-a-record)
- [Associations](#associations)
- [Pagination](#pagination)
- [Ordering](#ordering)

### Key Features:

Expand Down Expand Up @@ -393,7 +397,11 @@ When a column is designated as `UpdatedAtColumn`, its value will be automaticall

### Syncing Tables

Syncing tables involves the process of creating tables from models and saving them to a database. After defining your tables, you will need to synchronize your database tables using the `sync` method. This method enables you to create and save tables into the database. For instance, if you have two models, `User` and `Post`, and you want to synchronize them with the database, you can achieve it as follows:
Syncing tables involves the process of creating tables from models and saving them to a database. After defining your tables, you will need to synchronize your database tables using the `sync` method.

#### 1. The `sync` method.

This method enables you to create and save tables into the database. For instance, if you have two models, `User` and `Post`, and you want to synchronize them with the database, you can achieve it as follows:

```py
tables = sqlite_loom.sync([Post, User], drop=True, force=True)
Expand All @@ -409,7 +417,11 @@ The method returns a list of table names that have been created or that exist in
| `force` | Forcefully drop tables during syncing or not. | `bool` | `False` |
| `alter` | Alter tables instead of dropping them during syncing or not. | `bool` | `False` |

> We've noticed two steps involved in starting to work with our `orm`. Initially, you need to create a connection and then synchronize the tables in another step. The `connect_and_sync` function proves to be very handy as it handles both the database connection and table synchronization. Here is an example demonstrating its usage:
> We've noticed two steps involved in starting to work with our `orm`. Initially, you need to create a connection and then synchronize the tables in another step.
#### 2. The `connect_and_sync` method.

The `connect_and_sync` function proves to be very handy as it handles both the database connection and table synchronization. Here is an example demonstrating its usage:

```py
# ....
Expand Down Expand Up @@ -597,16 +609,6 @@ db.create(post)

> We have created `3` posts that belongs to `Crispen`.
<table border="1">
<thead>
<tr><th>Argument</th><th>Description</th>
<th>Type</th><th>Default</th>
</tr>
</thead>
<tbody>
<tr>
<td></td><td></td>
<td></td><td></td>
</tr>
</tbody>
</table>
### Pagination

### Ordering
5 changes: 4 additions & 1 deletion dataloom.sql
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@
[2024-02-04 11:48:58.073059] : Dataloom[sqlite]: SELECT `id`, `name`, `username` FROM `users` WHERE `name` = ? AND `username` = ?;
[2024-02-04 11:48:58.103291] : Dataloom[sqlite]: SELECT `completed`, `createdAt`, `id`, `title`, `updatedAt`, `userId` FROM `posts`;
[2024-02-04 11:48:58.152371] : Dataloom[sqlite]: DROP TABLE IF EXISTS posts;
[2024-02-04 11:48:58.202978] : Dataloom[sqlite]: CREATE TABLE IF NOT EXISTS `posts` (`completed` BOOLEAN DEFAULT False, `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, `title` VARCHAR NOT NULL, `createdAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `updatedAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `userId` INTEGER NOT NULL REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE);
[2024-02-04 11:48:58.202978] : Dataloom[sqlite]: CREATE TABLE IF NOT EXISTS `posts`
(`completed` BOOLEAN DEFAULT False, `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
`title` VARCHAR NOT NULL, `createdAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `updatedAt`
TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `userId` INTEGER NOT NULL REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE);
[2024-02-04 11:48:58.240048] : Dataloom[sqlite]: DROP TABLE IF EXISTS users;
[2024-02-04 11:48:58.278033] : Dataloom[sqlite]: CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, `name` TEXT NOT NULL DEFAULT 'Bob', `username` VARCHAR UNIQUE, `createdAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `updatedAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
[2024-02-04 11:48:58.329710] : Dataloom[sqlite]: SELECT name FROM sqlite_master WHERE type='table';
Expand Down
25 changes: 0 additions & 25 deletions dataloom/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,6 @@ def _get_delete_bulk_where_stm(cls, args: dict = {}):
)
return sql, params

@classmethod
def _get_update_by_pk_stm(cls, pk_name: str = "id", args: dict = {}):
updatedAtColumName = None
for name, field in inspect.getmembers(cls):
if isinstance(field, UpdatedAtColumn):
updatedAtColumName = name

values = list()
placeholders = list()
for key, value in args.items():
placeholders.append(f'"{key}" = %s')
values.append(value)

if updatedAtColumName is not None:
placeholders.append(f'"{updatedAtColumName}" = %s')
values.append(current_time_stamp)

sql = PgStatements.UPDATE_BY_PK_COMMAND.format(
table_name=cls._get_name(),
pk="%s",
pk_name=pk_name,
placeholder_values=", ".join(placeholders),
)
return sql, values

@classmethod
def _get_update_one_stm(
cls, pk_name: str = "", filters: dict = {}, args: dict = {}
Expand Down

0 comments on commit de73c2d

Please sign in to comment.