mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
aurweb.db: add query, create, delete helpers
Takes sqlalchemy kwargs or stanzas: query(Model, Model.Column == value) query(Model, and_(Model.Column == value, Model.Column != "BAD!")) Updated tests to reflect the new utility and a comment about upcoming function deprecation is added to get_account_type(). From here on, phase out the use of get_account_type(). + aurweb.db: Added create utility function + aurweb.db: Added delete utility function The `delete` function can be used to delete a record by search kwargs directly. Example: delete(User, User.ID == 6) All three functions added in this commit are typically useful to perform these operations without having to import aurweb.db.session. Removes a bit of redundancy overall. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
5185df629e
commit
a836892cde
5 changed files with 84 additions and 9 deletions
18
aurweb/db.py
18
aurweb/db.py
|
@ -11,6 +11,24 @@ Session = None
|
|||
session = None
|
||||
|
||||
|
||||
def query(model, *args, **kwargs):
|
||||
return session.query(model).filter(*args, **kwargs)
|
||||
|
||||
|
||||
def create(model, *args, **kwargs):
|
||||
instance = model(*args, **kwargs)
|
||||
session.add(instance)
|
||||
session.commit()
|
||||
return instance
|
||||
|
||||
|
||||
def delete(model, *args, **kwargs):
|
||||
instance = session.query(model).filter(*args, **kwargs)
|
||||
for record in instance:
|
||||
session.delete(record)
|
||||
session.commit()
|
||||
|
||||
|
||||
def get_sqlalchemy_url():
|
||||
"""
|
||||
Build an SQLAlchemy for use with create_engine based on the aurweb configuration.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue