|
| 1 | +//! # Collection |
| 2 | +//! |
| 3 | +//! In Typesense, a group of related documents is called a collection. A collection |
| 4 | +//! is roughly equivalent to a table in a relational database. |
| 5 | +//! |
| 6 | +use bon::builder; |
| 7 | +use typesense_codegen::models::{CollectionSchema, Field, VoiceQueryModelCollectionConfig}; |
| 8 | + |
| 9 | +/// Creates a new [`CollectionSchema`] builder. |
| 10 | +/// |
| 11 | +/// In Typesense, a collection is a group of related documents, similar to a table |
| 12 | +/// in a relational database. This builder enforces that `name` must be provided |
| 13 | +/// before [`build`](CollectionSchemaBuilder::build) can be called. |
| 14 | +/// |
| 15 | +/// # Example |
| 16 | +/// |
| 17 | +/// ``` |
| 18 | +/// use typesense::builders::new_collection_schema; |
| 19 | +/// let fields = vec![]; |
| 20 | +/// let schema = new_collection_schema("companies", fields) |
| 21 | +/// .default_sorting_field("num_employees") |
| 22 | +/// .build(); |
| 23 | +/// ``` |
| 24 | +#[builder( |
| 25 | + builder_type(name = CollectionSchemaBuilder, vis = "pub"), |
| 26 | + finish_fn(name = build, vis = "pub"), |
| 27 | + state_mod(name = collection_schema_builder, vis = "pub"), |
| 28 | + on(String, into) |
| 29 | +)] |
| 30 | +pub fn new_collection_schema( |
| 31 | + /// The name of the collection. Must be unique within the Typesense instance. |
| 32 | + #[builder(start_fn)] |
| 33 | + name: String, |
| 34 | + |
| 35 | + /// The list of fields that describe the schema of documents in this collection. |
| 36 | + #[builder(start_fn)] |
| 37 | + fields: Vec<Field>, |
| 38 | + |
| 39 | + /// The name of the default sorting field for the collection. |
| 40 | + default_sorting_field: Option<String>, |
| 41 | + |
| 42 | + /// A list of token separators to use when indexing text fields. |
| 43 | + token_separators: Option<Vec<String>>, |
| 44 | + |
| 45 | + /// Whether nested fields are enabled. |
| 46 | + enable_nested_fields: Option<bool>, |
| 47 | + |
| 48 | + /// Symbols that should be indexed for this collection. |
| 49 | + symbols_to_index: Option<Vec<String>>, |
| 50 | + |
| 51 | + /// Configuration for Typesense’s Voice Query Model. |
| 52 | + voice_query_model: Option<Box<VoiceQueryModelCollectionConfig>>, |
| 53 | +) -> CollectionSchema { |
| 54 | + CollectionSchema { |
| 55 | + name, |
| 56 | + fields, |
| 57 | + default_sorting_field, |
| 58 | + token_separators, |
| 59 | + enable_nested_fields, |
| 60 | + symbols_to_index, |
| 61 | + voice_query_model, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// custom convenience methods; the typestate module name matches `state_mod` |
| 66 | +impl<S: collection_schema_builder::State> CollectionSchemaBuilder<S> { |
| 67 | + /// Adds a single [`Field`] to the collection schema. |
| 68 | + /// |
| 69 | + /// This is a convenience method for pushing one field at a time. |
| 70 | + pub fn field(mut self, field: Field) -> Self { |
| 71 | + self.fields.push(field); |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + /// Adds multiple [`Field`] values to the collection schema. |
| 76 | + /// |
| 77 | + /// This is a convenience method for appending a slice of fields. |
| 78 | + pub fn fields(mut self, fields: &[Field]) -> Self |
| 79 | + where |
| 80 | + Field: Clone, |
| 81 | + { |
| 82 | + self.fields.extend_from_slice(fields); |
| 83 | + self |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod test { |
| 89 | + use super::*; |
| 90 | + use crate::builders::new_collection_field; |
| 91 | + use serde_json::json; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn collection_schema_serializes_as_expected() { |
| 95 | + let fields = [ |
| 96 | + ("company_name", "string".to_owned(), None), |
| 97 | + ("num_employees", "int32".to_owned(), None), |
| 98 | + ("country", "string".to_owned(), Some(true)), |
| 99 | + ] |
| 100 | + .map(|(name, typesense_type, facet)| { |
| 101 | + if facet.is_some() { |
| 102 | + new_collection_field(name, typesense_type.into()) |
| 103 | + .facet(facet.unwrap()) |
| 104 | + .build() |
| 105 | + } else { |
| 106 | + new_collection_field(name, typesense_type.into()).build() |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + let collection: CollectionSchema = |
| 111 | + new_collection_schema("companies", fields.clone().to_vec()) |
| 112 | + .fields(&fields) |
| 113 | + .field(new_collection_field("size", "string".into()).build()) |
| 114 | + .default_sorting_field("num_employees") |
| 115 | + .build(); |
| 116 | + |
| 117 | + let expected = json!( |
| 118 | + { |
| 119 | + "name": "companies", |
| 120 | + "fields": [ |
| 121 | + { "name": "company_name", "type": "string" }, |
| 122 | + { "name": "num_employees", "type": "int32" }, |
| 123 | + { "name": "country", "type": "string", "facet": true }, |
| 124 | + |
| 125 | + { "name": "company_name", "type": "string" }, |
| 126 | + { "name": "num_employees", "type": "int32" }, |
| 127 | + { "name": "country", "type": "string", "facet": true }, |
| 128 | + |
| 129 | + { "name": "size", "type": "string" }, |
| 130 | + ], |
| 131 | + "default_sorting_field": "num_employees" |
| 132 | + } |
| 133 | + ); |
| 134 | + |
| 135 | + assert_eq!(serde_json::to_value(&collection).unwrap(), expected) |
| 136 | + } |
| 137 | +} |
0 commit comments