|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +use diesel::pg::Pg; |
| 6 | +use diesel::Column; |
| 7 | +use diesel::ExpressionMethods; |
| 8 | +use diesel::Selectable; |
| 9 | +use std::fmt::Debug; |
| 10 | + |
| 11 | +/// Trait to be implemented by any structs representing a collection. |
| 12 | +/// For example, since Organizations have a one-to-many relationship with |
| 13 | +/// Projects, the Organization datatype should implement this trait. |
| 14 | +/// ``` |
| 15 | +/// # use diesel::prelude::*; |
| 16 | +/// # use nexus_db_model::DatastoreCollectionConfig; |
| 17 | +/// # use nexus_db_model::Generation; |
| 18 | +/// # |
| 19 | +/// # table! { |
| 20 | +/// # test_schema.organization (id) { |
| 21 | +/// # id -> Uuid, |
| 22 | +/// # time_deleted -> Nullable<Timestamptz>, |
| 23 | +/// # rcgen -> Int8, |
| 24 | +/// # } |
| 25 | +/// # } |
| 26 | +/// # |
| 27 | +/// # table! { |
| 28 | +/// # test_schema.project (id) { |
| 29 | +/// # id -> Uuid, |
| 30 | +/// # time_deleted -> Nullable<Timestamptz>, |
| 31 | +/// # organization_id -> Uuid, |
| 32 | +/// # } |
| 33 | +/// # } |
| 34 | +/// |
| 35 | +/// #[derive(Queryable, Insertable, Debug, Selectable)] |
| 36 | +/// #[diesel(table_name = project)] |
| 37 | +/// struct Project { |
| 38 | +/// pub id: uuid::Uuid, |
| 39 | +/// pub time_deleted: Option<chrono::DateTime<chrono::Utc>>, |
| 40 | +/// pub organization_id: uuid::Uuid, |
| 41 | +/// } |
| 42 | +/// |
| 43 | +/// #[derive(Queryable, Insertable, Debug, Selectable)] |
| 44 | +/// #[diesel(table_name = organization)] |
| 45 | +/// struct Organization { |
| 46 | +/// pub id: uuid::Uuid, |
| 47 | +/// pub time_deleted: Option<chrono::DateTime<chrono::Utc>>, |
| 48 | +/// pub rcgen: Generation, |
| 49 | +/// } |
| 50 | +/// |
| 51 | +/// impl DatastoreCollectionConfig<Project> for Organization { |
| 52 | +/// // Type of Organization::identity::id and Project::organization_id |
| 53 | +/// type CollectionId = uuid::Uuid; |
| 54 | +/// |
| 55 | +/// type GenerationNumberColumn = organization::dsl::rcgen; |
| 56 | +/// type CollectionTimeDeletedColumn = organization::dsl::time_deleted; |
| 57 | +/// |
| 58 | +/// type CollectionIdColumn = project::dsl::organization_id; |
| 59 | +/// } |
| 60 | +/// ``` |
| 61 | +pub trait DatastoreCollectionConfig<ResourceType> { |
| 62 | + /// The Rust type of the collection id (typically Uuid for us) |
| 63 | + type CollectionId: Copy + Debug; |
| 64 | + |
| 65 | + /// The column in the CollectionTable that acts as a generation number. |
| 66 | + /// This is the "child-resource-generation-number" in RFD 192. |
| 67 | + type GenerationNumberColumn: Column + Default; |
| 68 | + |
| 69 | + /// The time deleted column in the CollectionTable |
| 70 | + // We enforce that this column comes from the same table as |
| 71 | + // GenerationNumberColumn when defining insert_resource() below. |
| 72 | + type CollectionTimeDeletedColumn: Column + Default; |
| 73 | + |
| 74 | + /// The column in the ResourceTable that acts as a foreign key into |
| 75 | + /// the CollectionTable |
| 76 | + type CollectionIdColumn: Column; |
| 77 | +} |
| 78 | + |
| 79 | +/// Trait to be implemented by structs representing an attachable collection. |
| 80 | +/// |
| 81 | +/// For example, since Instances have a one-to-many relationship with |
| 82 | +/// Disks, the Instance datatype should implement this trait. |
| 83 | +/// ``` |
| 84 | +/// # use diesel::prelude::*; |
| 85 | +/// # use nexus_db_model::DatastoreAttachTargetConfig; |
| 86 | +/// # |
| 87 | +/// # table! { |
| 88 | +/// # test_schema.instance (id) { |
| 89 | +/// # id -> Uuid, |
| 90 | +/// # time_deleted -> Nullable<Timestamptz>, |
| 91 | +/// # } |
| 92 | +/// # } |
| 93 | +/// # |
| 94 | +/// # table! { |
| 95 | +/// # test_schema.disk (id) { |
| 96 | +/// # id -> Uuid, |
| 97 | +/// # time_deleted -> Nullable<Timestamptz>, |
| 98 | +/// # instance_id -> Nullable<Uuid>, |
| 99 | +/// # } |
| 100 | +/// # } |
| 101 | +/// |
| 102 | +/// #[derive(Queryable, Debug, Selectable)] |
| 103 | +/// #[diesel(table_name = disk)] |
| 104 | +/// struct Disk { |
| 105 | +/// pub id: uuid::Uuid, |
| 106 | +/// pub time_deleted: Option<chrono::DateTime<chrono::Utc>>, |
| 107 | +/// pub instance_id: Option<uuid::Uuid>, |
| 108 | +/// } |
| 109 | +/// |
| 110 | +/// #[derive(Queryable, Debug, Selectable)] |
| 111 | +/// #[diesel(table_name = instance)] |
| 112 | +/// struct Instance { |
| 113 | +/// pub id: uuid::Uuid, |
| 114 | +/// pub time_deleted: Option<chrono::DateTime<chrono::Utc>>, |
| 115 | +/// } |
| 116 | +/// |
| 117 | +/// impl DatastoreAttachTargetConfig<Disk> for Instance { |
| 118 | +/// // Type of instance::id and disk::id. |
| 119 | +/// type Id = uuid::Uuid; |
| 120 | +/// |
| 121 | +/// type CollectionIdColumn = instance::dsl::id; |
| 122 | +/// type CollectionTimeDeletedColumn = instance::dsl::time_deleted; |
| 123 | +/// |
| 124 | +/// type ResourceIdColumn = disk::dsl::id; |
| 125 | +/// type ResourceCollectionIdColumn = disk::dsl::instance_id; |
| 126 | +/// type ResourceTimeDeletedColumn = disk::dsl::time_deleted; |
| 127 | +/// } |
| 128 | +/// ``` |
| 129 | +pub trait DatastoreAttachTargetConfig<ResourceType>: |
| 130 | + Selectable<Pg> + Sized |
| 131 | +{ |
| 132 | + /// The Rust type of the collection and resource ids (typically Uuid). |
| 133 | + type Id: Copy + Debug + PartialEq + Send + 'static; |
| 134 | + |
| 135 | + /// The primary key column of the collection. |
| 136 | + type CollectionIdColumn: Column; |
| 137 | + |
| 138 | + /// The time deleted column in the CollectionTable |
| 139 | + type CollectionTimeDeletedColumn: Column<Table = <Self::CollectionIdColumn as Column>::Table> |
| 140 | + + Default |
| 141 | + + ExpressionMethods; |
| 142 | + |
| 143 | + /// The primary key column of the resource |
| 144 | + type ResourceIdColumn: Column; |
| 145 | + |
| 146 | + /// The column in the resource acting as a foreign key into the Collection |
| 147 | + type ResourceCollectionIdColumn: Column<Table = <Self::ResourceIdColumn as Column>::Table> |
| 148 | + + Default |
| 149 | + + ExpressionMethods; |
| 150 | + |
| 151 | + /// The time deleted column in the ResourceTable |
| 152 | + type ResourceTimeDeletedColumn: Column<Table = <Self::ResourceIdColumn as Column>::Table> |
| 153 | + + Default |
| 154 | + + ExpressionMethods; |
| 155 | +} |
0 commit comments