From aece001b9c61561bf40f0fe4146dea4a64883635 Mon Sep 17 00:00:00 2001 From: Timothee Date: Tue, 21 Nov 2023 18:55:54 +0100 Subject: [PATCH] Fixed writing duplicate keys when importing --- server/src/spotify/dbTools.ts | 8 ++++---- server/src/tools/misc.ts | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/server/src/spotify/dbTools.ts b/server/src/spotify/dbTools.ts index 44ebe62c..10be449c 100644 --- a/server/src/spotify/dbTools.ts +++ b/server/src/spotify/dbTools.ts @@ -4,7 +4,7 @@ import { SpotifyAlbum, Album } from '../database/schemas/album'; import { SpotifyArtist, Artist } from '../database/schemas/artist'; import { SpotifyTrack, Track } from '../database/schemas/track'; import { logger } from '../tools/logger'; -import { minOfArray, retryPromise } from '../tools/misc'; +import { minOfArray, retryPromise, uniqBy } from '../tools/misc'; import { SpotifyAPI } from '../tools/apis/spotifyApi'; import { addTrackIdsToUser, @@ -196,13 +196,13 @@ export async function storeTrackAlbumArtist({ artists?: Artist[]; }) { if (tracks) { - await TrackModel.create(tracks); + await TrackModel.create(uniqBy(tracks, item => item.id)); } if (albums) { - await AlbumModel.create(albums); + await AlbumModel.create(uniqBy(albums, item => item.id)); } if (artists) { - await ArtistModel.create(artists); + await ArtistModel.create(uniqBy(artists, item => item.id)); } } diff --git a/server/src/tools/misc.ts b/server/src/tools/misc.ts index c3db63fe..81430e9a 100644 --- a/server/src/tools/misc.ts +++ b/server/src/tools/misc.ts @@ -336,3 +336,19 @@ export function chunk(array: T[], chunkSize: number) { } return chunks; } + +export function uniqBy(source: T[], getId: (item: T) => string): T[] { + const final: T[] = []; + const alreadyAdded = new Set(); + + for (const item of source) { + const itemId = getId(item); + if (alreadyAdded.has(itemId)) { + continue; + } + alreadyAdded.add(itemId); + final.push(item); + } + + return final; +}