Files
notesnook/packages/core/collections/content.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-04-16 02:14:53 +05:00
import Collection from "./collection";
2020-03-19 11:30:05 +05:00
import getId from "../utils/id";
2020-04-16 02:14:53 +05:00
export default class Content extends Collection {
2020-03-19 11:30:05 +05:00
async add(content) {
if (!content) return;
if (content.id && (await this._collection.exists(content.id))) {
content = {
...(await this.raw(content.id)),
...content,
};
}
2020-03-19 11:30:05 +05:00
const id = content.id || getId();
2020-03-19 12:38:33 +05:00
await this._collection.addItem({
noteId: content.noteId,
id,
type: content.type,
data: content.data || content,
conflicted: content.conflicted || false,
resolved: !!content.resolved,
dateEdited: content.dateEdited,
dateCreated: content.dateCreated,
remote: content.remote,
2020-03-19 12:38:33 +05:00
});
2020-03-19 11:30:05 +05:00
return id;
}
async get(id) {
2020-03-19 12:38:33 +05:00
const content = await this.raw(id);
2020-03-19 11:30:05 +05:00
if (!content) return;
return content.data;
}
2020-03-19 12:38:33 +05:00
async raw(id) {
const content = await this._collection.getItem(id);
if (!content) return;
return content;
}
2020-03-19 11:30:05 +05:00
remove(id) {
if (!id) return;
return this._collection.removeItem(id);
}
2020-03-19 12:38:33 +05:00
2020-03-19 14:03:29 +05:00
all() {
return this._collection.getItems();
2020-03-19 14:03:29 +05:00
}
2020-03-19 11:30:05 +05:00
}