2022-07-13 22:50:19 +02:00
|
|
|
const {
|
2022-08-11 14:50:05 +02:00
|
|
|
DocumentType,
|
|
|
|
ViewName,
|
2022-07-13 22:50:19 +02:00
|
|
|
DeprecatedViews,
|
|
|
|
SEPARATOR,
|
|
|
|
} = require("./utils")
|
2022-02-11 23:24:48 +01:00
|
|
|
const { getGlobalDB } = require("../tenancy")
|
2022-08-23 10:37:13 +02:00
|
|
|
const { StaticDatabases } = require("./constants")
|
|
|
|
const { doWithDB } = require("./");
|
2021-04-20 18:17:44 +02:00
|
|
|
|
2022-07-13 22:50:19 +02:00
|
|
|
const DESIGN_DB = "_design/database"
|
|
|
|
|
2021-04-20 18:17:44 +02:00
|
|
|
function DesignDoc() {
|
|
|
|
return {
|
2022-07-13 22:50:19 +02:00
|
|
|
_id: DESIGN_DB,
|
2021-04-20 18:17:44 +02:00
|
|
|
// view collation information, read before writing any complex views:
|
|
|
|
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
|
|
|
|
views: {},
|
|
|
|
}
|
|
|
|
}
|
2021-04-19 18:31:47 +02:00
|
|
|
|
2022-07-13 22:50:19 +02:00
|
|
|
async function removeDeprecated(db, viewName) {
|
|
|
|
if (!DeprecatedViews[viewName]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const designDoc = await db.get(DESIGN_DB)
|
|
|
|
for (let deprecatedNames of DeprecatedViews[viewName]) {
|
|
|
|
delete designDoc.views[deprecatedNames]
|
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
} catch (err) {
|
|
|
|
// doesn't exist, ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.createNewUserEmailView = async () => {
|
2022-02-11 23:24:48 +01:00
|
|
|
const db = getGlobalDB()
|
2021-04-20 18:17:44 +02:00
|
|
|
let designDoc
|
|
|
|
try {
|
2022-07-13 22:50:19 +02:00
|
|
|
designDoc = await db.get(DESIGN_DB)
|
2021-04-20 18:17:44 +02:00
|
|
|
} catch (err) {
|
|
|
|
// no design doc, make one
|
|
|
|
designDoc = DesignDoc()
|
|
|
|
}
|
2021-04-19 18:31:47 +02:00
|
|
|
const view = {
|
|
|
|
// if using variables in a map function need to inject them before use
|
|
|
|
map: `function(doc) {
|
2022-08-11 14:50:05 +02:00
|
|
|
if (doc._id.startsWith("${DocumentType.USER}${SEPARATOR}")) {
|
2021-10-26 13:40:30 +02:00
|
|
|
emit(doc.email.toLowerCase(), doc._id)
|
2021-04-19 18:31:47 +02:00
|
|
|
}
|
|
|
|
}`,
|
|
|
|
}
|
|
|
|
designDoc.views = {
|
|
|
|
...designDoc.views,
|
2022-08-11 14:50:05 +02:00
|
|
|
[ViewName.USER_BY_EMAIL]: view,
|
2021-04-19 18:31:47 +02:00
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
}
|
2022-03-04 14:42:50 +01:00
|
|
|
|
2022-08-23 10:37:13 +02:00
|
|
|
exports.createAccountEmailView = async () => {
|
|
|
|
await doWithDB(StaticDatabases.PLATFORM_INFO.name, async (db) => {
|
|
|
|
let designDoc
|
|
|
|
try {
|
|
|
|
designDoc = await db.get(DESIGN_DB)
|
|
|
|
} catch (err) {
|
|
|
|
// no design doc, make one
|
|
|
|
designDoc = DesignDoc()
|
|
|
|
}
|
|
|
|
const view = {
|
|
|
|
// if using variables in a map function need to inject them before use
|
|
|
|
map: `function(doc) {
|
|
|
|
if (doc._id.startsWith("${DocumentType.ACCOUNT}${SEPARATOR}")) {
|
|
|
|
emit(doc.email.toLowerCase(), doc.tenantId)
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
}
|
|
|
|
designDoc.views = {
|
|
|
|
...designDoc.views,
|
|
|
|
[ViewName.ACCOUNT_BY_EMAIL]: view,
|
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:09:05 +02:00
|
|
|
exports.createUserAppView = async () => {
|
|
|
|
const db = getGlobalDB()
|
|
|
|
let designDoc
|
|
|
|
try {
|
|
|
|
designDoc = await db.get("_design/database")
|
|
|
|
} catch (err) {
|
|
|
|
// no design doc, make one
|
|
|
|
designDoc = DesignDoc()
|
|
|
|
}
|
|
|
|
const view = {
|
|
|
|
// if using variables in a map function need to inject them before use
|
|
|
|
map: `function(doc) {
|
2022-08-11 14:50:05 +02:00
|
|
|
if (doc._id.startsWith("${DocumentType.USER}${SEPARATOR}") && doc.roles) {
|
2022-07-06 17:09:05 +02:00
|
|
|
for (let prodAppId of Object.keys(doc.roles)) {
|
|
|
|
let emitted = prodAppId + "${SEPARATOR}" + doc._id
|
|
|
|
emit(emitted, null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
}
|
|
|
|
designDoc.views = {
|
|
|
|
...designDoc.views,
|
2022-08-11 14:50:05 +02:00
|
|
|
[ViewName.USER_BY_APP]: view,
|
2022-07-06 17:09:05 +02:00
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
}
|
|
|
|
|
2022-02-11 23:24:48 +01:00
|
|
|
exports.createApiKeyView = async () => {
|
|
|
|
const db = getGlobalDB()
|
|
|
|
let designDoc
|
|
|
|
try {
|
|
|
|
designDoc = await db.get("_design/database")
|
|
|
|
} catch (err) {
|
|
|
|
designDoc = DesignDoc()
|
|
|
|
}
|
|
|
|
const view = {
|
|
|
|
map: `function(doc) {
|
2022-08-11 14:50:05 +02:00
|
|
|
if (doc._id.startsWith("${DocumentType.DEV_INFO}") && doc.apiKey) {
|
2022-02-11 23:24:48 +01:00
|
|
|
emit(doc.apiKey, doc.userId)
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
}
|
|
|
|
designDoc.views = {
|
|
|
|
...designDoc.views,
|
2022-08-11 14:50:05 +02:00
|
|
|
[ViewName.BY_API_KEY]: view,
|
2022-02-11 23:24:48 +01:00
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
}
|
|
|
|
|
2022-03-22 01:23:22 +01:00
|
|
|
exports.createUserBuildersView = async () => {
|
|
|
|
const db = getGlobalDB()
|
2022-03-04 14:42:50 +01:00
|
|
|
let designDoc
|
|
|
|
try {
|
|
|
|
designDoc = await db.get("_design/database")
|
|
|
|
} catch (err) {
|
|
|
|
// no design doc, make one
|
|
|
|
designDoc = DesignDoc()
|
|
|
|
}
|
|
|
|
const view = {
|
|
|
|
map: `function(doc) {
|
|
|
|
if (doc.builder && doc.builder.global === true) {
|
|
|
|
emit(doc._id, doc._id)
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
}
|
|
|
|
designDoc.views = {
|
|
|
|
...designDoc.views,
|
2022-08-11 14:50:05 +02:00
|
|
|
[ViewName.USER_BY_BUILDERS]: view,
|
2022-03-04 14:42:50 +01:00
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
}
|
2022-03-21 18:13:16 +01:00
|
|
|
|
2022-08-23 10:37:13 +02:00
|
|
|
exports.queryView = async (viewName, params, db, CreateFuncByName) => {
|
2022-02-11 23:24:48 +01:00
|
|
|
try {
|
|
|
|
let response = (await db.query(`database/${viewName}`, params)).rows
|
|
|
|
response = response.map(resp =>
|
|
|
|
params.include_docs ? resp.doc : resp.value
|
|
|
|
)
|
2022-08-23 10:37:13 +02:00
|
|
|
if (params.arrayResponse) {
|
|
|
|
return response
|
|
|
|
} else {
|
|
|
|
return response.length <= 1 ? response[0] : response
|
|
|
|
}
|
2022-02-11 23:24:48 +01:00
|
|
|
} catch (err) {
|
|
|
|
if (err != null && err.name === "not_found") {
|
|
|
|
const createFunc = CreateFuncByName[viewName]
|
2022-07-13 22:50:19 +02:00
|
|
|
await removeDeprecated(db, viewName)
|
2022-02-11 23:24:48 +01:00
|
|
|
await createFunc()
|
|
|
|
return exports.queryGlobalView(viewName, params)
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-23 10:37:13 +02:00
|
|
|
|
|
|
|
exports.queryPlatformView = async (viewName, params) => {
|
|
|
|
const CreateFuncByName = {
|
|
|
|
[ViewName.ACCOUNT_BY_EMAIL]: exports.createAccountEmailView,
|
|
|
|
}
|
|
|
|
|
|
|
|
return doWithDB(StaticDatabases.PLATFORM_INFO.name, async (db) => {
|
|
|
|
return exports.queryView(viewName, params, db, CreateFuncByName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.queryGlobalView = async (viewName, params, db = null) => {
|
|
|
|
const CreateFuncByName = {
|
|
|
|
[ViewName.USER_BY_EMAIL]: exports.createNewUserEmailView,
|
|
|
|
[ViewName.BY_API_KEY]: exports.createApiKeyView,
|
|
|
|
[ViewName.USER_BY_BUILDERS]: exports.createUserBuildersView,
|
|
|
|
[ViewName.USER_BY_APP]: exports.createUserAppView,
|
|
|
|
}
|
|
|
|
// can pass DB in if working with something specific
|
|
|
|
if (!db) {
|
|
|
|
db = getGlobalDB()
|
|
|
|
}
|
|
|
|
return exports.queryView(viewName, params, db, CreateFuncByName)
|
|
|
|
}
|