ldc-com-app/ComApp/src/App/Models/Document.js
2022-10-06 16:25:15 +02:00

64 lines
1.9 KiB
JavaScript

class DocumentModel {
constructor(realm) {
this.schema = 'Documents';
this.realm = realm;
}
add(document) {
this.realm.write(() => {
this.realm.create(this.schema, document, 'never');
});
}
update(document, updatedAtChange = false) {
let tmp = this.get(document.id);
if (tmp === undefined) {
this.add(document);
} else {
this.realm.write(() => {
if (updatedAtChange === false) {
this.realm.create(this.schema, {
id: tmp.id,
uuid: document.uuid,
title: document.title,
type: document.type,
path: document.path,
directory: document.directory
}, 'modified');
} else {
this.realm.create(this.schema, document, 'modified');
}
});
}
}
get(id) {
let tmp = this.realm.objects(this.schema).filtered('id = ' + id)[0];
return tmp;
}
getAll(authorizedDirs = false) {
let tmp = this.realm.objects(this.schema);
if (authorizedDirs) {
authorizedDirs = authorizedDirs.map(item => item.id);
tmp = tmp.filter(doc => authorizedDirs.includes(doc.directory));
}
return tmp;
}
clear(docs, toDelete = []) {
const localDocs = this.getAll();
localDocs.forEach(doc => {
let tmp = docs.filter(item => item.uuid === doc.uuid);
if (tmp.length === 0) {
toDelete.push({ uuid: doc.uuid, type: doc.type });
this.realm.write(() => {
this.realm.delete(doc);
});
}
});
return toDelete;
}
}
export default DocumentModel;