repository is now stateless

This commit is contained in:
azivner
2018-03-31 09:07:58 -04:00
parent 05676f3459
commit 088fb00ca9
9 changed files with 87 additions and 98 deletions

View File

@@ -3,6 +3,7 @@
const sql = require('./sql');
const utils = require('./utils');
const sync_table = require('./sync_table');
const repository = require('./repository');
const BUILTIN_LABELS = [
'frontend_startup',
@@ -29,7 +30,7 @@ async function getNoteIdWithLabel(name, value) {
AND labels.value = ?`, [name, value]);
}
async function getNotesWithLabel(repository, name, value) {
async function getNotesWithLabel(name, value) {
let notes;
if (value !== undefined) {
@@ -44,8 +45,8 @@ async function getNotesWithLabel(repository, name, value) {
return notes;
}
async function getNoteWithLabel(repository, name, value) {
const notes = getNotesWithLabel(repository, name, value);
async function getNoteWithLabel(name, value) {
const notes = getNotesWithLabel(name, value);
return notes.length > 0 ? notes[0] : null;
}

View File

@@ -1,70 +1,73 @@
"use strict";
const sql = require('./sql');
const protected_session = require('./protected_session');
const Note = require('../entities/note');
const NoteRevision = require('../entities/note_revision');
const Branch = require('../entities/branch');
const Label = require('../entities/label');
const sync_table = require('../services/sync_table');
class Repository {
async getEntities(query, params = []) {
const rows = await sql.getRows(query, params);
async function getEntities(query, params = []) {
const rows = await sql.getRows(query, params);
return rows.map(row => this.createEntityFromRow(row));
}
async getEntity(query, params = []) {
const row = await sql.getRowOrNull(query, params);
if (!row) {
return null;
}
return this.createEntityFromRow(row);
}
async getNote(noteId) {
return await this.getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
}
createEntityFromRow(row) {
let entity;
if (row.labelId) {
entity = new Label(this, row);
}
else if (row.noteRevisionId) {
entity = new NoteRevision(this, row);
}
else if (row.branchId) {
entity = new Branch(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;
}
async updateEntity(entity) {
if (entity.beforeSaving) {
entity.beforeSaving();
}
const clone = Object.assign({}, entity);
delete clone.jsonContent;
delete clone.repository;
await sql.replace(entity.constructor.tableName, clone);
const primaryKey = entity[entity.constructor.primaryKeyName];
await sync_table.addEntitySync(entity.constructor.tableName, primaryKey);
}
return rows.map(row => this.createEntityFromRow(row));
}
module.exports = Repository;
async function getEntity(query, params = []) {
const row = await sql.getRowOrNull(query, params);
if (!row) {
return null;
}
return this.createEntityFromRow(row);
}
async function getNote(noteId) {
return await this.getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
}
function createEntityFromRow(row) {
let entity;
if (row.labelId) {
entity = new Label(row);
}
else if (row.noteRevisionId) {
entity = new NoteRevision(row);
}
else if (row.branchId) {
entity = new Branch(row);
}
else if (row.noteId) {
entity = new Note(row);
}
else {
throw new Error('Unknown entity type for row: ' + JSON.stringify(row));
}
return entity;
}
async function updateEntity(entity) {
if (entity.beforeSaving) {
entity.beforeSaving();
}
const clone = Object.assign({}, entity);
delete clone.jsonContent;
await sql.replace(entity.constructor.tableName, clone);
const primaryKey = entity[entity.constructor.primaryKeyName];
await sync_table.addEntitySync(entity.constructor.tableName, primaryKey);
}
module.exports = {
getEntities,
getEntity,
getNote,
updateEntity
};

View File

@@ -1,11 +1,9 @@
const script = require('./script');
const Repository = require('./repository');
const repository = require('./repository');
const cls = require('./cls');
const repo = new Repository();
async function runNotesWithLabel(runAttrValue) {
const notes = await repo.getEntities(`
const notes = await repository.getEntities(`
SELECT notes.*
FROM notes
JOIN labels ON labels.noteId = notes.noteId
@@ -17,7 +15,7 @@ async function runNotesWithLabel(runAttrValue) {
AND notes.isDeleted = 0`, [runAttrValue]);
for (const note of notes) {
script.executeNote(null, note);
script.executeNote(note);
}
}

View File

@@ -1,6 +1,6 @@
const sql = require('./sql');
const ScriptContext = require('./script_context');
const Repository = require('./repository');
const repository = require('./repository');
async function executeNote(note) {
if (!note.isJavaScript()) {
@@ -36,7 +36,6 @@ async function executeBundle(bundle, startNote) {
* bundle's startNote.
*/
async function executeScript(script, params, startNoteId, currentNoteId) {
const repository = new Repository();
const startNote = await repository.getNote(startNoteId);
const currentNote = await repository.getNote(currentNoteId);

View File

@@ -1,12 +1,11 @@
const log = require('./log');
const protected_session = require('./protected_session');
const notes = require('./notes');
const sql = require('./sql');
const utils = require('./utils');
const labels = require('./labels');
const date_notes = require('./date_notes');
const config = require('./config');
const Repository = require('./repository');
const repository = require('./repository');
const axios = require('axios');
function ScriptContext(startNote, allNotes) {
@@ -28,7 +27,6 @@ function ScriptContext(startNote, allNotes) {
}
function ScriptApi(startNote, currentNote) {
const repository = new Repository();
this.startNote = startNote;
this.currentNote = currentNote;
@@ -47,7 +45,7 @@ function ScriptApi(startNote, currentNote) {
};
this.getNotesWithLabel = async function (attrName, attrValue) {
return await labels.getNotesWithLabel(repository, attrName, attrValue);
return await labels.getNotesWithLabel(attrName, attrValue);
};
this.getNoteWithLabel = async function (attrName, attrValue) {