Mandarine.TS
Latest
Latest
  • Mandarine.TS
  • Getting started
    • Installing Deno Runtime
    • Setting up Environment
    • Quick Example
  • Concepts
  • Mandarine Project Structure
  • Mandarine CLI
    • CLI Reference
      • mandarine new
      • mandarine generate
      • mandarine run
  • Mandarine Core
    • Core Initialization
    • Mandarine.json
    • Dot env file
    • Properties
      • The @Value Decorator
    • Components
      • Controller
      • Middleware
      • Repository
      • Component
      • Service
      • Configuration
      • Manual Component
    • Dependency Injection
      • Accessing DI container
    • Resource Handlers
      • Resource Handler Registry
      • Resource Handler
      • Resource Resolver
  • Mandarine MVC
    • Web MVC
    • Controllers
      • Routes
        • Parameterized Routes
      • HTTP Handlers
      • @ResponseStatus
    • Custom Middleware
    • Session Middleware
    • CORS Middleware
    • Template Engine
      • @Render Decorator
      • Accessing data from template
    • Launching web-app
    • Serving Static Content
    • Multipart Form Data
  • Mandarine Security
    • Sessions
  • Mandarine Data
    • ORM
      • Data source
      • Models
      • Repositories
        • Interacting with data
        • Updating data
    • Mandarine Query Language
  • Resources
    • Changelog
  • Plugins
    • Optional
    • Promise Repeater
Powered by GitBook
On this page
  • Concepts
  • Database interaction
  • Declaring an entity
  • Declaring a column
  • Declaring a primary key
  • Creating a model

Was this helpful?

  1. Mandarine Data
  2. ORM

Models

Concepts

  • Entity

    • A representation of a table in a programatic way

  • Entity registry

    • Mandarine keeps a list of all the entities that your application contains. This list is called Entity Registry as it works as a registry for Mandarine's engine to interact with your entities.

Database interaction

  • Your tables (entities) as well as the columns of those tables are created automatically in the database if they do not exist. This process happens at mandarine compile time.

    • If one of your entities is already in the database, but you have added a new column, Mandarine will resolve the new column and will add it to your table in your database.

Declaring an entity

An entity is declared by using the decorator @Table . This decorator will add your virtual object database to the entity registry to then be used across your application.

The table decorator targets a class.

Syntax:

@Table({ name?: string, schema: string})
  • name

    • Optional

    • If name is not defined, the name of the target class will be considered the table's name

  • schema

    • Required

    • Schema where your table will be located

Declaring a column

In order to declare a column for your entity, you will need to use the decorator @Column .

The @Column decorator targets a class field/property.

Syntax:

@Column({ 
            name?: string, // Default = property's key
            type?: Types // See more below
            unique?: boolean, // Default = false
            nullable?: boolean, // Default = true
            length?: number, // Default = 255
            precision?: number, // Default = 8
            scale?: number // Default 2
       })
  • name: Name of the column

    • If the type of the variable is: numeric, string, boolean. Mandarine will automatically read & detect its type

  • unique: Specifies whether the column will have an unique constraint

  • nullable: Specifies whether the column can be null

  • length: Specifies the length of the column (used in certain cases, such as when the column's type is character varying/varchar)

  • precision: Specifies the precision of a numeric character (used in certain cases, such as when the type is decimal)

  • scale: Specifies the scale of the numeric character (used in certain cases, such as when the type is decimal)

Declaring a primary key

The declaration of a primary key depends on two decorators, the decorator @Id and the decorator @GeneratedValue. Both decorators target a class field previously decorated with the decorator @Column

  • @Id will specify that a column is a primary key

  • @GeneratedValue will specify the type of strategy to be used for the declared primary key

Syntax:

@Id()
@GeneratedValue({strategy: "SEQUENCE" | "MANUAL", manualHandler?: Function})
  • Declares what strategy the table's primary key will use.

  • If strategy is SEQUENCE, then the column is assumed to be serial (auto-increment)

  • If strategy is MANUAL, then when an insert is invoked through Mandarine's ORM, the value will be generated by invoking manualHandler.

Creating a model

Now that we have seen the concepts involved in Models, we can create one.

@Table({ schema: "public" })
class Users {

    @Id()
    @GeneratedValue({strategy: "SEQUENCE"})
    @Column()
    public id: number;

    @Column()
    public firstname: string;

    @Column()
    public lastname: string;

    @Column()
    public country: string;

}

In the example above we are declaring an entity that will have Users as name, it has 4 columns: id, firstname, lastname, country. The column id has been annotated with ID, and GeneratedValue, meaning id will be a primary key and its strategy is SEQUENCE/Auto-increment. Note that if your model has a constructor, ALL its parameters must be optional.

PreviousData sourceNextRepositories

Last updated 4 years ago

Was this helpful?

type: The reference of a SQL type given by a mandarine internal enum . Note that if your current dialect does not recognize the indicated value, then the value will be set to character varying

(See enum here)