Document Model

Document Model

To build out the Document model for the application, we need to consider that based on our application requirement, a standalone user or user in a team can own a document.

We can define the Document model like this:

model Document {
    fields {
        title Text
        content Text
        user User
        team Team?
    }
 
    actions {
        get getDocument(id, user.id) {
            @permission(expression: ctx.identity == profileImage.user.identity)
        }
        list listDocuments(user.id?, team.id?) {
            @where(document.owner == ctx.identity or ctx.identity in document.team.members.user.identity)
        }
        delete deleteDocument(id, user.id?, team.id?) {
            @permission(expression: document.user.identity == ctx.identity)
        }
        create createDocument() with (title, content, team.id?, user.id) {
            @permission(expression: ctx.isAuthenticated)
        }
        update updateDocument(id) with (title?, content?, team.id?, user.id?)
    }
 
    @permission(
        actions: [get, list, update, delete],
        expression: document.user.identity == ctx.identity or ctx.identity in document.team.members.user.identity
    )
}

The Document model fields title and content which are of type text and meant to hold the title and content of the document.

The field user directly reference the User model, this field establishes which specific user is tied to this document. Finally, An optional field, team provides a relationship to the Team model. It indicates that while a document can be a standalone entity associated with a user, it can also be tied to a specific team if the user that created it is in a team.

We have also specificied several actions (opens in a new tab) that can be carried out on the model and finally, for permission, we’ve specified that these actions can only be carried out by the user identity that created the document or an identity that exists in the array of identities in a team.