Hello and welcome to modeling data basics with OutSystems. Before we move to
what is modeling data, let's first understand what is a data type. All of
the information inside an application is called data and every single piece of
information, every single piece of data is of a certain data type, that dictates what
kind of operations you can carry out with that piece of data. Now the
OutSystems language is what you'd call a strongly typed language
this means that when you create a variable of a certain type you need to
stick to that type for the duration of the life of that variable. It is
important to note that the OutSystems language is not object oriented like C#
or Java or C++, this means that there is no type inheritance and certain
types of notation that you come to expect from these object-oriented
languages do not exist in the OutSystems language. In this slide you can
see some of the basic data types that the OutSystems language supports, you
have texts, you have integers, booleans and date-time related variable types and
you can see here some of the default values that variables take up to
the point that you have assigned something to them.
So now that we know about data types, what is data modeling? Data modeling is
the process of mapping concepts that you have in your application into data types.
Now, the majority of concepts are actually complex, they are made up of
several items of basic data types, these complex types are what we call the
compound types and the OutSystems platform has three kinds of compound
types that you can use. They're called the entities, the static entities and the
structures. An entity is the most common data construct in the OutSystems
platform, you normally have an entity for each concept of your application so a
customer and order, or an order item will very likely end up as three distinct
entities. An important thing
to know is that an entity is always persisted in the database table for you, every
single record that you create, every single customer order that you create
will end up as a new row in the respective table.
You don't have to worry about maintaining the database representations
of entities, because the platform does this for you. An entity in the OutSystems
platform is actually made up of several fields called attributes, where your
actual data is stored and it's also supported by system create, read, update,
and delete actions that are provided for you. The data of an entity is actually
found in the fields that it contains and each of these fields is called an
attribute. Each attribute in the OutSystems platform, inside of an entity
needs to be one of the basic types that we've seen before.
When you create a new entity it's created for you already an ID attribute
that allows for relational operations, we will cover these later on.
Now every attribute that you create inside an entity ends up being a column
in the database table for that entity. Much like the entity itself
you don't have to worry about maintaining the database representation
of the attributes of these columns because the OutSystems platform will
take care of that for you.
Much like everything in the OutSystems platform the process of creating an
entity is very very simple, in service studio you want to go into the data layer
right click the entities folder and select add an entity, the important
property that you need to setup is the name of the entity, this will dictate
obviously how you interact and how you refer to it and also will dictate the
physical table name of this entity in the database.
Once you have your entity, attribute creation is a breeze, you just right
click the desired entity and select
add entity attribute and you need to make sure that you set up at least the
name of the attribute and the data type for obvious reasons.
There's also a property that very commonly you will want to set up which is the
is mandatory:
you want to flip this to yes if you want to enforce that, for example, when this
attribute is asked of the user in an input that he needs to fill in this in the
form. The OutSystems platform tries to help you in the process of creating your
attributes and it does so by looking at the name that you give to an attribute
that you've created and depending on what the name is, it tries to infer what the
type of that attribute is. Over here you can see some of the matches, some of the
substrings that it will look for and what data type it will assign by default to
the attribute. Of course you can afterwards go over there and change it
in case the match isn't the correct one. As mentioned, every entity that you
create in your application has a set of system actions, system procedures that you
can apply, that you can execute on top of that entity. These operations, these
CRUD operations are what we call entity actions and you'll be able to use them
to create, update, to delete and retrieve entities from the database. This slide
just summarizes some of the entity actions, in fact it has all of the entity
actions that exist, together with a small explanation of what each one of them
does. There is a special kind of entity called static entity: it is very similar
to an entity as we described, it has its own database table, but unlike a normal
entity you cannot dynamically create new records for a static entity, they're
actually created at design time within service studio, so every single record
that you create over there is known intrinsically by the OutSystems
language
this allows you
to gain access to certain accelerators while referring to the known records of
an entity, so in a sense you can also use them not just as if they were a database
table but also as if they were an enumerate. And obviously, goes without
saying, that you do not have creation and deletion and alter operations over
static entities, you just have the retrieve ones because again they cannot
be created, updated or deleted dynamically in runtime. The process of
defining a static entity is very very similar to an entity, you just create a
static entity by adding it and then you create the attributes within. You're
also subject to the limitations of having the basic data types for
these attributes. Static entities are used for concepts in your application
whose values don't change during the life of an application, a good example is
the list of countries in the world, so you could create a country static entity,
define whatever number of attributes you want that will define each and every
country and then by right-clicking the records folder, add as many records, as
many countries in this case, as you want. Then upon selecting each record you can
define what are the values for the attributes in that particular record, in
this example over here we can see what is the country code for Portugal. Here
you can see the example of usage of this fast way to reference a specific static
entity record. You can see on point one that Portugal is being referenced
directly by name and we are fetching the country information, the
full country record for Portugal and then accessing its country code.
Don't worry too much right now if some of this, or if this dialogue doesn't mean
too much, it will become clear once you start to use it.
The main point, the key takeaway point, is that you can use a static entity as you would use a
normal entity but you can also use it in almost an enumerate kind of way.
The third and final kind of compound data-type that we have in OutSystems
is called the structure, it is somewhat similar to an entity but unlike an entity
there isn't a table for each structure that you create, so effectively you normally
create structures to hold temporary data formats that you then want to process
and once you do process them, you want to throw away that result. For example, the
returns of integration systems. Since structures exist solely in memory, they
don't have the limitations of a database column, so this means that its attributes
don't necessarily need to be confined to basic data types like entities and
static entities. So it is possible to have structures inside structures, or
even entities inside structures. Again the process of creating a structure is
very very similar to an entity and static entity: just right clicking on
the appropriate folder and then add in the attributes. Now over here in this
particular example we're highlighting the fact that this structure, the
combined customer info, actually has three attributes of very diverse types
we have a customer entity, we have a country static entity and we just have a
normal basic data type attribute, in this case, a date. Before we continue
let's just make a small detour and talk a little bit about variables, if you use
variables in other languages you know already what we're talking about:
they are storage locations referenced by a name. It's stored data of a certain
type can be either a basic type or one of the compound types that we've
discussed. Now, every single variable depending on its nature has a certain
scope, this is the area of the application where that variable is
visible and you can access
its value. Now depending on how the value of a variable is born on its scope, you
can classify it as either an input variable, an output variable or a local
variable. Over here we have an action that does something, a piece of code that
does something. We're creating an input parameter that is of integer type, this
means that when you call this action you can pass into the action a number to
be used by the code. Over here you can see the process of adding an output
parameter, this means that this variable will be visible, its value will be
visible, from the context where this section is called so you will want to
affect the value before the code inside the section completes and then access it from
the caller.
Finally, you can have a pure local variable, this means that this variable
is not at all visible from the context that calls this action
it's just born over there. In here we have a sample of a customer that is
local to this some action. So far we focused on variables that have a single
item of their data type but you can actually have a sequence of variables, of
values of that data type and we call those lists, you can dynamically add and
remove from a list, and this list, the atom of a list, can be from a basic
type such as an integer, or a compound type such as a list of customers, or you
can even combine things and have, as is in the example here, a list of a combination
of countries, dates and texts. The most common places where you see lists are
obviously the outputs of queries to the database but there's nothing keeping you
from creating, ad-hoc in memory, lists of a certain atom type... and obviously the use
of the lists comes when you iterate over them and you apply a certain operation
over them
be it a renderization or a processing step. Here you can see an example of a list
that blue square node that you see over there is a query, it's what we call an
aggregate and let's just assume that it fetches, in some way that you will learn
later, all of the customers in the database whose first name is Michael. On
the screenshot to the right you can see how we can access the LastName of a row
of this aggregate output. Reading from the right to the left,
we're accessing the last name of a customer that is in the current row of
the list that is the output of GetCustomersNamedMichael. Very very easy.
And this is it for modeling data basics in the OutSystems platform. In a later
lesson we will expand on this information and tell you how you can
create relationships between entities. Stay tuned for that.