Skip to content
Strata is pre-1.0. APIs, protocol, and storage format may change.

strata_document/schema

Portable phantom-typed fields and whole-document record schemas.

A Field(tag, value) can only be used with a Document(tag). Record schemas derive their decoder and per-key writes from the same Prop declarations, preventing the two directions from drifting apart.

A typed key with its JSON encoder and decoder.

pub type Field(a, b)

The reason a typed field or record read failed.

pub type FieldError {
Missing(key: String)
Invalid(reason: json.DecodeError)
UnknownKeys(keys: List(String))
SchemaMismatch(expected: Int, found: Int)
WrongValueType(key: String, found: String)
ReservedKey(key: String)
}

A required field was absent.

Stored JSON was malformed or did not have the expected type.

A sealed schema encountered undeclared keys.

A versioned schema encountered a different stamped version.

WrongValueType(key: String, found: String)

Section titled “WrongValueType(key: String, found: String)”

A document entry did not contain an LWW register.

A field attempted to use the document's reserved clock key.

One record property, pairing a field with its record getter.

pub type Prop(a, b, c)

A bidirectional whole-record codec for a typed document.

pub type Schema(a, b)

Errors returned while configuring a schema.

pub type SchemaError {
SealedKnownRequiresDeclaredKeys
}

sealed_known needs a record1 through record9 schema so the declared keys are available.

One per-key operation produced by a whole-record encoder.

pub type WriteOp {
Put(key: String, value: json.Json)
Delete(key: String)
}

Store a JSON value.

Remove a key.

The reserved key used by versioned schemas.

pub const version_key: String

Decode a whole record from active document entries.

pub fn decode_entries(
Schema(a, b),
List(#(String, json.Json))
) -> Result(b, FieldError)

Decode an optional field value.

pub fn decode_optional(
Field(a, b),
option.Option(json.Json)
) -> Result(option.Option(b), FieldError)

Decode a field directly from its JSON-string storage representation.

pub fn decode_string(
Field(a, b),
String
) -> Result(b, FieldError)

Decode a field value from JSON.

pub fn decode_value(
Field(a, b),
json.Json
) -> Result(b, FieldError)

Encode a whole record as independent per-key operations.

pub fn encode_ops(
Schema(a, b),
b
) -> List(WriteOp)

Encode a field value as JSON.

pub fn encode_value(
Field(a, b),
b
) -> json.Json

Define a typed field.

pub fn field(
String,
fn(a) -> json.Json,
decode.Decoder(a)
) -> Field(b, a)

Return a field's storage key.

pub fn field_key(Field(a, b)) -> String

Define an optional record property.

None decodes from an absent key or JSON null and encodes as Delete.

pub fn optional_prop(
Field(a, b),
fn(c) -> option.Option(b)
) -> Prop(a, c, option.Option(b))

Define a required record property.

pub fn prop(
Field(a, b),
fn(c) -> b
) -> Prop(a, c, b)

Build a one-property record schema.

pub fn record1(
fn(a) -> b,
Prop(c, b, a)
) -> Schema(c, b)

Build a two-property record schema.

pub fn record2(
fn(a, b) -> c,
Prop(d, c, a),
Prop(d, c, b)
) -> Schema(d, c)

Build a three-property record schema.

pub fn record3(
fn(a, b, c) -> d,
Prop(e, d, a),
Prop(e, d, b),
Prop(e, d, c)
) -> Schema(e, d)

Build a four-property record schema.

pub fn record4(
fn(a, b, c, d) -> e,
Prop(f, e, a),
Prop(f, e, b),
Prop(f, e, c),
Prop(f, e, d)
) -> Schema(f, e)

Build a five-property record schema.

pub fn record5(
fn(a, b, c, d, e) -> f,
Prop(g, f, a),
Prop(g, f, b),
Prop(g, f, c),
Prop(g, f, d),
Prop(g, f, e)
) -> Schema(g, f)

Build a six-property record schema.

pub fn record6(
fn(a, b, c, d, e, f) -> g,
Prop(h, g, a),
Prop(h, g, b),
Prop(h, g, c),
Prop(h, g, d),
Prop(h, g, e),
Prop(h, g, f)
) -> Schema(h, g)

Build a seven-property record schema.

pub fn record7(
fn(a, b, c, d, e, f, g) -> h,
Prop(i, h, a),
Prop(i, h, b),
Prop(i, h, c),
Prop(i, h, d),
Prop(i, h, e),
Prop(i, h, f),
Prop(i, h, g)
) -> Schema(i, h)

Build an eight-property record schema.

pub fn record8(
fn(a, b, c, d, e, f, g, h) -> i,
Prop(j, i, a),
Prop(j, i, b),
Prop(j, i, c),
Prop(j, i, d),
Prop(j, i, e),
Prop(j, i, f),
Prop(j, i, g),
Prop(j, i, h)
) -> Schema(j, i)

Build a nine-property record schema.

pub fn record9(
fn(a, b, c, d, e, f, g, h, i) -> j,
Prop(k, j, a),
Prop(k, j, b),
Prop(k, j, c),
Prop(k, j, d),
Prop(k, j, e),
Prop(k, j, f),
Prop(k, j, g),
Prop(k, j, h),
Prop(k, j, i)
) -> Schema(k, j)

Define an open, unversioned schema from hand-written codecs.

pub fn schema(
decode.Decoder(a),
fn(a) -> List(#(String, json.Json))
) -> Schema(b, a)

Restrict a schema to the supplied keys.

The reserved version_key remains permitted.

pub fn sealed(
Schema(a, b),
List(String)
) -> Schema(a, b)

Seal a record1 through record9 schema to its declared props.

Returns Error(SealedKnownRequiresDeclaredKeys) for a hand-written schema that has no declared keys; use sealed(schema, keys) there.

pub fn sealed_known(Schema(a, b)) -> Result(Schema(a, b), SchemaError)

Return the version entry to write, if this schema is versioned.

pub fn stamp_entry(Schema(a, b)) -> option.Option(#(String, json.Json))

Attach an integer version checked by reads and written by stamp.

pub fn versioned(
Schema(a, b),
Int
) -> Schema(a, b)