pg-worm
PostgreSQL's Worst ORM
pg-worm is a straightforward, fully typed, async ORM and Query Builder for PostgreSQL.
Well, at least that's the goal. Currently it's mainly experimental.
This library is based on tokio_postgres
and is intended to be used with tokio.
Usage
Fortunately, using this library is very easy.
Just derive the Model trait for your type, connect to your database
and you are ready to go!
Here's a quick example:
use ;
use try_join;
// First easily define your models.
async
Filters
Filters can be used to easily include WHERE clauses in your queries.
They can be constructed by calling functions of the respective column.
pg_worm automatically constructs a Column constant for each field
of your Model.
A practical example would look like this:
select
Currently the following filter functions are supported:
Filter::all()- doesn't check anythingeq(T)- checks whether the column is equal to a given valueone_of(Vec<T>)- checks whether the column is (at least) one of the given valueslike(String)- check whether aStringisLIKEa given pattern
You can also do filter logic using !, & and |: MyModel::my_field.eq(5) & !MyModel::other_field.eq("Foo").
This works as you expect logical OR and AND to work.
Please notice that, at this point, custom priorization via parantheses
is not possible.
Query Builder
Simply attaching a Filter to your query often does not suffice.
For this reason, pg-worm provides a QueryBuilder interface for
constructing more complex queries.
Start building your query by calling Query::select() and passing
the columns you want to select.
Normally you want to query all columns of a Model which you can do by passing
YourModel::columns().
You can modify your query using the following methods:
.filter()- add aWHEREclause.join()- add aJOINfor querying accross tables/models.limit()- add aLIMITto how many rows are returned
After you have configured your query, build it using the .build() method.
Then, execute it by calling .exec::<M>(), where M is the Model which
should be parsed from the query result. It may be inferred.
Opiniatedness
As mentioned before, pg_worm is opiniated in a number of ways.
These include:
-
panics. For the sake of conveniencepg_wormonly returns aResultwhen inserting data, since in that case Postgres might reject the data because of some constraint.This means that should something go wrong, like:
- the connection to the database collapsed,
pg_wormis unable to parse Postgres' response,- ...
the program will panic.
-
ease of use. The goal of
pg_wormis not to become an enterprise solution. If adding an option means infringing the ease of use then it will likely not be added.
License
This project is dual-licensed under the MIT and Apache 2.0 licenses.