cognite/dto/data_modeling/
containers.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4use serde_with::skip_serializing_none;
5
6use crate::{
7    models::{TaggedContainerReference, UsedFor},
8    to_query, IntoParams, RawValue, SetCursor,
9};
10
11use super::common::{CDFExternalIdReference, EnumProperty, PrimitiveProperty, TextProperty};
12
13#[derive(Serialize, Deserialize, Clone, Debug)]
14#[serde(rename_all = "camelCase", tag = "type")]
15/// Property variants in containers.
16pub enum ContainerPropertyType {
17    /// Text property
18    Text(TextProperty),
19    /// Boolean property
20    Boolean(PrimitiveProperty),
21    /// 32-bit floating point property
22    Float32(PrimitiveProperty),
23    /// 64-bit floating point property
24    Float64(PrimitiveProperty),
25    /// 32-bit integer property.
26    Int32(PrimitiveProperty),
27    /// 64-bit integer property.
28    Int64(PrimitiveProperty),
29    /// Timestamp property.
30    Timestamp(PrimitiveProperty),
31    /// Date property.
32    Date(PrimitiveProperty),
33    /// JSON property.
34    Json(PrimitiveProperty),
35    /// Time series reference property.
36    Timeseries(CDFExternalIdReference),
37    /// File reference property.
38    File(CDFExternalIdReference),
39    /// Sequence reference property.
40    Sequence(CDFExternalIdReference),
41    /// Node reference property.
42    Direct(DirectNodeRelationType),
43    /// Enum property.
44    Enum(EnumProperty),
45}
46
47#[skip_serializing_none]
48#[derive(Serialize, Deserialize, Clone, Debug, Default)]
49#[serde(rename_all = "camelCase")]
50/// Constraints for a property that is a reference to a node.
51pub struct DirectNodeRelationType {
52    /// Container the referenced node must be in.
53    pub container: Option<TaggedContainerReference>,
54}
55
56#[skip_serializing_none]
57#[derive(Serialize, Deserialize, Clone, Debug)]
58#[serde(
59    rename_all = "camelCase",
60    rename_all_fields = "camelCase",
61    tag = "constraintType"
62)]
63/// Constraint on a container.
64pub enum ContainerConstraint {
65    /// In order to have values in this container,
66    /// a node or edge must also have values in a different
67    /// container.
68    Requires {
69        /// Required container
70        require: TaggedContainerReference,
71    },
72    /// The given properties must contain only unique sets of values.
73    Uniqueness {
74        /// Properties in constraint
75        properties: Vec<String>,
76    },
77}
78
79#[skip_serializing_none]
80#[derive(Serialize, Deserialize, Clone, Debug)]
81#[serde(
82    rename_all = "camelCase",
83    rename_all_fields = "camelCase",
84    tag = "indexType"
85)]
86/// Index on a container.
87pub enum ContainerIndex {
88    /// BTree index on a set of properties
89    Btree {
90        /// List of properties in index.
91        properties: Vec<String>,
92        /// Whether it should be possible to paginate on this cursor.
93        cursorable: Option<bool>,
94    },
95    /// Inverted index on a set of properties.
96    Inverted {
97        /// Properties in index.
98        properties: Vec<String>,
99    },
100}
101
102#[skip_serializing_none]
103#[derive(Serialize, Deserialize, Clone, Debug)]
104#[serde(rename_all = "camelCase")]
105/// Definition for a container property
106pub struct ContainerPropertyDefinition {
107    /// Whether this property is nullable, default `true`
108    pub nullable: Option<bool>,
109    /// Whether this property auto-increments.
110    pub auto_increment: Option<bool>,
111    /// Default value of this property.
112    pub default_value: Option<RawValue>,
113    /// Property description.
114    pub description: Option<String>,
115    /// Property name.
116    pub name: Option<String>,
117    #[serde(rename = "type")]
118    /// Property type.
119    pub r#type: ContainerPropertyType,
120}
121
122#[skip_serializing_none]
123#[derive(Serialize, Deserialize, Clone, Debug, Default)]
124#[serde(rename_all = "camelCase")]
125/// Create a container.
126pub struct ContainerCreate {
127    /// Container space.
128    pub space: String,
129    /// Container external ID.
130    pub external_id: String,
131    /// Container name.
132    pub name: Option<String>,
133    /// Container description.
134    pub description: Option<String>,
135    /// Whether this container can be used for nodes, edges, or both.
136    pub used_for: Option<UsedFor>,
137    /// Properties in this container.
138    pub properties: HashMap<String, ContainerPropertyDefinition>,
139    /// Container constraints.
140    pub constraints: HashMap<String, ContainerConstraint>,
141    /// Container indexes.
142    pub indexes: HashMap<String, ContainerIndex>,
143}
144
145#[skip_serializing_none]
146#[derive(Serialize, Deserialize, Clone, Debug, Default)]
147#[serde(rename_all = "camelCase")]
148/// Data modeling container.
149pub struct ContainerDefinition {
150    /// Container space.
151    pub space: String,
152    /// Container external ID.
153    pub external_id: String,
154    /// Container name.
155    pub name: Option<String>,
156    /// Container description.
157    pub description: Option<String>,
158    /// Whether this container can be used for nodes, edges, or both.
159    pub used_for: Option<UsedFor>,
160    /// Properties in this container.
161    pub properties: HashMap<String, ContainerPropertyDefinition>,
162    /// Container constraints.
163    pub constraints: HashMap<String, ContainerConstraint>,
164    /// Container indexes.
165    pub indexes: HashMap<String, ContainerIndex>,
166    /// Time this file was created, in milliseconds since epoch.
167    pub created_time: i64,
168    /// Time this file was last updated, in milliseconds since epoch.
169    pub last_updated_time: i64,
170    /// Whether this is a global container
171    pub is_global: bool,
172}
173
174impl From<ContainerDefinition> for ContainerCreate {
175    fn from(value: ContainerDefinition) -> Self {
176        Self {
177            space: value.space,
178            external_id: value.external_id,
179            name: value.name,
180            description: value.description,
181            used_for: value.used_for,
182            properties: value.properties,
183            constraints: value.constraints,
184            indexes: value.indexes,
185        }
186    }
187}
188
189#[skip_serializing_none]
190#[derive(Serialize, Deserialize, Clone, Debug, Default)]
191#[serde(rename_all = "camelCase")]
192/// ID of a container index or constraint.
193pub struct ContainerComponentId {
194    /// Container space
195    pub space: String,
196    /// Container external ID.
197    pub container_external_id: String,
198    /// Index or constraint identifier.
199    pub identifier: String,
200}
201
202#[derive(Default, Clone, Debug)]
203/// Query for listing containers.
204pub struct ContainerQuery {
205    /// Maximum number of containers in the result.
206    pub limit: Option<i32>,
207    /// Optional cursor for pagination.
208    pub cursor: Option<String>,
209    /// Filter on container space.
210    pub space: Option<String>,
211    /// Include global containers.
212    pub include_global: Option<bool>,
213}
214
215impl IntoParams for ContainerQuery {
216    fn into_params(self) -> Vec<(String, String)> {
217        let mut params = Vec::new();
218        to_query("limit", &self.limit, &mut params);
219        to_query("cursor", &self.cursor, &mut params);
220        to_query("space", &self.space, &mut params);
221        to_query("includeGlobal", &self.include_global, &mut params);
222        params
223    }
224}
225
226impl SetCursor for ContainerQuery {
227    fn set_cursor(&mut self, cursor: Option<String>) {
228        self.cursor = cursor;
229    }
230}