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;
|
2020-04-15 11:53:57 +05:00
|
|
|
|
|
|
|
|
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,
|
2020-03-29 19:21:55 +05:00
|
|
|
data: content.data || content,
|
2020-04-07 15:26:09 +05:00
|
|
|
conflicted: content.conflicted || false,
|
2020-04-11 11:17:58 +05:00
|
|
|
resolved: !!content.resolved,
|
2020-04-07 15:26:09 +05:00
|
|
|
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
|
|
|
|
|
|
|
|
multi(ids) {
|
|
|
|
|
return this._collection.getItems(ids);
|
|
|
|
|
}
|
2020-03-19 14:03:29 +05:00
|
|
|
|
|
|
|
|
all() {
|
|
|
|
|
return this._collection.getItems(this._collection.indexer.indices);
|
|
|
|
|
}
|
2020-03-19 11:30:05 +05:00
|
|
|
}
|