basic entity support with lazy loaded relations

This commit is contained in:
azivner
2018-01-29 18:34:59 -05:00
parent b44412bc32
commit 587f3d833e
6 changed files with 95 additions and 13 deletions

11
src/entities/attribute.js Normal file
View 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
View 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;

View File

@@ -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]);
}
}

View 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
View 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;