Notes/src/becca/entity_constructor.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

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