Created
September 11, 2020 18:42
-
-
Save danjenson/c06736a7bf6910068440ff86f0f1e879 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// inside users.rs | |
use crate::db::{traits::Model as DbModel, Conn}; | |
use crate::schema::users; | |
use chrono::{DateTime, Utc}; | |
use diesel::{ | |
associations::{HasTable, Identifiable}, | |
delete, insert_into, | |
result::QueryResult, | |
ExpressionMethods, QueryDsl, RunQueryDsl, | |
}; | |
use serde::{Deserialize, Serialize}; | |
use uuid::Uuid; | |
#[derive( | |
Debug, | |
Eq, | |
PartialEq, | |
Insertable, | |
Identifiable, | |
Queryable, | |
Serialize, | |
Deserialize, | |
)] | |
#[primary_key(uuid)] | |
pub struct User { | |
pub created_at: DateTime<Utc>, | |
pub updated_at: DateTime<Utc>, | |
pub uuid: Uuid, | |
pub display_name: String, | |
pub name: String, | |
pub email: String, | |
} | |
impl User { | |
pub fn create( | |
conn: &Conn, | |
display_name: &str, | |
name: &str, | |
email: &str, | |
) -> QueryResult<Self> { | |
insert_into(users::table) | |
.values(( | |
users::display_name.eq(display_name), | |
users::name.eq(name), | |
users::email.eq(email), | |
)) | |
.get_result(conn) | |
} | |
// pub fn get<'ident>( | |
// conn: &Conn, | |
// pk: &<&'ident Self as Identifiable>::Id, | |
// ) -> QueryResult<Self> { | |
// users::table.find(pk).first(conn) | |
// } | |
pub fn delete(&self, conn: &Conn) -> QueryResult<usize> { | |
delete(self).execute(conn) | |
} | |
} | |
impl DbModel for User {} | |
// inside db.rs | |
pub mod traits { | |
use diesel::{ | |
associations::HasTable, | |
connection::Connection, | |
deserialize::Queryable, | |
helper_types::{Find, Limit}, | |
query_builder::{AsQuery, IntoUpdateTarget, QueryFragment, QueryId}, | |
query_dsl::{ | |
methods::{FindDsl, LimitDsl}, | |
RunQueryDsl, | |
}, | |
query_source::{QuerySource, Table}, | |
result::QueryResult, | |
sql_types::HasSqlType, | |
}; | |
type Tbl<T> = <T as HasTable>::Table; | |
type Pk<T> = <<T as HasTable>::Table as Table>::PrimaryKey; | |
type Backend<Conn> = <Conn as Connection>::Backend; | |
type FindLimit<T> = Limit<Find<Tbl<T>, Pk<T>>>; | |
type FindLimitQuery<T> = <FindLimit<T> as AsQuery>::Query; | |
type FindLimitSqlType<T> = <FindLimit<T> as AsQuery>::SqlType; | |
type FromClause<T> = <Tbl<T> as QuerySource>::FromClause; | |
type WhereClause<T> = <T as IntoUpdateTarget>::WhereClause; | |
pub trait Model: Sized + HasTable { | |
fn get<Conn: Connection>(conn: &Conn, pk: Pk<Self>) -> QueryResult<Self> | |
where | |
Self: Queryable<FindLimitSqlType<Self>, Backend<Conn>>, | |
Tbl<Self>: FindDsl<Pk<Self>>, | |
Find<Tbl<Self>, Pk<Self>>: Table + LimitDsl, | |
FindLimit<Self>: Table, | |
FindLimitQuery<Self>: QueryFragment<Backend<Conn>> + QueryId, | |
Backend<Conn>: HasSqlType<FindLimitSqlType<Self>>, | |
{ | |
Self::table().find(pk).first(conn) | |
} | |
fn delete<Conn: Connection>(self, conn: &Conn) -> QueryResult<usize> | |
where | |
Self: IntoUpdateTarget, | |
Tbl<Self>: QueryId, | |
FromClause<Self>: QueryFragment<Backend<Conn>>, | |
WhereClause<Self>: QueryFragment<Backend<Conn>> + QueryId, | |
{ | |
diesel::delete(self).execute(conn) | |
} | |
} | |
} | |
// ERRORS | |
error[E0277]: the trait bound `diesel::query_builder::select_statement::SelectStatement<gql::schema::users::table, diesel::query_builder::select_clause::D | |
efaultSelectClause, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::WhereClause<diesel::expression::operato | |
rs::Eq<gql::schema::users::uuid, gql::schema::users::uuid>>>: diesel::query_source::Table` is not satisfied | |
--> tests/db.rs:30:21 | |
| | |
30 | let same_user = User::get(&conn, user.uuid.clone()); | |
| ^^^^^^^^^ the trait `diesel::query_source::Table` is not implemented for `diesel::query_builder::select_statement::SelectStatemen | |
t<gql::schema::users::table, diesel::query_builder::select_clause::DefaultSelectClause, diesel::query_builder::distinct_clause::NoDistinctClause, diesel:: | |
query_builder::where_clause::WhereClause<diesel::expression::operators::Eq<gql::schema::users::uuid, gql::schema::users::uuid>>>` | |
| | |
::: /data/repos/motoko/backend/rs/gql/src/db.rs:78:35 | |
| | |
78 | FindLimitQuery<Self>: QueryFragment<Backend<Conn>> + QueryId, | |
| ---------------------------- required by this bound in `gql::db::traits::Model::get` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment