import { Injectable } from "@nestjs/common"; import { InjectRepository } from "@nestjs/typeorm"; import { Repository } from "typeorm"; import { EntityJson } from "../entity/entityJson.entity"; import { create } from "domain"; @Injectable() export class EntityJSONRepository { constructor( @InjectRepository(EntityJson) private readonly entityJSONRepository: Repository, ) {} async create(flatJson: Partial) { const { entity_type, entity_id } = flatJson; // Step 1 — check if exists by unique keys const existing = await this.entityJSONRepository.findOne({ where: { entity_type, entity_id }, }); if (existing) { // Step 2 — normal update (no merge) await this.entityJSONRepository.update(existing.id, flatJson); // Optionally return updated row (common practice) return this.entityJSONRepository.findOne({ where: { id: existing.id }, }); } // Step 3 — insert new const created = this.entityJSONRepository.create(flatJson); return this.entityJSONRepository.save(created); } }