JS20

Documentation

JS20 - Documentation

Register models

Here's how to register models.

import { Model } from '@js20/core';
import { sBoolean, sString } from '@js20/schema';

interface Car {
    isLeased: boolean;
    registrationNumber: string;
}

const sCar: Car = {
    isLeased: sBoolean().type(),
    registrationNumber: sString().matches(/^[A-Z0-9]{1,7}$/).type(),
}

// Models are defined in the Models interface

interface Models {
    // Make sure to type each model with Model<T>
    car: Model<Car>;
}

const models: Models = {
    car: {
        name: 'car',
        schema: sCar,
    }
};

// Make sure to type the App with your Models interface, will be important later!
const app = new App<Models>();
const database = new MySqlDatabase(connectOptions);

database.addModels(models);

What's happening here?

  • We define the Models interface. We recommend to name it like this (will be used in frontend generation)
  • Make sure to type each model with Model<T>
  • Point to the right Schema, make sure the schema extends the model interface
  • Register the models in the database where you want them stored
  • If the Models interface becomes too big, break it up like type Models = SomeModels & SomeOtherModels;
JS20