From de73c2de583882061dc27585be1d600c11629762 Mon Sep 17 00:00:00 2001 From: Crispen Gari Date: Sun, 4 Feb 2024 11:58:58 +0200 Subject: [PATCH] documentation updates --- README.md | 32 +++++++++++++++++--------------- dataloom.sql | 5 ++++- dataloom/model/model.py | 25 ------------------------- 3 files changed, 21 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 9f9e0e6..d85f68f 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ - [`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) @@ -30,6 +32,8 @@ - [4. Deleting a record](#4-deleting-a-record) - [5. Updating a record](#5-updating-a-record) - [Associations](#associations) +- [Pagination](#pagination) +- [Ordering](#ordering) ### Key Features: @@ -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) @@ -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 # .... @@ -597,16 +609,6 @@ db.create(post) > We have created `3` posts that belongs to `Crispen`. - - - - - - - - - - - - -
ArgumentDescriptionTypeDefault
+### Pagination + +### Ordering diff --git a/dataloom.sql b/dataloom.sql index a4c0be6..bb2a9e2 100644 --- a/dataloom.sql +++ b/dataloom.sql @@ -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'; diff --git a/dataloom/model/model.py b/dataloom/model/model.py index 6c14e8d..fcccfba 100644 --- a/dataloom/model/model.py +++ b/dataloom/model/model.py @@ -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 = {}