Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relations & grouping #6

Merged
merged 18 commits into from
Feb 21, 2024
Merged

Relations & grouping #6

merged 18 commits into from
Feb 21, 2024

Conversation

CrispenGari
Copy link
Owner

===
Dataloom 2.0.0

Release Notes - dataloom

We have release the new dataloom Version 2.0.0 (2024-02-12)

Features
  • Renaming the class Dataloom to Loom.

    from dataloom import Loom
  • Eager data fetching in relationships

    • Now you can fetch your child relationship together in your query
    user = mysql_loom.find_one(
      instance=User,
      filters=[Filter(column="id", value=userId)],
      include=[Include(model=Profile, select=["id", "avatar"], has="one")],
    )
    print(user)
    • You can apply limits, offsets, filters and orders to your child associations during queries
      post = mysql_loom.find_one(
        instance=Post,
        filters=[Filter(column="userId", value=userId)],
        select=["title", "id"],
        include=[
            Include(
                model=User,
                select=["id", "username"],
                has="one",
                include=[Include(model=Profile, select=["avatar", "id"], has="one")],
            ),
            Include(
                model=Category,
                select=["id", "type"],
                has="many",
                order=[Order(column="id", order="DESC")],
                limit=2,
            ),
        ],
      )
  • Now return_dict has bee removed as an option in dataloom in the query functions like find_by_pk, find_one, find_many and find_all now works starting from this version. If you enjoy working with python objects you have to maneuver them manually using experimental features.

    from dataloom.decorators import initialize
    
    @initialize(repr=True, to_dict=True, init=True, repr_identifier="id")
    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",
        )
    
    # now you can do this
    
    profile = mysql_loom.find_many(
        instance=Profile,
    )
    print([Profile(**p) for p in profile]) # ? = [<Profile:id=1>]
    print([Profile(**p) for p in profile][0].id) # ? = 1
    • These are experimental decorators they are little bit slow and they work perfect in a single instance, you can not nest relationships on them.
    • You can use them if you know how your data is structured and also if you know how to manipulate dictionaries
  • Deprecated join_next_filter_with to the use of join_next_with

  • Values that was required as list e.g, select, include etc can now be passed as a single value.

    • Before
    res = mysql_loom.find_by_pk(Profile, pk=profileId, select={"id", "avatar"}) # invalid
    res = mysql_loom.find_by_pk(Profile, pk=profileId, select=("id", "avatar")) # invalid
    res = mysql_loom.find_by_pk(Profile, pk=profileId, select="id") # invalid
    res = mysql_loom.find_by_pk(Profile, pk=profileId, select=["id"]) # valid
    • Now
    res = mysql_loom.find_by_pk(Profile, pk=profileId, select={"id", "avatar"}) # valid
    res = mysql_loom.find_by_pk(Profile, pk=profileId, select=("id", "avatar")) # valid
    res = mysql_loom.find_by_pk(Profile, pk=profileId, select="id") # valid
    res = mysql_loom.find_by_pk(Profile, pk=profileId, select=["id"]) # valid
  • Updated the documentation.

  • Grouping data in queries will also be part of this release, using the class Group and Having.

posts = pg_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,
          ),
)

@CrispenGari CrispenGari merged commit d9dd1c5 into main Feb 21, 2024
3 checks passed
@CrispenGari CrispenGari deleted the relations branch February 21, 2024 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant