spacetimedb_lib/db/raw_def/
v9.rs

1//! ABI Version 9 of the raw module definitions.
2//!
3//! This is the ABI that will be used for 1.0.
4//! We are keeping around the old ABI (v8) for now, to allow ourselves to convert the codebase
5//! a component-at-a-time.
6
7use std::any::TypeId;
8use std::collections::btree_map;
9use std::collections::BTreeMap;
10use std::fmt;
11
12use itertools::Itertools;
13use spacetimedb_primitives::*;
14use spacetimedb_sats::typespace::TypespaceBuilder;
15use spacetimedb_sats::AlgebraicType;
16use spacetimedb_sats::AlgebraicTypeRef;
17use spacetimedb_sats::AlgebraicValue;
18use spacetimedb_sats::ProductType;
19use spacetimedb_sats::ProductTypeElement;
20use spacetimedb_sats::SpacetimeType;
21use spacetimedb_sats::Typespace;
22
23use crate::db::auth::StAccess;
24use crate::db::auth::StTableType;
25
26/// A not-yet-validated identifier.
27pub type RawIdentifier = Box<str>;
28
29/// A not-yet-validated `sql`.
30pub type RawSql = Box<str>;
31
32/// A possibly-invalid raw module definition.
33///
34/// ABI Version 9.
35///
36/// These "raw definitions" may contain invalid data, and are validated by the `validate` module into a proper `spacetimedb_schema::ModuleDef`, or a collection of errors.
37///
38/// The module definition has a single logical global namespace, which maps `Identifier`s to:
39///
40/// - database-level objects:
41///     - logical schema objects:
42///         - tables
43///         - constraints
44///         - sequence definitions
45///     - physical schema objects:
46///         - indexes
47/// - module-level objects:
48///     - reducers
49///     - schedule definitions
50/// - binding-level objects:
51///     - type aliases
52///
53/// All of these types of objects must have unique names within the module.
54/// The exception is columns, which need unique names only within a table.
55#[derive(Debug, Clone, Default, SpacetimeType)]
56#[sats(crate = crate)]
57#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
58pub struct RawModuleDefV9 {
59    /// The `Typespace` used by the module.
60    ///
61    /// `AlgebraicTypeRef`s in the table, reducer, and type alias declarations refer to this typespace.
62    ///
63    /// The typespace must satisfy `Typespace::is_valid_for_client_code_generation`. That is, all types stored in the typespace must either:
64    /// 1. satisfy `AlgebraicType::is_valid_for_client_type_definition`
65    /// 2. and/or `AlgebraicType::is_valid_for_client_type_use`.
66    ///
67    /// Types satisfying condition 1 correspond to generated classes in client code.
68    /// (Types satisfying condition 2 are an artifact of the module bindings, and do not affect the semantics of the module definition.)
69    ///
70    /// Types satisfying condition 1 are required to have corresponding `RawTypeDefV9` declarations in the module.
71    pub typespace: Typespace,
72
73    /// The tables of the database definition used in the module.
74    ///
75    /// Each table must have a unique name.
76    pub tables: Vec<RawTableDefV9>,
77
78    /// The reducers exported by the module.
79    pub reducers: Vec<RawReducerDefV9>,
80
81    /// The types exported by the module.
82    pub types: Vec<RawTypeDefV9>,
83
84    /// Miscellaneous additional module exports.
85    ///
86    /// The enum [`RawMiscModuleExportV9`] can have new variants added
87    /// without breaking existing compiled modules.
88    /// As such, this acts as a sort of dumping ground for any exports added after we defined `RawModuleDefV9`.
89    ///
90    /// If/when we define `RawModuleDefV10`, these should be moved out of `misc_exports` and into their own fields,
91    /// and the new `misc_exports` should once again be initially empty.
92    pub misc_exports: Vec<RawMiscModuleExportV9>,
93
94    /// Row level security definitions.
95    ///
96    /// Each definition must have a unique name.
97    pub row_level_security: Vec<RawRowLevelSecurityDefV9>,
98}
99
100impl RawModuleDefV9 {
101    /// Find a [`RawTableDefV9`] by name in this raw module def
102    fn find_table_def(&self, table_name: &str) -> Option<&RawTableDefV9> {
103        self.tables
104            .iter()
105            .find(|table_def| table_def.name.as_ref() == table_name)
106    }
107
108    /// Find a [`RawViewDefV9`] by name in this raw module def
109    fn find_view_def(&self, view_name: &str) -> Option<&RawViewDefV9> {
110        self.misc_exports.iter().find_map(|misc_export| match misc_export {
111            RawMiscModuleExportV9::View(view_def) if view_def.name.as_ref() == view_name => Some(view_def),
112            _ => None,
113        })
114    }
115
116    /// Find and return the product type ref for a table in this module def
117    fn type_ref_for_table(&self, table_name: &str) -> Option<AlgebraicTypeRef> {
118        self.find_table_def(table_name)
119            .map(|table_def| table_def.product_type_ref)
120    }
121
122    /// Find and return the product type ref for a view in this module def
123    fn type_ref_for_view(&self, view_name: &str) -> Option<AlgebraicTypeRef> {
124        self.find_view_def(view_name)
125            .map(|view_def| &view_def.return_type)
126            .and_then(|return_type| {
127                return_type
128                    .as_option()
129                    .and_then(|inner| inner.clone().into_ref().ok())
130                    .or_else(|| {
131                        return_type
132                            .as_array()
133                            .and_then(|inner| inner.elem_ty.clone().into_ref().ok())
134                    })
135            })
136    }
137
138    /// Find and return the product type ref for a table or view in this module def
139    pub fn type_ref_for_table_like(&self, name: &str) -> Option<AlgebraicTypeRef> {
140        self.type_ref_for_table(name).or_else(|| self.type_ref_for_view(name))
141    }
142}
143
144/// The definition of a database table.
145///
146/// This struct holds information about the table, including its name, columns, indexes,
147/// constraints, sequences, type, and access rights.
148///
149/// Validation rules:
150/// - The table name must be a valid `spacetimedb_schema::identifier::Identifier`.
151/// - The table's indexes, constraints, and sequences need not be sorted; they will be sorted according to their respective ordering rules.
152/// - The table's column types may refer only to types in the containing `RawModuleDefV9`'s typespace.
153/// - The table's column names must be unique.
154#[derive(Debug, Clone, SpacetimeType)]
155#[sats(crate = crate)]
156#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
157pub struct RawTableDefV9 {
158    /// The name of the table.
159    /// Unique within a module, acts as the table's identifier.
160    /// Must be a valid `spacetimedb_schema::identifier::Identifier`.
161    pub name: RawIdentifier,
162
163    /// A reference to a `ProductType` containing the columns of this table.
164    /// This is the single source of truth for the table's columns.
165    /// All elements of the `ProductType` must have names.
166    ///
167    /// Like all types in the module, this must have the [default element ordering](crate::db::default_element_ordering),
168    /// UNLESS a custom ordering is declared via a `RawTypeDefv9` for this type.
169    pub product_type_ref: AlgebraicTypeRef,
170
171    /// The primary key of the table, if present. Must refer to a valid column.
172    ///
173    /// Currently, there must be a unique constraint and an index corresponding to the primary key.
174    /// Eventually, we may remove the requirement for an index.
175    ///
176    /// The database engine does not actually care about this, but client code generation does.
177    ///
178    /// A list of length 0 means no primary key. Currently, a list of length >1 is not supported.
179    pub primary_key: ColList,
180
181    /// The indices of the table.
182    pub indexes: Vec<RawIndexDefV9>,
183
184    /// Any unique constraints on the table.
185    pub constraints: Vec<RawConstraintDefV9>,
186
187    /// The sequences for the table.
188    pub sequences: Vec<RawSequenceDefV9>,
189
190    /// The schedule for the table.
191    pub schedule: Option<RawScheduleDefV9>,
192
193    /// Whether this is a system- or user-created table.
194    pub table_type: TableType,
195
196    /// Whether this table is public or private.
197    pub table_access: TableAccess,
198}
199
200/// Whether the table was created by the system or the user.
201#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, SpacetimeType)]
202#[sats(crate = crate)]
203pub enum TableType {
204    /// Created by the system.
205    System,
206    /// Created by the user.
207    User,
208}
209impl From<StTableType> for TableType {
210    fn from(t: StTableType) -> Self {
211        match t {
212            StTableType::System => TableType::System,
213            StTableType::User => TableType::User,
214        }
215    }
216}
217impl From<TableType> for StTableType {
218    fn from(t: TableType) -> Self {
219        match t {
220            TableType::System => StTableType::System,
221            TableType::User => StTableType::User,
222        }
223    }
224}
225
226/// The visibility of the table.
227#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, SpacetimeType)]
228#[sats(crate = crate)]
229pub enum TableAccess {
230    /// Visible to all
231    Public,
232    /// Visible only to the owner
233    Private,
234}
235impl From<StAccess> for TableAccess {
236    fn from(t: StAccess) -> Self {
237        match t {
238            StAccess::Public => TableAccess::Public,
239            StAccess::Private => TableAccess::Private,
240        }
241    }
242}
243impl From<TableAccess> for StAccess {
244    fn from(t: TableAccess) -> Self {
245        match t {
246            TableAccess::Public => StAccess::Public,
247            TableAccess::Private => StAccess::Private,
248        }
249    }
250}
251
252/// A sequence definition for a database table column.
253#[derive(Debug, Clone, SpacetimeType)]
254#[sats(crate = crate)]
255#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
256pub struct RawSequenceDefV9 {
257    /// In the future, the user may FOR SOME REASON want to override this.
258    /// Even though there is ABSOLUTELY NO REASON TO.
259    /// If `None`, a nicely-formatted unique default will be chosen.
260    pub name: Option<Box<str>>,
261
262    /// The position of the column associated with this sequence.
263    /// This refers to a column in the same `RawTableDef` that contains this `RawSequenceDef`.
264    /// The column must have integral type.
265    /// This must be the unique `RawSequenceDef` for this column.
266    pub column: ColId,
267
268    /// The value to start assigning to this column.
269    /// Will be incremented by 1 for each new row.
270    /// If not present, an arbitrary start point may be selected.
271    pub start: Option<i128>,
272
273    /// The minimum allowed value in this column.
274    /// If not present, no minimum.
275    pub min_value: Option<i128>,
276
277    /// The maximum allowed value in this column.
278    /// If not present, no maximum.
279    pub max_value: Option<i128>,
280
281    /// The increment used when updating the SequenceDef.
282    pub increment: i128,
283}
284
285/// The definition of a database index.
286#[derive(Debug, Clone, SpacetimeType)]
287#[sats(crate = crate)]
288#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
289pub struct RawIndexDefV9 {
290    /// In the future, the user may FOR SOME REASON want to override this.
291    /// Even though there is ABSOLUTELY NO REASON TO.
292    pub name: Option<Box<str>>,
293
294    /// Accessor name for the index used in client codegen.
295    ///
296    /// This is set the user and should not be assumed to follow
297    /// any particular format.
298    ///
299    /// May be set to `None` if this is an auto-generated index for which the user
300    /// has not supplied a name. In this case, no client code generation for this index
301    /// will be performed.
302    ///
303    /// This name is not visible in the system tables, it is only used for client codegen.
304    pub accessor_name: Option<RawIdentifier>,
305
306    /// The algorithm parameters for the index.
307    pub algorithm: RawIndexAlgorithm,
308}
309
310/// Data specifying an index algorithm.
311/// New fields MUST be added to the END of this enum, to maintain ABI compatibility.
312#[non_exhaustive]
313#[derive(Debug, Clone, SpacetimeType)]
314#[sats(crate = crate)]
315#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
316pub enum RawIndexAlgorithm {
317    /// Implemented using a B-Tree.
318    ///
319    /// Currently, this uses a rust `std::collections::BTreeMap`.
320    BTree {
321        /// The columns to index on. These are ordered.
322        columns: ColList,
323    },
324    /// Currently forbidden.
325    Hash {
326        /// The columns to index on. These are ordered.
327        columns: ColList,
328    },
329    /// Implemented using direct indexing in list(s) of `RowPointer`s.
330    /// The column this is placed on must also have a unique constraint.
331    Direct {
332        /// The column to index on.
333        /// Only one is allowed, as direct indexing with more is nonsensical.
334        column: ColId,
335    },
336}
337
338/// Returns a btree index algorithm for the columns `cols`.
339pub fn btree(cols: impl Into<ColList>) -> RawIndexAlgorithm {
340    RawIndexAlgorithm::BTree { columns: cols.into() }
341}
342
343/// Returns a direct index algorithm for the column `col`.
344pub fn direct(col: impl Into<ColId>) -> RawIndexAlgorithm {
345    RawIndexAlgorithm::Direct { column: col.into() }
346}
347
348/// Marks a table as a timer table for a scheduled reducer or procedure.
349///
350/// The table must have columns:
351/// - `scheduled_id` of type `u64`.
352/// - `scheduled_at` of type `ScheduleAt`.
353#[derive(Debug, Clone, SpacetimeType)]
354#[sats(crate = crate)]
355#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
356pub struct RawScheduleDefV9 {
357    /// In the future, the user may FOR SOME REASON want to override this.
358    /// Even though there is ABSOLUTELY NO REASON TO.
359    pub name: Option<Box<str>>,
360
361    /// The name of the reducer or procedure to call.
362    ///
363    /// Despite the field name here, this may be either a reducer or a procedure.
364    pub reducer_name: RawIdentifier,
365
366    /// The column of the `scheduled_at` field of this scheduled table.
367    pub scheduled_at_column: ColId,
368}
369
370/// A constraint definition attached to a table.
371#[derive(Debug, Clone, SpacetimeType)]
372#[sats(crate = crate)]
373#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
374pub struct RawConstraintDefV9 {
375    /// In the future, the user may FOR SOME REASON want to override this.
376    /// Even though there is ABSOLUTELY NO REASON TO.
377    pub name: Option<Box<str>>,
378
379    /// The data for the constraint.
380    pub data: RawConstraintDataV9,
381}
382
383/// Raw data attached to a constraint.
384/// New fields MUST be added to the END of this enum, to maintain ABI compatibility.
385#[derive(Debug, Clone, SpacetimeType)]
386#[sats(crate = crate)]
387#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
388#[non_exhaustive]
389pub enum RawConstraintDataV9 {
390    Unique(RawUniqueConstraintDataV9),
391}
392
393/// Requires that the projection of the table onto these `columns` is a bijection.
394///
395/// That is, there must be a one-to-one relationship between a row and the `columns` of that row.
396#[derive(Debug, Clone, SpacetimeType)]
397#[sats(crate = crate)]
398#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
399pub struct RawUniqueConstraintDataV9 {
400    /// The columns that must be unique.
401    pub columns: ColList,
402}
403
404/// Data for the `RLS` policy on a table.
405#[derive(Debug, Clone, PartialEq, Eq, SpacetimeType)]
406#[sats(crate = crate)]
407#[cfg_attr(feature = "test", derive(PartialOrd, Ord))]
408pub struct RawRowLevelSecurityDefV9 {
409    /// The `sql` expression to use for row-level security.
410    pub sql: RawSql,
411}
412
413/// A miscellaneous module export.
414///
415/// All of the variants here were added after the format of [`RawModuleDefV9`] was already stabilized.
416/// If/when we define `RawModuleDefV10`, these should allbe moved out of `misc_exports` and into their own fields.
417#[derive(Debug, Clone, SpacetimeType)]
418#[sats(crate = crate)]
419#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord, derive_more::From))]
420#[non_exhaustive]
421pub enum RawMiscModuleExportV9 {
422    /// A default value for a column added during a supervised automigration.
423    ColumnDefaultValue(RawColumnDefaultValueV9),
424    /// A procedure definition.
425    Procedure(RawProcedureDefV9),
426    /// A view definition.
427    View(RawViewDefV9),
428}
429
430/// Marks a particular table's column as having a particular default.
431#[derive(Debug, Clone, SpacetimeType)]
432#[sats(crate = crate)]
433#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
434pub struct RawColumnDefaultValueV9 {
435    /// Identifies which table that has the default value.
436    /// This corresponds to `name` in `RawTableDefV9`.
437    pub table: RawIdentifier,
438    /// Identifies which column of `table` that has the default value.
439    pub col_id: ColId,
440    /// A BSATN-encoded [`AlgebraicValue`] valid at the table column's type.
441    /// (We cannot use `AlgebraicValue` directly as it isn't `Spacetimetype`.)
442    pub value: Box<[u8]>,
443}
444
445/// A type declaration.
446///
447/// Exactly of these must be attached to every `Product` and `Sum` type used by a module.
448#[derive(Debug, Clone, SpacetimeType)]
449#[sats(crate = crate)]
450#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
451pub struct RawTypeDefV9 {
452    /// The name of the type declaration.
453    pub name: RawScopedTypeNameV9,
454
455    /// The type to which the declaration refers.
456    /// This must point to an `AlgebraicType::Product` or an `AlgebraicType::Sum` in the module's typespace.
457    pub ty: AlgebraicTypeRef,
458
459    /// Whether this type has a custom ordering.
460    pub custom_ordering: bool,
461}
462
463/// A scoped type name, in the form `scope0::scope1::...::scopeN::name`.
464///
465/// These are the names that will be used *in client code generation*, NOT the names used for types
466/// in the module source code.
467#[derive(Clone, SpacetimeType, PartialEq, Eq, PartialOrd, Ord)]
468#[sats(crate = crate)]
469pub struct RawScopedTypeNameV9 {
470    /// The scope for this type.
471    ///
472    /// Empty unless a sats `name` attribute is used, e.g.
473    /// `#[sats(name = "namespace.name")]` in Rust.
474    pub scope: Box<[RawIdentifier]>,
475
476    /// The name of the type. This must be unique within the module.
477    ///
478    /// Eventually, we may add more information to this, such as generic arguments.
479    pub name: RawIdentifier,
480}
481
482impl fmt::Debug for RawScopedTypeNameV9 {
483    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
484        for module in self.scope.iter() {
485            fmt::Debug::fmt(module, f)?;
486            f.write_str("::")?;
487        }
488        fmt::Debug::fmt(&self.name, f)?;
489        Ok(())
490    }
491}
492
493/// A view definition.
494#[derive(Debug, Clone, SpacetimeType)]
495#[sats(crate = crate)]
496#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
497pub struct RawViewDefV9 {
498    /// The name of the view function as defined in the module
499    pub name: RawIdentifier,
500
501    /// The index of the view in the module's list of views.
502    pub index: u32,
503
504    /// Is this a public or a private view?
505    /// Currently only public views are supported.
506    /// Private views may be supported in the future.
507    pub is_public: bool,
508
509    /// Is this view anonymous?
510    /// An anonymous view does not know who called it.
511    /// Specifically, it is a view that has an `AnonymousViewContext` as its first argument.
512    /// This type does not have access to the `Identity` of the caller.
513    pub is_anonymous: bool,
514
515    /// The types and optional names of the parameters, in order.
516    /// This `ProductType` need not be registered in the typespace.
517    pub params: ProductType,
518
519    /// The return type of the view.
520    /// Either `T`, `Option<T>`, or `Vec<T>` where `T` is a `SpacetimeType`.
521    ///
522    /// More strictly `T` must be a SATS `ProductType`,
523    /// however this will be validated by the server on publish.
524    ///
525    /// This is the single source of truth for the views's columns.
526    /// All elements of the inner `ProductType` must have names.
527    /// This again will be validated by the server on publish.
528    pub return_type: AlgebraicType,
529}
530
531/// A reducer definition.
532#[derive(Debug, Clone, SpacetimeType)]
533#[sats(crate = crate)]
534#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
535pub struct RawReducerDefV9 {
536    /// The name of the reducer.
537    pub name: RawIdentifier,
538
539    /// The types and optional names of the parameters, in order.
540    /// This `ProductType` need not be registered in the typespace.
541    pub params: ProductType,
542
543    /// If the reducer has a special role in the module lifecycle, it should be marked here.
544    pub lifecycle: Option<Lifecycle>,
545}
546
547/// Special roles a reducer can play in the module lifecycle.
548#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, SpacetimeType)]
549#[cfg_attr(feature = "enum-map", derive(enum_map::Enum))]
550#[sats(crate = crate)]
551#[non_exhaustive]
552pub enum Lifecycle {
553    /// The reducer will be invoked upon module initialization.
554    Init,
555    /// The reducer will be invoked when a client connects.
556    OnConnect,
557    /// The reducer will be invoked when a client disconnects.
558    OnDisconnect,
559}
560
561/// A procedure definition.
562///
563/// Will be wrapped in [`RawMiscModuleExportV9`] and included in the [`RawModuleDefV9`]'s `misc_exports` vec.
564#[derive(Debug, Clone, SpacetimeType)]
565#[sats(crate = crate)]
566#[cfg_attr(feature = "test", derive(PartialEq, Eq, PartialOrd, Ord))]
567pub struct RawProcedureDefV9 {
568    /// The name of the procedure.
569    pub name: RawIdentifier,
570
571    /// The types and optional names of the parameters, in order.
572    /// This `ProductType` need not be registered in the typespace.
573    pub params: ProductType,
574
575    /// The type of the return value.
576    ///
577    /// If this is a user-defined product or sum type,
578    /// it should be registered in the typespace and indirected through an [`AlgebraicType::Ref`].
579    pub return_type: AlgebraicType,
580}
581
582/// A builder for a [`RawModuleDefV9`].
583#[derive(Default)]
584pub struct RawModuleDefV9Builder {
585    /// The module definition.
586    module: RawModuleDefV9,
587    /// The type map from `T: 'static` Rust types to sats types.
588    type_map: BTreeMap<TypeId, AlgebraicTypeRef>,
589}
590
591impl RawModuleDefV9Builder {
592    /// Create a new, empty `RawModuleDefBuilder`.
593    pub fn new() -> Self {
594        Default::default()
595    }
596
597    /// Add a type to the in-progress module.
598    ///
599    /// The returned type must satisfy `AlgebraicType::is_valid_for_client_type_definition` or  `AlgebraicType::is_valid_for_client_type_use` .
600    pub fn add_type<T: SpacetimeType>(&mut self) -> AlgebraicType {
601        TypespaceBuilder::add_type::<T>(self)
602    }
603
604    /// Create a table builder.
605    ///
606    /// Does not validate that the product_type_ref is valid; this is left to the module validation code.
607    pub fn build_table(
608        &mut self,
609        name: impl Into<RawIdentifier>,
610        product_type_ref: AlgebraicTypeRef,
611    ) -> RawTableDefBuilder<'_> {
612        let name = name.into();
613        RawTableDefBuilder {
614            module_def: &mut self.module,
615            table: RawTableDefV9 {
616                name,
617                product_type_ref,
618                indexes: vec![],
619                constraints: vec![],
620                sequences: vec![],
621                schedule: None,
622                primary_key: ColList::empty(),
623                table_type: TableType::User,
624                table_access: TableAccess::Public,
625            },
626        }
627    }
628
629    /// Build a new table with a product type.
630    /// Adds the type to the module.
631    pub fn build_table_with_new_type(
632        &mut self,
633        table_name: impl Into<RawIdentifier>,
634        product_type: impl Into<spacetimedb_sats::ProductType>,
635        custom_ordering: bool,
636    ) -> RawTableDefBuilder<'_> {
637        let table_name = table_name.into();
638
639        let product_type_ref = self.add_algebraic_type(
640            [],
641            table_name.clone(),
642            AlgebraicType::from(product_type.into()),
643            custom_ordering,
644        );
645
646        self.build_table(table_name, product_type_ref)
647    }
648
649    /// Build a new table with a product type, for testing.
650    /// Adds the type to the module.
651    pub fn build_table_with_new_type_for_tests(
652        &mut self,
653        table_name: impl Into<RawIdentifier>,
654        mut product_type: spacetimedb_sats::ProductType,
655        custom_ordering: bool,
656    ) -> RawTableDefBuilder<'_> {
657        self.add_expand_product_type_for_tests(&mut 0, &mut product_type);
658
659        self.build_table_with_new_type(table_name, product_type, custom_ordering)
660    }
661
662    fn add_expand_type_for_tests(&mut self, name_gen: &mut usize, ty: &mut AlgebraicType) {
663        if ty.is_valid_for_client_type_use() {
664            return;
665        }
666
667        match ty {
668            AlgebraicType::Product(prod_ty) => self.add_expand_product_type_for_tests(name_gen, prod_ty),
669            AlgebraicType::Sum(sum_type) => {
670                if let Some(wrapped) = sum_type.as_option_mut() {
671                    self.add_expand_type_for_tests(name_gen, wrapped);
672                } else {
673                    for elem in sum_type.variants.iter_mut() {
674                        self.add_expand_type_for_tests(name_gen, &mut elem.algebraic_type);
675                    }
676                }
677            }
678            AlgebraicType::Array(ty) => {
679                self.add_expand_type_for_tests(name_gen, &mut ty.elem_ty);
680                return;
681            }
682            _ => return,
683        }
684
685        // Make the type into a ref.
686        let name = *name_gen;
687        let add_ty = core::mem::replace(ty, AlgebraicType::U8);
688        *ty = AlgebraicType::Ref(self.add_algebraic_type([], format!("gen_{name}"), add_ty, true));
689        *name_gen += 1;
690    }
691
692    fn add_expand_product_type_for_tests(&mut self, name_gen: &mut usize, ty: &mut ProductType) {
693        for elem in ty.elements.iter_mut() {
694            self.add_expand_type_for_tests(name_gen, &mut elem.algebraic_type);
695        }
696    }
697
698    /// Add a type to the typespace, along with a type alias declaring its name.
699    /// This method should only be use for `AlgebraicType`s not corresponding to a Rust
700    /// type that implements `SpacetimeType`.
701    ///
702    /// Returns a reference to the newly-added type.
703    ///
704    /// NOT idempotent, calling this twice with the same name will cause errors during
705    /// validation.
706    ///
707    /// You must set `custom_ordering` if you're not using the default element ordering.
708    pub fn add_algebraic_type(
709        &mut self,
710        scope: impl IntoIterator<Item = RawIdentifier>,
711        name: impl Into<RawIdentifier>,
712        ty: spacetimedb_sats::AlgebraicType,
713        custom_ordering: bool,
714    ) -> AlgebraicTypeRef {
715        let ty = self.module.typespace.add(ty);
716        let scope = scope.into_iter().collect();
717        let name = name.into();
718        self.module.types.push(RawTypeDefV9 {
719            name: RawScopedTypeNameV9 { name, scope },
720            ty,
721            custom_ordering,
722        });
723        // We don't add a `TypeId` to `self.type_map`, because there may not be a corresponding Rust type! e.g. if we are randomly generating types in proptests.
724        ty
725    }
726
727    /// Add a reducer to the in-progress module.
728    /// Accepts a `ProductType` of reducer arguments for convenience.
729    /// The `ProductType` need not be registered in the typespace.
730    ///
731    /// Importantly, if the reducer's first argument is a `ReducerContext`, that
732    /// information should not be provided to this method.
733    /// That is an implementation detail handled by the module bindings and can be ignored.
734    /// As far as the module definition is concerned, the reducer's arguments
735    /// start with the first non-`ReducerContext` argument.
736    ///
737    /// (It is impossible, with the current implementation of `ReducerContext`, to
738    /// have more than one `ReducerContext` argument, at least in Rust.
739    /// This is because `SpacetimeType` is not implemented for `ReducerContext`,
740    /// so it can never act like an ordinary argument.)
741    pub fn add_reducer(
742        &mut self,
743        name: impl Into<RawIdentifier>,
744        params: spacetimedb_sats::ProductType,
745        lifecycle: Option<Lifecycle>,
746    ) {
747        self.module.reducers.push(RawReducerDefV9 {
748            name: name.into(),
749            params,
750            lifecycle,
751        });
752    }
753
754    /// Add a procedure to the in-progress module.
755    ///
756    /// Accepts a `ProductType` of arguments.
757    /// The arguments `ProductType` need not be registered in the typespace.
758    ///
759    /// Also accepts an `AlgebraicType` return type.
760    /// If this is a user-defined product or sum type,
761    /// it should be registered in the typespace and indirected through an `AlgebraicType::Ref`.
762    ///
763    /// The `&mut ProcedureContext` first argument to the procedure should not be included in the `params`.
764    pub fn add_procedure(
765        &mut self,
766        name: impl Into<RawIdentifier>,
767        params: spacetimedb_sats::ProductType,
768        return_type: spacetimedb_sats::AlgebraicType,
769    ) {
770        self.module
771            .misc_exports
772            .push(RawMiscModuleExportV9::Procedure(RawProcedureDefV9 {
773                name: name.into(),
774                params,
775                return_type,
776            }))
777    }
778
779    pub fn add_view(
780        &mut self,
781        name: impl Into<RawIdentifier>,
782        index: usize,
783        is_public: bool,
784        is_anonymous: bool,
785        params: ProductType,
786        return_type: AlgebraicType,
787    ) {
788        self.module.misc_exports.push(RawMiscModuleExportV9::View(RawViewDefV9 {
789            name: name.into(),
790            index: index as u32,
791            is_public,
792            is_anonymous,
793            params,
794            return_type,
795        }));
796    }
797
798    /// Add a row-level security policy to the module.
799    ///
800    /// The `sql` expression should be a valid SQL expression that will be used to filter rows.
801    ///
802    /// **NOTE**: The `sql` expression must be unique within the module.
803    pub fn add_row_level_security(&mut self, sql: &str) {
804        self.module
805            .row_level_security
806            .push(RawRowLevelSecurityDefV9 { sql: sql.into() });
807    }
808
809    /// Get the typespace of the module.
810    pub fn typespace(&self) -> &Typespace {
811        &self.module.typespace
812    }
813
814    /// Finish building, consuming the builder and returning the module.
815    /// The module should be validated before use.
816    pub fn finish(self) -> RawModuleDefV9 {
817        self.module
818    }
819}
820
821/// Convert a string from a sats type-name annotation like `#[sats(name = "namespace.name")]` to a `RawScopedTypeNameV9`.
822/// We split the input on the strings `"::"` and `"."` to split up module paths.
823///
824/// TODO(1.0): build namespacing directly into the bindings macros so that we don't need to do this.
825pub fn sats_name_to_scoped_name(sats_name: &str) -> RawScopedTypeNameV9 {
826    // We can't use `&[char]: Pattern` for `split` here because "::" is not a char :/
827    let mut scope: Vec<RawIdentifier> = sats_name.split("::").flat_map(|s| s.split('.')).map_into().collect();
828    // Unwrapping to "" will result in a validation error down the line, which is exactly what we want.
829    let name = scope.pop().unwrap_or_default();
830    RawScopedTypeNameV9 {
831        scope: scope.into(),
832        name,
833    }
834}
835
836impl TypespaceBuilder for RawModuleDefV9Builder {
837    fn add(
838        &mut self,
839        typeid: TypeId,
840        name: Option<&'static str>,
841        make_ty: impl FnOnce(&mut Self) -> AlgebraicType,
842    ) -> AlgebraicType {
843        let r = match self.type_map.entry(typeid) {
844            btree_map::Entry::Occupied(o) => *o.get(),
845            btree_map::Entry::Vacant(v) => {
846                // Bind a fresh alias to the unit type.
847                let slot_ref = self.module.typespace.add(AlgebraicType::unit());
848                // Relate `typeid -> fresh alias`.
849                v.insert(slot_ref);
850
851                // Alias provided? Relate `name -> slot_ref`.
852                if let Some(sats_name) = name {
853                    let name = sats_name_to_scoped_name(sats_name);
854
855                    self.module.types.push(RawTypeDefV9 {
856                        name,
857                        ty: slot_ref,
858                        // TODO(1.0): we need to update the `TypespaceBuilder` trait to include
859                        // a `custom_ordering` parameter.
860                        // For now, we assume all types have custom orderings, since the derive
861                        // macro doesn't know about the default ordering yet.
862                        custom_ordering: true,
863                    });
864                }
865
866                // Borrow of `v` has ended here, so we can now convince the borrow checker.
867                let ty = make_ty(self);
868                self.module.typespace[slot_ref] = ty;
869                slot_ref
870            }
871        };
872        AlgebraicType::Ref(r)
873    }
874}
875
876/// Builder for a `RawTableDef`.
877pub struct RawTableDefBuilder<'a> {
878    module_def: &'a mut RawModuleDefV9,
879    table: RawTableDefV9,
880}
881
882impl RawTableDefBuilder<'_> {
883    /// Sets the type of the table and return it.
884    ///
885    /// This is not about column algebraic types, but about whether the table
886    /// was created by the system or the user.
887    pub fn with_type(mut self, table_type: TableType) -> Self {
888        self.table.table_type = table_type;
889        self
890    }
891
892    /// Sets the access rights for the table and return it.
893    pub fn with_access(mut self, table_access: TableAccess) -> Self {
894        self.table.table_access = table_access;
895        self
896    }
897
898    /// Generates a [RawConstraintDefV9] using the supplied `columns`.
899    pub fn with_unique_constraint(mut self, columns: impl Into<ColList>) -> Self {
900        let columns = columns.into();
901        self.table.constraints.push(RawConstraintDefV9 {
902            name: None,
903            data: RawConstraintDataV9::Unique(RawUniqueConstraintDataV9 { columns }),
904        });
905        self
906    }
907
908    /// Adds a primary key to the table.
909    /// You must also add a unique constraint on the primary key column.
910    pub fn with_primary_key(mut self, column: impl Into<ColId>) -> Self {
911        self.table.primary_key = ColList::new(column.into());
912        self
913    }
914
915    /// Adds a primary key to the table, with corresponding unique constraint and sequence definitions.
916    /// You will also need to call [`Self::with_index`] to create an index on `column`.
917    pub fn with_auto_inc_primary_key(self, column: impl Into<ColId>) -> Self {
918        let column = column.into();
919        self.with_primary_key(column)
920            .with_unique_constraint(column)
921            .with_column_sequence(column)
922    }
923
924    /// Generates a [RawIndexDefV9] using the supplied `columns`.
925    pub fn with_index(mut self, algorithm: RawIndexAlgorithm, accessor_name: impl Into<RawIdentifier>) -> Self {
926        let accessor_name = accessor_name.into();
927
928        self.table.indexes.push(RawIndexDefV9 {
929            name: None,
930            accessor_name: Some(accessor_name),
931            algorithm,
932        });
933        self
934    }
935
936    /// Generates a [RawIndexDefV9] using the supplied `columns` but with no `accessor_name`.
937    pub fn with_index_no_accessor_name(mut self, algorithm: RawIndexAlgorithm) -> Self {
938        self.table.indexes.push(RawIndexDefV9 {
939            name: None,
940            accessor_name: None,
941            algorithm,
942        });
943        self
944    }
945
946    /// Adds a [RawSequenceDefV9] on the supplied `column`.
947    pub fn with_column_sequence(mut self, column: impl Into<ColId>) -> Self {
948        let column = column.into();
949        self.table.sequences.push(RawSequenceDefV9 {
950            name: None,
951            column,
952            start: None,
953            min_value: None,
954            max_value: None,
955            increment: 1,
956        });
957
958        self
959    }
960
961    /// Adds a schedule definition to the table.
962    ///
963    /// The `function_name` should name a reducer or procedure
964    /// which accepts one argument, a row of this table.
965    ///
966    /// The table must have the appropriate columns for a scheduled table.
967    pub fn with_schedule(
968        mut self,
969        function_name: impl Into<RawIdentifier>,
970        scheduled_at_column: impl Into<ColId>,
971    ) -> Self {
972        let reducer_name = function_name.into();
973        let scheduled_at_column = scheduled_at_column.into();
974        self.table.schedule = Some(RawScheduleDefV9 {
975            name: None,
976            reducer_name,
977            scheduled_at_column,
978        });
979        self
980    }
981
982    /// Adds a default value for the `column`.
983    pub fn with_default_column_value(self, column: impl Into<ColId>, value: AlgebraicValue) -> Self {
984        // Added to `misc_exports` for backwards-compatibility reasons.
985        self.module_def
986            .misc_exports
987            .push(RawMiscModuleExportV9::ColumnDefaultValue(RawColumnDefaultValueV9 {
988                table: self.table.name.clone(),
989                col_id: column.into(),
990                value: spacetimedb_sats::bsatn::to_vec(&value).unwrap().into(),
991            }));
992        self
993    }
994
995    /// Build the table and add it to the module, returning the `product_type_ref` of the table.
996    pub fn finish(self) -> AlgebraicTypeRef {
997        self.table.product_type_ref
998        // self is now dropped.
999    }
1000
1001    /// Get the column ID of the column with the specified name, if any.
1002    ///
1003    /// Returns `None` if this `TableDef` has been constructed with an invalid `ProductTypeRef`,
1004    /// or if no column exists with that name.
1005    pub fn find_col_pos_by_name(&self, column: impl AsRef<str>) -> Option<ColId> {
1006        let column = column.as_ref();
1007        self.columns()?
1008            .iter()
1009            .position(|x| x.name().is_some_and(|s| s == column))
1010            .map(|x| x.into())
1011    }
1012
1013    /// Get the columns of this type.
1014    ///
1015    /// Returns `None` if this `TableDef` has been constructed with an invalid `ProductTypeRef`.
1016    fn columns(&self) -> Option<&[ProductTypeElement]> {
1017        self.module_def
1018            .typespace
1019            .get(self.table.product_type_ref)
1020            .and_then(|ty| ty.as_product())
1021            .map(|p| &p.elements[..])
1022    }
1023}
1024
1025impl Drop for RawTableDefBuilder<'_> {
1026    fn drop(&mut self) {
1027        self.module_def.tables.push(self.table.clone());
1028    }
1029}