mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 02:30:42 +01:00
basic entity support with lazy loaded relations
This commit is contained in:
@@ -7,6 +7,9 @@ const sqlite = require('sqlite');
|
||||
const app_info = require('./app_info');
|
||||
const resource_dir = require('./resource_dir');
|
||||
const Note = require('../entities/note');
|
||||
const NoteRevision = require('../entities/note_revision');
|
||||
const NoteTree = require('../entities/note_tree');
|
||||
const Attribute = require('../entities/attribute');
|
||||
|
||||
async function createConnection() {
|
||||
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
|
||||
@@ -137,13 +140,40 @@ async function getRows(query, params = []) {
|
||||
async function getEntities(query, params = []) {
|
||||
const rows = await getRows(query, params);
|
||||
|
||||
return rows.map(row => new Note(module.exports, row));
|
||||
return rows.map(createEntityFromRow);
|
||||
}
|
||||
|
||||
async function getEntity(query, params = []) {
|
||||
const rows = await getRows(query, params);
|
||||
const row = await getRowOrNull(query, params);
|
||||
|
||||
return rows.map(row => new Note(module.exports, row));
|
||||
if (!row) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return createEntityFromRow(row);
|
||||
}
|
||||
|
||||
function createEntityFromRow(row) {
|
||||
let entity;
|
||||
let sql = module.exports;
|
||||
|
||||
if (row.attributeId) {
|
||||
entity = new Attribute(sql, row);
|
||||
}
|
||||
else if (row.noteRevisionId) {
|
||||
entity = new NoteRevision(sql, row);
|
||||
}
|
||||
else if (row.noteTreeId) {
|
||||
entity = new NoteTree(sql, row);
|
||||
}
|
||||
else if (row.noteId) {
|
||||
entity = new Note(sql, row);
|
||||
}
|
||||
else {
|
||||
throw new Error('Unknown entity type for row: ' + JSON.stringify(row));
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
async function getMap(query, params = []) {
|
||||
|
||||
Reference in New Issue
Block a user