Search...
ctrl/
Light
Dark
System
Sign in
Environment:

Adding shared properties

One common pattern in applications is to add shared properties to the schema that are used by multiple objects. For example, you might want to add a created_at and updated_at property to every object in your schema. You can do this by adding an abstract type and using it as a mixin for your other object types.

dbschema/default.gel
Copy
module default {
  abstract type Timestamped {
    required created_at: datetime {
      default := datetime_of_statement();
    };
    required updated_at: datetime {
      default := datetime_of_statement();
    };
  }

  type Deck {
  type Deck extending Timestamped {
    required name: str;
    description: str;

Show 3 hidden lines...
    );
  };

  type Card {
  type Card extending Timestamped {
    required order: int64;
    required front: str;
    required back: str;
Show 4 hidden lines...

Since you don't have historical data for when these objects were actually created or modified, the migration will fall back to the default values set in the Timestamped type.

Copy
$ 
gel migration create
did you create object type 'default::Timestamped'? [y,n,l,c,b,s,q,?]
> y
did you alter object type 'default::Card'? [y,n,l,c,b,s,q,?]
> y
did you alter object type 'default::Deck'? [y,n,l,c,b,s,q,?]
> y
Created /home/strinh/projects/flashcards/dbschema/migrations/00004-m1d2m5n.edgeql, id: m1d2m5n5ajkalyijrxdliioyginonqbtfzihvwdfdmfwodunszstya
Copy
$ 
gel migrate
Applying m1d2m5n5ajkalyijrxdliioyginonqbtfzihvwdfdmfwodunszstya (00004-m1d2m5n.edgeql)
... parsed
... applied

Update the get_decks query to sort the decks by updated_at in descending order.

main.py
Copy
Show 13 hidden lines...
                order by .order
            )
        }
        order by .updated_at desc
    """)
    return decks