add getSiteStore

This commit is contained in:
AJ ONeal 2017-05-26 22:06:21 +00:00
parent 72032b644b
commit 69a18ad7d4
1 changed files with 51 additions and 0 deletions

View File

@ -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) { function loadRestHelper(myConf, clientUrih, pkgId) {
var pkgPath = path.join(myConf.restPath, 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; req._walnutOriginalUrl = req.url;
// "/path/api/com.example/hello".replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/') => '/hello' // "/path/api/com.example/hello".replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/') => '/hello'
req.url = req.url.replace(/\/api\//, '').replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/'); req.url = req.url.replace(/\/api\//, '').replace(/.*\/api\//, '').replace(/([^\/]*\/+)/, '/');