Data Structures and Types
Each document is nested with a hash-stable tree structure that self-describes the data to be stored and provides the predetermined rules that ensure data consistency and accuracy.
The following snippet provides an example of a basic JSON-encoded document object:
A single document consists of one or more fields that self‑describe the data it encodes. Each field is associated with a value:
Ditto automatically generates and assigns each new document a unique identifier, or _id. However, if desired, you can pass your own custom _id as a parameter when performing an INSERT operation to create a new document. (See CREATE)
In addition to having the option to supply your own _id, in complex scenarios where you want to create a more intricate and unique identifier for your documents, you can combine two or more distinct elements to form a composite key.
For example, the following snippet demonstrates how to create (INSERT) a new document with a composite key formed by the vin, make, and year fields — all of which remain immutable. So, once set at initial document creation, their values cannot be changed.
As a semi-structured database, data formatting in Ditto is categorized into two main groups:
- Data Types — Advanced types that guarantee conflict-free resolution during merging, which includes the REGISTER, MAP, and ATTACHMENT data type. The default data type is REGISTER ; you'll use other data types in specific scenarios where appropriate.
- Scalar subtypes — Basic primitive types, such as string and boolean, and the JSON object, which functions as a single value capable of encapsulating multiple key-value pairs.
To improve performance, instead of storing a file that encodes large amounts of binary data within a document, consider storing a reference to it in a separate, explicitly fetched object (token) known as an ATTACHMENT.
With the ATTACHMENT data type, you can implement lazy loading. Lazy loading is when you delay retrieval until necessary rather than aggressively fetching the data in anticipation of hypothetical future use. This "on-demand" retrieval pattern enhances performance by optimizing resource usage.
The following table provides a complete overview of the relationships you can establish in Ditto:
Associates a parent element with children elements to establish a hierarchy.
Associates multiple entities in one collection with multiple entities in another collection.
Associates two or more collections, where one collection refers to the primary key of another collection to create a meaningful relationship between the datasets.