From 06dc16bcb4e4b6aa52546a77609a9be7e0167702 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 17 Jun 2017 19:40:08 +0100 Subject: [PATCH] Improved handling of base item type --- CliClient/app/cmd.js | 8 ++++-- ReactNativeClient/src/base-model.js | 27 ++++++++++++++++--- ReactNativeClient/src/models/base-item.js | 12 ++++++--- ReactNativeClient/src/models/folder.js | 2 +- ReactNativeClient/src/models/note.js | 2 +- .../src/services/note-folder-service.js | 8 ++---- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/CliClient/app/cmd.js b/CliClient/app/cmd.js index c09f405faa..85ca044bdf 100644 --- a/CliClient/app/cmd.js +++ b/CliClient/app/cmd.js @@ -51,6 +51,7 @@ async function runTest() { await clearDatabase(); let folder = await Folder.save({ title: "folder1" }); + //console.info(folder); let note1 = await Note.save({ title: "un", parent_id: folder.id }); await Note.save({ title: "deux", parent_id: folder.id }); folder = await Folder.save({ title: "folder2" }); @@ -59,13 +60,16 @@ async function runTest() { await synchronizer.start(); note1 = await Note.load(note1.id); + //console.info(note1); note1.title = 'un update'; await Note.save(note1); - await synchronizer.start(); + return await synchronizer.start(); } -runTest(); +runTest().catch((error) => { + console.error(error); +}); diff --git a/ReactNativeClient/src/base-model.js b/ReactNativeClient/src/base-model.js index 67b9726fe7..63a66d5a09 100644 --- a/ReactNativeClient/src/base-model.js +++ b/ReactNativeClient/src/base-model.js @@ -4,6 +4,22 @@ import { uuid } from 'src/uuid.js'; class BaseModel { + static addModelMd(model) { + if (!model) return model; + + if (Array.isArray(model)) { + let output = []; + for (let i = 0; i < model.length; i++) { + output.push(this.addModelMd(model[i])); + } + return output; + } else { + model = Object.assign({}, model); + model.type_ = this.itemType(); + return model; + } + } + static tableName() { throw new Error('Must be overriden'); } @@ -92,12 +108,16 @@ class BaseModel { static modelSelectOne(sql, params = null) { if (params === null) params = []; - return this.db().selectOne(sql, params); + return this.db().selectOne(sql, params).then((model) => { + return this.addModelMd(model); + }); } static modelSelectAll(sql, params = null) { if (params === null) params = []; - return this.db().selectAll(sql, params); + return this.db().selectAll(sql, params).then((models) => { + return this.addModelMd(models); + }); } static loadByField(fieldName, fieldValue) { @@ -145,7 +165,7 @@ class BaseModel { if (isNew) { if (this.useUuid() && !o.id) { - o = Object.assign({}, o); + //o = Object.assign({}, o); itemId = uuid.create(); o.id = itemId; } @@ -212,6 +232,7 @@ class BaseModel { return this.db().transactionExecBatch(queries).then(() => { o = Object.assign({}, o); o.id = itemId; + o = this.addModelMd(o); return o; }).catch((error) => { Log.error('Cannot save model', error); diff --git a/ReactNativeClient/src/models/base-item.js b/ReactNativeClient/src/models/base-item.js index f7dc0fd0da..301508cccb 100644 --- a/ReactNativeClient/src/models/base-item.js +++ b/ReactNativeClient/src/models/base-item.js @@ -15,6 +15,12 @@ class BaseItem extends BaseModel { return folderItemFilename(item) + '.md'; } + static itemClass(item) { + if (!item) throw new Error('Item cannot be null'); + if (!('type_' in item)) throw new Error('Item does not have a type_ property'); + return item.type_ == BaseModel.ITEM_TYPE_NOTE ? Note : Folder; + } + static pathToId(path) { let s = path.split('.'); return s[0]; @@ -40,7 +46,7 @@ class BaseItem extends BaseModel { } static fromFriendlyString_format(propName, propValue) { - if (propName == 'type') return propValue; + if (propName == 'type_') return propValue; if (['created_time', 'updated_time'].indexOf(propName) >= 0) { if (!propValue) return 0; @@ -65,8 +71,6 @@ class BaseItem extends BaseModel { output.push(shownKeys[i] + ': ' + v); } - output.push('type: ' + type); - return output.join("\n"); } @@ -100,7 +104,7 @@ class BaseItem extends BaseModel { let title = body.splice(0, 2); output.title = title[0]; - if (output.type == 'note') output.body = body.join("\n"); + if (output.type_ == BaseModel.ITEM_TYPE_NOTE) output.body = body.join("\n"); return output; } diff --git a/ReactNativeClient/src/models/folder.js b/ReactNativeClient/src/models/folder.js index 5cdd8db62c..51b8927525 100644 --- a/ReactNativeClient/src/models/folder.js +++ b/ReactNativeClient/src/models/folder.js @@ -14,7 +14,7 @@ class Folder extends BaseItem { } static toFriendlyString(folder) { - return super.toFriendlyString(folder, 'folder', ['id', 'created_time', 'updated_time']); + return super.toFriendlyString(folder, 'folder', ['id', 'created_time', 'updated_time', 'type_']); } static itemType() { diff --git a/ReactNativeClient/src/models/note.js b/ReactNativeClient/src/models/note.js index 0c6cb2e43e..1341beef37 100644 --- a/ReactNativeClient/src/models/note.js +++ b/ReactNativeClient/src/models/note.js @@ -13,7 +13,7 @@ class Note extends BaseItem { } static toFriendlyString(note, type = null, shownKeys = null) { - return super.toFriendlyString(note, 'note', ["author", "longitude", "latitude", "is_todo", "todo_due", "todo_completed", 'created_time', 'updated_time', 'id', 'parent_id']); + return super.toFriendlyString(note, 'note', ["author", "longitude", "latitude", "is_todo", "todo_due", "todo_completed", 'created_time', 'updated_time', 'id', 'parent_id', 'type_']); } static itemType() { diff --git a/ReactNativeClient/src/services/note-folder-service.js b/ReactNativeClient/src/services/note-folder-service.js index dd858f10ec..98d903bd3f 100644 --- a/ReactNativeClient/src/services/note-folder-service.js +++ b/ReactNativeClient/src/services/note-folder-service.js @@ -2,6 +2,7 @@ import { BaseService } from 'src/base-service.js'; import { BaseModel } from 'src/base-model.js'; +import { BaseItem } from 'src/models/base-item.js'; import { Note } from 'src/models/note.js'; import { Folder } from 'src/models/folder.js'; import { Log } from 'src/log.js'; @@ -20,12 +21,7 @@ class NoteFolderService extends BaseService { } } - let ItemClass = null; - if (type == 'note') { - ItemClass = Note; - } else if (type == 'folder') { - ItemClass = Folder; - } + let ItemClass = BaseItem.itemClass(item); let isNew = !item.id; let output = null;