1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Improved handling of base item type

This commit is contained in:
Laurent Cozic 2017-06-17 19:40:08 +01:00
parent b0cc9ec18d
commit 06dc16bcb4
6 changed files with 42 additions and 17 deletions

View File

@ -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);
});

View File

@ -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);

View File

@ -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;
}

View File

@ -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() {

View File

@ -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() {

View File

@ -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;