2022-07-18 22:11:52 +02:00
|
|
|
import {
|
2022-07-13 22:50:19 +02:00
|
|
|
DocumentTypes,
|
|
|
|
ViewNames,
|
2022-07-18 22:11:52 +02:00
|
|
|
GlobalViewName,
|
2022-07-13 22:50:19 +02:00
|
|
|
DeprecatedViews,
|
|
|
|
SEPARATOR,
|
2022-07-18 22:11:52 +02:00
|
|
|
} from "./utils"
|
|
|
|
import { getGlobalDB } from "../tenancy"
|
|
|
|
import PouchDB from "pouchdb"
|
|
|
|
import { Document } from "@budibase/types"
|
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-18 22:11:52 +02:00
|
|
|
interface DesignDocument {
|
|
|
|
views: any
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeDeprecated(db: PouchDB.Database, viewName: ViewNames) {
|
|
|
|
// @ts-ignore
|
2022-07-13 22:50:19 +02:00
|
|
|
if (!DeprecatedViews[viewName]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try {
|
2022-07-18 22:11:52 +02:00
|
|
|
const designDoc = await db.get<DesignDocument>(DESIGN_DB)
|
|
|
|
// @ts-ignore
|
2022-07-13 22:50:19 +02:00
|
|
|
for (let deprecatedNames of DeprecatedViews[viewName]) {
|
|
|
|
delete designDoc.views[deprecatedNames]
|
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
} catch (err) {
|
|
|
|
// doesn't exist, ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 22:11:52 +02:00
|
|
|
export const 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-07-13 22:50:19 +02:00
|
|
|
if (doc._id.startsWith("${DocumentTypes.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,
|
|
|
|
[ViewNames.USER_BY_EMAIL]: view,
|
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
}
|
2022-03-04 14:42:50 +01:00
|
|
|
|
2022-07-18 22:11:52 +02:00
|
|
|
export const createApiKeyView = async () => {
|
2022-02-11 23:24:48 +01:00
|
|
|
const db = getGlobalDB()
|
|
|
|
let designDoc
|
|
|
|
try {
|
|
|
|
designDoc = await db.get("_design/database")
|
|
|
|
} catch (err) {
|
|
|
|
designDoc = DesignDoc()
|
|
|
|
}
|
|
|
|
const view = {
|
|
|
|
map: `function(doc) {
|
|
|
|
if (doc._id.startsWith("${DocumentTypes.DEV_INFO}") && doc.apiKey) {
|
|
|
|
emit(doc.apiKey, doc.userId)
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
}
|
|
|
|
designDoc.views = {
|
|
|
|
...designDoc.views,
|
|
|
|
[ViewNames.BY_API_KEY]: view,
|
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
}
|
|
|
|
|
2022-07-18 22:11:52 +02:00
|
|
|
export const createUserBuildersView = async () => {
|
2022-03-22 01:23:22 +01:00
|
|
|
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,
|
|
|
|
[ViewNames.USER_BY_BUILDERS]: view,
|
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
}
|
2022-03-21 18:13:16 +01:00
|
|
|
|
2022-07-18 22:11:52 +02:00
|
|
|
export const queryGlobalView = async <T extends Document>(
|
|
|
|
viewName: GlobalViewName,
|
|
|
|
params: PouchDB.Query.Options<T, T>,
|
|
|
|
db?: PouchDB.Database
|
|
|
|
): Promise<T[] | T | undefined> => {
|
2022-02-11 23:24:48 +01:00
|
|
|
const CreateFuncByName = {
|
2022-07-18 22:11:52 +02:00
|
|
|
[ViewNames.USER_BY_EMAIL]: createNewUserEmailView,
|
|
|
|
[ViewNames.BY_API_KEY]: createApiKeyView,
|
|
|
|
[ViewNames.USER_BY_BUILDERS]: createUserBuildersView,
|
2022-02-11 23:24:48 +01:00
|
|
|
}
|
|
|
|
// can pass DB in if working with something specific
|
|
|
|
if (!db) {
|
2022-07-18 22:11:52 +02:00
|
|
|
db = getGlobalDB() as PouchDB.Database
|
2022-02-11 23:24:48 +01:00
|
|
|
}
|
2022-07-18 22:11:52 +02:00
|
|
|
|
2022-02-11 23:24:48 +01:00
|
|
|
try {
|
2022-07-18 22:11:52 +02:00
|
|
|
const response = await db.query<T, T>(`database/${viewName}`, params)
|
|
|
|
const rows = response.rows
|
|
|
|
const docs = rows.map(row => (params.include_docs ? row.doc : row.value))
|
|
|
|
return docs.length <= 1 ? docs[0] : docs
|
|
|
|
} catch (err: any) {
|
2022-02-11 23:24:48 +01:00
|
|
|
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()
|
2022-07-18 22:11:52 +02:00
|
|
|
return queryGlobalView(viewName, params)
|
2022-02-11 23:24:48 +01:00
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|