Query Builders - circuitsacul/apgorm GitHub Wiki

apgorm models can do more than just .create, .fetch, .delete, and .save. For more advanced operations, you can use the query builders. Here's a couple examples:

class User(apgorm.Model):
    name = types.VarChar(32).field()
    nickname = types.VarChar(32).nullablefield()

    primary_key = (name,)

class Database(apgorm.Database):
    users = User


# basic
all_users = await User.fetch_query().fetchmany()  # fetch all users
users_with_no_nick = await User.fetch_query().where(User.nickname.is_null).fetchmany()

# you can also do this
query = User.update_query()
query.where(User.nickname.is_null)
query.set(nickname=User.name)  # set their nickname to their name
await query.execute()

# the .where() part of the query can accept raw sql (as done above), or it can accept kwargs:
query = User.delete_query()
query.where(nickname="Bad User")
await query.execute()