Skip to content

Build your personal knowledge base with Trilium Notes

basic entity support with lazy loaded relations

azivner587f3d833e14
diff --git a/src/entities/attribute.js b/src/entities/attribute.js
new file mode 100644
index 0000000000..2cecc29ddd
--- /dev/null
+++ b/src/entities/attribute.js
@@ -0,0 +1,11 @@
+"use strict";
+
+const Entity = require('./entity');
+
+class Attribute extends Entity {
+ async getNote() {
+ return this.sql.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
+ }
+}
+
+module.exports = Attribute;
\ No newline at end of file
diff --git a/src/entities/entity.js b/src/entities/entity.js
new file mode 100644
index 0000000000..0228fab42c
--- /dev/null
+++ b/src/entities/entity.js
@@ -0,0 +1,13 @@
+"use strict";
+
+class Entity {
+ constructor(sql, row) {
+ this.sql = sql;
+
+ for (const key in row) {
+ this[key] = row[key];
+ }
+ }
+}
+
+module.exports = Entity;
\ No newline at end of file
diff --git a/src/entities/note.js b/src/entities/note.js
index 367ef4ce1a..9e255df1a8 100644
--- a/src/entities/note.js
+++ b/src/entities/note.js
@@ -1,20 +1,22 @@
"use strict";
-class Note {
- constructor(sql, row) {
- this.sql = sql;
+const Entity = require('./entity');
- for (const key in row) {
- this[key] = row[key];
- }
+class Note extends Entity {
+ async getAttributes() {
+ return this.sql.getEntities("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]);
}
- async attributes() {
- return this.sql.getRows("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]);
+ async getAttribute(name) {
+ return this.sql.getEntity("SELECT * FROM attributes WHERE noteId = ? AND name = ?", [this.noteId, name]);
}
- async revisions() {
- return this.sql.getRows("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]);
+ async getRevisions() {
+ return this.sql.getEntities("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]);
+ }
+
+ async getTrees() {
+ return this.sql.getEntities("SELECT * FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [this.noteId]);
}
}
diff --git a/src/entities/note_revision.js b/src/entities/note_revision.js
new file mode 100644
index 0000000000..17419b6e1f
--- /dev/null
+++ b/src/entities/note_revision.js
@@ -0,0 +1,11 @@
+"use strict";
+
+const Entity = require('./entity');
+
+class NoteRevision extends Entity {
+ async getNote() {
+ return this.sql.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
+ }
+}
+
+module.exports = NoteRevision;
\ No newline at end of file
diff --git a/src/entities/note_tree.js b/src/entities/note_tree.js
new file mode 100644
index 0000000000..391c4780b5
--- /dev/null
+++ b/src/entities/note_tree.js
@@ -0,0 +1,15 @@
+"use strict";
+
+const Entity = require('./entity');
+
+class NoteTree extends Entity {
+ async getNote() {
+ return this.sql.getEntity("SELECT * FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [this.noteId]);
+ }
+
+ async getParentNote() {
+ return this.sql.getEntity("SELECT * FROM note_tree WHERE isDeleted = 0 AND parentNoteId = ?", [this.parentNoteId]);
+ }
+}
+
+module.exports = NoteTree;
\ No newline at end of file
diff --git a/src/services/sql.js b/src/services/sql.js
index 82dedba64b..cbbd732d41 100644
--- a/src/services/sql.js
+++ b/src/services/sql.js
@@ -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);
+
+ 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 rows.map(row => new Note(module.exports, row));
+ return entity;
}
async function getMap(query, params = []) {