|
|
|
@ -1,5 +1,6 @@ |
|
|
|
const { newid } = require("../hashing") |
|
|
|
const Replication = require("./Replication") |
|
|
|
const { getCouch } = require("./index") |
|
|
|
|
|
|
|
const UNICODE_MAX = "\ufff0" |
|
|
|
const SEPARATOR = "_" |
|
|
|
@ -25,6 +26,7 @@ const DocumentTypes = { |
|
|
|
APP: "app", |
|
|
|
APP_DEV: "app_dev", |
|
|
|
APP_METADATA: "app_metadata", |
|
|
|
ROLE: "role", |
|
|
|
} |
|
|
|
|
|
|
|
exports.DocumentTypes = DocumentTypes |
|
|
|
@ -32,6 +34,29 @@ exports.APP_PREFIX = DocumentTypes.APP + SEPARATOR |
|
|
|
exports.APP_DEV_PREFIX = DocumentTypes.APP_DEV + SEPARATOR |
|
|
|
exports.SEPARATOR = SEPARATOR |
|
|
|
|
|
|
|
/** |
|
|
|
* If creating DB allDocs/query params with only a single top level ID this can be used, this |
|
|
|
* is usually the case as most of our docs are top level e.g. tables, automations, users and so on. |
|
|
|
* More complex cases such as link docs and rows which have multiple levels of IDs that their |
|
|
|
* ID consists of need their own functions to build the allDocs parameters. |
|
|
|
* @param {string} docType The type of document which input params are being built for, e.g. user, |
|
|
|
* link, app, table and so on. |
|
|
|
* @param {string|null} docId The ID of the document minus its type - this is only needed if looking |
|
|
|
* for a singular document. |
|
|
|
* @param {object} otherProps Add any other properties onto the request, e.g. include_docs. |
|
|
|
* @returns {object} Parameters which can then be used with an allDocs request. |
|
|
|
*/ |
|
|
|
function getDocParams(docType, docId = null, otherProps = {}) { |
|
|
|
if (docId == null) { |
|
|
|
docId = "" |
|
|
|
} |
|
|
|
return { |
|
|
|
...otherProps, |
|
|
|
startkey: `${docType}${SEPARATOR}${docId}`, |
|
|
|
endkey: `${docType}${SEPARATOR}${docId}${UNICODE_MAX}`, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Generates a new group ID. |
|
|
|
* @returns {string} The new group ID which the group doc can be stored under. |
|
|
|
@ -101,6 +126,51 @@ exports.getTemplateParams = (ownerId, templateId, otherProps = {}) => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Generates a new role ID. |
|
|
|
* @returns {string} The new role ID which the role doc can be stored under. |
|
|
|
*/ |
|
|
|
exports.generateRoleID = id => { |
|
|
|
return `${DocumentTypes.ROLE}${SEPARATOR}${id || newid()}` |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Gets parameters for retrieving a role, this is a utility function for the getDocParams function. |
|
|
|
*/ |
|
|
|
exports.getRoleParams = (roleId = null, otherProps = {}) => { |
|
|
|
return getDocParams(DocumentTypes.ROLE, roleId, otherProps) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Lots of different points in the system need to find the full list of apps, this will |
|
|
|
* enumerate the entire CouchDB cluster and get the list of databases (every app). |
|
|
|
* NOTE: this operation is fine in self hosting, but cannot be used when hosting many |
|
|
|
* different users/companies apps as there is no security around it - all apps are returned. |
|
|
|
* @return {Promise<object[]>} returns the app information document stored in each app database. |
|
|
|
*/ |
|
|
|
exports.getAllApps = async (devApps = false) => { |
|
|
|
const CouchDB = getCouch() |
|
|
|
let allDbs = await CouchDB.allDbs() |
|
|
|
const appDbNames = allDbs.filter(dbName => |
|
|
|
dbName.startsWith(exports.APP_PREFIX) |
|
|
|
) |
|
|
|
const appPromises = appDbNames.map(db => new CouchDB(db).get(DocumentTypes.APP_METADATA)) |
|
|
|
if (appPromises.length === 0) { |
|
|
|
return [] |
|
|
|
} else { |
|
|
|
const response = await Promise.allSettled(appPromises) |
|
|
|
const apps = response |
|
|
|
.filter(result => result.status === "fulfilled") |
|
|
|
.map(({ value }) => value) |
|
|
|
return apps.filter(app => { |
|
|
|
if (devApps) { |
|
|
|
return app.appId.startsWith(exports.APP_DEV_PREFIX) |
|
|
|
} |
|
|
|
return !app.appId.startsWith(exports.APP_DEV_PREFIX) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Generates a new configuration ID. |
|
|
|
* @returns {string} The new configuration ID which the config doc can be stored under. |
|
|
|
|