Mandarine repositories work directly with MQL, 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();