EGF2 Guide - Configuring Objects and Edges
In the previous post I outlined what objects and edges we need to have in the system. Now we are ready to actually add them!
Open config.json
file from client-data
service. First we need to adjust User
object declaration. We have added some edges so we need to let the system know about them. After the changes User
object declaration should look like:
"user": {
"code": "03",
"GET": "self",
"PUT": "self",
"fields": {
"name": {
"type": "struct",
"schema": "human_name",
"required": true,
"edit_mode": "E"
},
"email": {
"type": "string",
"validator": "email",
"required": true,
"unique": true
},
"system": {
"type": "object_id",
"object_types": ["system_user"],
"required": true
},
"verified": {
"type": "boolean",
"default": false
},
"date_of_birth": {
"type": "string",
"edit_mode": "E"
}
},
"edges": {
"roles": {
"contains": ["customer_role", "admin_role"],
"GET": "self"
},
"posts": {
"contains": ["post"],
"GET": "self",
"POST": "self",
"DELETE": "self"
},
"timeline": {
"contains": ["post"],
"GET": "self"
},
"follows": {
"contains": ["user"],
"GET": "self",
"POST": "self",
"DELETE": "self"
},
"followers": {
"contains": ["user"],
"GET": "self"
}
}
}
As you can see if you compare original User
declaration with the changed version I made the following changes:
- In
"edges"
section for"roles"
edge I specified what target objects this edge can take. We know what roles we have in the system now. - Edge declarations for
posts
,timeline
,follows
andfollowers
edges were added. As you can see, edgesposts
andfollows
are fully editable by users who own them (edge that have authorised user as a source are owned by this user). Edgesfollowers
andtimeline
are read-only - the system will populate them automatically as a result of other users' actions.
With User
object out of the way we can start adding objects that are totally new to the system. Let's start with Roles objects:
"customer_role": {
"code": "10",
"GET": "self",
"fields": {
"user": {
"type": "object_id",
"object_types": ["user"],
"required": true
}
}
}
"admin_role": {
"code": "11",
"GET": "self",
"fields": {
"user": {
"type": "object_id",
"object_types": ["user"],
"required": true
}
},
"edges": {
"offending_posts": {
"contains": ["post"],
"GET": "self",
"DELETE": "self"
}
}
}
These two objects are pretty straightforward, the only thing of note here is that AdminRole/offending_posts
edge is readable and deletable by an admin, but admins can't create them directly. The system will create these edges automatically.
Next object is Post
, declaration:
"post": {
"code": "12",
"GET": "any",
"PUT": "self",
"DELETE": "self",
"fields": {
"creator": {
"type": "object_id",
"object_types": ["user"],
"required": true,
"auto_value": "req.user"
},
"image": {
"type": "object_id",
"object_types": ["file"],
"required": true
"edit_mode": "NE"
},
"desc": {
"type": "string",
"required": true,
"edit_mode": "E",
"min": 2,
"max": 4096
},
},
"edges": {
"comments": {
"contains": ["comment"],
"GET": "any",
"POST": "registered_user"
},
"offended": {
"contains": ["user"],
"GET": "admin_role",
"POST": "registered_user"
}
}
}
Our Post objects will be accessible by anybody, even users who have not registered with the system. Only registered users can comment and mark posts as offensive. Only admins can see who finds a particular post offensive. Please note that once a Post
is created fields creator
and image
are not editable, thus we have "edit_mode": "NE"
for them. In case "edit_mode"
is ommitted it means that a field can not be specified when an object is created and it can not be changed. Usually such fields are set automatically by the system.
Another thing of note with Post
is "auto_value"
parameter in "creator"
field declaration. As you can see, "creator"
field can't be affected by a user at all. "auto_value": "req.user"
parameter specifies that this field should be populated with User
object of the currently authenticated user.
And the last object we will define is Comment
:
"comment": {
"code": "13",
"GET": "any",
"PUT": "self",
"DELETE": "self",
"fields": {
"creator": {
"type": "object_id",
"object_types": ["user"],
"required": true,
"auto_value": "req.user"
},
"post": {
"type": "object_id",
"object_types": ["post"],
"required": true
"edit_mode": "NE"
},
"text": {
"type": "string",
"required": true,
"edit_mode": "E",
"min": 2,
"max": 2048
},
}
}
We've got no edges defined for Comment objects. Comments are deletable and editable by creators.
With the prepared configuration we can proceed to deploying the system!