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
  • Updating Data
  • Example

Was this helpful?

  1. Mandarine Data
  2. ORM
  3. Repositories

Updating data

PreviousInteracting with dataNextMandarine Query Language

Last updated 4 years ago

Was this helpful?

Please see the reserved keyword and

Main

Updating data through the use of Mandarine-powered repositories is not hard. All it requires is to know how to save a record in your entity (Please refer to the links above).

Updating Data

In order to update existent data in your tables by using Mandarine-powered repositories, you will be using the same method that is used to save new information. The method.

takes one argument which will be your model, but unlike when saving new data, your model's primary key cannot be null/undefined. When your model's primary key has a value, Mandarine automatically detects that the model is not meant for saving information but for updating.

Example

// 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;
}
// 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 findByFirstname(firstname: string) {}
}
import { Component } from "https://deno.land/x/mandarinets/mod.ts";
import { Users } from "./usersModel.ts";
import { UsersRepository } from "./usersRepository.ts";

@Component()
export class MyComponent() {

    constructor(private readonly usersRepo: UsersRepository) {
    }

    public updateBillsCountry() {
        // [0] to select the first record found
        let billUser: Users = await this.usersRepo.findByFirstname("Bill")[0];
        // { id: 1, firstname: "Bill", lastname: "Clark", country: "Canada" }
        
        billUser.country = "Croatia";
        
        // Updating bill's country.
        await this.usersRepo.save(billUser);
    }
}

save
Interacting with data
save
save