A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
feat: impl automatic database migrations (exp)
thecodrrfc7c1001e6d0
diff --git a/packages/core/api/index.js b/packages/core/api/index.jsindex 65da9ca1e..57ddbab78 100644--- a/packages/core/api/index.js+++ b/packages/core/api/index.js@@ -14,6 +14,7 @@ import Session from "./session";import Constants from "../utils/constants";import { EV } from "../common";import Settings from "./settings";+import Migrations from "./migrations";/*** @type {EventSource}@@ -57,6 +58,7 @@ class Database {this.lookup = new Lookup(this);this.backup = new Backup(this);this.settings = new Settings(this);+ this.migrations = new Migrations(this);// collections/** @type {Notes} */@@ -74,6 +76,7 @@ class Database {await this.settings.init();await this.user.sync();+ await this.migrations.migrate();}async _onUserStateChanged(user) {diff --git a/packages/core/api/migrations.js b/packages/core/api/migrations.jsnew file mode 100644index 000000000..7a295a9f5--- /dev/null+++ b/packages/core/api/migrations.js@@ -0,0 +1,63 @@+const CURRENT_VERSION = 3;+class Migrations {+ /**+ *+ * @param {import("./index").default} db+ */+ constructor(db) {+ this._db = db;+ this.dbVersion = CURRENT_VERSION;+ }++ async init() {+ this.dbVersion = (await this._db.context.read("v")) || 2;+ }++ get _shouldMigrate() {+ return this.dbVersion < CURRENT_VERSION;+ }++ _migrationFunction(collectionId) {+ let migrationFunction = migrations[version][collectionId];+ if (!migrationFunction)+ migrationFunction = migrations[CURRENT_VERSION][collectionId];+ return migrationFunction;+ }++ async migrate() {+ if (!this._shouldMigrate) return;+ const collections = [+ "notes",+ "notebooks",+ "tags",+ "colors",+ "trash",+ "delta",+ "text",+ "content",+ "settings",+ ];++ await Promise.all(+ collections.map(async (collectionId) => {+ const collection = this._db[collectionId];+ if (!collection) return;++ const items =+ collectionId === "content" || collectionId === "delta"+ ? await collection.all()+ : collectionId === "settings"+ ? [collection.raw]+ : collection.raw;++ await Promise.all(+ items.map(async (item) => {+ await this._migrationFunction(collectionId)(this._db, item);+ })+ );+ })+ );+ await this._db.context.write("v", CURRENT_VERSION);+ }+}+export default Migrations;diff --git a/packages/core/database/backup.js b/packages/core/database/backup.jsindex 1d55eb441..0c45478eb 100644--- a/packages/core/database/backup.js+++ b/packages/core/database/backup.js@@ -1,5 +1,6 @@import Hashes from "jshashes";import { CHECK_IDS, sendCheckUserStatusEvent } from "../common.js";+import { migrations } from "../migrations.js";const md5 = new Hashes.MD5();const invalidKeys = ["user", "t", "lastBackupTime"];@@ -131,26 +132,22 @@ export default class Backup {];await Promise.all(- collections.map(async (collection) => {- const collectionIndex = data[collection];- if (!collectionIndex) return;+ collections.map(async (collectionId) => {+ let collection = data[collectionId];+ if (!collection) return;- if (!Array.isArray(collectionIndex)) {- let migrationFunction = migrations[version][collection];- if (!migrationFunction)- migrationFunction = migrations[CURRENT_BACKUP_VERSION][collection];- await migrationFunction(this._db, collectionIndex);- return;+ if (!Array.isArray(collection)) {+ collection = [collectionId];}await Promise.all(- collectionIndex.map(async (id) => {+ collection.map(async (id) => {const item = data[id];if (!item) return;- let migrationFunction = migrations[version][collection];+ let migrationFunction = migrations[version][collectionId];if (!migrationFunction)migrationFunction =- migrations[CURRENT_BACKUP_VERSION][collection];+ migrations[CURRENT_BACKUP_VERSION][collectionId];await migrationFunction(this._db, item);}));@@ -179,88 +176,3 @@ export default class Backup {}}}--const migrations = {- handleDeleted: async function (db, collection, item) {- if (item.deleted) {- await db[collection]._collection.addItem(item);- return true;- }- return false;- },- 0: {- notes: async function (db, item) {- if (await migrations.handleDeleted(db, "notes", item)) return;-- const contentId = item.content.delta;- const notebook = item.notebook;- delete item.content;- delete item.notebook;- item.contentId = contentId;- item.remote = true;- if (notebook) item.notebooks = [notebook];- await db.notes.add(item);- },- delta: async function (db, item) {- if (await migrations.handleDeleted(db, "content", item)) return;-- item.data = item.data.ops;- item.type = "delta";- await db.content.add(item);- },- trash: async function (db, item) {- if (await migrations.handleDeleted(db, "trash", item)) return;-- item.itemType = item.type;- item.type = "trash";- if (item.itemType === "note") {- item.contentId = item.content.delta;- delete item.content;- }- await db.trash.add(item);- },- text: function () {},- },- 2: {- notes: async function (db, item) {- if (await migrations.handleDeleted(db, "notes", item)) return;-- // notebook -> notebooks- const notebook = item.notebook;- delete item.notebook;- item.remote = true;- if (notebook) item.notebooks = [notebook];-- await db.notes.add({ ...item, remote: true });- },- },- 3: {- notes: async function (db, item) {- if (await migrations.handleDeleted(db, "notes", item)) return;- await db.notes.add({ ...item, remote: true });- },- notebooks: async function (db, item) {- if (await migrations.handleDeleted(db, "notebooks", item)) return;- await db.notebooks.add(item);- },- tags: async function (db, item) {- if (await migrations.handleDeleted(db, "tags", item)) return;- await db.tags.merge(item);- },- colors: async function (db, item) {- if (await migrations.handleDeleted(db, "colors", item)) return;- await db.colors.merge(item);- },- trash: async function (db, item) {- if (await migrations.handleDeleted(db, "trash", item)) return;- await db.trash.add(item);- },- content: async function (db, item) {- if (await migrations.handleDeleted(db, "content", item)) return;- await db.content.add(item);- },- settings: async function (db, item) {- db.settings.merge(item);- },- },-};diff --git a/packages/core/migrations.js b/packages/core/migrations.jsnew file mode 100644index 000000000..ca849c83a--- /dev/null+++ b/packages/core/migrations.js@@ -0,0 +1,84 @@+export const migrations = {+ handleDeleted: async function (db, collection, item) {+ if (item.deleted) {+ await db[collection]._collection.addItem(item);+ return true;+ }+ return false;+ },+ 0: {+ notes: async function (db, item) {+ if (await migrations.handleDeleted(db, "notes", item)) return;++ const contentId = item.content.delta;+ const notebook = item.notebook;+ delete item.content;+ delete item.notebook;+ item.contentId = contentId;+ item.remote = true;+ if (notebook) item.notebooks = [notebook];+ await db.notes.add(item);+ },+ delta: async function (db, item) {+ if (await migrations.handleDeleted(db, "content", item)) return;++ item.data = item.data.ops;+ item.type = "delta";+ await db.content.add(item);+ },+ trash: async function (db, item) {+ if (await migrations.handleDeleted(db, "trash", item)) return;++ item.itemType = item.type;+ item.type = "trash";+ if (item.itemType === "note") {+ item.contentId = item.content.delta;+ delete item.content;+ }+ await db.trash.add(item);+ },+ text: function () {},+ },+ 2: {+ notes: async function (db, item) {+ if (await migrations.handleDeleted(db, "notes", item)) return;++ // notebook -> notebooks+ const notebook = item.notebook;+ delete item.notebook;+ item.remote = true;+ if (notebook) item.notebooks = [notebook];++ await db.notes.add({ ...item, remote: true });+ },+ },+ 3: {+ notes: async function (db, item) {+ if (await migrations.handleDeleted(db, "notes", item)) return;+ await db.notes.add({ ...item, remote: true });+ },+ notebooks: async function (db, item) {+ if (await migrations.handleDeleted(db, "notebooks", item)) return;+ await db.notebooks.add(item);+ },+ tags: async function (db, item) {+ if (await migrations.handleDeleted(db, "tags", item)) return;+ await db.tags.merge(item);+ },+ colors: async function (db, item) {+ if (await migrations.handleDeleted(db, "colors", item)) return;+ await db.colors.merge(item);+ },+ trash: async function (db, item) {+ if (await migrations.handleDeleted(db, "trash", item)) return;+ await db.trash.add(item);+ },+ content: async function (db, item) {+ if (await migrations.handleDeleted(db, "content", item)) return;+ await db.content.add(item);+ },+ settings: async function (db, item) {+ db.settings.merge(item);+ },+ },+};