Tiny ORM
A minimal ORM for CRUD operations. Built on top of SQLx and with its QueryBuilder. Support Sqlite, Postgres and MySQL right off the bat. It has smart defaults and is also flexible.
This library is the one I wished I had when I built a project in Rust in production and did not want a heavy ORM.
Principles & advantages of TinyORM
The goals of this library are
- To have the least amount of dependencies
- Tackle all the boilerplate of the CRUD operations
- Add no complexity by default
- Can fit real world problems
Why TinyORM over another one?
- All the queries are built with the QueryBuilder
-- All the inputs are passed as database arguments -- All the queries are compatible with Sqlite, Postgres and MySQL
- Minimal set of dependencies (fast compile time)
-- SQLx -- Standard libraries for a proc macro (syn, etc.) -- convert_case
- Intuitive with smart defaults and flexible
Installation
Add this to your Cargo.toml
:
[]
= { = "0.1.3", = ["postgres"] } # Choose between sqlite, mysql and postgres
License
This project is licensed under [MIT] - see the LICENSE file for details.
Usage
Basic
use TinyORM;
The code above would generate the following methods on the Todos object
Examples
More examples can be found in the examples directory.
Options
TinyORM comes with a few options to give flexibility to the user
At the Struct level
- table_name: The name of the table in the database.
Default being a lower_case version of the Struct name. So
MyStruct
would havemy_struct
as a defaulttable_name
. - only: The methods that will only be available to that struct. Multiple values are comma separated
Default would be the equivalent of
only = "created,get,list,update,delete"
- exclude: The methods that will be excluded for that struct. Multiple values are comma separated Default would be nothing.
- return_object: A custom object that would be returned instead of
Self
. Useful when creating or updating records with partial information. Default isSelf
which corresponds to the current Strut.
Note: only
and exclude
cannot be used together.
Example
The above takes the assumption that some columns can be nullable and others are auto generated (eg an ID column that would be auto increment). Thus it would generate the following
At the field level
- primary_key: The field that would be used as a primary key for the queries. For some methods the primary key is mandatory (eg:
get_by_id()
) Default is any field namedid
.
Example
The above takes the assumption that some columns can be nullable and others are auto generated (eg an ID column that would be auto increment). Thus it would generate the following
Roadmap
Goal of TinyORM is to stay tiny. That being said there are still a few things I would like to have
- Get all the CRUD methods.
- Ability to have custom fields like a custom primary_key or table_name.
- Ability to choose the operations available on the method through an opt-in strategy or an exclusion one.
- Ability to return a custom object type for more complex setup.
- Support MySQL with non auto increment PK. Won't be able to use the
last_return_id()
function in the case (eg with UUID). - Auto generate the
created_at
/updated_at
column based on some default value when needed. -- For example, being able to haveUtc.now()
on theupdate()
method for theupdated_at
if the field is of typeDateTime<Utc>
- Ability to skip some fields if not set (especially for the create and update methods).
-- For example if the field is of type
SetOption<DateTime<Utc>>
then it would only put the field as part of the query if the value isSet(value)
. It would skip it if it has the valueNotSet
.
Release
One PR with the following
then add the tag and push it to main