diff --git a/src/entities/note.js b/src/entities/note.js new file mode 100644 index 000000000..e94cef380 --- /dev/null +++ b/src/entities/note.js @@ -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; \ No newline at end of file diff --git a/src/services/sql.js b/src/services/sql.js index 514ff3e69..a2d45f6b5 100644 --- a/src/services/sql.js +++ b/src/services/sql.js @@ -6,6 +6,7 @@ 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'); async function createConnection() { 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)); } +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 = []) { const map = {}; const results = await getAll(query, params); @@ -266,6 +273,7 @@ module.exports = { getFirst, getFirstOrNull, getAll, + getAllEntities, getMap, getFirstColumn, execute,