🤖 Backend
NestJS
Nestjs Snippets

Repository

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import {
  InferAttributes,
  Transaction,
  UpdateOptions,
  WhereOptions,
} from 'sequelize';
import { Repository } from 'sequelize-typescript';
import { ConfigCreation, Config } from '../models/config.model';
 
@Injectable()
export class ConfigRepository {
  constructor(
    @InjectModel(Config)
    private config: Repository<Config>,
  ) {}
 
  async create(
    payload: ConfigCreation,
    transaction?: Transaction,
  ): Promise<Config> {
    return this.config.create(payload, { transaction });
  }
 
  async findOne(where: WhereOptions<InferAttributes<Config>>): Promise<Config> {
    return this.config.findOne({ where });
  }
 
  async findAll(
    where: WhereOptions<InferAttributes<Config>>,
  ): Promise<Config[]> {
    return this.config.findAll({ where });
  }
 
  async update(
    values: Partial<InferAttributes<Config>>,
    options: UpdateOptions<InferAttributes<Config>>,
  ) {
    return this.config.update(values, options);
  }
 
  async delete(where: WhereOptions<InferAttributes<Config>>): Promise<number> {
    return this.config.destroy({
      where,
    });
  }
}