According to the documentation provided by TypeOrm Framework, the Repository.save
function is supposed to save/insert new values and ignore/update existing ones.
However, I am currently experiencing an issue where it is throwing a duplication error
for an existing value and halting the entire insertion process. (I have a unique column named key
)
Definition of My entity:
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, PrimaryColumn } from 'typeorm';
import { Source } from '../sources/source.entity';
@Entity({ name: 'articles' })
export class Article {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column({
nullable: true
})
image: string | null;
@Column({
type: "text",
})
url: string;
@Column({
type: "varchar",
unique: true
})
key: string;
@Column({
type: 'datetime',
nullable: true
})
date: Date | null;
@ManyToOne(type => Source, source => source.articles, {eager: true})
@JoinColumn({name: 'source'})
source: Source;
@Column({
type: `text`,
nullable: true
})
description: string | null
}
Definition of My Service:
constructor(
@InjectRepository(Article) private readonly articleRepository: Repository<Article>,
private readonly articlesScraper: BlogScraperService
) {
}
async clonningFromScraper() {
let articles = await this.articlesScraper.articles('1');
articles = articles.map(article => ({ ...article, key: decodeURIComponent(article.url).substring(0, 255) }));
return this.articleRepository
.save(articles);
}