From 4151d68f2e49cf8244f14aaa245943b8e5af6f0d Mon Sep 17 00:00:00 2001 From: Lachlan Kermode Date: Fri, 7 Dec 2018 16:37:27 +0000 Subject: [PATCH 1/5] update docs Interface.js, abstract model layer errors --- src/copy/en.js | 3 ++- src/lib/errors.js | 13 ++++++++++++ src/models/Interface.js | 45 +++++++++++++++++++++++++---------------- src/models/StoreJson.js | 8 ++++---- 4 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 src/lib/errors.js diff --git a/src/copy/en.js b/src/copy/en.js index ef50d69..8c3350b 100644 --- a/src/copy/en.js +++ b/src/copy/en.js @@ -5,7 +5,8 @@ export default { onlyTab: 'You cannot query a tab directly. The URL needs to be in the format /:sheet/:tab/:resource.', noSheet: sheet => `The sheet ${sheet} is not available in this server.`, noResource: prts => `The resource '${prts[2]}' does not exists in the tab '${prts[1]}' in this sheet.`, - noFragment: prts => `Fragment index does not exist` + noFragment: prts => `Fragment index does not exist`, + modelLayer: prts => `Something went wrong at the model layer` }, success: { update: 'All sheets updated' diff --git a/src/lib/errors.js b/src/lib/errors.js new file mode 100644 index 0000000..d15b7f0 --- /dev/null +++ b/src/lib/errors.js @@ -0,0 +1,13 @@ +import copy from '../copy/en' + +export function modelLayerGeneric (parts) { + return new Error(copy.errors.modelLayer(parts)) +} + +export function noFragment (parts) { + return new Error(copy.errors.noFragment(parts)) +} + +export function noResource (parts) { + return new Error(copy.errors.noResource(parts)) +} diff --git a/src/models/Interface.js b/src/models/Interface.js index 60f98dd..71854fc 100644 --- a/src/models/Interface.js +++ b/src/models/Interface.js @@ -1,30 +1,41 @@ /* eslint-disable */ /** - * Model is a class whose sole responsibility is to save and load blueprints. - * It allows for different storage mechanisms for different kinds of blueprints. + * Model is a class whose sole responsibility is to save and load data through a custom URL format. + * As an interfacce, it allows for different storage mechanisms, and different scale/performance for different kinds of data. * * ERRORS: * When a load function fails, it should throw either: - * 1. A __ error if the resource doesn't exist on that sheet/tab. - * 2. A __ error if a fragment lookup fails because it doesn't exist. - * 3. A __ error if something else goes wrong. + * 1. noResource(parts) if the resource doesn't exist on that sheet/tab. + * 2. noFragment(parts) if a fragment lookup fails because it doesn't exist. + * 3. modelLayerGeneric(parts) if something else goes wrong. + * + * This is a WIP layer. See StoreJson.js for an example in action. */ class Model { /** - * save - save a Blueprint, using the information it contains. - * - * @param {type} blueprint the Blueprint to be saved. - * @return {type} Promise which returns True. + * Index the data stored by this model, returning a list of the available URLs. + * @return {Promise(boolean)} Unpacks to a list of available URLs if successful, throws an error otherwise. */ - save (blueprint) {} + index () {} + + /** + * Save data at a URL. The URL is in the format + * /:fetcherID/:tab/:resource + * Fetcher IDs must be unique, tabs and resources can be duplicated across + * different fetchers. + * + * @param {string} url - the URL at which to save the data. + * @param {object} data - the data to be saved. + * @return {Promise(boolean)} Unpacks to true if the update was successful, false if otherwise. + */ + save (url, data) {} /** - * load - load a resource from a data model, using a Blueprint object as - * well as a REST-like URL of the format /:source/:tab/:resource. - * - * @param {type} url String that represents the path to resource. - * @param {type} blueprint Blueprint object (desaturated?). - * @return {type} Object containing the resource data. + * Load data from a URL, in the format + * /:fetcherID/:tab/:resource + * + * @param {string} url - the URL at which to load the data. + * @return {Promise(object)} a Promise that unpacks to the data retrieved. An error will be thrown if the URL is invalid. */ - load (url, blueprint) {} + load (url) {} } diff --git a/src/models/StoreJson.js b/src/models/StoreJson.js index 1124960..72ca593 100644 --- a/src/models/StoreJson.js +++ b/src/models/StoreJson.js @@ -1,5 +1,5 @@ import fs from 'mz/fs' -import copy from '../copy/en' +import errors from '../lib/errors' const STORAGE_DIRNAME = 'temp' @@ -45,7 +45,7 @@ class StoreJson { if (!isNaN(id) && id >= 0 && id < data.length) { return data[id] } else { - throw new Error(copy.errors.noFragment(parts)) + throw errors.noFragment(parts) } } else { // Do a lookup if fragment is included to filter a relevant item @@ -53,12 +53,12 @@ class StoreJson { if (!isNaN(index) && index >= 0 && index < data.length) { return data.filter((vl, idx) => idx === index)[0] } else { - throw new Error(copy.errors.noFragment(parts)) + throw errors.noFragment(parts) } } }) } else { - return Promise.reject(new Error(copy.errors.noResource(parts))) + return Promise.reject(errors.noResource(parts)) } } From b2276c694ee8e27e55b5dcd62b30ae929101ba5d Mon Sep 17 00:00:00 2001 From: Lachlan Kermode Date: Tue, 11 Dec 2018 11:56:36 +0000 Subject: [PATCH 2/5] export errors on default obj --- src/lib/errors.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib/errors.js b/src/lib/errors.js index d15b7f0..81708b8 100644 --- a/src/lib/errors.js +++ b/src/lib/errors.js @@ -11,3 +11,9 @@ export function noFragment (parts) { export function noResource (parts) { return new Error(copy.errors.noResource(parts)) } + +export default { + modelLayerGeneric, + noFragment, + noResource +} From f909abfdc0fa13afc325c170e950019f394399ec Mon Sep 17 00:00:00 2001 From: Lachlan Kermode Date: Wed, 12 Dec 2018 16:07:19 +0000 Subject: [PATCH 3/5] fix retrieveFrag call --- src/lib/Fetcher.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/Fetcher.js b/src/lib/Fetcher.js index a84e49c..20beca9 100644 --- a/src/lib/Fetcher.js +++ b/src/lib/Fetcher.js @@ -189,7 +189,7 @@ class Fetcher { retrieveFrag (tab, resource, frag) { const title = fmtName(tab) - const url = `${this.sheetName}/${tab}/${resource}/${frag}` + const url = `${this.id}/${tab}/${resource}/${frag}` return this.db.load(url, this.blueprints[title]) } } From bbea550c876a4136e9c66281ea9906053b2ce0b4 Mon Sep 17 00:00:00 2001 From: Lachlan Kermode Date: Thu, 13 Dec 2018 12:35:27 +0000 Subject: [PATCH 4/5] fix ids blueprinter --- src/blueprinters/ids.js | 3 ++- src/lib/util.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/blueprinters/ids.js b/src/blueprinters/ids.js index d027be1..26705b9 100644 --- a/src/blueprinters/ids.js +++ b/src/blueprinters/ids.js @@ -32,11 +32,12 @@ export default function ids ( const itemLabels = data[0] const fmt = fmtObj(itemLabels) bp.resources[label] = R.clone(defaultResource) - bp.resources[label].data = [] + bp.resources[label].data = {} data.forEach((row, idx) => { if (idx === 0) return bp.resources[label].data[fmt(row).id] = fmt(row) }) + return bp } diff --git a/src/lib/util.js b/src/lib/util.js index c6b29b8..59cb6f4 100755 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -37,7 +37,8 @@ export const fmtObj = R.curry( } } columnNames.forEach((columnName, idx) => { - obj[fmtColName(columnName)] = row[idx] + const value = row[idx] ? row[idx] : "" + obj[fmtColName(columnName)] = value }) return obj } From 84237fcf14cf799c430ec7e4539b71b1bef1e33d Mon Sep 17 00:00:00 2001 From: Lachlan Kermode Date: Thu, 13 Dec 2018 12:42:53 +0000 Subject: [PATCH 5/5] fix lint --- src/lib/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/util.js b/src/lib/util.js index 59cb6f4..e213df1 100755 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -37,7 +37,7 @@ export const fmtObj = R.curry( } } columnNames.forEach((columnName, idx) => { - const value = row[idx] ? row[idx] : "" + const value = row[idx] ? row[idx] : '' obj[fmtColName(columnName)] = value }) return obj