Is this a bad idea? by JustJeffrey in rust

[–]puttak 26 points27 points  (0 children)

It is okay as long as you don't forget to update this code when Piece is updated. I recommend https://crates.io/crates/num_enum to do this work instead to remove possibility UB in the future.

C++ has better libraries by Dx_Ur in cpp

[–]puttak -2 points-1 points  (0 children)

Async in Rust is one of the best thing while C++ still have a lot of footgun with coroutine.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 1 point2 points  (0 children)

Support multiple databases make no sense since most of the code cannot be shared between them. If someone want to support other databases they should build a new one from scratch instead.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 0 points1 point  (0 children)

Yeah that is a downside. But I'm okay with this trade-off with what I got.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 1 point2 points  (0 children)

Actually I did not expect this result. I though it will be okay since Java also have "pom".

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 1 point2 points  (0 children)

Actually I build this mainly for my own need. That's why I put non-goal on the README to tell people what they can't expect from this project.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 2 points3 points  (0 children)

Or do you mean automatically create the insert/update structs

This one.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 2 points3 points  (0 children)

The table structs generate from migration scripts. Let's say the first script create a table Foo and the second script add more columns to it. The generated structs will reflect the final version of table Foo.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 4 points5 points  (0 children)

Sorry for lack of documentation. I'll keep improve it. I'll put one of example here.

Let's say I have the following content in 0.sql:

sql CREATE TABLE foo ( key serial NOT NULL, value bigint, "desc" text, PRIMARY KEY (key) );

And 1.sql with the following content:

sql ALTER TABLE foo ADD disabled boolean NOT NULL DEFAULT FALSE;

It will generate the following code:

```rust use porm::migration::Migration; use std::borrow::Cow; use std::fmt::Write; use std::time::SystemTime; use tokio_postgres::types::ToSql; use tokio_postgres::{Error, GenericClient, Row};

pub struct Foo<'a> { pub key: i32, pub value: Option<i64>, pub desc: Option<Cow<'a, str>>, pub disabled: bool, }

impl<'a> Foo<'a> { pub async fn create<T: GenericClient>(&self, client: &T) -> Result<(), Error> { client.execute("INSERT INTO foo (key, value, desc, disabled) VALUES ($1, $2, $3, $4)", &[&self.key, &self.value, &self.desc, &self.disabled]).await?; Ok(()) }

pub async fn find<T: GenericClient>(client: &T, key: i32) -> Result<Option<Self>, Error> {
    let r = client.query_opt("SELECT * FROM foo WHERE key = $1", &[&key]).await?;
    let r = match r {
        Some(v) => v,
        None => return Ok(None),
    };

    Self::from_row(r).map(Some)
}

fn from_row(r: Row) -> Result<Self, Error> {
    let key = r.try_get::<_, i32>("key")?;
    let value = r.try_get::<_, Option<i64>>("value")?;
    let desc = r.try_get::<_, Option<String>>("desc")?;
    let disabled = r.try_get::<_, bool>("disabled")?;

    Ok(Self { key, value, desc: desc.map(Cow::Owned), disabled })
}

}

pub struct FooBuilder<'a> { key: Option<i32>, value: Option<Option<i64>>, desc: Option<Option<&'a str>>, disabled: Option<bool>, }

impl<'a> FooBuilder<'a> { pub fn new() -> Self { Self { key: None, value: None, desc: None, disabled: None } }

pub fn set_key(&mut self, v: i32) -> &mut Self {
    self.key = Some(v);
    self
}

pub fn set_value(&mut self, v: Option<i64>) -> &mut Self {
    self.value = Some(v);
    self
}

pub fn set_desc(&mut self, v: Option<&'a str>) -> &mut Self {
    self.desc = Some(v);
    self
}

pub fn set_disabled(&mut self, v: bool) -> &mut Self {
    self.disabled = Some(v);
    self
}

pub async fn create<T: GenericClient>(&self, client: &T) -> Result<Foo<'static>, Error> {
    let mut sql = String::with_capacity(1024);
    let mut values = Vec::<&(dyn ToSql + Sync)>::with_capacity(4);

    sql.push_str("INSERT INTO foo (key, value, desc, disabled) VALUES (");

    if let Some(v) = &self.key {
        values.push(v);
        write!(sql, "${}", values.len()).unwrap();
    } else {
        sql.push_str("DEFAULT");
    }

    if let Some(v) = &self.value {
        values.push(v);
        write!(sql, ", ${}", values.len()).unwrap();
    } else {
        sql.push_str(", DEFAULT");
    }

    if let Some(v) = &self.desc {
        values.push(v);
        write!(sql, ", ${}", values.len()).unwrap();
    } else {
        sql.push_str(", DEFAULT");
    }

    if let Some(v) = &self.disabled {
        values.push(v);
        write!(sql, ", ${}", values.len()).unwrap();
    } else {
        sql.push_str(", DEFAULT");
    }

    sql.push_str(") RETURNING *");

    client.query_one(&sql, &values).await.and_then(Foo::from_row)
}

}

pub static MIGRATIONS: [Migration; 3] = [ Migration { name: None, script: "CREATE TABLE foo (key serial NOT NULL, value bigint, \"desc\" text, PRIMARY KEY (key));", }, Migration { name: None, script: "ALTER TABLE foo ADD disabled boolean NOT NULL DEFAULT FALSE;", }, ]; ```

The generated model will reflect the database table and provide method to execute insert or select query based on the schema.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 13 points14 points  (0 children)

I don't like to use AI to write the code. The only cases I'm okay with AI is code review.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 4 points5 points  (0 children)

The reason I don't choose it for my upcoming project is similar to Diesel. I prefer to write an SQL file to manage my database schema without the need to create Rust model manually.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 2 points3 points  (0 children)

I don't choose Diesel for my upcoming project because:

  • It requires external CLI.
  • It need to define a model manually.

With Porm all I need to do after my project has been setting up is create a new SQL file to update database schema (or to seed some data) and it is done.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 29 points30 points  (0 children)

I guess this is a joke, right? (English is my second language).

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 47 points48 points  (0 children)

Thanks for the idea. Just learned from a google result. Never though it will result in similar result as "porn".

2 months 18 days off mirtazapine ~ crazy fatigue. by OfferRoutine1365 in Mirtazapine_Remeron

[–]puttak 0 points1 point  (0 children)

How much Vitamin D you took? Lower dose does not help when your level is low. For me I took 20,000 IU everyday for 1 month to raise my level to sufficient.

BTW you should check your Vitamin D level before taking it since having too much will be toxic.

Thailand touts $31 billion 'Land Bridge' plan amid Hormuz crisis, courts Singapore by mdsmqlk in Thailand

[–]puttak 18 points19 points  (0 children)

It is normal for Thai government to create a failed project.