created repository object to access entities

This commit is contained in:
azivner
2018-01-29 23:17:44 -05:00
parent 9a091408e3
commit 6fa6891496
12 changed files with 104 additions and 95 deletions

View File

@@ -6,10 +6,6 @@ const fs = require('fs');
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,45 +133,6 @@ async function getRows(query, params = []) {
return await wrap(async db => db.all(query, ...params));
}
async function getEntities(query, params = []) {
const rows = await getRows(query, params);
return rows.map(createEntityFromRow);
}
async function getEntity(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 entity;
}
async function getMap(query, params = []) {
const map = {};
const results = await getRows(query, params);
@@ -309,8 +266,6 @@ module.exports = {
getRow,
getRowOrNull,
getRows,
getEntities,
getEntity,
getMap,
getColumn,
execute,