Skip to content

Commit

Permalink
All: Resolves #918: Skip properties that are on sync target but not h…
Browse files Browse the repository at this point in the history
…andled by local client
  • Loading branch information
laurent22 committed Oct 31, 2018
1 parent 990591c commit e41896d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
26 changes: 20 additions & 6 deletions CliClient/tests/models_BaseItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { time } = require('lib/time-utils.js');
const { asyncTest, fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('test-utils.js');
const Folder = require('lib/models/Folder.js');
const Note = require('lib/models/Note.js');
const BaseItem = require('lib/models/BaseItem.js');
const Resource = require('lib/models/Resource.js');
const BaseModel = require('lib/BaseModel.js');
const { shim } = require('lib/shim');
Expand All @@ -26,12 +27,25 @@ describe('models_BaseItem', function() {
done();
});

it('should be able to exclude keys when syncing', asyncTest(async () => {
let folder1 = await Folder.save({ title: "folder1" });
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg');
let resource1 = (await Resource.all())[0];
console.info(await Resource.serializeForSync(resource1));
// it('should be able to exclude keys when syncing', asyncTest(async () => {
// let folder1 = await Folder.save({ title: "folder1" });
// let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
// await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg');
// let resource1 = (await Resource.all())[0];
// console.info(await Resource.serializeForSync(resource1));
// }));

// This is to handle the case where a property is removed from a BaseItem table - in that case files in
// the sync target will still have the old property but we don't need it locally.
it('should ignore properties that are present in sync file but not in database when serialising', asyncTest(async () => {
let folder = await Folder.save({ title: "folder1" });

let serialized = await Folder.serialize(folder);
serialized += "\nignore_me: true"

let unserialized = await Folder.unserialize(serialized);

expect('ignore_me' in unserialized).toBe(false);
}));

});
10 changes: 10 additions & 0 deletions ReactNativeClient/lib/BaseModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class BaseModel {
return this.db().tableFields(this.tableName());
}

static removeUnknownFields(model) {
const newModel = {};
for (let n in model) {
if (!model.hasOwnProperty(n)) continue;
if (!this.hasField(n) && n !== 'type_') continue;
newModel[n] = model[n];
}
return newModel;
}

static new() {
let fields = this.fields();
let output = {};
Expand Down
5 changes: 4 additions & 1 deletion ReactNativeClient/lib/models/BaseItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class BaseItem extends BaseModel {
static unserialize_format(type, propName, propValue) {
if (propName[propName.length - 1] == '_') return propValue; // Private property

let ItemClass = this.itemClass(type);
const ItemClass = this.itemClass(type);

if (['created_time', 'updated_time', 'user_created_time', 'user_updated_time'].indexOf(propName) >= 0) {
if (!propValue) return 0;
Expand Down Expand Up @@ -378,6 +378,9 @@ class BaseItem extends BaseModel {

if (output.type_ === BaseModel.TYPE_NOTE) output.body = body.join("\n");

const ItemClass = this.itemClass(output.type_);
output = ItemClass.removeUnknownFields(output);

for (let n in output) {
if (!output.hasOwnProperty(n)) continue;
output[n] = await this.unserialize_format(output.type_, n, output[n]);
Expand Down

0 comments on commit e41896d

Please sign in to comment.