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

View File

@@ -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 = []) {