Defining the Data Model
Once you know the use cases that you have to support for your application and what you want it to look like, I find a good next step would is to dig into the data structures that you need and see if the data can support the use cases.
Getting started
This process has 3 general steps:
- Identify the Domain Objects in your app
- Outline the names and properties of the different Domain Objects
- Structure the data as a JSON object and use some code to see if it works for your use case.
Identify Domain Objects
A domain object refers to a group of data that your application needs to interact with.
For example:
- Trello - Boards, Lists, and Cards
- Wordpress - Blog, Posts, Pages
- Google Slides - Slides, Slide, Content
As you look at the use cases for your application, try to identify the types/groups of data that you need.
Outlining a Domain Object
Once you know the name of the groups of data that you need, you can start giving them some properties.
Back to the Trello example:
- A Board has a name, and can have multiple Lists
- Lists can have a name, and can have multiple Cards
- Cards have titles, descriptions, authors, timestamps
Structure the data as JSON
Here's an example of one way of describing the Trello domain objects. I've described:
- a CS 5356 To Do Board
- with 3 lists - "Not Started", "Doing", "Done"
- and one Card titled "Milestone 1" in the "Not Started" list
{
"board": {
"name": "CS 5356 To Do",
"lists": [{
"name": "Not Started",
"cards": [{
"title": "Milestone 1",
"description": "",
"author": "da335",
"timestamps": "2023-02-01"
}]
}, {
"name": "Doing",
"cards": []
}, {
"name": "Done",
"cards": []
}]
}
}
Once we have a prototype of the data for our app, we can start writing some code to build towards it. This step is the same whether you're working on a relational database like MySQL, or a non-relational database like Firebase Firestore.
Let's walk through an example where you can build that structure out yourself.
Example: A Chat Application like WhatsApp
Let's start by taking a look at an example - a chat application.
Here are 3 user stories that we would like to support in our chat app:
- As a user, I can join and view chat rooms that I’m a part of
- As a member of a chat room, I can send and receive messages
- As a user, I can view members of a chat room that I’m a part of
1. Identify Domain Objects
What might you consider to be the bits of data that you need to work with? At this stage, you can describe in plain english.
View an example
Users, User Names, Chat Room Names, and Messages, Message Timestamps, Message Content
2. Outline the data you may need
For every Domain Object, outline the properties you may need. At this stage, theres no such thing as a wrong answer - the point of the exercise is to consider what information you may need. You'll find out later on if this works or not.
View an Example
3. Structure the data as JSON
Based on your description above, try to describe it as JSON. Here are some guidelines to help you start:
- Start with an empty JSON object. This will represent your entire database:
{}
- Add a Property for every Domain Object you may have. You can imagine that the equivalent of this could be a table in a SQL database, a Collection in a NoSQL database, or even a Sheet in a Spreadsheet.
{
"users": [],
"chatrooms": [],
"messages": []
}
- Create a User by adding a
user
to theusers
array -{"name": "da335"}
{
"users": [{
"name": "da335"
}],
...
}
- Add a
chatroom
by creating achatroom
object within thechatrooms
array -{ "name": "class-channel" }
{
"users": [{
"name": "da335"
}],
"chatrooms": [{
"name": "class-channel",
...
}]
}
- Send a
message
by creating amessage
object within themessages
array -{ "content": "hello!" }
{
"users": [{
"name": "da335"
}],
"chatrooms": [{
"name": "class-channel",
...
}],
"messages": [{
"content": "hello!",
...
}]
}
4. Define the Relationships
For our use case, Chatrooms can have many messages. This is called a One-To-Many relationship. You can model this relationship in 2 ways:
A) Make a chatroom
contain the messages
array
{
"chatrooms": [{
"name": "class-channel",
"messages": [{
"content": "hello!"
...
}]
}]
}
- B) Add a
chatroomName
tomessage
to serve as a pointer for the chatroom where the message was asked.
{
"messages": [{
"content": "hello!",
"chatroomName": "class-channel",
...
}]
}
Either one is perfectly valid and is possible in either Relational/NoSQL databases. It depends on how you want to save your data.
View a finished example
{
"users": [{
"name": "da335"
}],
"chatrooms": [{
"name": "2023-cs-5356-class",
"members": ["da335"],
"messages": [{
"content": "hello world!",
"name": "da335",
"timestamp": "2023-02-01"
}]
}]
}