first experiments with note entity

This commit is contained in:
azivner 2018-01-28 23:16:50 -05:00
parent 52ad7f64b4
commit 9b53a17168
2 changed files with 25 additions and 0 deletions

17
src/entities/note.js Normal file
View File

@ -0,0 +1,17 @@
"use strict";
class Note {
constructor(sql, row) {
this.sql = sql;
for (const key in row) {
this[key] = row[key];
}
}
async attributes() {
return this.sql.getAll("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]);
}
}
module.exports = Note;

View File

@ -6,6 +6,7 @@ const fs = require('fs');
const sqlite = require('sqlite'); const sqlite = require('sqlite');
const app_info = require('./app_info'); const app_info = require('./app_info');
const resource_dir = require('./resource_dir'); const resource_dir = require('./resource_dir');
const Note = require('../entities/note');
async function createConnection() { async function createConnection() {
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise}); return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
@ -133,6 +134,12 @@ async function getAll(query, params = []) {
return await wrap(async db => db.all(query, ...params)); return await wrap(async db => db.all(query, ...params));
} }
async function getAllEntities(query, params = []) {
const rows = await getAll(query, params);
return rows.map(row => new Note(module.exports, row));
}
async function getMap(query, params = []) { async function getMap(query, params = []) {
const map = {}; const map = {};
const results = await getAll(query, params); const results = await getAll(query, params);
@ -266,6 +273,7 @@ module.exports = {
getFirst, getFirst,
getFirstOrNull, getFirstOrNull,
getAll, getAll,
getAllEntities,
getMap, getMap,
getFirstColumn, getFirstColumn,
execute, execute,