trilium/src/entities/entity_constructor.js

74 lines
2 KiB
JavaScript
Raw Normal View History

2020-06-20 18:31:38 +08:00
const repository = require('../services/repository');
const Note = require('../entities/note');
const NoteRevision = require('../entities/note_revision');
const Branch = require('../entities/branch');
const Attribute = require('../entities/attribute');
2018-04-02 00:03:21 +08:00
const RecentNote = require('../entities/recent_note');
2018-04-02 05:38:24 +08:00
const ApiToken = require('../entities/api_token');
const Option = require('../entities/option');
2020-04-07 04:21:09 +08:00
const cls = require('../services/cls');
const ENTITY_NAME_TO_ENTITY = {
2018-08-10 20:31:57 +08:00
"attributes": Attribute,
"branches": Branch,
"notes": Note,
"note_contents": Note,
2018-08-10 20:31:57 +08:00
"note_revisions": NoteRevision,
"note_revision_contents": NoteRevision,
2018-08-10 20:31:57 +08:00
"recent_notes": RecentNote,
"options": Option,
2019-01-03 05:36:06 +08:00
"api_tokens": ApiToken,
2018-08-10 02:08:00 +08:00
};
function getEntityFromEntityName(entityName) {
if (!(entityName in ENTITY_NAME_TO_ENTITY)) {
throw new Error(`Entity for table ${entityName} not found!`);
2018-08-10 20:31:57 +08:00
}
return ENTITY_NAME_TO_ENTITY[entityName];
2018-08-10 02:08:00 +08:00
}
function createEntityFromRow(row) {
let entity;
if (row.attributeId) {
entity = new Attribute(row);
2020-04-07 04:08:54 +08:00
cls.setEntityToCache('attributes', row.attributeId, entity);
}
else if (row.noteRevisionId) {
entity = new NoteRevision(row);
2020-04-07 04:08:54 +08:00
cls.setEntityToCache('note_revisions', row.noteRevisionId, entity);
}
2018-04-02 00:03:21 +08:00
else if (row.branchId && row.notePath) {
entity = new RecentNote(row);
}
2018-04-02 05:38:24 +08:00
else if (row.apiTokenId) {
entity = new ApiToken(row);
}
else if (row.branchId) {
entity = new Branch(row);
2020-04-07 04:08:54 +08:00
cls.setEntityToCache('branches', row.branchId, entity);
}
else if (row.noteId) {
entity = new Note(row);
2020-04-07 04:08:54 +08:00
cls.setEntityToCache('notes', row.noteId, entity);
}
else if (row.name) {
entity = new Option(row);
}
else {
throw new Error('Unknown entity type for row: ' + JSON.stringify(row));
}
return entity;
}
module.exports = {
2018-08-10 02:08:00 +08:00
createEntityFromRow,
getEntityFromEntityName
2018-08-10 02:08:00 +08:00
};