Models & Classes

Overview:

In the context of VisualBoost, a Model defines a technical object than can be specified by property and functions (CRUD and custom functions). Classes, on the other hand, represent simple data objects which can only be extended with their own business logic but not with CRUD functions.

At the database level (MongoDB), a separate collection is created for each model. Classes, on the other hand, correspond to subdocuments.

To specify a model, properties can be created. These characterize the entity. For example, for an address data model, properties such as email, name, and birthdate can be defined.

Model
Class

The name property is indicated by an exclamation mark (!), signifying that it is required.

VisualBoost offers the possibility to further customize these properties, such as specifying whether an attribute is required or should be unique. Overall, a model helps to clearly define and organize how a backend application is structured and stores data, making it easier to develop and manage the application.

In the next sections, we'll explain how we use the defined models to generate source code.

Model in NodeJS (Backend):

When generating the backend application, a model is translated into two distinct files. One file contains the database structure of the model, while the other file includes the routes for the REST API.

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ObjectId = mongoose.Types.ObjectId;

const AddressDataSchema = new Schema(
    {
        email: { type: String, required: true, indexed: false, unique: false },
        name: { type: String, required: false, indexed: false, unique: false },
        birthdate: {
            type: Date,
            required: false,
            indexed: false,
            unique: false,
        },
    },
    { timestamps: true },
);

module.exports = mongoose.model(
    "AddressData",
    AddressDataSchema,
    "AddressData",
);

Model in Java (Client):

When generating the client code, a model is translated into multiple files.

  • Data Class: The Model as Java class.

  • DTO Class: Represents the request body for CREATE and UPDATE functions.

  • API Class: Contains all functions.

package api.model;

import java.util.Date;

public class AddressData {

  private String _id;
  private String email;
  private String name;
  private Date birthdate;

  public AddressData(String _id, String email, String name, Date birthdate) {
    this._id = _id;
    this.email = email;
    this.name = name;
    this.birthdate = birthdate;
  }

  public String getId() {
    return this._id;
  }

  public void setId(String _id) {
    this._id = _id;
  }

  public String getEmail() {
    return this.email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Date getBirthdate() {
    return this.birthdate;
  }

  public void setBirthdate(Date birthdate) {
    this.birthdate = birthdate;
  }
}

Last updated