diff --git a/packages/core/api/migrations.js b/packages/core/api/migrations.js index 6b0680a71..4fefdaadc 100644 --- a/packages/core/api/migrations.js +++ b/packages/core/api/migrations.js @@ -14,6 +14,7 @@ class Migrations { async init() { this.dbVersion = (await this._db.context.read("v")) || CURRENT_DATABASE_VERSION; + this._db.context.write("v", this.dbVersion); } async migrate() { diff --git a/packages/core/api/settings.js b/packages/core/api/settings.js index 7ceb3e1b0..e7c0aec73 100644 --- a/packages/core/api/settings.js +++ b/packages/core/api/settings.js @@ -75,10 +75,10 @@ class Settings { .notebook(pin.data.notebookId) .topics.topic(pin.data.id)._topic; } else if (pin.type === "tag") { - item = - this._db.tags.tag(pin.data.id) || this._db.tags.tag(pin.data.title); + item = this._db.tags.tag(pin.data.id); } if (item) prev.push(item); + else this.unpin(pin.data.id); // TODO risky. return prev; }, []); } diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js index d2f63b677..b819c0a6d 100644 --- a/packages/core/collections/notes.js +++ b/packages/core/collections/notes.js @@ -145,15 +145,24 @@ export default class Notes extends Collection { } tagged(tagId) { - const tag = this._db.tags.tag(tagId); - if (!tag || tag.noteIds.length <= 0) return []; - return tag.noteIds.map((id) => this._collection.getItem(id)); + return this._getTagItems(tagId, "tags"); } colored(colorId) { - const color = this._db.colors.tag(colorId); - if (!color || color.noteIds.length <= 0) return []; - return color.noteIds.map((id) => this._collection.getItem(id)); + return this._getTagItems(colorId, "colors"); + } + + /** + * @private + */ + _getTagItems(tagId, collection) { + const tag = this._db[collection].tag(tagId); + if (!tag || tag.noteIds.length <= 0) return []; + return tag.noteIds.reduce((arr, id) => { + const item = this._collection.getItem(id); + if (item) arr.push(item); + return arr; + }, []); } /** diff --git a/packages/core/collections/tags.js b/packages/core/collections/tags.js index 5c49c443c..e2f7d2854 100644 --- a/packages/core/collections/tags.js +++ b/packages/core/collections/tags.js @@ -1,7 +1,5 @@ import Collection from "./collection"; -import getId from "../utils/id"; import { qclone } from "qclone"; -import set from "../utils/set"; export default class Tags extends Collection { tag(id) { @@ -20,7 +18,7 @@ export default class Tags extends Collection { title: tagId, }; - let id = tag.id || getId(); + let id = tag.id || makeId(tag.title); let notes = tag.noteIds || []; if (notes.find((id) => id === noteId)) return id; diff --git a/packages/core/common.js b/packages/core/common.js index f805f14c3..2c3448e2f 100644 --- a/packages/core/common.js +++ b/packages/core/common.js @@ -32,4 +32,4 @@ export const EVENTS = { noteRemoved: "note:removed", }; -export const CURRENT_DATABASE_VERSION = 5.1; +export const CURRENT_DATABASE_VERSION = 5.2; diff --git a/packages/core/database/backup.js b/packages/core/database/backup.js index a49660101..b8b0ded16 100644 --- a/packages/core/database/backup.js +++ b/packages/core/database/backup.js @@ -94,6 +94,7 @@ export default class Backup { switch (version) { case CURRENT_DATABASE_VERSION: case 5.0: + case 5.1: case 4: case 4.1: case 4.2: diff --git a/packages/core/migrations.js b/packages/core/migrations.js index 57b990777..226f2bfff 100644 --- a/packages/core/migrations.js +++ b/packages/core/migrations.js @@ -60,7 +60,8 @@ export const migrations = { return item; }, }, - 5.1: { + 5.1: {}, + 5.2: { note: false, notebook: false, tag: false, diff --git a/packages/core/utils/id.js b/packages/core/utils/id.js index d64d92333..e3d02ab05 100644 --- a/packages/core/utils/id.js +++ b/packages/core/utils/id.js @@ -1,3 +1,4 @@ +import SparkMD5 from "spark-md5"; /** * * @param {number} size @@ -25,3 +26,7 @@ function cryptoRandom(size, type) { export default function () { return cryptoRandom(12, "hex"); } + +export function makeId(text) { + return SparkMD5.hash(text); +}