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

@@ -3,7 +3,7 @@
const sql = require('./sql');
const utils = require('./utils');
const sync_table = require('./sync_table');
const protected_session = require('./protected_session');
const Repository = require('./repository');
async function getNoteAttributeMap(noteId) {
return await sql.getMap(`SELECT name, value FROM attributes WHERE noteId = ?`, [noteId]);
@@ -15,21 +15,19 @@ async function getNoteIdWithAttribute(name, value) {
}
async function getNotesWithAttribute(dataKey, name, value) {
const repository = new Repository(dataKey);
let notes;
if (value !== undefined) {
notes = await sql.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
}
else {
notes = await sql.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]);
}
for (const note of notes) {
protected_session.decryptNote(dataKey, note);
}
return notes;
}

View File

@@ -5,21 +5,6 @@ const sync_table = require('./sync_table');
const attributes = require('./attributes');
const protected_session = require('./protected_session');
async function getNoteById(noteId, dataKey) {
const note = await sql.getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
protected_session.decryptNote(dataKey, note);
return note;
}
async function getJsonNoteById(noteId, dataKey) {
const note = await getNoteById(noteId, dataKey);
note.data = JSON.parse(note.content);
return note;
}
async function updateJsonNote(noteId, data) {
const ret = await createNewNote(noteId, {
title: name,
@@ -329,7 +314,6 @@ async function deleteNote(noteTreeId, sourceId) {
}
module.exports = {
getNoteById,
createNewNote,
updateNote,
deleteNote,

View File

@@ -0,0 +1,62 @@
const sql = require('./sql');
const protected_session = require('./protected_session');
const Note = require('../entities/note');
const NoteRevision = require('../entities/note_revision');
const NoteTree = require('../entities/note_tree');
const Attribute = require('../entities/attribute');
class Repository {
constructor(dataKey) {
this.dataKey = protected_session.getDataKey(dataKey);
}
async getEntities(query, params = []) {
const rows = await sql.getRows(query, params);
for (const row of rows) {
row.dataKey = this.dataKey;
}
return rows.map(row => this.createEntityFromRow(row));
}
async getEntity(query, params = []) {
const row = await sql.getRowOrNull(query, params);
if (!row) {
return null;
}
row.dataKey = this.dataKey;
return this.createEntityFromRow(row);
}
async getNote(noteId) {
return await this.getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
}
createEntityFromRow(row) {
let entity;
if (row.attributeId) {
entity = new Attribute(this, row);
}
else if (row.noteRevisionId) {
entity = new NoteRevision(this, row);
}
else if (row.noteTreeId) {
entity = new NoteTree(this, row);
}
else if (row.noteId) {
entity = new Note(this, row);
}
else {
throw new Error('Unknown entity type for row: ' + JSON.stringify(row));
}
return entity;
}
}
module.exports = Repository;

View File

@@ -5,17 +5,19 @@ const attributes = require('./attributes');
const date_notes = require('./date_notes');
const sql = require('./sql');
const sync_table = require('./sync_table');
const Repository = require('./repository');
function ScriptContext(noteId, dataKey) {
this.scriptNoteId = noteId;
this.dataKey = protected_session.getDataKey(dataKey);
this.repository = new Repository(dataKey);
function serializePayload(payload) {
return JSON.stringify(payload, null, '\t');
}
this.getNoteById = async function(noteId) {
return notes.getNoteById(noteId, this.dataKey);
return this.repository.getNote(noteId);
};
this.getNotesWithAttribute = async function (attrName, attrValue) {
@@ -60,7 +62,7 @@ function ScriptContext(noteId, dataKey) {
};
this.updateNote = async function (note) {
if (note.type === 'code' && note.mime === 'application/json') {
if (note.isJson()) {
note.content = serializePayload(note.jsonContent);
}

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,