diff --git a/packages/core/src/collections/tags.ts b/packages/core/src/collections/tags.ts index 8dbcec394..487f7391f 100644 --- a/packages/core/src/collections/tags.ts +++ b/packages/core/src/collections/tags.ts @@ -49,19 +49,17 @@ export class Tags implements ICollection { return this.all.find((eb) => eb.and([eb("title", "==", title)])); } - async add(item: Partial) { - item.title = item.title ? Tags.sanitize(item.title) : item.title; + async add(item: Partial & { title: string }) { + item.title = Tags.sanitize(item.title); const id = item.id || getId(); - const oldTag = item.id - ? await this.tag(item.id) - : item.title - ? await this.find(item.title) - : undefined; + const oldTag = item.id ? await this.tag(item.id) : undefined; - if (!item.title && !oldTag?.title) throw new Error("Title is required."); if (oldTag && item.title === oldTag.title) return oldTag.id; + if (await this.find(item.title)) + throw new Error("Tag with this title already exists."); + await this.collection.upsert({ id, dateCreated: item.dateCreated || oldTag?.dateCreated || Date.now(), diff --git a/packages/core/src/migrations.ts b/packages/core/src/migrations.ts index 7b9c89840..830cd6166 100644 --- a/packages/core/src/migrations.ts +++ b/packages/core/src/migrations.ts @@ -21,7 +21,7 @@ import { parseHTML } from "./utils/html-parser"; import { decodeHTML5 } from "entities"; import { CURRENT_DATABASE_VERSION } from "./common"; import Database from "./api"; -import { getId, makeId } from "./utils/id"; +import { makeId } from "./utils/id"; import { Color, ContentItem,