mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
basic entity support with lazy loaded relations
This commit is contained in:
parent
b44412bc32
commit
587f3d833e
11
src/entities/attribute.js
Normal file
11
src/entities/attribute.js
Normal file
@ -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;
|
13
src/entities/entity.js
Normal file
13
src/entities/entity.js
Normal file
@ -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;
|
@ -1,20 +1,22 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
class Note {
|
const Entity = require('./entity');
|
||||||
constructor(sql, row) {
|
|
||||||
this.sql = sql;
|
|
||||||
|
|
||||||
for (const key in row) {
|
class Note extends Entity {
|
||||||
this[key] = row[key];
|
async getAttributes() {
|
||||||
}
|
return this.sql.getEntities("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async attributes() {
|
async getAttribute(name) {
|
||||||
return this.sql.getRows("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]);
|
return this.sql.getEntity("SELECT * FROM attributes WHERE noteId = ? AND name = ?", [this.noteId, name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async revisions() {
|
async getRevisions() {
|
||||||
return this.sql.getRows("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]);
|
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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
src/entities/note_revision.js
Normal file
11
src/entities/note_revision.js
Normal file
@ -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;
|
15
src/entities/note_tree.js
Normal file
15
src/entities/note_tree.js
Normal file
@ -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;
|
@ -7,6 +7,9 @@ const sqlite = require('sqlite');
|
|||||||
const app_info = require('./app_info');
|
const app_info = require('./app_info');
|
||||||
const resource_dir = require('./resource_dir');
|
const resource_dir = require('./resource_dir');
|
||||||
const Note = require('../entities/note');
|
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() {
|
async function createConnection() {
|
||||||
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
|
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
|
||||||
@ -137,13 +140,40 @@ async function getRows(query, params = []) {
|
|||||||
async function getEntities(query, params = []) {
|
async function getEntities(query, params = []) {
|
||||||
const rows = await getRows(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 = []) {
|
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 = []) {
|
async function getMap(query, params = []) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user