Data Modeling
Introduction
Before diving into the document schemas, let’s take a look again at the mobile app. When you ran the mobile app in the previous lesson, you may remember two different options were presented on the login screen:
-
Sync mode: with the user credentials provided, this user can synchronize documents with Couchbase Server and, in turn, with other users.
-
Guest / Non Sync mode: no user credentials are required for this option. This is a local only mode. Documents created in the local database are not synchronized to the Sync Gateway.
Throughout the next lessons, you will be switching between those two modes to test different functionalities. It’s worth noting that the data model is slightly different between those two modes. Let’s review the data model for each one.
Sync mode
Here, the application communicates with Couchbase Server (via Sync Gateway). The documents stored in the Couchbase Server bucket have the following types:
-
airline
-
airport
-
hotel
-
route
-
user
Except for the user document (right on the diagram below), the rest of documents are static/semi-static in nature. In the demo app,
-
The
hotel
andairport
documents are bundled into a prebuilt Couchbase Lite database and loaded when the app launches. We will discuss more about locating and using prebuilt databases in the Pre-built Database section of the tutorial. -
The
airline
,airport
,hotel`and `route
documents are NOT synchronized to the mobile app. Instead, they are fetched by the app via the REST webservices API exposed by the Travel Sample Web backend.Figure 2. Data model diagram
Guest/Non Sync mode
In guest mode, the mobile app creates a new database for the anonymous user. It is an empty database for storing the list of bookmarked hotels locally.
It is conceivable that in a real-world application, a user of the Travel Sample Mobile app may be interested in browsing for hotels in specific locations meeting specific search criteria without having to actually sign up. They could bookmark these hotels and later add them to their trip reservations. These bookmarked hotels could also be shared with other users, for instance with the user making the trip bookings.
In guest mode, the Couchbase Lite database hosts the following types of documents:
-
bookmarkedhotels
-
hotel
Document Types
Unlike tables, in Couchbase, all the documents are stored in the same namespace. So you typically use an additional property to differentiate between each entity. Let’s call it "type".
Document Key/ID
Every document in Couchbase is associated with a unique key that must be provided by the user when the document is created.
The key is the unique identifier of the document and can take any format.
However, it is recommended that you give it a value that provides some context about the contents of the document.
For instance, in the travel app data set, the document Key/ID is of the format {doc.type}_{alphanumeric_string}
.
Here {doc.type}
provides some context of the purpose of the document and in combination with the {alphanumeric_string}
, it uniquely identifies the string.
The document Key will be listed as “ID” on Couchbase Server admin console.
The key is also referred to as the Document ID.
Document _id
When Sync Gateway processes a document, it adds relevant metadata to the document.
The metadata includes an _id
property that corresponds to the document ID.
You can see this property if you query for the document via the the SyncGateway REST API.
{
"_id": "airline_137",
"_rev": "1-b4e60280a1a0e3d46efad7bfd0e2068c",
"callsign": "AIRFRANS",
"country": "France",
"iata": "AF",
"icao": "AFR",
"id": 137,
"name": "Air France",
"type": "airline"
}
Mobile App Developers using Couchbase Lite should typically never have to directly read or write the _id property. You would query the meta().id field to fetch the document ID. We will learn more about this in our lesson on Queries.
We will learn more about this in the Query lesson.