#postgresql #async #objectmapper

macro adom

async-postgres macros for object mapping

6 releases

0.0.6 Jul 29, 2022
0.0.5 Oct 29, 2021
0.0.3 Jun 8, 2021
0.0.2 May 28, 2021
0.0.1 Apr 9, 2021

#1836 in Database interfaces

24 downloads per month

BSD-3-Clause

27KB
599 lines

Adom - A light weight Object Mapper for async-postgres in Rust

This crate is a set of macros used to generate simple CRUD functions for your Serde structs

Adom is not intended to be a full on ORM like diesel. Its for the people that want to use the postgres driver directly. But want to eliminate some of the boiler plate code on their simple table objects.

A simple example of using adom in a project can be found here

Setup your structs

Add an attribute to the top of your struct to connect it to the DB table

#[AdomTable = "orders"]
pub struct Order{
  id: i32,
  customer: String,
  total: f64,
}

Congratulations nothing happened, but you can now use Adom

Adom derive attribute

To use Adom add derive statements to add useful functions to your struct

#[derive(Deserialize, AdomSelect, AdomUpdate, AdomCreate, AdomDelete)]
#[AdomTable = "orders"]
pub struct Order{
  id: i32,
  customer: String,
  total: f64,
}

Each derive adds functions related to the DB action. Add only what you want or need

AdomSelect

  • Order::find_by_id(... )
  • Order::with_ids(... )
  • Order::one_where(... )
  • Order::find_where(... )
  • Order::find_all(... )

AdomCreate

  • order.create(.... )

AdomUpdate

  • order.update(.... )

AdomDelete

  • order.delete(.... )

Helper attributes

There are a couple of helper attributes that modify the behavior of Adom

AdomColumn - used to if the struct field name doesn't match the DB columns name

#[derive(Deserialize, AdomSelect, AdomUpdate, AdomCreate, AdomDelete)]
#[AdomTable = "orders"]
pub struct Order{
  id: i32,
  #[AdomColumn = "customer_name"]
  customer: String,
  total: f64,
}

AdomIgnore - ignore a field on your struct

#[derive(Deserialize, AdomSelect, AdomUpdate, AdomCreate, AdomDelete)]
#[AdomTable = "orders"]
pub struct Order{
  id: i32,
  customer: String,
  total: f64,
  #[AdomIgnore]
  random_extra_field_that_has_nothing_to_do_with_db: Vec<u8>,
}

AdomAuditable - auto fill and update the traditional DB auditing fields

#[derive(Deserialize, AdomSelect, AdomUpdate, AdomCreate, AdomDelete)]
#[AdomTable = "orders"]
#[AdomAuditable]
pub struct Order{
  id: i32,
  customer: String,
  total: f64,
  updated_at: SystemTime,
  created_at: SystemTime,
  updated_by: String,
  created_by: String,
}

Dependencies

~7–17MB
~233K SLoC