diff --git a/README.md b/README.md index a7b39b6..5c4d970 100644 --- a/README.md +++ b/README.md @@ -704,7 +704,7 @@ Returns a `conn` and the list of `tablenames` that exist in the database. The me ### CRUD Operations with Dataloom -In this section of the documentation, we will illustrate how to perform basic `CRUD` operations using `dataloom` on simple `Models`. Please note that in the following code snippets, I will be utilizing `sqlite_loom`. However, it's important to highlight that you can use any `loom` of your choice to follow along. +In this section of the documentation, we will illustrate how to perform basic `CRUD` operations using `dataloom` on simple `Models`. Please note that in the following code snippets, I will be utilizing `sqlite_loom`, `mysql_loom`, `pg_loom` or `loom` interchangeably. However, it's important to highlight that you can use any `loom` of your choice to follow along. #### 1. Creating a Record @@ -846,7 +846,7 @@ The `find_many()` method takes in the following arguments: ##### 3. `find_one()` -Here is an example showing you how you can use `find_by_pk()` locate a single record in the database. +Here is an example showing you how you can use `find_one()` locate a single record in the database. ```py user = mysql_loom.find_one( @@ -1040,8 +1040,11 @@ To mitigate the potential risks associated with `delete_bulk()`, follow these gu - When calling `delete_bulk()`, make sure to provide a filter to specify the subset of records to be deleted. This helps prevent unintentional deletions. ```python - # Example: Delete records where 'status' is 'inactive' - sqlite_loom.delete_bulk(filter={'status': 'inactive'}) + # Example: Delete records where 'status' is 'inactive' + affected_rows = mysql_loom.delete_bulk( + instance=User, + filters=Filter(column="status", value='inactive'), + ) ``` 2. **Consider Usage When Necessary:** diff --git a/docs/source/api/operations/delete.rst b/docs/source/api/operations/delete.rst deleted file mode 100644 index e69de29..0000000 diff --git a/docs/source/api/operations/index.rst b/docs/source/api/operations/index.rst deleted file mode 100644 index 06a890d..0000000 --- a/docs/source/api/operations/index.rst +++ /dev/null @@ -1,19 +0,0 @@ -CRUD Operations with Dataloom -+++++++++++++++++++++++++++++ - -In this section of the documentation, we will illustrate how to perform basic ``CRUD`` operations using ``dataloom`` on simple ``Models``. Please note that in the following code snippets, I will be utilizing ``sqlite_loom``. However, it's important to highlight that you can use any ``loom`` of your choice to follow along. - -#. `Creating a Record `_ -#. `Updating a Record `_ - - - -.. toctree:: - :maxdepth: 2 - :hidden: - - create - update - read - delete - \ No newline at end of file diff --git a/docs/source/api/operations/read.rst b/docs/source/api/operations/read.rst deleted file mode 100644 index e69de29..0000000 diff --git a/docs/source/api/operations/update.rst b/docs/source/api/operations/update.rst deleted file mode 100644 index e69de29..0000000 diff --git a/docs/source/api/qb.rst b/docs/source/api/qb.rst new file mode 100644 index 0000000..643d2b8 --- /dev/null +++ b/docs/source/api/qb.rst @@ -0,0 +1,92 @@ + +Query Builder ++++++++++++++ + +``Dataloom`` exposes a method called ``getQueryBuilder``, which allows you to obtain a ``qb`` object. This object enables you to execute SQL queries directly from SQL scripts. + +.. code-block:: + + qb = loom.getQueryBuilder() + print(qb) # ? = Loom QB + + +The ``qb`` object contains the method called ``run``, which is used to execute SQL scripts or SQL queries. + +.. code-block:: + + ids = qb.run("select id from posts;", fetchall=True) + print(ids) # ? = [(1,), (2,), (3,), (4,)] + + +You can also execute SQL files. In the following example, we will demonstrate how you can execute SQL scripts using the ``qb``. Let's say we have an SQL file called ``qb.sql`` which contains the following SQL code: + +.. code-block:: SQL + -- qb.sql + + SELECT id, title FROM posts WHERE id IN (1, 3, 2, 4) LIMIT 4 OFFSET 1; + SELECT COUNT(*) FROM ( + SELECT DISTINCT `id` + FROM `posts` + WHERE `id` < 5 + LIMIT 3 OFFSET 2 + ) AS subquery; + + +We can use the query builder to execute the SQL as follows: + +.. code-block:: + + with open("qb.sql", "r") as reader: + sql = reader.read() + res = qb.run( + sql, + fetchall=True, + is_script=True, + ) + print(res) + + +.. tip:: 👍 **Pro Tip:** Executing a script using query builder does not return a result. The result value is always ``None``. + +The ``run`` method takes the following as arguments: + +.. rst-class:: my-table + ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| Argument | Description | Type | Required | Default | ++===================+==========================================================================================+==================================================================+==========+===========+ +| ``sql`` | SQL query to execute. | ``str`` | `Yes` | | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``args`` | Parameters for the SQL query. | ``Any`` or ``None`` | ``No`` | ``None`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``fetchone`` | Whether to fetch only one result. | ``bool`` | ``No`` | ``False`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``fetchmany`` | Whether to fetch multiple results. | ``bool`` | ``No`` | ``False`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``fetchall`` | Whether to fetch all results. | ``bool`` | ``No`` | ``False`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``mutation`` | Whether the query is a mutation (insert, update, delete). | ``bool`` | ``No`` | ``True`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``bulk`` | Whether the query is a bulk operation. | ``bool`` | ``No`` | ``False`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``affected_rows`` | Whether to return affected rows. | ``bool`` | ``No`` | ``False`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``operation`` | Type of operation being performed. | ``'insert'``, ``'update'``, ``'delete'``, ``'read'`` or ``None`` | ``No`` | ``False`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``verbose`` | Verbosity level for logging . Set this option to ``0`` if you don't want logging at all. | ``int`` | ``No`` | ``1`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ +| ``is_script`` | Whether the SQL is a script. | ``bool`` | ``No`` | ``False`` | ++-------------------+------------------------------------------------------------------------------------------+------------------------------------------------------------------+----------+-----------+ + + +Why Use Query Builder? +---------------------- + +- The query builder empowers developers to seamlessly execute ``SQL`` queries directly. +- While Dataloom primarily utilizes ``subqueries`` for eager data fetching on models, developers may prefer to employ JOIN operations, which are achievable through the ``qb`` object. + +.. code-block:: + qb = loom.getQueryBuilder() + result = qb.run("SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.table1_id;") + print(result) + \ No newline at end of file diff --git a/docs/source/api/tables/index.rst b/docs/source/api/tables/index.rst index 4b68e63..09fdd62 100644 --- a/docs/source/api/tables/index.rst +++ b/docs/source/api/tables/index.rst @@ -11,17 +11,17 @@ Syncing tables involves the process of creating tables from models and saving th .. rst-class:: my-table -+----------+----------------------------------------------------------------------------------------------------------------------------+-------+---------+ -| Argument | Description | Type | Default | -+==========+============================================================================================================================+=======+=========+ -| models | A collection or a single instance(s) of your table classes that inherit from the Model class. | Model | [] | -+----------+----------------------------------------------------------------------------------------------------------------------------+-------+---------+ -| drop | Whether to drop tables during syncing or not. | bool | False | -+----------+----------------------------------------------------------------------------------------------------------------------------+-------+---------+ -| force | Forcefully drop tables during syncing. In mysql this will temporarily disable foreign key checks when dropping the tables. | bool | False | -+----------+----------------------------------------------------------------------------------------------------------------------------+-------+---------+ -| alter | Alter tables instead of dropping them during syncing or not. | bool | False | -+----------+----------------------------------------------------------------------------------------------------------------------------+-------+---------+ ++------------+----------------------------------------------------------------------------------------------------------------------------+-----------+-----------+ +| Argument | Description | Type | Default | ++============+============================================================================================================================+===========+===========+ +| ``models`` | A collection or a single instance(s) of your table classes that inherit from the ``Model`` class. | ``Model`` | ``[]`` | ++------------+----------------------------------------------------------------------------------------------------------------------------+-----------+-----------+ +| ``drop`` | Whether to drop tables during syncing or not. | ``bool`` | ``False`` | ++------------+----------------------------------------------------------------------------------------------------------------------------+-----------+-----------+ +| ``force`` | Forcefully drop tables during syncing. In mysql this will temporarily disable foreign key checks when dropping the tables. | ``bool`` | ``False`` | ++------------+----------------------------------------------------------------------------------------------------------------------------+-----------+-----------+ +| ``alter`` | Alter tables instead of dropping them during syncing or not. | ``bool`` | ``False`` | ++------------+----------------------------------------------------------------------------------------------------------------------------+-----------+-----------+ .. tip:: 🥇 We recommend you to use ``drop`` or ``force`` if you are going to change or modify ``foreign`` and ``primary`` keys. This is because setting the option ``alter`` does not have an effect on ``primary`` key columns. diff --git a/docs/source/examples/associations/bi_directional.rst b/docs/source/examples/associations/bi_directional.rst new file mode 100644 index 0000000..ea2fcd8 --- /dev/null +++ b/docs/source/examples/associations/bi_directional.rst @@ -0,0 +1,83 @@ +4. What about ``bidirectional`` queries? +++++++++++++++++++++++++++++++++++++++++ + +In ``Dataloom``, we support bidirectional relations with eager loading on-the-fly. You can query from a ``parent`` to a ``child`` and from a ``child`` to a ``parent``. You just need to know how the relationship is mapped between these two models. In this case, the `has` option is very important in the `Include` class. Here are some examples demonstrating bidirectional querying between `user` and `post`, where the `user` is the parent table and the `post` is the child table in this case. + +1. Child to Parent +================== + +Here is an example illustrating how we can query a parent from child table. + +.. code-block:: + + posts_users = mysql_loom.find_many( + Post, + limit=2, + offset=3, + order=[Order(column="id", order="DESC")], + select=["id", "title"], + include=[ + Include( + model=User, + select=["id", "username"], + has="one", + include=[Include(model=Profile, select=["id", "avatar"], has="one")], + ), + Include( + model=Category, + select=["id", "type"], + order=[Order(column="id", order="DESC")], + has="many", + limit=2, + ), + ], + ) + print(posts_users) # ? = [{'id': 1, 'title': 'Hey', 'user': {'id': 1, 'username': '@miller', 'profile': {'id': 1, 'avatar': 'hello.jpg'}}, 'categories': [{'id': 4, 'type': 'sport'}, {'id': 3, 'type': 'tech'}]}] + + +2. Parent to Child +================== + +Here is an example of how we can query a child table from parent table + +.. code-block:: + + user_post = mysql_loom.find_by_pk( + User, + pk=userId, + select=["id", "username"], + include=[ + Include( + model=Post, + limit=2, + offset=3, + order=[Order(column="id", order="DESC")], + select=["id", "title"], + include=[ + Include( + model=User, + select=["id", "username"], + has="one", + include=[ + Include(model=Profile, select=["id", "avatar"], has="one") + ], + ), + Include( + model=Category, + select=["id", "type"], + order=[Order(column="id", order="DESC")], + has="many", + limit=2, + ), + ], + ), + Include(model=Profile, select=["id", "avatar"], has="one"), + ], + ) + + + print(user_post) """ ? = + {'id': 1, 'username': '@miller', 'user': {'id': 1, 'username': '@miller', 'profile': {'id': 1, 'avatar': 'hello.jpg'}}, 'categories': [{'id': 4, 'type': 'sport'}, {'id': 3, 'type': 'tech'}], 'posts': [{'id': 1, 'title': 'Hey', 'user': {'id': 1, 'username': '@miller', 'profile': {'id': 1, 'avatar': 'hello.jpg'}}, 'categories': [{'id': 4, 'type': 'sport'}, {'id': 3, 'type': 'tech'}]}], 'profile': {'id': 1, 'avatar': 'hello.jpg'}} + """ + + diff --git a/docs/source/examples/associations/index.rst b/docs/source/examples/associations/index.rst new file mode 100644 index 0000000..9a2c513 --- /dev/null +++ b/docs/source/examples/associations/index.rst @@ -0,0 +1,25 @@ +Associations +++++++++++++ + +In dataloom you can create association using the ``foreign-keys`` column during model creation. You just have to specify a single model to have a relationship with another model using the ``ForeignKeyColum``. Just by doing that ``dataloom`` will be able to learn bidirectional relationship between your models. Let's have a look at the following examples: + +#. `One to One Association `_ +#. `One to Many Association `_ +#. `Many to One Association `_ +#. `What about bidirectional queries? `_ +#. `Self Association `_ +#. `Many to Many Association `_ + + + +.. toctree:: + :maxdepth: 2 + :hidden: + + one_to_one + one_to_n + n_to_one + bi_directional + self_relations + n_to_n + \ No newline at end of file diff --git a/docs/source/examples/associations/n_to_n.rst b/docs/source/examples/associations/n_to_n.rst new file mode 100644 index 0000000..d86aaa7 --- /dev/null +++ b/docs/source/examples/associations/n_to_n.rst @@ -0,0 +1,216 @@ +6. ``Many to Many`` Relationship +++++++++++++++++++++++++++++++++ + +Let's consider a scenario where we have tables for ``Students`` and ``Courses``. In this scenario, a student can enroll in many courses, and a single course can have many students enrolled. This represents a ``Many-to-Many`` relationship. The model definitions for this scenario can be done as follows in ``dataloom``: + +**Table: Student** + +.. rst-class:: my-table + ++-------------+-------------+ +| Column Name | Data Type | ++=============+=============+ +| ``id`` | ``INT`` | ++-------------+-------------+ +| ``name`` | ``VARCHAR`` | ++-------------+-------------+ + +**Table: Course** +.. rst-class:: my-table + ++-------------+-------------+ +| Column Name | Data Type | ++=============+=============+ +| ``id`` | ``INT`` | ++-------------+-------------+ +| ``name`` | ``VARCHAR`` | ++-------------+-------------+ +| ... | ... | ++-------------+-------------+ + +**Table: Student_Courses** (junction table) + +.. rst-class:: my-table + ++---------------+-----------+ +| Column Name | Data Type | ++===============+===========+ +| ``studentId`` | ``INT`` | ++---------------+-----------+ +| ``courseId`` | ``INT`` | ++---------------+-----------+ + + +.. tip:: 👍 **Pro Tip:** Note that the ``junction`` table can also be called ``association``-table or ``reference``-table or ``joint``-table. + +In ``dataloom`` we can model the above relations as follows: + +.. code-block:: + + class Course(Model): + __tablename__: TableColumn = TableColumn(name="courses") + id = PrimaryKeyColumn(type="int", auto_increment=True) + name = Column(type="text", nullable=False, default="Bob") + + + class Student(Model): + __tablename__: TableColumn = TableColumn(name="students") + id = PrimaryKeyColumn(type="int", auto_increment=True) + name = Column(type="text", nullable=False, default="Bob") + + + class StudentCourses(Model): + __tablename__: TableColumn = TableColumn(name="students_courses") + studentId = ForeignKeyColumn(table=Student, type="int") + courseId = ForeignKeyColumn(table=Course, type="int") + + + +- The tables ``students`` and ``courses`` will not have foreign keys. +- The ``students_courses`` table will have two columns that joins these two tables together in an ``N-N`` relational mapping. + +.. tip:: 👍 **Pro Tip:** In a joint table no other columns such as ``CreateAtColumn``, ``UpdatedAtColumn``, ``Column`` and ``PrimaryKeyColumn`` are allowed and only exactly ``2`` foreign keys should be in this table. + +Inserting Records +================= + +Here is how we can insert ``students`` and ``courses`` in their respective tables. + +.. code-block:: + + + # insert the courses + mathId = mysql_loom.insert_one( + instance=Course, values=ColumnValue(name="name", value="Mathematics") + ) + engId = mysql_loom.insert_one( + instance=Course, values=ColumnValue(name="name", value="English") + ) + phyId = mysql_loom.insert_one( + instance=Course, values=ColumnValue(name="name", value="Physics") + ) + + # create students + + stud1 = mysql_loom.insert_one( + instance=Student, values=ColumnValue(name="name", value="Alice") + ) + stud2 = mysql_loom.insert_one( + instance=Student, values=ColumnValue(name="name", value="Bob") + ) + stud3 = mysql_loom.insert_one( + instance=Student, values=ColumnValue(name="name", value="Lisa") + ) + + +- You will notice that we are keeping in track of the ``studentIds`` and the ``courseIds`` because we will need them in the ``joint-table`` or ``association-table``. +- Now we can enrol students to their courses by inserting them in their id's in the association table. + +.. code-block:: + + # enrolling students + mysql_loom.insert_bulk( + instance=StudentCourses, + values=[ + [ + ColumnValue(name="studentId", value=stud1), + ColumnValue(name="courseId", value=mathId), + ], # enrolling Alice to mathematics + [ + ColumnValue(name="studentId", value=stud1), + ColumnValue(name="courseId", value=phyId), + ], # enrolling Alice to physics + [ + ColumnValue(name="studentId", value=stud1), + ColumnValue(name="courseId", value=engId), + ], # enrolling Alice to english + [ + ColumnValue(name="studentId", value=stud2), + ColumnValue(name="courseId", value=engId), + ], # enrolling Bob to english + [ + ColumnValue(name="studentId", value=stud3), + ColumnValue(name="courseId", value=phyId), + ], # enrolling Lisa to physics + [ + ColumnValue(name="studentId", value=stud3), + ColumnValue(name="courseId", value=engId), + ], # enrolling Lisa to english + ], + ) + + + +Retrieving Records +================== + +Now let's query a student called ``Alice`` with her courses. We can do it as follows: + +.. code-block:: + + s = mysql_loom.find_by_pk( + Student, + pk=stud1, + select=["id", "name"], + ) + c = mysql_loom.find_many( + StudentCourses, + filters=Filter(column="studentId", value=stud1), + select=["courseId"], + ) + courses = mysql_loom.find_many( + Course, + filters=Filter(column="id", operator="in", value=[list(i.values())[0] for i in c]), + select=["id", "name"], + ) + + alice = {**s, "courses": courses} + print(courses) # ? = {'id': 1, 'name': 'Alice', 'courses': [{'id': 1, 'name': 'Mathematics'}, {'id': 2, 'name': 'English'}, {'id': 3, 'name': 'Physics'}]} + + +We're querying the database to retrieve information about a ``student`` and their associated ``courses``. Here are the steps in achieving that: + +1. **Querying Student**: + + - We use ``mysql_loom.find_by_pk()`` to fetch a single ``student`` record from the database in the table ``students``. + +2. **Querying Course Id's**: + - Next we are going to query all the course ids of that student and store them in ``c`` in the joint table ``students_courses``. + - We use ``mysql_loom.find_many()`` to retrieve the course ``ids`` of ``alice``. +3. **Querying Course**: + - Next we will query all the courses using the operator ``in`` in the ``courses`` table based on the id's we obtained previously. + +As you can see we are doing a lot of work to get the information about ``Alice``. With eager loading this can be done in one query as follows the above can be done as follows: + +.. code-block:: + + alice = mysql_loom.find_by_pk( + Student, + pk=stud1, + select=["id", "name"], + include=Include( + model=Course, junction_table=StudentCourses, alias="courses", has="many" + ), + ) + + print(alice) # ? = {'id': 1, 'name': 'Alice', 'courses': [{'id': 1, 'name': 'Mathematics'}, {'id': 2, 'name': 'English'}, {'id': 3, 'name': 'Physics'}]} + + +- We use ``mysql_loom.find_by_pk()`` to retrieve a single student record from the database. +- Furthermore, we include the associated ``course`` records using ``eager`` loading with an ``alias`` of ``courses``. +- We specify a ``junction_table`` in our ``Include`` statement. This allows dataloom to recognize the relationship between the ``students`` and ``courses`` tables through this ``junction_table``. + +.. note:: 👍 **Pro Tip:** It is crucial to specify the ``junction_table`` when querying in a many-to-many (``N-N``) relationship. This is because, by default, the models will not establish a direct many-to-many relationship without referencing the ``junction_table``. They lack foreign key columns within them to facilitate this relationship. + +As for our last example let's query all the students that are enrolled in the ``English`` class. We can easily do it as follows: + +.. code-block:: + + english = mysql_loom.find_by_pk( + Course, + pk=engId, + select=["id", "name"], + include=Include(model=Student, junction_table=StudentCourses, has="many"), + ) + + print(english) # ? = {'id': 2, 'name': 'English', 'students': [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Lisa'}]} diff --git a/docs/source/examples/associations/n_to_one.rst b/docs/source/examples/associations/n_to_one.rst new file mode 100644 index 0000000..c0c4847 --- /dev/null +++ b/docs/source/examples/associations/n_to_one.rst @@ -0,0 +1,148 @@ +3. ``Many to One`` Association +++++++++++++++++++++++++++++++ + +Models can have ``Many`` to ``One`` relationship, it depends on how you define them. Let's have a look at the relationship between a ``Category`` and a ``Post``. Many categories can belong to a single post. + +.. code-block:: + + class Post(Model): + __tablename__: Optional[TableColumn] = TableColumn(name="posts") + id = PrimaryKeyColumn(type="int", auto_increment=True, nullable=False, unique=True) + completed = Column(type="boolean", default=False) + title = Column(type="varchar", length=255, nullable=False) + # timestamps + createdAt = CreatedAtColumn() + # relations + userId = ForeignKeyColumn( + User, + maps_to="1-N", + type="int", + required=True, + onDelete="CASCADE", + onUpdate="CASCADE", + ) + + class Category(Model): + __tablename__: Optional[TableColumn] = TableColumn(name="categories") + id = PrimaryKeyColumn(type="int", auto_increment=True, nullable=False, unique=True) + type = Column(type="varchar", length=255, nullable=False) + + postId = ForeignKeyColumn( + Post, + maps_to="N-1", + type="int", + required=True, + onDelete="CASCADE", + onUpdate="CASCADE", + ) + + + +In the provided code, we have two models: ``Post`` and ``Category``. The relationship between these two models can be described as a ``Many-to-One`` relationship. + +This means that many categories can belong to a single post. In other words: + +- For each ``Post`` instance, there can be multiple ``Category`` instances associated with it. +- However, each ``Category`` instance can only be associated with one ``Post``. + +For example, consider a blogging platform where each ``Post`` represents an article and each ``Category`` represents a topic or theme. Each article (post) can be assigned to multiple topics (categories), such as "Technology", "Travel", "Food", etc. However, each topic (category) can only be associated with one specific article (post). + +This relationship allows for a hierarchical organization of data, where posts can be categorized into different topics or themes represented by categories. + +Inserting Records +================= + +Let's illustrate the following example where we insert categories into a post with the ``id`` 1. + +.. code-block:: + + for title in ["Hey", "Hello", "What are you doing", "Coding"]: + mysql_loom.insert_one( + instance=Post, + values=[ + ColumnValue(name="userId", value=userId), + ColumnValue(name="title", value=title), + ], + ) + + for cat in ["general", "education", "tech", "sport"]: + mysql_loom.insert_one( + instance=Category, + values=[ + ColumnValue(name="postId", value=1), + ColumnValue(name="type", value=cat), + ], + ) + + +- **Inserting Posts** + We're inserting new posts into the ``Post`` table. Each post is associated with a user (``userId``), and we're iterating over a list of titles to insert multiple posts. + +- **Inserting Categories** + We're inserting new categories into the ``Category`` table. Each category is associated with a specific post (``postId``), and we're inserting categories for a post with ``id`` 1. + +.. tip:: In summary, we're creating a relationship between posts and categories by inserting records into their respective tables. Each category record is linked to a specific post record through the ``postId`` attribute. + +Retrieving Records +================== + +Let's attempt to retrieve a post with an ID of ``1`` along with its corresponding categories. We can achieve this as follows: + +.. code-block:: + + post = mysql_loom.find_by_pk(Post, 1, select=["id", "title"]) + categories = mysql_loom.find_many( + Category, + select=["type", "id"], + filters=Filter(column="postId", value=1), + order=[ + Order(column="id", order="DESC"), + ], + ) + post_with_categories = {**post, "categories": categories} + print(post_with_categories) # ? = {'id': 1, 'title': 'Hey', 'categories': [{'type': 'sport', 'id': 4}, {'type': 'tech', 'id': 3}, {'type': 'education', 'id': 2}, {'type': 'general', 'id': 1}]} + + +- We use the ``mysql_loom.find_by_pk()`` method to retrieve a single post (``Post``) with an ``id`` equal to 1. We select only specific columns (``id`` and ``title``) for the post. +- We use the ``mysql_loom.find_many()`` method to retrieve multiple categories (``Category``) associated with the post. We select only specific columns (``type`` and ``id``) for the categories. We apply a filter to only fetch categories associated with the post with ``postId`` equal to 1. We sort the categories based on the ``id`` column in descending order. +- We create a dictionary (``post_with_categories``) that contains the retrieved post and its associated categories. The post information is stored under the key ``post``, and the categories information is stored under the key ``categories``. + +.. note:: The above task can be accomplished using ``eager`` document retrieval as shown below. + +.. code-block:: + + post_with_categories = mysql_loom.find_by_pk( + Post, + 1, + select=["id", "title"], + include=[ + Include( + model=Category, + select=["type", "id"], + order=[ + Order(column="id", order="DESC"), + ], + ) + ], + ) + + + +The code snippet queries a database to retrieve a post with an ``id`` of ``1`` along with its associated categories. Here's a breakdown: + +1. **Querying for Post**: + + - The ``mysql_loom.find_by_pk()`` method fetches a single post from the database. + - It specifies the ``Post`` model and ID ``1``, retrieving only the ``id`` and ``title`` columns. + +2. **Including Categories**: + + - The ``include`` parameter specifies additional related data to fetch. + - Inside ``include``, an ``Include`` instance is created for categories related to the post. + - It specifies the ``Category`` model and selects only the ``type`` and ``id`` columns. + - Categories are ordered by ``id`` in descending order. + +3. **Result**: + - The result is stored in ``post_with_categories``, containing the post information and associated categories. + +.. tip:: In summary, this code is retrieving a specific post along with its categories from the database, and it's using ``eager`` loading to efficiently fetch related data in a single query. \ No newline at end of file diff --git a/docs/source/examples/associations/one_to_n.rst b/docs/source/examples/associations/one_to_n.rst new file mode 100644 index 0000000..12f1cc4 --- /dev/null +++ b/docs/source/examples/associations/one_to_n.rst @@ -0,0 +1,137 @@ +2. ``One to Many`` Association +++++++++++++++++++++++++++++++ + +Let's consider a scenario where a ``User`` has multiple ``Post``. here is how the relationships are mapped. + +.. code-block:: + + class User(Model): + __tablename__: Optional[TableColumn] = TableColumn(name="users") + id = PrimaryKeyColumn(type="int", auto_increment=True) + name = Column(type="text", nullable=False, default="Bob") + username = Column(type="varchar", unique=True, length=255) + tokenVersion = Column(type="int", default=0) + + class Post(Model): + __tablename__: Optional[TableColumn] = TableColumn(name="posts") + id = PrimaryKeyColumn(type="int", auto_increment=True, nullable=False, unique=True) + completed = Column(type="boolean", default=False) + title = Column(type="varchar", length=255, nullable=False) + # timestamps + createdAt = CreatedAtColumn() + # relations + userId = ForeignKeyColumn( + User, + maps_to="1-N", + type="int", + required=True, + onDelete="CASCADE", + onUpdate="CASCADE" + ) + + +So clearly we can see that when creating a ``post`` we need to have a ``userId`` + +Inserting Records +================= + +Here is how we can insert a user and a post to the database tables. + +.. code-block:: + + userId = mysql_loom.insert_one( + instance=User, + values=ColumnValue(name="username", value="@miller"), + ) + for title in ["Hey", "Hello", "What are you doing", "Coding"]: + mysql_loom.insert_one( + instance=Post, + values=[ + ColumnValue(name="userId", value=userId), + ColumnValue(name="title", value=title), + ], + ) + + +We're performing database operations to insert records for a user and multiple posts associated with that user. + +- We insert a user record into the database using ``mysql_loom.insert_one()`` method. +- We iterate over a list of titles. +- For each title in the list, we insert a new post record into the database. +- Each post is associated with the user we inserted earlier, identified by the ``userId``. +- The titles for the posts are set based on the titles in the list. + +Retrieving Records +================== + +Now let's query the user with his respective posts. we can do it as follows: + +.. code-block:: + + user = mysql_loom.find_by_pk( + User, + 1, + select=["id", "username"], + ) + posts = mysql_loom.find_many( + Post, + filters=Filter(column="userId", value=userId, operator="eq"), + select=["id", "title"], + order=[Order(column="id", order="DESC")], + limit=2, + offset=1, + ) + + user_with_posts = {**user, "posts": posts} + print( + user_with_posts + ) # ? = {'id': 1, 'username': '@miller', 'posts': [{'id': 3, 'title': 'What are you doing'}, {'id': 2, 'title': 'Hello'}]} + + +We're querying the database to retrieve information about a ``user`` and their associated ``posts``. + +1. **Querying User**: + + - We use ``mysql_loom.find_by_pk()`` to fetch a single user record from the database. + - The user's ID is specified as ``1``. + - We select only the ``id`` and ``username`` columns for the user. + +2. **Querying Posts**: + + - We use ``mysql_loom.find_many()`` to retrieve multiple post records associated with the user. + - A filter is applied to only fetch posts where the ``userId`` matches the ID of the user retrieved earlier. + - We select only the ``id`` and ``title`` columns for the posts. + - The posts are ordered by the ``id`` column in descending order. + - We set a limit of ``2`` posts to retrieve, and we skip the first post using an offset of ``1``. + - We create a dictionary ``user_with_posts`` containing the user information and a list of their associated posts under the key ``"posts"``. + +With eager loading this can be done as follows the above can be done as follows: + +.. code-block:: + + user_with_posts = mysql_loom.find_by_pk( + User, + 1, + select=["id", "username"], + include=[ + Include( + model=Post, + select=["id", "title"], + order=[Order(column="id", order="DESC")], + limit=2, + offset=1, + ) + ], + ) + print( + user_with_posts + ) # ? = {'id': 1, 'username': '@miller', 'posts': [{'id': 3, 'title': 'What are you doing'}, {'id': 2, 'title': 'Hello'}]} + + +- We use ``mysql_loom.find_by_pk()`` to fetch a single user record from the database. +- The user's ID is specified as ``1``. +- We select only the ``id`` and ``username`` columns for the user. +- Additionally, we include associated post records using ``eager`` loading. +- Inside the ``include`` parameter, we specify the ``Post`` model and select only the ``id`` and ``title`` columns for the posts. +- The posts are ordered by the ``id`` column in descending order. +- We set a limit of ``2`` posts to retrieve, and we skip the first post using an offset of ``1``. \ No newline at end of file diff --git a/docs/source/examples/associations/one_to_one.rst b/docs/source/examples/associations/one_to_one.rst new file mode 100644 index 0000000..df4df2f --- /dev/null +++ b/docs/source/examples/associations/one_to_one.rst @@ -0,0 +1,137 @@ +1. ``One to One`` Association ++++++++++++++++++++++++++++++ + +Let's consider an example where we want to map the relationship between a ``User`` and a ``Profile``: + +.. code-block:: + + class User(Model): + __tablename__: Optional[TableColumn] = TableColumn(name="users") + id = PrimaryKeyColumn(type="int", auto_increment=True) + name = Column(type="text", nullable=False, default="Bob") + username = Column(type="varchar", unique=True, length=255) + tokenVersion = Column(type="int", default=0) + + class Profile(Model): + __tablename__: Optional[TableColumn] = TableColumn(name="profiles") + id = PrimaryKeyColumn(type="int", auto_increment=True) + avatar = Column(type="text", nullable=False) + userId = ForeignKeyColumn( + User, + maps_to="1-1", + type="int", + required=True, + onDelete="CASCADE", + onUpdate="CASCADE", + ) + + + +The above code demonstrates how to establish a ``one-to-one`` relationship between a ``User`` and a ``Profile`` using the ``dataloom``. + +- ``User`` and ``Profile`` are two model classes inheriting from ``Model``. +- Each model is associated with a corresponding table in the database, defined by the ``__tablename__`` attribute. +- Both models have a primary key column (``id``) defined using ``PrimaryKeyColumn``. +- Additional columns (``name``, ``username``, ``tokenVersion`` for ``User`` and ``avatar``, ``userId`` for ``Profile``) are defined using ``Column``. +- The ``userId`` column in the ``Profile`` model establishes a foreign key relationship with the ``id`` column of the ``User`` model using ``ForeignKeyColumn``. This relationship is specified to be a ``one-to-one`` relationship (``maps_to="1-1"``). +- Various constraints such as ``nullable``, ``unique``, ``default``, and foreign key constraints (``onDelete``, ``onUpdate``) are specified for the columns. + +Inserting Records +================= + +In the following code example we are going to demonstrate how we can create a ``user`` with a ``profile``, first we need to create a user first so that we get reference to the user of the profile that we will create. + +.. code-block:: + + userId = mysql_loom.insert_one( + instance=User, + values=ColumnValue(name="username", value="@miller"), + ) + + profileId = mysql_loom.insert_one( + instance=Profile, + values=[ + ColumnValue(name="userId", value=userId), + ColumnValue(name="avatar", value="hello.jpg"), + ], + ) + + +This Python code snippet demonstrates how to insert data into the database using the ``mysql_loom.insert_one`` method, it also work on other methods like ``insert_bulk``. + +1. **Inserting a User Record**: + + - The ``mysql_loom.insert_one`` method is used to insert a new record into the ``User`` table. + - The ``instance=User`` parameter specifies that the record being inserted belongs to the ``User`` model. + - The ``values=ColumnValue(name="username", value="@miller")`` parameter specifies the values to be inserted into the ``User`` table, where the ``username`` column will be set to ``"@miller"``. + - The ID of the newly inserted record is obtained and assigned to the variable ``userId``. + +2. **Inserting a Profile Record**: + + - Again, the ``mysql_loom.insert_one`` method is called to insert a new record into the ``Profile`` table. + - The ``instance=Profile`` parameter specifies that the record being inserted belongs to the ``Profile`` model. + - The ``values`` parameter is a list containing two ``ColumnValue`` objects: + - The first ``ColumnValue`` object specifies that the ``userId`` column of the ``Profile`` table will be set to the ``userId`` value obtained from the previous insertion. + - The second ``ColumnValue`` object specifies that the ``avatar`` column of the ``Profile`` table will be set to ``"hello.jpg"``. + - The ID of the newly inserted record is obtained and assigned to the variable ``profileId``. + +Retrieving Records +================== + +The following example shows you how you can retrieve the data in a associations + +.. code-block:: + + profile = mysql_loom.find_one( + instance=Profile, + select=["id", "avatar"], + filters=Filter(column="userId", value=userId), + ) + user = mysql_loom.find_by_pk( + instance=User, + pk=userId, + select=["id", "username"], + ) + user_with_profile = {**user, "profile": profile} + print(user_with_profile) # ? = {'id': 1, 'username': '@miller', 'profile': {'id': 1, 'avatar': 'hello.jpg'}} + + +This Python code snippet demonstrates how to query data from the database using the ``mysql_loom.find_one`` and ``mysql_loom.find_by_pk`` methods, and combine the results of these two records that have association. + +1. **Querying a Profile Record**: + + - The ``mysql_loom.find_one`` method is used to retrieve a single record from the ``Profile`` table. + - The ``filters=Filter(column="userId", value=userId)`` parameter filters the results to only include records where the ``userId`` column matches the ``userId`` value obtained from a previous insertion. + +2. **Querying a User Record**: + + - The ``mysql_loom.find_by_pk`` method is used to retrieve a single record from the ``User`` table based on its primary key (``pk=userId``). + - The ``instance=User`` parameter specifies that the record being retrieved belongs to the ``User`` model. + - The ``select=["id", "username"]`` parameter specifies that only the ``id`` and ``username`` columns should be selected. + - The retrieved user data is assigned to the variable ``user``. + +3. **Combining User and Profile Data**: + + - The user data (``user``) and profile data (``profile``) are combined into a single dictionary (``user_with_profile``) using dictionary unpacking (``{**user, "profile": profile}``). + - This dictionary represents a user with their associated profile. + +.. tip:: 🏒 We have realized that we are performing three steps when querying records, which can be verbose. However, in dataloom, we have introduced ``eager`` data fetching for all methods that retrieve data from the database. The following example demonstrates how we can achieve the same result as before using eager loading: + +.. code-block:: + + # With eager loading + user_with_profile = mysql_loom.find_by_pk( + instance=User, + pk=userId, + select=["id", "username"], + include=[Include(model=Profile, select=["id", "avatar"], has="one")], + ) + print(user_with_profile) # ? = {'id': 1, 'username': '@miller', 'profile': {'id': 1, 'avatar': 'hello.jpg'}} + + +This Python code snippet demonstrates how to use eager loading with the ``mysql_loom.find_by_pk`` method to efficiently retrieve data from the ``User`` and ``Profile`` tables in a single query. + +- Eager loading allows us to retrieve related data from multiple tables in a single database query, reducing the need for multiple queries and improving performance. +- In this example, the ``include`` parameter is used to specify eager loading for the ``Profile`` model associated with the ``User`` model. +- By including the ``Profile`` model with the ``User`` model in the ``find_by_pk`` method call, we instruct the database to retrieve both the user data (``id`` and ``username``) and the associated profile data (`id` and `avatar`) in a single query. +- This approach streamlines the data retrieval process and minimizes unnecessary database calls, leading to improved efficiency and performance in applications. \ No newline at end of file diff --git a/docs/source/examples/associations/self_relations.rst b/docs/source/examples/associations/self_relations.rst new file mode 100644 index 0000000..7c1f954 --- /dev/null +++ b/docs/source/examples/associations/self_relations.rst @@ -0,0 +1,102 @@ +5. ``Self`` Association ++++++++++++++++++++++++ + +Let's consider a scenario where we have a table ``Employee``, where an employee can have a supervisor, which in this case a supervisor is also an employee. This is an example of self relations. The model definition for this can be done as follows in ``dataloom``. + +.. code-block:: + + class Employee(Model): + __tablename__: TableColumn = TableColumn(name="employees") + id = PrimaryKeyColumn(type="int", auto_increment=True) + name = Column(type="text", nullable=False, default="Bob") + supervisorId = ForeignKeyColumn( + "Employee", maps_to="1-1", type="int", required=False + ) + + +So clearly we can see that when creating a ``employee`` it is not a must to have a ``supervisorId`` as this relationship is optional. + +.. tip:: 👍 **Pro Tip:** Note that when doing self relations the referenced table must be a string that matches the table class name irrespective of case. In our case we used ``"Employee"`` and also ``"employee"`` and ``"EMPLOYEe"`` will be valid, however ``"Employees"`` and also ``"employees"`` and ``"EMPLOYEEs"`` are invalid. + +Inserting Records +================== + +Here is how we can insert employees to this table and we will make ``John`` the supervisor of other employees. + +.. code-block:: + + empId = mysql_loom.insert_one( + instance=Employee, values=ColumnValue(name="name", value="John Doe") + ) + + rows = mysql_loom.insert_bulk( + instance=Employee, + values=[ + [ + ColumnValue(name="name", value="Michael Johnson"), + ColumnValue(name="supervisorId", value=empId), + ], + [ + ColumnValue(name="name", value="Jane Smith"), + ColumnValue(name="supervisorId", value=empId), + ], + ], + ) + + + +- Some employees is are associated with a supervisor ``John`` which are ``Jane`` and ``Michael``. +- However the employee ``John`` does not have a supervisor. + +Retrieving Records +================== + +Now let's query employee ``Michael`` with his supervisor. + +.. code-block:: + + emp = mysql_loom.find_by_pk( + instance=Employee, pk=2, select=["id", "name", "supervisorId"] + ) + sup = mysql_loom.find_by_pk( + instance=Employee, select=["id", "name"], pk=emp["supervisorId"] + ) + emp_and_sup = {**emp, "supervisor": sup} + print(emp_and_sup) # ? = {'id': 2, 'name': 'Michael Johnson', 'supervisorId': 1, 'supervisor': {'id': 1, 'name': 'John Doe'}} + + +We're querying the database to retrieve information about a ``employee`` and their associated ``supervisor``. + +1. **Querying an Employee**: + + - We use ``mysql_loom.find_by_pk()`` to fetch a single employee record from the database. + - The employee's ID is specified as ``2``. + +2. **Querying Supervisor**: + + - We use ``mysql_loom.find_by_pk()`` to retrieve a supervisor that is associated with this employee. + - We create a dictionary ``emp_and_sup`` containing the ``employee`` information and their ``supervisor``. + +With eager loading this can be done in one query as follows the above can be done as follows: + +.. code-block:: + + emp_and_sup = mysql_loom.find_by_pk( + instance=Employee, + pk=2, + select=["id", "name", "supervisorId"], + include=Include( + model=Employee, + has="one", + select=["id", "name"], + alias="supervisor", + ), + ) + + print(emp_and_sup) # ? = {'id': 2, 'name': 'Michael Johnson', 'supervisorId': 1, 'supervisor': {'id': 1, 'name': 'John Doe'}} + + +- We use ``mysql_loom.find_by_pk()`` to fetch a single an employee record from the database. +- Additionally, we include associated ``employee`` record using ``eager`` loading with an ``alias`` of ``supervisor``. + +.. tip:: 👍 **Pro Tip:** Note that the ``alias`` is very important in this situation because it allows you to get the included relationships with objects that are named well, if you don't give an alias dataloom will just use the model class name as the alias of your included models, in this case you will get an object that looks like ``{'id': 2, 'name': 'Michael Johnson', 'supervisorId': 1, 'employee': {'id': 1, 'name': 'John Doe'}}``, which practically and theoretically doesn't make sense. \ No newline at end of file diff --git a/docs/source/examples/data_aggregation.rst b/docs/source/examples/data_aggregation.rst new file mode 100644 index 0000000..bc3df9a --- /dev/null +++ b/docs/source/examples/data_aggregation.rst @@ -0,0 +1,75 @@ + +Data Aggregation +++++++++++++++++ + +With the ``Having`` and the ``Group`` classes you can perform some powerful powerful queries. In this section we are going to demonstrate an example of how we can do the aggregate queries. + +.. code-block:: + + posts = mysql_loom.find_many( + Post, + select="id", + filters=Filter(column="id", operator="gt", value=1), + group=Group( + column="id", + function="MAX", + having=Having(column="id", operator="in", value=(2, 3, 4)), + return_aggregation_column=True, + ), + ) + + +The following will be the output from the above query. + +.. code-block:: shell + + [{'id': 2, 'MAX(`id`)': 2}, {'id': 3, 'MAX(`id`)': 3}, {'id': 4, 'MAX(`id`)': 4}] + + +However you can remove the aggregation column from the above query by specifying the ``return_aggregation_column`` to be ``False``: + +.. code-block:: + + posts = mysql_loom.find_many( + Post, + select="id", + filters=Filter(column="id", operator="gt", value=1), + group=Group( + column="id", + function="MAX", + having=Having(column="id", operator="in", value=(2, 3, 4)), + return_aggregation_column=False, + ), + ) + print(posts) + + +This will output: + +.. code-block:: shell + + [{'id': 2}, {'id': 3}, {'id': 4}] + + +Aggregation Functions +===================== + +You can use the following aggregation functions that ``dataloom`` supports: + +.. rst-class:: my-table + ++-------------+--------------------------------------------------+ +| Description | | ++=============+==================================================+ +| ``"AVG"`` | Computes the average of the values in the group. | ++-------------+--------------------------------------------------+ +| ``"COUNT"`` | Counts the number of items in the group. | ++-------------+--------------------------------------------------+ +| ``"SUM"`` | Computes the sum of the values in the group. | ++-------------+--------------------------------------------------+ +| ``"MAX"`` | Retrieves the maximum value in the group. | ++-------------+--------------------------------------------------+ +| ``"MIN"`` | Retrieves the minimum value in the group. | ++-------------+--------------------------------------------------+ + +.. tip:: 👍 **Pro Tip**: Note that data aggregation only works without ``eager`` loading and also works only with ``find_may()`` and ``find_all()`` in ``dataloom``. \ No newline at end of file diff --git a/docs/source/examples/filtering.rst b/docs/source/examples/filtering.rst new file mode 100644 index 0000000..bc45c3e --- /dev/null +++ b/docs/source/examples/filtering.rst @@ -0,0 +1,247 @@ + +Filtering Records ++++++++++++++++++ + +There are different find of filters that you can use when filtering documents for mutations and queries. Filters are very important to use when updating and deleting documents as they give you control on which documents should be updated or deleted. When doing a mutation you can use a single or multiple filters. Bellow is an example that shows you how you can use a single filter in deleting a single record that has an ``id`` greater than ``1`` from the database. + +.. code-block:: + + res2 = mysql_loom.delete_one( + instance=Post, + offset=0, + order=Order(column="id", order="DESC"), + filters=Filter(column="id", value=1, operator="gt"), + ) + + +Or you can use it as follows: + +.. code-block:: + + res2 = mysql_loom.delete_one( + instance=Post, + offset=0, + order=[Order(column="id", order="DESC")], + filters=[Filter(column="id", value=1, operator="gt")], + ) + + +As you have noticed, you can join your filters together and they will be applied sequentially using the ``join_next_with`` which can be either ``OR`` or ``AND`` te default value is ``AND``. Here is an of filter usage in sequential. + +.. code-block:: + + res2 = mysql_loom.delete_one( + instance=Post, + offset=0, + order=[Order(column="id", order="DESC")], + filters=[ + Filter(column="id", value=1, operator="gt"), + Filter(column="userId", value=1, operator="eq", join_next_with="OR"), + Filter( + column="title", + value='"What are you doing general?"', + operator="=", + join_next_with="AND", + ), + ], + ) + + +Operators to use with Filters +============================= + +You can use the ``operator`` to match the values. Here is the table of description for these filters. + +.. rst-class:: my-table + ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| Operator | Explanation | Expect | ++===============+==============================================================================================================+==============================+ +| ``'eq'`` | Indicates equality. It checks if the value is equal to the specified criteria. | ``Value == Criteria`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'lt'`` | Denotes less than. It checks if the value is less than the specified criteria. | ``Value < Criteria`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'gt'`` | Denotes greater than. It checks if the value is greater than the specified criteria. | ``Value > Criteria`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'leq'`` | Denotes less than or equal to. It checks if the value is less than or equal to the specified criteria. | ``Value <= Criteria`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'geq'`` | Denotes greater than or equal to. It checks if the value is greater than or equal to the specified criteria. | ``Value >= Criteria`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'in'`` | Checks if the value is included in a specified list of values. | ``Value in List`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'notIn'`` | Checks if the value is not included in a specified list of values. | ``Value in List`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'like'`` | Performs a pattern matching operation. It checks if the value is similar to a specified pattern. | ``Value matches Pattern`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'not'`` | Indicates non-equality. It checks if the column value that does not equal to the specified criteria. | ``Not Value = Criteria`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'neq'`` | Indicates non-equality. It checks if the value is not equal to the specified criteria. | ``Value != Criteria`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ +| ``'between'`` | It checks range values that matches a given range between the minimum and maximum. | ``Value BETWEEN (min, max)`` | ++---------------+--------------------------------------------------------------------------------------------------------------+------------------------------+ + +Let's talk about these filters in detail of code by example. Let's say you want to update a ``Post`` where the ``id`` matches ``1`` you can do it as follows: + +.. code-block:: + + res2 = mysql_loom.update_one( + instance=Post, + filters=Filter( + column="id", + value=1, + operator="eq", + ), + values=[ColumnValue(name="title", value="Bob")], + ) + + +What if you want to update a post where ``id`` is not equal to ``1`` you can do it as follows + +.. code-block:: + + res2 = mysql_loom.update_bulk( + instance=Post, + filters=Filter( + column="id", + value=1, + operator="neq", + ), + values=[ColumnValue(name="title", value="Bob")], + ) + + +What if i want to update the records that have an ``id`` less than ``3``? + +.. code-block:: + + res2 = mysql_loom.update_bulk( + instance=Post, + filters=Filter( + column="id", + value=3, + operator="lt", + ), + values=[ColumnValue(name="title", value="Bob")], + ) + + +What if i want to update the records that have an ``id`` less than or equal ``3``? + +.. code-block:: + + res2 = mysql_loom.update_bulk( + instance=Post, + filters=Filter( + column="id", + value=1, + operator="neq", + ), + values=[ColumnValue(name="title", value="Bob")], + ) + + +What if i want to update the records that have an ``id`` greater than ``3``? + +.. code-block:: + + res = mysql_loom.update_bulk( + instance=Post, + filters=Filter( + column="id", + value=3, + operator="gt", + ), + values=[ColumnValue(name="title", value="Bob")], + ) + + +What if i want to update the records that have an ``id`` greater or equal to ``3``? + +.. code-block:: + + res = mysql_loom.update_bulk( + instance=Post, + filters=Filter( + column="id", + value=3, + operator="geq", + ), + values=[ColumnValue(name="title", value="Bob")], + ) + + +You can use the ``in`` to update or query records that matches values in a specified ``list`` of values or ``tuple``. Here is an example showing you how you can update records that does matches ``id`` in ``[1, 2]``. + +.. code-block:: + + res = mysql_loom.update_bulk( + instance=Post, + filters=Filter( + column="id", + value=[1, 2], + operator="in", + ), + values=[ColumnValue(name="title", value="Bob")], + ) + + +You can use the ``notIn`` to update or query records that does not matches values in a specified ``list`` of values or ``tuple``. Here is an example showing you how you can update records that does not matches ``id`` in ``[1, 2]``. + +.. code-block:: + + + res = mysql_loom.update_bulk( + instance=Post, + filters=Filter( + column="id", + value=[1, 2], + operator="notIn", + ), + values=[ColumnValue(name="title", value="Bob")], + ) + + +You can use the ``like`` operator to match some patens in your query filters. Let's say we want to match a post that has the title ends with ``general`` we can use the ``like`` operator as follows + +.. code-block:: + + general = mysql_loom.find_one( + instance=Post, + filters=Filter( + column="title", + value="% general?", + operator="like", + ), + select=["id", "title"], + ) + + print(general) # ? {'id': 1, 'title': 'What are you doing general?'} + + +The following table show you some expression that you can use with this ``like`` operator. + +.. rst-class:: my-table + ++------------------+--------------------------------------------------------------------------------------------------------------------------+ +| Value | Description | ++==================+==========================================================================================================================+ +| ``%pattern`` | Finds values that end with the specified pattern. | ++------------------+--------------------------------------------------------------------------------------------------------------------------+ +| ``pattern%`` | Finds values that start with the specified pattern. | ++------------------+--------------------------------------------------------------------------------------------------------------------------+ +| ``%pattern%`` | Finds values that contain the specified pattern anywhere within the string. | ++------------------+--------------------------------------------------------------------------------------------------------------------------+ +| ``_pattern`` | Finds values that have any single character followed by the specified pattern. | ++------------------+--------------------------------------------------------------------------------------------------------------------------+ +| ``pattern_`` | Finds values that have the specified pattern followed by any single character. | ++------------------+--------------------------------------------------------------------------------------------------------------------------+ +| ``[charlist]%`` | Finds values that start with any character in the specified character list. | ++------------------+--------------------------------------------------------------------------------------------------------------------------+ +| ``[!charlist]%`` | Finds values that start with any character not in the specified character list. | ++------------------+--------------------------------------------------------------------------------------------------------------------------+ +| ``_pattern_`` | Finds values that have any single character followed by the specified pattern and then followed by any single character. | ++------------------+--------------------------------------------------------------------------------------------------------------------------+ + + + + diff --git a/docs/source/api/operations/create.rst b/docs/source/examples/operations/create.rst similarity index 100% rename from docs/source/api/operations/create.rst rename to docs/source/examples/operations/create.rst diff --git a/docs/source/examples/operations/delete.rst b/docs/source/examples/operations/delete.rst new file mode 100644 index 0000000..2ed83b4 --- /dev/null +++ b/docs/source/examples/operations/delete.rst @@ -0,0 +1,135 @@ +4. Deleting a record +++++++++++++++++++++ + +To delete a record or records in a database table you make use of the following functions: + +1. ``delete_by_pk()`` +2. ``delete_one()`` +3. ``delete_bulk()`` + +1. ``delete_by_pk()`` +===================== + +Using the ``delete_by_pk()`` method, you can delete a record in a database based on the primary-key value. + +.. code-block:: + + affected_rows = mysql_loom.delete_by_pk(instance=User, pk=1) + + +The above take the following as arguments: + +.. rst-class:: my-table + ++--------------+----------------------------------------------------+-----------+---------+----------+ +| Argument | Description | Type | Default | Required | ++==============+====================================================+===========+=========+==========+ +| ``instance`` | The model class from which to delete the instance. | ``Model`` | | ``Yes`` | ++--------------+----------------------------------------------------+-----------+---------+----------+ +| ``pk`` | The primary key value of the instance to delete. | ``Any`` | | ``Yes`` | ++--------------+----------------------------------------------------+-----------+---------+----------+ + + +2. ``delete_one()`` +=================== + +You can also use ``filters`` to delete a record in a database. The ``delete_one()`` function enables you to delete a single record in a database that matches a filter. + +.. code-block:: + + affected_rows = mysql_loom.delete_one( + instance=User, filters=[Filter(column="username", value="@miller")] + ) + + +The method takes in the following arguments: + +.. rst-class:: my-table + ++--------------+-------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| Argument | Description | Type | Default | Required | ++==============+===========================================================================================+============================================+==========+==========+ +| ``instance`` | The model class from which to delete the instance(s). | ``Model`` | | ``Yes`` | ++--------------+-------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``filters`` | Filter or collection of filters to apply to the deletion query. | ``Filter`` or ``list[Filter]`` or ``None`` | ``None`` | ``No`` | ++--------------+-------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``offset`` | Number of instances to skip before deleting. | ``int`` or ``None`` | ``No`` | | ++--------------+-------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``order`` | Collection of ``Order`` or as single ``Order`` to order the instances by before deletion. | ``list[Order]`` or ``Order``or ``None`` | ``[]`` | ``No`` | ++--------------+-------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ + + +3. ``delete_bulk()`` +==================== + +You can also use the ``delete_bulk()`` method to delete a multitude of records that match a given filter: + +.. code-block:: + + affected_rows = mysql_loom.delete_bulk( + instance=User, filters=[Filter(column="username", value="@miller")] + ) + + +The method takes the following as arguments: + +.. rst-class:: my-table + ++--------------+------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| Argument | Description | Type | Default | Required | ++==============+==========================================================================================+============================================+==========+==========+ +| ``instance`` | The model class from which to delete instances. | ``Model`` | | ``Yes`` | ++--------------+------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``filters`` | Filter or collection of filters to apply to the deletion query. | ``Filter`` or ``list[Filter]`` or ``None`` | ``None`` | ``No`` | ++--------------+------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``limit`` | Maximum number of instances to delete. | ``int`` or ``None`` | ``No`` | | ++--------------+------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``offset`` | Number of instances to skip before deleting. | ``int`` or ``None`` | ``No`` | | ++--------------+------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``order`` | Collection of ``Order`` or a single ``Order`` to order the instances by before deletion. | ``list[Order]`` or ``Order``or ``None`` | ``[]`` | ``No`` | ++--------------+------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ + + +Warning: Potential Risk with ``delete_bulk()`` +---------------------------------------------- + +.. warning:: When using the ``delete_bulk()`` function, exercise caution as it can be aggressive. If the filter is not explicitly provided, there is a risk of mistakenly deleting all records in the table. + +Guidelines for Safe Usage +---------------------------------------------- + +To mitigate the potential risks associated with ``delete_bulk()``, follow these guidelines: + +1. **Always Provide a Filter:** + + - When calling ``delete_bulk()``, make sure to provide a filter to specify the subset of records to be deleted. This helps prevent unintentional deletions. + + .. code-block:: + + # Example: Delete records where 'status' is 'inactive' + affected_rows = mysql_loom.delete_bulk( + instance=User, + filters=Filter(column="status", value='inactive'), + ) + + +2. **Consider Usage When Necessary:** + +- When contemplating data deletion, it is advisable to consider more targeted methods before resorting to ``delete_bulk()``. Prioritize the use of ``delete_one()`` or ``delete_by_pk()`` methods to remove specific records based on your needs. This ensures a more precise and controlled approach to data deletion. + +3. **Use limit and offsets options** + +- You can consider using the ``limit`` and offset options during invocation of ``delete_bulk`` + +.. code-block:: + + affected_rows = mysql_loom.delete_bulk( + instance=Post, + order=[Order(column="id", order="DESC"), Order(column="createdAt", order="ASC")], + filters=[Filter(column="id", operator="gt", value=0)], + offset=0, + limit=10, + ) + + +By following these guidelines, you can use the ``delete_bulk()`` function safely and minimize the risk of unintended data loss. Always exercise caution and adhere to best practices when performing bulk deletion operations. \ No newline at end of file diff --git a/docs/source/examples/operations/index.rst b/docs/source/examples/operations/index.rst new file mode 100644 index 0000000..cbb7457 --- /dev/null +++ b/docs/source/examples/operations/index.rst @@ -0,0 +1,21 @@ +CRUD Operations ++++++++++++++++ + +In this section of the documentation, we will illustrate how to perform basic ``CRUD`` operations using ``dataloom`` on simple ``Models``. Please note that in the following code snippets, I will be utilizing ``sqlite_loom``, ``mysql_loom``, ``pg_loom`` or ``loom`` interchangeably. However, it's important to highlight that you can use any ``loom`` of your choice to follow along. + +#. `Creating a Records `_ +#. `Reading a Records `_ +#. `Updating a Records `_ +#. `Deleting a Records `_ + + + +.. toctree:: + :maxdepth: 2 + :hidden: + + create + read + update + delete + \ No newline at end of file diff --git a/docs/source/examples/operations/read.rst b/docs/source/examples/operations/read.rst new file mode 100644 index 0000000..37ecdf9 --- /dev/null +++ b/docs/source/examples/operations/read.rst @@ -0,0 +1,156 @@ +2. Reading records +++++++++++++++++++ + +To retrieve documents or a document from the database, you can make use of the following functions: + +1. ``find_all()``: This function is used to retrieve all documents from the database. +2. ``find_by_pk()``: This function is used to retrieve a document by its primary key (or ID). +3. ``find_one()``: This function is used to retrieve a single document based on a specific condition. +4. ``find_many()``: This function is used to retrieve multiple documents based on a specific condition. + +1. ``find_all()`` +================ + +This method is used to retrieve all the records that are in the database table. Below are examples demonstrating how to do it: + +.. code-block:: + + users = pg_loom.find_all( + instance=User, + select=["id", "username"], + limit=3, + offset=0, + order=[Order(column="id", order="DESC")], + ) + print(users) # ? [{'id': 1, 'username': '@miller'}] + +The ``find_all()`` method takes in the following arguments: + +.. rst-class:: my-table + ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+----------+----------+ +| Argument | Description | Type | Default | Required | ++==============+==============================================================================================+==================================+==========+==========+ +| ``instance`` | The model class to retrieve documents from. | ``Model`` | ``None`` | ``Yes`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+----------+----------+ +| ``select`` | Collection or a string of fields to select from the documents. | ``list[str]`` or ``str`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+----------+----------+ +| ``limit`` | Maximum number of documents to retrieve. | ``int`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+----------+----------+ +| ``offset`` | Number of documents to skip before retrieving. | ``int`` | ``0`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+----------+----------+ +| ``order`` | Collection of ``Order`` or a single ``Order`` to order the documents when querying. | ``list[Order]`` or ``Order`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+----------+----------+ +| ``include`` | Collection or a ``Include`` of related models to eagerly load. | ``list[Include]`` or ``Include`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+----------+----------+ +| ``group`` | Collection of ``Group`` which specifies how you want your data to be grouped during queries. | ``list[Group]`` or ``Group`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+----------+----------+ +| ``distinct`` | Boolean telling ``dataloom`` to return distinct row values based on selected fields or not. | ``bool`` | ``No`` | | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+----------+----------+ + +.. tip:: 👍 **Pro Tip**: A collection can be any python iterable, the supported iterables are ``list``, ``set``, ``tuple``. + +2. ``find_many()`` +================= + +Here is an example demonstrating the usage of the ``find_many()`` function with specific filters. + +.. code-block:: + + users = mysql_loom.find_many( + User, + filters=[Filter(column="username", value="@miller")], + select=["id", "username"], + offset=0, + limit=10, + ) + + print(users) # ? [{'id': 1, 'username': '@miller'}] + + +The ``find_many()`` method takes in the following arguments: + +.. rst-class:: my-table + ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ +| Argument | Description | Type | Default | Required | ++==============+==============================================================================================+==================================+===========+==========+ +| ``instance`` | The model class to retrieve documents from. | ``Model`` | ``None`` | ``Yes`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ +| ``select`` | Collection or a string of fields to select from the documents. | ``list[str]`` or ``str`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ +| ``limit`` | Maximum number of documents to retrieve. | ``int`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ +| ``offset`` | Number of documents to skip before retrieving. | ``int`` | ``0`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ +| ``order`` | Collection of ``Order`` or a single ``Order`` to order the documents when querying. | ``list[Order]`` or ``Order`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ +| ``include`` | Collection or a ``Include`` of related models to eagerly load. | ``list[Include]`` or ``Include`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ +| ``group`` | Collection of ``Group`` which specifies how you want your data to be grouped during queries. | ``list[Group]`` or ``Group`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ +| ``filters`` | Collection of ``Filter`` or a ``Filter`` to apply to the query. | ``list[Filter]`` or ``Filter`` | ``None`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ +| ``distinct`` | Boolean telling ``dataloom`` to return distinct row values based on selected fields or not. | ``bool`` | ``False`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------------+----------------------------------+-----------+----------+ + +.. tip:: 👍 **Pro Tip**: The distinction between the ``find_all()`` and ``find_many()`` methods lies in the fact that ``find_many()`` enables you to apply specific filters, whereas ``find_all()`` retrieves all the documents within the specified model. + +3. ``find_one()`` +================= + +Here is an example showing you how you can use ``find_one()`` locate a single record in the database. + +.. code-block:: + + user = mysql_loom.find_one( + User, + filters=[Filter(column="username", value="@miller")], + select=["id", "username"], + ) + print(user) # ? {'id': 1, 'username': '@miller'} + + +This method take the following as arguments + +.. rst-class:: my-table + ++--------------+------------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| Argument | Description | Type | Default | Required | ++==============+================================================================================================+============================================+==========+==========+ +| ``instance`` | The model class to retrieve instances from. | ``Model`` | | ``Yes`` | ++--------------+------------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``filters`` | ``Filter`` or a collection of ``Filter`` to apply to the query. | ``Filter`` or ``list[Filter]`` or ``None`` | ``None`` | ``No`` | ++--------------+------------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``select`` | Collection of ``str`` or ``str`` of which is the name of the columns or column to be selected. | ``list[str]``or``str`` | ``[]`` | ``No`` | ++--------------+------------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``include`` | Collection of ``Include`` or a single ``Include`` of related models to eagerly load. | ``list[Include]``or``Include`` | ``[]`` | ``No`` | ++--------------+------------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ +| ``offset`` | Number of instances to skip before retrieving. | ``int`` or ``None`` | ``No`` | | ++--------------+------------------------------------------------------------------------------------------------+--------------------------------------------+----------+----------+ + +4. ``find_by_pk()`` +=================== + +Here is an example showing how you can use the ``find_by_pk()`` to locate a single record in the database. + +.. code-block:: + + user = mysql_loom.find_by_pk(User, pk=userId, select=["id", "username"]) + print(user) # ? {'id': 1, 'username': '@miller'} + +The method takes the following as arguments: + +.. rst-class:: my-table + ++--------------+----------------------------------------------------------------------------------------+----------------------------------+---------+----------+ +| Argument | Description | Type | Default | Required | ++==============+========================================================================================+==================================+=========+==========+ +| ``instance`` | The model class to retrieve instances from. | ``Model`` | | ``Yes`` | ++--------------+----------------------------------------------------------------------------------------+----------------------------------+---------+----------+ +| ``pk`` | The primary key value to use for retrieval. | ``Any`` | | ``Yes`` | ++--------------+----------------------------------------------------------------------------------------+----------------------------------+---------+----------+ +| ``select`` | Collection column names to select from the instances. | ``list[str]`` | ``[]`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------+----------------------------------+---------+----------+ +| ``include`` | A Collection of ``Include`` or a single ``Include`` of related models to eagerly load. | ``list[Include]`` or ``Include`` | ``[]`` | ``No`` | ++--------------+----------------------------------------------------------------------------------------+----------------------------------+---------+----------+ diff --git a/docs/source/examples/operations/update.rst b/docs/source/examples/operations/update.rst new file mode 100644 index 0000000..9f7df2c --- /dev/null +++ b/docs/source/examples/operations/update.rst @@ -0,0 +1,105 @@ +3. Updating a record +++++++++++++++++++++ + +To update records in your database table you make use of the following functions: + +1. ``update_by_pk()`` +2. ``update_one()`` +3. ``update_bulk()`` + +1. ``update_by_pk()`` +===================== + +The ``update_pk()`` method can be used as follows: + +.. code-block:: + + affected_rows = mysql_loom.update_by_pk( + instance=Post, + pk=1, + values=[ + ColumnValue(name="title", value="Updated?"), + ], + ) + + +The above method takes in the following as arguments: + +.. rst-class:: my-table + ++--------------+--------------------------------------------------------------------+------------------------------------------+---------+----------+ +| Argument | Description | Type | Default | Required | ++==============+====================================================================+==========================================+=========+==========+ +| ``instance`` | The model class for which to update the instance. | ``Model`` | | ``Yes`` | ++--------------+--------------------------------------------------------------------+------------------------------------------+---------+----------+ +| ``pk`` | The primary key value of the instance to update. | ``Any`` | | ``Yes`` | ++--------------+--------------------------------------------------------------------+------------------------------------------+---------+----------+ +| ``values`` | Single or Collection of ``ColumnValue`` to update in the instance. | ``ColumnValue`` or ``list[ColumnValue]`` | | ``Yes`` | ++--------------+--------------------------------------------------------------------+------------------------------------------+---------+----------+ + +2. ``update_one()`` +=================== + +Here is an example illustrating how to use the ``update_one()`` method: + +.. code-block:: + + affected_rows = mysql_loom.update_one( + instance=Post, + filters=[ + Filter(column="id", value=8, join_next_with="OR"), + Filter(column="userId", value=1, join_next_with="OR"), + ], + values=[ + ColumnValue(name="title", value="Updated?"), + ], + ) + + +The method takes the following as arguments: + +.. rst-class:: my-table + ++--------------+-----------------------------------------------------------------------+--------------------------------------------+---------+----------+ +| Argument | Description | Type | Default | Required | ++==============+=======================================================================+============================================+=========+==========+ +| ``instance`` | The model class for which to update the instance(s). | ``Model`` | | ``Yes`` | ++--------------+-----------------------------------------------------------------------+--------------------------------------------+---------+----------+ +| ``filters`` | Filter or collection of filters to apply to the update query. | ``Filter`` or ``list[Filter]`` or ``None`` | | ``Yes`` | ++--------------+-----------------------------------------------------------------------+--------------------------------------------+---------+----------+ +| ``values`` | Single or collection of column-value pairs to update in the instance. | ``ColumnValue`` or ``list[ColumnValue]`` | | ``Yes`` | ++--------------+-----------------------------------------------------------------------+--------------------------------------------+---------+----------+ + + +3. ``update_bulk()`` +==================== + +The ``update_bulk()`` method updates all records that match a filter in a database table. + +.. code-block:: python + + affected_rows = mysql_loom.update_bulk( + instance=Post, + filters=[ + Filter(column="id", value=8, join_next_with="OR"), + Filter(column="userId", value=1, join_next_with="OR"), + ], + values=[ + ColumnValue(name="title", value="Updated?"), + ], + ) + + +The above method takes in the following as argument: + +.. rst-class:: my-table + ++--------------+-----------------------------------------------------------------------+--------------------------------------------+---------+----------+ +| Argument | Description | Type | Default | Required | ++==============+=======================================================================+============================================+=========+==========+ +| ``instance`` | The model class for which to update instances. | ``Model`` | | ``Yes`` | ++--------------+-----------------------------------------------------------------------+--------------------------------------------+---------+----------+ +| ``filters`` | Filter or collection of filters to apply to the update query. | ``Filter`` or ``list[Filter]`` or ``None`` | | ``Yes`` | ++--------------+-----------------------------------------------------------------------+--------------------------------------------+---------+----------+ +| ``values`` | Single or collection of column-value pairs to update in the instance. | ``ColumnValue`` or ``list[ColumnValue]`` | | ``Yes`` | ++--------------+-----------------------------------------------------------------------+--------------------------------------------+---------+----------+ diff --git a/docs/source/examples/ordering.rst b/docs/source/examples/ordering.rst new file mode 100644 index 0000000..cd1033c --- /dev/null +++ b/docs/source/examples/ordering.rst @@ -0,0 +1,22 @@ +Ordering Records +++++++++++++++++ + +In ``dataloom`` you can order documents in either ```DESC``` (descending) or ```ASC``` (ascending) order using the helper class ``Order``. + +.. code-block:: + + posts = mysql_loom.find_all( + instance=Post, + order=[Order(column="id", order="DESC")], + ) + + +You can apply multiple and these orders will ba applied in sequence of application here is an example: + +.. code-block:: + + posts = mysql_loom.find_all( + instance=Post, + order=[Order(column="id", order="DESC"), Order(column="createdAt", order="ASC")], + ) + diff --git a/docs/source/index.rst b/docs/source/index.rst index a5c46b6..bbd668e 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -52,9 +52,27 @@ Why choose ``dataloom``? api/classes/index api/tables/index - api/operations/index + api/qb + +.. toctree:: + :maxdepth: 2 + :hidden: + :caption: Examples + + examples/operations/index + examples/ordering + examples/filtering + examples/data_aggregation + examples/associations/index +.. toctree:: + :maxdepth: 2 + :hidden: + :caption: Utilities + + utilities/index + .. toctree:: :maxdepth: 2 diff --git a/docs/source/utilities/avg.rst b/docs/source/utilities/avg.rst new file mode 100644 index 0000000..e24f53c --- /dev/null +++ b/docs/source/utilities/avg.rst @@ -0,0 +1,41 @@ +6. ``avg()`` +++++++++++++ + +This is a utility function that comes within the ``loom`` object that is used to calculate the average value in rows of data in a database table that meets a specific criteria. Here is an example on how to use this utility function. + +.. code-block:: + + # example + _avg = mysql_loom.avg( + instance=Post, + filters=Filter( + column="id", + operator="between", + value=[1, 7], + ), + column="id", + limit=3, + offset=0, + distinct=True, + ) + print(_avg) + + +The ``max`` function takes the following arguments: +.. rst-class:: my-table + ++--------------+-------------------------------------------------------------------------------------------------------------------------+ +| Argument | Description | ++==============+=========================================================================================================================+ +| ``instance`` | The model class to retrieve documents from. | ++--------------+-------------------------------------------------------------------------------------------------------------------------+ +| ``column`` | A string of column to calculate average values based on. | ++--------------+-------------------------------------------------------------------------------------------------------------------------+ +| ``limit`` | Maximum number of documents to retrieve. | ++--------------+-------------------------------------------------------------------------------------------------------------------------+ +| ``offset`` | Number of documents to skip before finding the calculating the average. | ++--------------+-------------------------------------------------------------------------------------------------------------------------+ +| ``filters`` | Collection of ``Filter`` or a ``Filter`` to apply to the rows to be used. | ++--------------+-------------------------------------------------------------------------------------------------------------------------+ +| ``distinct`` | Boolean telling ``dataloom`` to calculate the average value in distinct rows of values based on selected column or not. | ++--------------+-------------------------------------------------------------------------------------------------------------------------+ diff --git a/docs/source/utilities/count.rst b/docs/source/utilities/count.rst new file mode 100644 index 0000000..5b93c12 --- /dev/null +++ b/docs/source/utilities/count.rst @@ -0,0 +1,41 @@ +3. ``count()`` +++++++++++++++ + +This is a utility function that comes within the ``loom`` object that is used to count rows in a database table that meets a specific criteria. Here is an example on how to use this utility function. + +.. code-block:: + + # example + count = mysql_loom.count( + instance=Post, + filters=Filter( + column="id", + operator="between", + value=[1, 7], + ), + column="id", + limit=3, + offset=0, + distinct=True, + ) + print(count) + +The ``count`` function takes the following arguments: + +.. rst-class:: my-table + ++--------------+--------------------------------------------------------------------------------------------+ +| Argument | Description | ++==============+============================================================================================+ +| ``instance`` | The model class to retrieve documents from. | ++--------------+--------------------------------------------------------------------------------------------+ +| ``column`` | A string of column to count values based on. | ++--------------+--------------------------------------------------------------------------------------------+ +| ``limit`` | Maximum number of documents to retrieve. | ++--------------+--------------------------------------------------------------------------------------------+ +| ``offset`` | Number of documents to skip before counting. | ++--------------+--------------------------------------------------------------------------------------------+ +| ``filters`` | Collection of ``Filter`` or a ``Filter`` to apply to the rows to be counted. | ++--------------+--------------------------------------------------------------------------------------------+ +| ``distinct`` | Boolean telling dataloom to count distinct rows of values based on selected column or not. | ++--------------+--------------------------------------------------------------------------------------------+ diff --git a/docs/source/utilities/decorators.rst b/docs/source/utilities/decorators.rst new file mode 100644 index 0000000..1967c19 --- /dev/null +++ b/docs/source/utilities/decorators.rst @@ -0,0 +1,95 @@ +2. ``decorators`` ++++++++++++++++++ + +These modules contain several decorators that can prove useful when creating models. These decorators originate from ``dataloom.decorators``, and at this stage, we are referring to them as "experimental." + +``@initialize()`` +================= + +Let's examine a model named ``Profile``, which appears as follows: + +.. code-block:: + + class Profile(Model): + __tablename__: Optional[TableColumn] = TableColumn(name="profiles") + id = PrimaryKeyColumn(type="int", auto_increment=True) + avatar = Column(type="text", nullable=False) + userId = ForeignKeyColumn( + User, + maps_to="1-1", + type="int", + required=True, + onDelete="CASCADE", + onUpdate="CASCADE", + ) + + +This is simply a Python class that inherits from the top-level class ``Model``. However, it lacks some useful ``dunder`` methods such as ``__init__`` and ``__repr__``. In standard Python, we can achieve this functionality by using ``dataclasses``. For example, we can modify our class as follows: + +.. code-block:: + + from dataclasses import dataclass + + @dataclass + class Profile(Model): + # .... + + + +However, this approach doesn't function as expected in ``dataloom`` columns. Hence, we've devised these experimental decorators to handle the generation of essential dunder methods required for working with ``dataloom``. If you prefer not to use decorators, you always have the option to manually create these dunder methods. Here's an example: + +.. code-block:: + + class Profile(Model): + # ... + def __init__(self, id: int | None, avatar: str | None, userId: int | None) -> None: + self.id = id + self.avatar = avatar + self.userId = userId + + def __repr__(self) -> str: + return f"<{self.__class__.__name__}:id={self.id}>" + + @property + def to_dict(self): + return {"id": self.id, "avatar": self.avatar, "userId": self.userId} + + +However, by using the ``initialize`` decorator, this functionality will be automatically generated for you. Here's all you need to do: + +.. code-block:: + + from dataloom.decorators import initialize + + @initialize(repr=True, to_dict=True, init=True, repr_identifier="id") + class Profile(Model): + # ... + + +.. tip:: 👉 **Tip**: Dataloom has a clever way of skipping the ``TableColumn`` because it doesn't matter in this case. + +The ``initialize`` decorator takes the following arguments: + +.. rst-class:: my-table + ++---------------------+---------------------------------------------------------------+---------------------+-----------+----------+ +| Argument | Description | Type | Default | Required | ++=====================+===============================================================+=====================+===========+==========+ +| ``to_dict`` | Flag indicating whether to generate a ``to_dict`` method. | ``bool`` | ``False`` | ``No`` | ++---------------------+---------------------------------------------------------------+---------------------+-----------+----------+ +| ``init`` | Flag indicating whether to generate an ``__init__`` method. | ``bool`` | ``True`` | ``No`` | ++---------------------+---------------------------------------------------------------+---------------------+-----------+----------+ +| ``repr`` | Flag indicating whether to generate a ``__repr__`` method. | ``bool`` | ``False`` | ``No`` | ++---------------------+---------------------------------------------------------------+---------------------+-----------+----------+ +| ``repr_identifier`` | Identifier for the attribute used in the ``__repr__`` method. | ``str`` or ``None`` | ``None`` | ``No`` | ++---------------------+---------------------------------------------------------------+---------------------+-----------+----------+ + + +.. tip:: 👍 **Pro Tip:** Note that this ``decorator`` function allows us to interact with our data from the database in an object-oriented way in Python. Below is an example illustrating this concept: + +.. code-block:: + + profile = mysql_loom.find_by_pk(Profile, pk=1, select=["avatar", "id"]) + profile = Profile(**profile) + print(profile) # ? = + print(profile.avatar) # ? hello.jpg diff --git a/docs/source/utilities/index.rst b/docs/source/utilities/index.rst new file mode 100644 index 0000000..71b58c3 --- /dev/null +++ b/docs/source/utilities/index.rst @@ -0,0 +1,27 @@ +Dataloom Utilities +++++++++++++++++++ + +Dataloom comes up with some utility functions that works on an instance of a model. This is very useful when ``debuging`` your tables to see how do they look like. These function include: + +#. `inspect `_ +#. `decorators `_ +#. `count `_ +#. `min `_ +#. `max `_ +#. `avg `_ +#. `sum `_ + + + +.. toctree:: + :maxdepth: 2 + :hidden: + + inspect + decorators + count + min + max + avg + sum + \ No newline at end of file diff --git a/docs/source/utilities/inspect.rst b/docs/source/utilities/inspect.rst new file mode 100644 index 0000000..d73cc8f --- /dev/null +++ b/docs/source/utilities/inspect.rst @@ -0,0 +1,53 @@ +1. ``inspect()`` +++++++++++++++++ + +This function takes in a model as argument and inspect the model fields or columns. The following examples show how we can use this handy function in inspecting table names. + +.. code-block:: + + table = mysql_loom.inspect(instance=User, fields=["name", "type"], print_table=False) + print(table) + +The above snippet returns a list of dictionaries containing the column name and the arguments that were passed. + +.. code-block:: shell + + [{'id': {'type': 'int'}}, {'name': {'type': 'varchar'}}, {'tokenVersion': {'type': 'int'}}, {'username': {'type': 'varchar'}}] + +You can print table format these fields with their types as follows + +.. code-block:: + + mysql_loom.inspect(instance=User) + +Output: + +.. rst-class:: my-table + ++--------------+---------+----------+---------+ +| name | type | nullable | default | ++==============+=========+==========+=========+ +| id | int | NO | None | ++--------------+---------+----------+---------+ +| name | varchar | NO | Bob | ++--------------+---------+----------+---------+ +| tokenVersion | int | YES | 0 | ++--------------+---------+----------+---------+ +| username | varchar | YES | None | ++--------------+---------+----------+---------+ + + + +The ``inspect`` function take the following arguments. + +.. rst-class:: my-table + ++-----------------+--------------------------------------------------------+---------------+---------------------------------------------+----------+ +| Argument | Description | Type | Default | Required | ++=================+========================================================+===============+=============================================+==========+ +| ``instance`` | The model instance to inspect. | ``Model`` | -- | ``Yes`` | ++-----------------+--------------------------------------------------------+---------------+---------------------------------------------+----------+ +| ``fields`` | The list of fields to include in the inspection. | ``list[str]`` | ``["name", "type", "nullable", "default"]`` | ``No`` | ++-----------------+--------------------------------------------------------+---------------+---------------------------------------------+----------+ +| ``print_table`` | Flag indicating whether to print the inspection table. | ``bool`` | ``True`` | ``No`` | ++-----------------+--------------------------------------------------------+---------------+---------------------------------------------+----------+ diff --git a/docs/source/utilities/max.rst b/docs/source/utilities/max.rst new file mode 100644 index 0000000..68bedf2 --- /dev/null +++ b/docs/source/utilities/max.rst @@ -0,0 +1,42 @@ +5. ``max()`` +++++++++++++ + +This is a utility function that comes within the ``loom`` object that is used to find the maximum value in rows of data in a database table that meets a specific criteria. Here is an example on how to use this utility function. + +.. code-block:: + + # example + _max = mysql_loom.max( + instance=Post, + filters=Filter( + column="id", + operator="between", + value=[1, 7], + ), + column="id", + limit=3, + offset=0, + distinct=True, + ) + print(_max) + + +The ``max`` function takes the following arguments: + +.. rst-class:: my-table + ++--------------+------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+----------+ +| Argument | Description | Type | Default | Required | ++==============+============================================================================================================+================================+===========+==========+ +| ``instance`` | The model class to retrieve documents from. | ``Model`` | ``None`` | ``Yes`` | ++--------------+------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+----------+ +| ``column`` | A string of column to find maximum values based on. | ``str`` | ``None`` | ``Yes`` | ++--------------+------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+----------+ +| ``limit`` | Maximum number of documents to retrieve. | ``int`` | ``None`` | ``No`` | ++--------------+------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+----------+ +| ``offset`` | Number of documents to skip before finding the maximum. | ``int`` | ``0`` | ``No`` | ++--------------+------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+----------+ +| ``filters`` | Collection of ``Filter`` or a ``Filter`` to apply to the rows to be used. | ``list[Filter]`` or ``Filter`` | ``None`` | ``No`` | ++--------------+------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+----------+ +| ``distinct`` | Boolean telling dataloom to find maximum value in distinct rows of values based on selected column or not. | ``bool`` | ``False`` | ``No`` | ++--------------+------------------------------------------------------------------------------------------------------------+--------------------------------+-----------+----------+ diff --git a/docs/source/utilities/min.rst b/docs/source/utilities/min.rst new file mode 100644 index 0000000..3b7daab --- /dev/null +++ b/docs/source/utilities/min.rst @@ -0,0 +1,42 @@ +4. ``min()`` +++++++++++++ + +This is a utility function that comes within the ``loom`` object that is used to find the minimum value in rows of data in a database table that meets a specific criteria. Here is an example on how to use this utility function. + +.. code-block:: + + # example + _min = mysql_loom.min( + instance=Post, + filters=Filter( + column="id", + operator="between", + value=[1, 7], + ), + column="id", + limit=3, + offset=0, + distinct=True, + ) + print(_min) + + +The ``min`` function takes the following arguments: + +.. rst-class:: my-table + ++--------------+---------------------------------------------------------------------------------------------------------+ +| Argument | Description | ++==============+=========================================================================================================+ +| ``instance`` | The model class to retrieve documents from. | ++--------------+---------------------------------------------------------------------------------------------------------+ +| ``column`` | A string of column to find minimum values based on. | ++--------------+---------------------------------------------------------------------------------------------------------+ +| ``limit`` | Maximum number of documents to retrieve. | ++--------------+---------------------------------------------------------------------------------------------------------+ +| ``offset`` | Number of documents to skip before finding the minimum. | ++--------------+---------------------------------------------------------------------------------------------------------+ +| ``filters`` | Collection of ``Filter`` or a ``Filter`` to apply to the rows to be used. | ++--------------+---------------------------------------------------------------------------------------------------------+ +| ``distinct`` | Boolean telling dataloom to find minimum value in distinct rows values based on selected column or not. | ++--------------+---------------------------------------------------------------------------------------------------------+ diff --git a/docs/source/utilities/sum.rst b/docs/source/utilities/sum.rst new file mode 100644 index 0000000..a4c3151 --- /dev/null +++ b/docs/source/utilities/sum.rst @@ -0,0 +1,42 @@ +7. ``sum()`` +++++++++++++ + +This is a utility function that comes within the ``loom`` object that is used to find the total sum in rows of data in a database table that meets a specific criteria. Here is an example on how to use this utility function. + +.. code-block:: + + # example + _sum = mysql_loom.sum( + instance=Post, + filters=Filter( + column="id", + operator="between", + value=[1, 7], + ), + column="id", + limit=3, + offset=0, + distinct=True, + ) + print(_sum) + + +The ``sum`` function takes the following arguments: + +.. rst-class:: my-table + ++--------------+------------------------------------------------------------------------------------------------+ +| Argument | Description | ++==============+================================================================================================+ +| ``instance`` | The model class to retrieve documents from. | ++--------------+------------------------------------------------------------------------------------------------+ +| ``column`` | A string of column to sum values based on. | ++--------------+------------------------------------------------------------------------------------------------+ +| ``limit`` | Maximum number of documents to retrieve. | ++--------------+------------------------------------------------------------------------------------------------+ +| ``offset`` | Number of documents to skip before summing. | ++--------------+------------------------------------------------------------------------------------------------+ +| ``filters`` | Collection of ``Filter`` or a ``Filter`` to apply to the rows to be used. | ++--------------+------------------------------------------------------------------------------------------------+ +| ``distinct`` | Boolean telling dataloom to sum value in distinct rows values based on selected column or not. | ++--------------+------------------------------------------------------------------------------------------------+