Typesafe DB like Prisma

It seems that there are only native sql client libraries in the Rust community. I hope there could be a library like Prisma. Prisma generates TypeScript code according to the schema file and provides typesafety and database abstraction (meaning that you don't need to care about SQL stuff). The ideal Prisma for Rust in my opinion will generate Rust code also based on a schema file. The generated Rust code may look like this:

pub mod schema {
    struct User {
        name: String
    }
}

pub mod db {
    use super::schema;

    trait Adapter {
        fn create(/* ... */) -> /* ... */;
        // ...
    }

    struct User<A: Adapter> {
        db: A
    }
    impl User {
        fn create(&self, /* Something like PartialUser */) -> Result</* ... */> {
            self.db.create(/* ... */);
            // ...
        }

        fn find(&self, /* Some query */) -> Vec<User> {
            // ...
        }

        fn findUnique(&self, /* ... */) -> User {
            // ...
        }

        // ...
    }
}

if not, I think I'm gonna make one.

Rest assured, this is a hard problem. I've been pursuing related research during my PhD, and it's not at all trivial to map between a database representation and in-memory objects.

However, ORM tools exist in the Rust ecosystem. The most widely used one is Diesel. You may want to give it a try before rolling your own in order to see whether it can do what you need.

1 Like

Thanks! That does what I want!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.