#postgresql #orm #query-builder #default #now #point #lightwight

yanked rusty-postgres

A lightwight ORM and Query Builder for postgres

new 0.1.6-beta Jan 28, 2025
0.1.5-beta Jan 27, 2025

#99 in #query-builder

Download history 120/week @ 2025-01-15 502/week @ 2025-01-22

622 downloads per month

MIT license

130KB
2.5K SLoC

License Crates.io

A Advanced Lightwight ORM and Query Builder for postgres

Usage

Client

  let mut postgres = Client::connect("host=127.0.0.1 port=5432 dbname=name user=postgres password=xxxxxxxxx connect_timeout=10 sslmode=prefer", NoTls);

Create Schema without any hassale

  let shop = model! {
        "shop" => {
            "id" => {
                ID(UUID),NOTNULL,UNIQUE
            },
            "name" => {
                Date(NOW)
            },
            "time" => {
                Time(NOW)
            },
            "fingerprint" => {
                DateTime(NOW)
            },
            "age" => {
                NUMBER,DEFAULT(24)
            },
            "point" => {
                FLOAT,DEFAULT(24.1)
            },
            "place" => {
                STRING,NOTNULL,UNIQUE,INDEX,PRIMARY
            },
            "location" => {
                Geography(POINT(Epsg4326))
            }
            "accuracy" => {
                FLOAT,DEFAULT(0.0)
            },
            "bool" => {
                BOOL
            }
        },
        partition:{             //optional
            type:"list",
            to:"place"
        }
    };

Container

Build Schema

  let container = container! {
      client =>  postgres,
      models => {
        shop
      }
  };
let search = similar_search {
    connection => postgres,
    model:"place",
    similarity:{
        score:"0.6", //similarity score
        believe:"name" //based_on
        text:"san" //text
    },
    order_by:{              //optional
        order:"asc",
        believe:"name" //based
    }
}
let search = full_search! {
    connection => postgres,
    model:"place",
    based_on:"name",
    search:{
        value:"billionaire"
    },
    select:{
        ,"name"
    },
    take:6,
    skip:0
};
let search = ranked_search! {
    connection => postgres,
    model:"place",
    based_on:"name",
    search:{
        value:"billionaire"
    },
    select:{
        "name"
    }
};

Horizontal Split

horizontal_splitting {
    connection => postgres,
    model:"",
    name:"name_of_spllit",
    based_on:{
    "country" => "usa"
    }
}

Partition

let partition = create_partition {
    connection => postgres,
    model:"place",
    name:"partition_name",
    field:"value_to_match"
}

Count

let count = count! {
    connection => postgres,
    model:"place",
    count:{
        "name"
    },
    conditions:{                //optional
        and => {
            "name" => "billionaires"
        }
    },
    group_by:{          //optional
        "name"
    }
};

Create

    let create = create! {
        connection => postgres,
        model:"user_",
        data:{
            "story" => "billionairehari",
            "age" => 24 as i32
        },
        select:{
            "id"
        }
    };

FindOne

    let find = find_one! {
        connection => postgres,
        model:"place",
        select:{
            "name"
        },
        conditions:{
            and => {
                "name" => "billionairehari"
            },
            or => {
                "name" => "billionairehari"
            }
        },
        order:{
            "name" => "asc"
        }
    };

FindMany

  let find = find_many! {
    connection => postgres,
    model:"billionaires",
    select:{               // optional
         "place"
    },
    case:{
    (
         "place" => ">22",
         "place" => "<22",
         "place" => "=22"
         ) => (ok:"billion_dollar",
         ok:"billionaire",
         ok:"billionaire"
         ,else:"trillionaire"
    ) => "status"
    },
    conditions:{           // optional
        and => {
            "place" => "san"
        },
        or => {
            "place" => "san"
        }
    },
    between => {                //optional
        and => {
            "place" => {
                "20" => "22"
            }
        },
        or => {
            "place" => {
                "20" => "22"
            }
        }
    },
    like => {               //optional
        and => {
             "name" => "billionaire"
        },
        or => {
             "billionaire" => "billionaire"
        }
    },
    inside:{
        "place" => {
            match:"user_id",
            select:{
                "name"
            },
            conditions:{
                and => {
                    "name" => "billionaire"
                }
            },
            order:6,
            limit:6
        }
    },
    order:{                // optional
        "place" => "asc"
    },
    limit:24,              // optional
    skip:24                // optional
  };

Update

let update = update! {
    connection => postgres,
    model:"place",
    select:{
        "id"
    },
    data:{
        "name" => "billionairehari"
    },
    conditions:{
        and => {
            "name" => "billionairehari"
        },
        or => {
            "" => ""
        }
    }
};

New Relation Update

let update = update! {
       connection => postgres,
       model:"billionaires",
       match:"id",
       inside:{
           "place"  => {
               match:"user_id",
               conditions:{
                   and => {
                       "name" => "billionaires",
                       "user_id" => "c4a97a50-8679-4f85-a1d8-5bba0113b596"
                   }
               },
               data:{
                   "name" => "billionairehari"
               },
               select:{
                   "name"
               }
           }
       }
   };

Delete

 let delete = delete! {
    connection => postgres,
    model:"billionaires",
    select:{                // Optional
        "place"
    },
    conditions:{            // Optional
        and => {            // Optional
            "place" => 24 as i32
        },
        or => {             // Optional
            "place" => 24 as i32
        }
    },
    cascade:true            // Optional
 };

DROP

    delete_table! {
        connection => postgres,
        model => "photo",
        cascade
    }
    .unwrap();

INCLUDED INDEX => Brin,GIN,B-TREE INDEX

Dependencies

~11–22MB
~321K SLoC