Как описано в TypeOrm FrameWork Repository.save
должен сохранять / вставлять новые значения и игнорировать / обновлять существующие один раз,
Но теперь я сталкиваюсь с проблемой, что он выбрасывает duplication error
на существующее значение и остановка всей вставки! (У меня есть уникальный столбец с именем key
)
Моя сущность:
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
}
Мое обслуживание:
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);
}