2020-10-03 00:37:46 +13:00
|
|
|
const newid = require("./newid")
|
|
|
|
|
2020-10-09 21:47:37 +13:00
|
|
|
const UNICODE_MAX = "\ufff0"
|
|
|
|
const SEPARATOR = "_"
|
|
|
|
|
2020-10-02 05:22:08 +13:00
|
|
|
const DocumentTypes = {
|
2020-10-10 06:49:23 +13:00
|
|
|
TABLE: "ta",
|
2020-10-10 07:16:33 +13:00
|
|
|
ROW: "ro",
|
2020-10-09 21:47:37 +13:00
|
|
|
USER: "us",
|
|
|
|
AUTOMATION: "au",
|
|
|
|
LINK: "li",
|
2020-10-02 05:22:08 +13:00
|
|
|
APP: "app",
|
2020-10-09 21:47:37 +13:00
|
|
|
ACCESS_LEVEL: "ac",
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.DocumentTypes = DocumentTypes
|
2020-10-09 21:47:37 +13:00
|
|
|
exports.SEPARATOR = SEPARATOR
|
2020-10-09 22:46:38 +13:00
|
|
|
exports.UNICODE_MAX = UNICODE_MAX
|
2020-10-02 05:22:08 +13:00
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
2020-10-03 02:19:39 +13:00
|
|
|
* If creating DB allDocs/query params with only a single top level ID this can be used, this
|
2020-10-10 06:49:23 +13:00
|
|
|
* is usually the case as most of our docs are top level e.g. tables, automations, users and so on.
|
2020-10-10 07:10:28 +13:00
|
|
|
* More complex cases such as link docs and rows which have multiple levels of IDs that their
|
2020-10-03 02:19:39 +13:00
|
|
|
* ID consists of need their own functions to build the allDocs parameters.
|
2020-10-03 00:37:46 +13:00
|
|
|
* @param {string} docType The type of document which input params are being built for, e.g. user,
|
2020-10-10 06:49:23 +13:00
|
|
|
* link, app, table and so on.
|
2020-10-03 02:19:39 +13:00
|
|
|
* @param {string|null} docId The ID of the document minus its type - this is only needed if looking
|
|
|
|
* for a singular document.
|
2020-10-03 00:37:46 +13:00
|
|
|
* @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.
|
|
|
|
*/
|
2020-10-03 02:19:39 +13:00
|
|
|
function getDocParams(docType, docId = null, otherProps = {}) {
|
|
|
|
if (docId == null) {
|
|
|
|
docId = ""
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
...otherProps,
|
2020-10-09 21:47:37 +13:00
|
|
|
startkey: `${docType}${SEPARATOR}${docId}`,
|
|
|
|
endkey: `${docType}${SEPARATOR}${docId}${UNICODE_MAX}`,
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
2020-10-10 06:49:23 +13:00
|
|
|
* Gets parameters for retrieving tables, this is a utility function for the getDocParams function.
|
2020-10-03 00:37:46 +13:00
|
|
|
*/
|
2020-10-10 06:49:23 +13:00
|
|
|
exports.getTableParams = (tableId = null, otherProps = {}) => {
|
|
|
|
return getDocParams(DocumentTypes.TABLE, tableId, otherProps)
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
2020-10-10 06:49:23 +13:00
|
|
|
* Generates a new table ID.
|
|
|
|
* @returns {string} The new table ID which the table doc can be stored under.
|
2020-10-03 00:37:46 +13:00
|
|
|
*/
|
2020-10-10 06:49:23 +13:00
|
|
|
exports.generateTableID = () => {
|
|
|
|
return `${DocumentTypes.TABLE}${SEPARATOR}${newid()}`
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
2020-10-10 07:10:28 +13:00
|
|
|
* Gets the DB allDocs/query params for retrieving a row.
|
2020-10-13 05:45:11 +13:00
|
|
|
* @param {string|null} tableId The table in which the rows have been stored.
|
2020-10-10 07:10:28 +13:00
|
|
|
* @param {string|null} rowId The ID of the row which is being specifically queried for. This can be
|
|
|
|
* left null to get all the rows in the table.
|
2020-10-03 00:37:46 +13:00
|
|
|
* @param {object} otherProps Any other properties to add to the request.
|
|
|
|
* @returns {object} Parameters which can then be used with an allDocs request.
|
|
|
|
*/
|
2020-10-13 05:56:40 +13:00
|
|
|
exports.getRowParams = (tableId = null, rowId = null, otherProps = {}) => {
|
2020-10-10 06:49:23 +13:00
|
|
|
if (tableId == null) {
|
2020-10-13 05:45:11 +13:00
|
|
|
return getDocParams(DocumentTypes.ROW, null, otherProps)
|
2020-10-13 04:37:08 +13:00
|
|
|
} else {
|
|
|
|
const endOfKey =
|
2020-10-13 05:45:11 +13:00
|
|
|
rowId == null
|
|
|
|
? `${tableId}${SEPARATOR}`
|
|
|
|
: `${tableId}${SEPARATOR}${rowId}`
|
|
|
|
return getDocParams(DocumentTypes.ROW, endOfKey, otherProps)
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
2020-10-10 07:10:28 +13:00
|
|
|
* Gets a new row ID for the specified table.
|
|
|
|
* @param {string} tableId The table which the row is being created for.
|
|
|
|
* @returns {string} The new ID which a row doc can be stored under.
|
2020-10-03 00:37:46 +13:00
|
|
|
*/
|
2020-10-10 07:10:28 +13:00
|
|
|
exports.generateRowID = tableId => {
|
|
|
|
return `${DocumentTypes.ROW}${SEPARATOR}${tableId}${SEPARATOR}${newid()}`
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
2020-10-03 02:19:39 +13:00
|
|
|
* Gets parameters for retrieving users, this is a utility function for the getDocParams function.
|
2020-10-03 00:37:46 +13:00
|
|
|
*/
|
|
|
|
exports.getUserParams = (username = null, otherProps = {}) => {
|
2020-10-03 02:19:39 +13:00
|
|
|
return getDocParams(DocumentTypes.USER, username, otherProps)
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
|
|
|
* Generates a new user ID based on the passed in username.
|
|
|
|
* @param {string} username The username which the ID is going to be built up of.
|
|
|
|
* @returns {string} The new user ID which the user doc can be stored under.
|
|
|
|
*/
|
2020-10-02 05:22:08 +13:00
|
|
|
exports.generateUserID = username => {
|
2020-10-09 21:47:37 +13:00
|
|
|
return `${DocumentTypes.USER}${SEPARATOR}${username}`
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
2020-10-03 02:19:39 +13:00
|
|
|
* Gets parameters for retrieving automations, this is a utility function for the getDocParams function.
|
2020-10-03 00:37:46 +13:00
|
|
|
*/
|
|
|
|
exports.getAutomationParams = (automationId = null, otherProps = {}) => {
|
2020-10-03 02:19:39 +13:00
|
|
|
return getDocParams(DocumentTypes.AUTOMATION, automationId, otherProps)
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
|
|
|
* Generates a new automation ID.
|
|
|
|
* @returns {string} The new automation ID which the automation doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateAutomationID = () => {
|
2020-10-09 21:47:37 +13:00
|
|
|
return `${DocumentTypes.AUTOMATION}${SEPARATOR}${newid()}`
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
|
|
|
* Generates a new link doc ID. This is currently not usable with the alldocs call,
|
|
|
|
* instead a view is built to make walking to tree easier.
|
2020-10-10 06:49:23 +13:00
|
|
|
* @param {string} tableId1 The ID of the linker table.
|
|
|
|
* @param {string} tableId2 The ID of the linked table.
|
2020-10-10 07:10:28 +13:00
|
|
|
* @param {string} rowId1 The ID of the linker row.
|
|
|
|
* @param {string} rowId2 The ID of the linked row.
|
2020-10-03 00:37:46 +13:00
|
|
|
* @returns {string} The new link doc ID which the automation doc can be stored under.
|
|
|
|
*/
|
2020-10-10 07:10:28 +13:00
|
|
|
exports.generateLinkID = (tableId1, tableId2, rowId1, rowId2) => {
|
2020-10-13 03:25:27 +13:00
|
|
|
return `${DocumentTypes.LINK}${SEPARATOR}${tableId1}${SEPARATOR}${tableId2}${SEPARATOR}${rowId1}${SEPARATOR}${rowId2}`
|
2020-10-13 02:31:17 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets parameters for retrieving link docs, this is a utility function for the getDocParams function.
|
|
|
|
*/
|
|
|
|
exports.getLinkParams = (otherProps = {}) => {
|
|
|
|
return getDocParams(DocumentTypes.LINK, null, otherProps)
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
|
|
|
* Generates a new app ID.
|
|
|
|
* @returns {string} The new app ID which the app doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateAppID = () => {
|
2020-10-09 21:47:37 +13:00
|
|
|
return `${DocumentTypes.APP}${SEPARATOR}${newid()}`
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
2020-10-03 02:19:39 +13:00
|
|
|
* Gets parameters for retrieving apps, this is a utility function for the getDocParams function.
|
2020-10-03 00:37:46 +13:00
|
|
|
*/
|
|
|
|
exports.getAppParams = (appId = null, otherProps = {}) => {
|
2020-10-03 02:19:39 +13:00
|
|
|
return getDocParams(DocumentTypes.APP, appId, otherProps)
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
|
|
|
* Generates a new access level ID.
|
|
|
|
* @returns {string} The new access level ID which the access level doc can be stored under.
|
|
|
|
*/
|
|
|
|
exports.generateAccessLevelID = () => {
|
2020-10-09 21:47:37 +13:00
|
|
|
return `${DocumentTypes.ACCESS_LEVEL}${SEPARATOR}${newid()}`
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|
|
|
|
|
2020-10-03 00:37:46 +13:00
|
|
|
/**
|
2020-10-03 02:19:39 +13:00
|
|
|
* Gets parameters for retrieving an access level, this is a utility function for the getDocParams function.
|
2020-10-03 00:37:46 +13:00
|
|
|
*/
|
|
|
|
exports.getAccessLevelParams = (accessLevelId = null, otherProps = {}) => {
|
2020-10-03 02:19:39 +13:00
|
|
|
return getDocParams(DocumentTypes.ACCESS_LEVEL, accessLevelId, otherProps)
|
2020-10-02 05:22:08 +13:00
|
|
|
}
|