JayStack OData V4 Server computed properties example
TypeScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
.npmignore
README.md
package.json
tsconfig.json

README.md

JayStack OData V4 Server - computed properties

With the latest version of the JayStack OData V4 Server library, you can even more easily create computed properties on your entity models as before. If you are not yet familiar with the basic concepts of the odata-v4-server module, consider reading a comprehensive tutorial here.

Entity model declaration

In this tutorial, we will use this simple Person entity type to show you how to use computed properties.

class Person{
    @Edm.Int32
    @Edm.Key
    Id:number

    @Edm.String
    FirstName:string

    @Edm.String
    LastName:string
}

Creating real computed properties

To create computed properties on OData entity or complex types, we will use simple property getters and annotate them with an Edm type decorator. We extend our former example entity model like this:

class Person{
    @Edm.Int32
    @Edm.Key
    Id:number

    @Edm.String
    FirstName:string

    @Edm.String
    LastName:string

    @Edm.Computed
    @Edm.String
    get FullName():string{
        return `${this.FirstName} ${this.LastName}`;
    }
}

Please notice, that we added the FullName property getter to the Person class. Inside the getter function, this will be your result entity instance. We will see the computed property in the service metadata and if we implement a controller using the Person entity type, we can see it in the result too.

The Edm.Computed property decorator is only used for annotational purpose. In the metadata, the property will be annotated properly as the OData v4 standard with an Annotation element.

You can see the full working example here.