From 69a18ad7d417eacaaa06ea275cdf764eaba4d94c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 26 May 2017 22:06:21 +0000 Subject: [PATCH] add getSiteStore --- lib/apis.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lib/apis.js b/lib/apis.js index a210b8f..7bce80f 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -75,6 +75,37 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { }); } + var modelsCache = {}; + function getSiteStore(clientUrih, pkgId, dir) { + if (modelsCache[clientUrih]) { + return modelsCache[clientUrih]; + } + + // DB scopes: + // system (global) + // experience (per domain) + // api (per api) + // account (per user account) + // client (per 3rd party client) + + // scope Experience to db + // scope Api by table + // scope Account and Client by column + modelsCache[clientUrih] = apiFactories.systemSqlFactory.create({ + init: true + , dbname: clientUrih // '#' is a valid file name character + }).then(function (db) { + var wrap = require('masterquest-sqlite3'); + + return wrap.wrap(db, dir).then(function (models) { + modelsCache[clientUrih] = PromiseA.resolve(models); + return models; + }); + }); + + return modelsCache[clientUrih]; + } + function loadRestHelper(myConf, clientUrih, pkgId) { var pkgPath = path.join(myConf.restPath, pkgId); @@ -183,6 +214,26 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { } }); + Object.defineProperty(req, 'getSiteStore', { + enumerable: true + , configurable: false + , writable: false + , value: function getSiteStoreProp() { + var restPath = path.join(myConf.restPath, pkgId); + var apiPath = path.join(myConf.apiPath, pkgId); + var dir; + + // TODO usage package.json as a falback if the standard location is not used + try { + dir = require(path.join(apiPath, 'models.js')); + } catch(e) { + dir = require(path.join(restPath, 'models.js')); + } + + return getSiteStore(clientUrih, pkgId, dir); + } + }); + req._walnutOriginalUrl = req.url; // "/path/api/com.example/hello".replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/') => '/hello' req.url = req.url.replace(/\/api\//, '').replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/');