fix: derive consistent md5 ids from tag title

This commit is contained in:
thecodrr
2021-02-20 11:31:44 +05:00
parent 25ee202d64
commit e898db9562
8 changed files with 28 additions and 13 deletions

View File

@@ -14,6 +14,7 @@ class Migrations {
async init() { async init() {
this.dbVersion = this.dbVersion =
(await this._db.context.read("v")) || CURRENT_DATABASE_VERSION; (await this._db.context.read("v")) || CURRENT_DATABASE_VERSION;
this._db.context.write("v", this.dbVersion);
} }
async migrate() { async migrate() {

View File

@@ -75,10 +75,10 @@ class Settings {
.notebook(pin.data.notebookId) .notebook(pin.data.notebookId)
.topics.topic(pin.data.id)._topic; .topics.topic(pin.data.id)._topic;
} else if (pin.type === "tag") { } else if (pin.type === "tag") {
item = item = this._db.tags.tag(pin.data.id);
this._db.tags.tag(pin.data.id) || this._db.tags.tag(pin.data.title);
} }
if (item) prev.push(item); if (item) prev.push(item);
else this.unpin(pin.data.id); // TODO risky.
return prev; return prev;
}, []); }, []);
} }

View File

@@ -145,15 +145,24 @@ export default class Notes extends Collection {
} }
tagged(tagId) { tagged(tagId) {
const tag = this._db.tags.tag(tagId); return this._getTagItems(tagId, "tags");
if (!tag || tag.noteIds.length <= 0) return [];
return tag.noteIds.map((id) => this._collection.getItem(id));
} }
colored(colorId) { colored(colorId) {
const color = this._db.colors.tag(colorId); return this._getTagItems(colorId, "colors");
if (!color || color.noteIds.length <= 0) return []; }
return color.noteIds.map((id) => this._collection.getItem(id));
/**
* @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;
}, []);
} }
/** /**

View File

@@ -1,7 +1,5 @@
import Collection from "./collection"; import Collection from "./collection";
import getId from "../utils/id";
import { qclone } from "qclone"; import { qclone } from "qclone";
import set from "../utils/set";
export default class Tags extends Collection { export default class Tags extends Collection {
tag(id) { tag(id) {
@@ -20,7 +18,7 @@ export default class Tags extends Collection {
title: tagId, title: tagId,
}; };
let id = tag.id || getId(); let id = tag.id || makeId(tag.title);
let notes = tag.noteIds || []; let notes = tag.noteIds || [];
if (notes.find((id) => id === noteId)) return id; if (notes.find((id) => id === noteId)) return id;

View File

@@ -32,4 +32,4 @@ export const EVENTS = {
noteRemoved: "note:removed", noteRemoved: "note:removed",
}; };
export const CURRENT_DATABASE_VERSION = 5.1; export const CURRENT_DATABASE_VERSION = 5.2;

View File

@@ -94,6 +94,7 @@ export default class Backup {
switch (version) { switch (version) {
case CURRENT_DATABASE_VERSION: case CURRENT_DATABASE_VERSION:
case 5.0: case 5.0:
case 5.1:
case 4: case 4:
case 4.1: case 4.1:
case 4.2: case 4.2:

View File

@@ -60,7 +60,8 @@ export const migrations = {
return item; return item;
}, },
}, },
5.1: { 5.1: {},
5.2: {
note: false, note: false,
notebook: false, notebook: false,
tag: false, tag: false,

View File

@@ -1,3 +1,4 @@
import SparkMD5 from "spark-md5";
/** /**
* *
* @param {number} size * @param {number} size
@@ -25,3 +26,7 @@ function cryptoRandom(size, type) {
export default function () { export default function () {
return cryptoRandom(12, "hex"); return cryptoRandom(12, "hex");
} }
export function makeId(text) {
return SparkMD5.hash(text);
}