Schema and Datatypes

Schema

Now that we learned all about data types, let's learn how to create your first Table.
In this example, we will be creating a schema for a Social Media app. Let's get started!

Plan

  1. Identify the main entities for the Social Media app (e.g., User, Post, Comment).
  2. Define the fields and data types for each entity.
  3. Establish relationships between tables (e.g., a User can have many Posts, a Post can have many Comments).
  4. Write the schema definition for each table.

Code

typescript

typescript

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

Our Schema is almost ready! we just need to define the relations between the fields so it suits the logic of our app
In the Posts table we defined a UserID field that is suposed to be a referance to one of the users in the Users table, lets make the UserID referance the ID in the Users Table

typescript

typescript

1

2

3

This meens that every user can have multiple Posts linked to it
We can later Query our data with `Extend` option and OptimaDB automaticly gets the referanced posts and joins them
They later can be accessible in the user object using the $Posts key

typescript

typescript

1

2

3

4

terminal

Terminal

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

Every field in the result, including the $Posts key, is fully type-safe.
OptimaDB also ensures that you can't make mistakes with the Extend option: it's type-safe and only allows you to extend tables that actually reference the Users table.
You can learn all about Relations in Optima in the next section