degen-sql

Crates.iodegen-sql
lib.rsdegen-sql
version0.1.6
sourcesrc
created_at2023-10-03 18:56:41.058441
updated_at2024-01-18 15:24:31.98124
descriptionA postgres database engine for rust that builds on top of tokio-postgres
homepage
repository
max_upload_size
id991423
size54,293
Ethereumdegen (ethereumdegen)

documentation

README

Degen Sql

An opinionated postgres driver for rust and a light wrapper around tokio-postgres.

Easily establish a postgres connection and run migrations from a folder.


cargo add degen-sql

A note about the migrate and rollback_full scripts

Since you cannot run scripts of dependencies, it is recommended that you copy and paste the 'scripts' folder from this repo into your project and then set up binaries in your Cargo.toml to run them.

Then, you will have a migrate script and a rollback script to use in your project.

Step 1.

Set your env vars for postgres



DB_HOST="db.co....blb.supabase.co"

DB_USER="postgres"
DB_NAME="postgres"

DB_PASSWORD="Foo....baR"


Step 2.

Use in your code


 
 
use degen_sql::db::postgres::postgres_db::{Database,DatabaseCredentials};
  

use dotenvy::dotenv;
use std::env;
 
 
async fn main() -> io::Result<()> {
    dotenv().ok();  //you dont HAVE to load them in like this but this is typical. Do not forget this if you use ::from_env()!!

   
 
   
    let database_credentials = DatabaseCredentials::from_env();   //or you can use DatabaseCredentials { ... } and create the struct manually
  
    let database = Arc::new(
        Database::connect(
        database_credentials, None
    ).await.unwrap());
      
  
    //EXAMPLE USING THE DATABASE CONNECTION WITH ACTIX 
     
    HttpServer::new(move || {
        

        let app_state = AppState {
            database: Arc::clone(&database) 
        };

        App::new()
            .app_data(Data::new(app_state)) // Here is where we inject our database for our endpoints to use 
             
            .configure(  ...routes ...   )
             
            
    })
    .bind("0.0.0.0:3000")?
    .run()
    .await
}




Step 3

Build your migrations and models. If you would like to see an example of this in action, see the vibegraph project

Commit count: 0

cargo fmt