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
  • Main
  • @CustomQuery decorator
  • Usage
  • Note

Was this helpful?

  1. Mandarine Data
  2. ORM
  3. Repositories

Interacting with data

PreviousRepositoriesNextUpdating data

Last updated 4 years ago

Was this helpful?

In order to continue, you must understand , &

Main

Mandarine repositories work directly with , since MQL serves as the primary query statement writer.

@CustomQuery decorator

The @CustomQuery decorator allows you to write SQL queries in your repositories. While it is true that MQL & Mandarine's ORM aim to remove as much SQL & database code from your application's layer, it is also true that some use cases may require the use of complex SQL queries.

Syntax:

@CustomQuery(query: string, secure?: boolean)
  • query: Query to execute

  • secure: whether the query has secured parameters such as $1, $2, ...

    • Default: True

Usage

// usersModel.ts

import { Table, Id, GeneratedValue, Column } from "https://deno.land/x/mandarinets/mod.ts";

@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;

    constructor(firstname?: string, lastname?: string, country?: string) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.country = country;
    }

}
// usersRepository.ts

import { Repository, MandarineRepository } from "https://deno.land/x/mandarinets/mod.ts";
import { Users } from "./usersModel.ts";

@Repository()
abstract class UsersRepository extends MandarineRepository<Users> {

    constructor() {
        super(Users);
    }
    
    public findByCountry(country: string) {}
    public findByFirstnameAndCountry(firstname: string, country: string) {}
    public countByCountry(country: string) {}
    public existsByLastname(lastname: string) {}

    @CustomQuery("SELECT * FROM public.users WHERE country = $1 AND firstname = 'Andres'")
    public myCustomQuery(country: string) {}
}
// controller.ts

import { Controller, GET } from "https://deno.land/x/mandarinets/mod.ts";
import { Users } from "./usersModel.ts";
import { UsersRepository } from "./usersRepository.ts";

@Controller()
export class MyController {

    constructor(private readonly repository: UsersRepository) {}

    @GET('/add-people')
    public async handleAddPeople() {
        let user1 = new Users("Andres", "Pirela", "United States");
        let user2 = new Users("Anastasia", "Skymonov", "Russia");
        let user3 = new Users("Bill", "Gates", "United States");

        await this.repository.save(user1);
        await this.repository.save(user2);
        await this.repository.save(user3);

        return true;
    }

    @GET('/get-people-from-united-states')
    public async handleGetPeopleFromUnitedStates() {
        return await this.repository.findByCountry("United States");
    }

    @GET('/count-all')
    public async handleCountAll() {
        return await this.repository.countAll();
    }

    @GET('/count-russia')
    public async handleCountRussia() {
        return await this.repository.countByCountry("Russia");
    }

    @GET('/find-anastasia')
    public async handleFindAnastasia() {
        return await this.repository.findByFirstnameAndCountry("Anastasia", "Russia");
    }

    @GET('/exists-by-last-name')
    public async existsByLastname() {
        return await this.repository.existsByLastname("Gates");
    }

    @GET('/custom-query')
    public async customQueryHandler() {
        return await this.repository.myCustomQuery("United States");
    }
}
import { MandarineCore } from "https://deno.land/x/mandarinets/mod.ts";

import { UsersRepository } from "./usersRepository.ts";
import { MyController } from "./controller.ts";

const controllers = [MyController];
const repositories = [UsersRepository];

new MandarineCore().MVC().run();
# http://127.0.0.1:8080/get-people-from-united-states
# ==> [{"id":1,"firstname":"Andres","lastname":"Pirela","country":"United States"},{"id":3,"firstname":"Bill","lastname":"Gates","country":"United States"}]

##########

# http://127.0.0.1:8080/count-all
# ==> 3

##########

# http://127.0.0.1:8080/count-russia
# ==> 1

##########

# http://127.0.0.1:8080/find-anastasia
# ==> [{"id":2,"firstname":"Anastasia","lastname":"Skymonov","country":"Russia"}]

##########

# http://127.0.0.1:8080/exists-by-last-name
# ==> true

##########

# http://127.0.0.1:8080/custom-query
# ==> [{"id":1,"firstname":"Andres","lastname":"Pirela","country":"United States"}]

Note

  • They keyword "await" should be used for all repository methods

Mandarine Query Language (MQL)
Models
Repositories
MQL