zero-client 0.1.0

A native Rust client for Zero.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! A Zero schema's tables and columns (`packages/zero-types/src/schema.ts`),
//! and the names they map to on the server
//! (`packages/zero-types/src/name-mapper.ts`).

use crate::ast::*;
use crate::decode::{self, DecodeError, Reader, Result};
use crate::json::{Object, Value};
use crate::map::OrderedMap;
use std::collections::{HashMap, HashSet};
use std::fmt;

crate::ast::string_enum!(
    /// The type of a column value (`ValueType` in `schema-value.ts`).
    ValueType { String = "string", Number = "number", Boolean = "boolean", Null = "null", Json = "json" }
);

/// A column (`SchemaValue`).
#[derive(Clone, Debug, PartialEq)]
pub struct Column {
    pub type_: ValueType,
    pub optional: bool,
    pub server_name: Option<String>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Table {
    pub name: String,
    pub server_name: Option<String>,
    /// In declaration order.
    pub columns: OrderedMap<Column>,
    pub primary_key: Vec<String>,
}

crate::ast::string_enum!(
    /// How many rows a relationship hop leads to.
    Cardinality { One = "one", Many = "many" }
);

/// One hop of a relationship: from `source_field` of the table to
/// `dest_field` of `dest_schema`.
#[derive(Clone, Debug, PartialEq)]
pub struct RelationshipHop {
    pub source_field: Vec<String>,
    pub dest_field: Vec<String>,
    pub dest_schema: String,
    pub cardinality: Cardinality,
}

/// A relationship: one hop, or two through a junction table.
pub type Relationship = Vec<RelationshipHop>;

/// The tables of a schema and their relationships, in declaration order.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct Schema {
    pub tables: OrderedMap<Table>,
    /// By table, then by name.
    pub relationships: OrderedMap<OrderedMap<Relationship>>,
}

impl Schema {
    /// A schema in the JSON form JS's schema object has (`tables`, each with
    /// `name`, `serverName`, `columns`, and `primaryKey`, and optionally
    /// `relationships`).
    pub fn from_json(v: &Value) -> Result<Schema> {
        let r = Reader::new(v, "schema")?;
        let tables = r.required("tables", |v| {
            decode::record(v, |t| {
                let t = Reader::new(t, "table")?;
                Ok(Table {
                    name: t.string("name")?,
                    server_name: t.optional_string("serverName")?,
                    columns: t
                        .required("columns", |v| {
                            decode::record(v, |c| {
                                let c = Reader::new(c, "column")?;
                                let ty = c.string("type")?;
                                Ok(Column {
                                    type_: ValueType::parse(&ty).ok_or_else(|| {
                                        DecodeError::new(format!("Invalid type \"{ty}\""))
                                            .at("type")
                                    })?,
                                    optional: c.optional_bool("optional")?.unwrap_or(false),
                                    server_name: c.optional_string("serverName")?,
                                })
                            })
                        })?
                        .into_iter()
                        .collect(),
                    primary_key: t.required("primaryKey", decode::compound_key)?,
                })
            })
        })?;
        let relationships = r
            .optional("relationships", |v| {
                decode::record(v, |rels| {
                    Ok(
                        decode::record(rels, |hops| decode::array(hops, hop_from_json))?
                            .into_iter()
                            .collect::<OrderedMap<_>>(),
                    )
                })
            })?
            .unwrap_or_default();
        Ok(Schema {
            tables: tables.into_iter().collect(),
            relationships: relationships.into_iter().collect(),
        })
    }

    /// The schema zero-cache checks the client's against, with server names
    /// (`clientSchemaFrom` in `packages/zero-schema/src/builder/schema-builder.ts`).
    pub fn client_schema(&self) -> crate::protocol::ClientSchema {
        use crate::protocol::{ClientSchema, ClientTable};
        let tables = self
            .tables
            .iter()
            .map(|(name, t)| {
                let server = |c: &str| {
                    t.columns
                        .get(c)
                        .and_then(|c| c.server_name.clone())
                        .unwrap_or_else(|| c.to_owned())
                };
                let table = ClientTable {
                    columns: t
                        .columns
                        .iter()
                        .map(|(c, col)| (server(c), col.type_))
                        .collect(),
                    primary_key: t.primary_key.iter().map(|k| server(k)).collect(),
                };
                (t.server_name.clone().unwrap_or_else(|| name.clone()), table)
            })
            .collect();
        ClientSchema { tables }.normalized()
    }

    /// The relationship `name` of `table`.
    pub fn relationship(&self, table: &str, name: &str) -> Option<&Relationship> {
        self.relationships.get(table)?.get(name)
    }
}

fn hop_from_json(v: &Value) -> Result<RelationshipHop> {
    let h = Reader::new(v, "relationship")?;
    let cardinality = h.string("cardinality")?;
    Ok(RelationshipHop {
        source_field: h.required("sourceField", decode::strings)?,
        dest_field: h.required("destField", decode::strings)?,
        dest_schema: h.string("destSchema")?,
        cardinality: Cardinality::parse(&cardinality).ok_or_else(|| {
            DecodeError::new(format!("Invalid cardinality \"{cardinality}\"")).at("cardinality")
        })?,
    })
}

/// An unknown table or column met while mapping names.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NameMappingError(pub String);

impl fmt::Display for NameMappingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for NameMappingError {}

type MapResult<T> = std::result::Result<T, NameMappingError>;

struct Destination {
    table_name: String,
    columns: HashMap<String, String>,
    all_columns_same: bool,
    /// The source names of the table's `json` columns.
    json_columns: HashSet<String>,
}

/// Maps table and column names between the client and the server
/// (`NameMapper`).
pub struct NameMapper {
    tables: HashMap<String, Destination>,
}

impl NameMapper {
    /// `clientToServer`.
    pub fn client_to_server(schema: &Schema) -> Self {
        Self::new(schema, true)
    }

    /// `serverToClient`.
    pub fn server_to_client(schema: &Schema) -> Self {
        Self::new(schema, false)
    }

    fn new(schema: &Schema, from_client: bool) -> Self {
        let mut tables = HashMap::new();
        for (name, table) in &schema.tables {
            let mut all_columns_same = true;
            let mut columns = HashMap::new();
            let mut json_columns = HashSet::new();
            for (column, c) in &table.columns {
                let server = c.server_name.as_deref().unwrap_or(column);
                if server != column {
                    all_columns_same = false;
                }
                let (src, dst) = if from_client {
                    (column.as_str(), server)
                } else {
                    (server, column.as_str())
                };
                columns.insert(src.to_owned(), dst.to_owned());
                if c.type_ == ValueType::Json {
                    json_columns.insert(src.to_owned());
                }
            }
            let server_name = table.server_name.as_deref().unwrap_or(name);
            let (src, dst) = if from_client {
                (name.as_str(), server_name)
            } else {
                (server_name, name.as_str())
            };
            tables.insert(
                src.to_owned(),
                Destination {
                    table_name: dst.to_owned(),
                    columns,
                    all_columns_same,
                    json_columns,
                },
            );
        }
        NameMapper { tables }
    }

    fn table(&self, src: &str) -> MapResult<&Destination> {
        self.tables
            .get(src)
            .ok_or_else(|| NameMappingError(format!("unknown table \"{src}\"")))
    }

    pub fn table_name(&self, src: &str) -> MapResult<String> {
        Ok(self.table(src)?.table_name.clone())
    }

    pub fn column_name(&self, table: &str, src: &str) -> MapResult<String> {
        self.table(table)?.columns.get(src).cloned().ok_or_else(|| {
            NameMappingError(format!("unknown column \"{src}\" of \"{table}\" table"))
        })
    }

    /// `jsonColumnName`: the column a JSON path reads, which must be a `json`
    /// column. An unknown column fails the same way as a known one that
    /// isn't json.
    pub fn json_column_name(&self, table: &str, src: &str) -> MapResult<String> {
        if !self.table(table)?.json_columns.contains(src) {
            return Err(NameMappingError(format!(
                "column \"{src}\" of \"{table}\" table is not a json column"
            )));
        }
        self.column_name(table, src)
    }

    /// The mapped name of table `src`, or `None` for a table the schema
    /// doesn't have.
    pub fn table_name_if_known(&self, src: &str) -> Option<String> {
        self.tables.get(src).map(|t| t.table_name.clone())
    }

    /// Renames the known columns of `table`; others keep their names.
    pub fn columns(&self, table: &str, columns: &[String]) -> MapResult<Vec<String>> {
        let dest = self.table(table)?;
        Ok(columns
            .iter()
            .map(|c| dest.columns.get(c).unwrap_or(c).clone())
            .collect())
    }

    fn column_names(&self, table: &str, columns: &[String]) -> MapResult<Vec<String>> {
        columns.iter().map(|c| self.column_name(table, c)).collect()
    }

    /// Renames the known columns of `row`; others keep their names.
    pub fn row(&self, table: &str, row: &Object) -> MapResult<Object> {
        let dest = self.table(table)?;
        if dest.all_columns_same {
            return Ok(row.clone());
        }
        Ok(row
            .iter()
            .map(|(c, v)| (dest.columns.get(c).unwrap_or(c).clone(), v.clone()))
            .collect())
    }

    /// `mapAST`: the AST with its table and column names mapped.
    pub fn map(&self, ast: &Ast) -> MapResult<Ast> {
        let mut out = ast.clone();
        out.table = self.table_name(&ast.table)?;
        out.where_ = ast
            .where_
            .as_ref()
            .map(|w| self.map_condition(w, &ast.table))
            .transpose()?;
        if let Some(related) = &ast.related {
            let mut mapped = Vec::with_capacity(related.len());
            for r in related {
                let mut r2 = r.clone();
                r2.correlation =
                    self.map_correlation(&r.correlation, &ast.table, &r.subquery.table)?;
                r2.subquery = Box::new(self.map(&r.subquery)?);
                // A junction aggregate's field is a column of the destination.
                let field_table = r
                    .subquery
                    .related
                    .as_ref()
                    .and_then(|rel| rel.first())
                    .map_or(&r.subquery.table, |j| &j.subquery.table);
                r2.aggregate = r
                    .aggregate
                    .as_ref()
                    .map(|a| self.map_aggregate(a, field_table))
                    .transpose()?;
                mapped.push(r2);
            }
            out.related = Some(mapped);
        }
        if let Some(start) = &ast.start {
            let mut row = Object::new();
            for (column, value) in &start.row {
                row.insert(self.column_name(&ast.table, column)?, value.clone());
            }
            out.start = Some(Bound {
                row,
                exclusive: start.exclusive,
            });
        }
        if let Some(order_by) = &ast.order_by {
            out.order_by = Some(
                order_by
                    .iter()
                    .map(|p| {
                        Ok(OrderPart {
                            field: self.column_name(&ast.table, &p.field)?,
                            direction: p.direction,
                        })
                    })
                    .collect::<MapResult<_>>()?,
            );
        }
        out.aggregate = ast
            .aggregate
            .as_ref()
            .map(|a| self.map_aggregate(a, &ast.table))
            .transpose()?;
        out.group_by = ast
            .group_by
            .as_ref()
            .map(|g| self.column_names(&ast.table, g))
            .transpose()?;
        Ok(out)
    }

    /// `transformAggregate`: the field maps, and must be a `json` column when
    /// there is a path; the path is data.
    fn map_aggregate(&self, a: &Aggregate, table: &str) -> MapResult<Aggregate> {
        let mut out = Aggregate::new(a.fn_);
        if let Some(distinct) = &a.distinct {
            out.distinct = Some(self.column_names(table, distinct)?);
        } else if let Some(field) = &a.field {
            match &a.path {
                None => out.field = Some(self.column_name(table, field)?),
                Some(path) => {
                    out.field = Some(self.json_column_name(table, field)?);
                    out.path = Some(path.clone());
                }
            }
        }
        Ok(out)
    }

    /// `mapCondition`. Only the left side of a comparison names a column.
    pub fn map_condition(&self, condition: &Condition, table: &str) -> MapResult<Condition> {
        Ok(match condition {
            Condition::Simple { op, left, right } => {
                let left = match left {
                    ValuePosition::Column(name) => {
                        ValuePosition::Column(self.column_name(table, name)?)
                    }
                    ValuePosition::Json { column, path } => ValuePosition::Json {
                        column: self.json_column_name(table, column)?,
                        path: path.clone(),
                    },
                    _ => return Ok(condition.clone()),
                };
                Condition::Simple {
                    op: *op,
                    left,
                    right: right.clone(),
                }
            }
            Condition::CorrelatedSubquery {
                related,
                op,
                flip,
                scalar,
            } => {
                let mut related = related.clone();
                related.correlation =
                    self.map_correlation(&related.correlation, table, &related.subquery.table)?;
                related.subquery = Box::new(self.map(&related.subquery)?);
                Condition::CorrelatedSubquery {
                    related,
                    op: *op,
                    flip: *flip,
                    scalar: *scalar,
                }
            }
            Condition::And(c) => Condition::And(
                c.iter()
                    .map(|c| self.map_condition(c, table))
                    .collect::<MapResult<_>>()?,
            ),
            Condition::Or(c) => Condition::Or(
                c.iter()
                    .map(|c| self.map_condition(c, table))
                    .collect::<MapResult<_>>()?,
            ),
        })
    }

    fn map_correlation(
        &self,
        c: &Correlation,
        parent: &str,
        child: &str,
    ) -> MapResult<Correlation> {
        Ok(Correlation {
            parent_field: self.column_names(parent, &c.parent_field)?,
            child_field: self.column_names(child, &c.child_field)?,
        })
    }
}