From 536d85d828b48ff5d6028221b958cc5a59869eb4 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 26 Oct 2023 15:04:33 +0100 Subject: [PATCH] WIP: Improving the use of DatabaseQueryOpts. --- packages/backend-core/src/docIds/params.ts | 29 ++++++++++++++----- packages/backend-core/src/users/db.ts | 6 +--- packages/backend-core/src/users/users.ts | 12 +++++--- .../server/src/db/linkedRows/linkUtils.ts | 11 +++++-- .../functions/backfill/global/configs.ts | 3 +- packages/server/src/sdk/app/links/links.ts | 13 +++++++-- packages/types/src/sdk/db.ts | 10 ++++--- 7 files changed, 56 insertions(+), 28 deletions(-) diff --git a/packages/backend-core/src/docIds/params.ts b/packages/backend-core/src/docIds/params.ts index 36fd75622b..d9baee3dc6 100644 --- a/packages/backend-core/src/docIds/params.ts +++ b/packages/backend-core/src/docIds/params.ts @@ -6,6 +6,7 @@ import { ViewName, } from "../constants" import { getProdAppID } from "./conversions" +import { DatabaseQueryOpts } from "@budibase/types" /** * If creating DB allDocs/query params with only a single top level ID this can be used, this @@ -22,8 +23,8 @@ import { getProdAppID } from "./conversions" export function getDocParams( docType: string, docId?: string | null, - otherProps: any = {} -) { + otherProps: Partial = {} +): DatabaseQueryOpts { if (docId == null) { docId = "" } @@ -45,8 +46,8 @@ export function getDocParams( export function getRowParams( tableId?: string | null, rowId?: string | null, - otherProps = {} -) { + otherProps: Partial = {} +): DatabaseQueryOpts { if (tableId == null) { return getDocParams(DocumentType.ROW, null, otherProps) } @@ -88,7 +89,10 @@ export const isDatasourceId = (id: string) => { /** * Gets parameters for retrieving workspaces. */ -export function getWorkspaceParams(id = "", otherProps = {}) { +export function getWorkspaceParams( + id = "", + otherProps: Partial = {} +): DatabaseQueryOpts { return { ...otherProps, startkey: `${DocumentType.WORKSPACE}${SEPARATOR}${id}`, @@ -99,7 +103,10 @@ export function getWorkspaceParams(id = "", otherProps = {}) { /** * Gets parameters for retrieving users. */ -export function getGlobalUserParams(globalId: any, otherProps: any = {}) { +export function getGlobalUserParams( + globalId: any, + otherProps: Partial = {} +): DatabaseQueryOpts { if (!globalId) { globalId = "" } @@ -117,11 +124,17 @@ export function getGlobalUserParams(globalId: any, otherProps: any = {}) { /** * Gets parameters for retrieving users, this is a utility function for the getDocParams function. */ -export function getUserMetadataParams(userId?: string | null, otherProps = {}) { +export function getUserMetadataParams( + userId?: string | null, + otherProps: Partial = {} +): DatabaseQueryOpts { return getRowParams(InternalTable.USER_METADATA, userId, otherProps) } -export function getUsersByAppParams(appId: any, otherProps: any = {}) { +export function getUsersByAppParams( + appId: any, + otherProps: Partial = {} +): DatabaseQueryOpts { const prodAppId = getProdAppID(appId) return { ...otherProps, diff --git a/packages/backend-core/src/users/db.ts b/packages/backend-core/src/users/db.ts index a2539e836e..78df262a4e 100644 --- a/packages/backend-core/src/users/db.ts +++ b/packages/backend-core/src/users/db.ts @@ -160,13 +160,9 @@ export class UserDB { } static async getUsersByAppAccess(opts: { appId?: string; limit?: number }) { - const params: any = { - include_docs: true, - limit: opts.limit || 50, - } let response: User[] = await usersCore.searchGlobalUsersByAppAccess( opts.appId, - params + { limit: opts.limit || 50 } ) return response } diff --git a/packages/backend-core/src/users/users.ts b/packages/backend-core/src/users/users.ts index 6237c23972..adf134f04a 100644 --- a/packages/backend-core/src/users/users.ts +++ b/packages/backend-core/src/users/users.ts @@ -20,6 +20,7 @@ import { SearchUsersRequest, User, DatabaseQueryOpts, + CouchFindOptions, } from "@budibase/types" import { getGlobalDB } from "../context" import * as context from "../context" @@ -140,7 +141,7 @@ export const getGlobalUserByEmail = async ( export const searchGlobalUsersByApp = async ( appId: any, - opts: any, + opts: DatabaseQueryOpts, getOpts?: GetOpts ) => { if (typeof appId !== "string") { @@ -166,7 +167,10 @@ export const searchGlobalUsersByApp = async ( Return any user who potentially has access to the application Admins, developers and app users with the explicitly role. */ -export const searchGlobalUsersByAppAccess = async (appId: any, opts: any) => { +export const searchGlobalUsersByAppAccess = async ( + appId: any, + opts?: { limit?: number } +) => { const roleSelector = `roles.${appId}` let orQuery: any[] = [ @@ -187,7 +191,7 @@ export const searchGlobalUsersByAppAccess = async (appId: any, opts: any) => { orQuery.push(roleCheck) } - let searchOptions = { + let searchOptions: CouchFindOptions = { selector: { $or: orQuery, _id: { @@ -198,7 +202,7 @@ export const searchGlobalUsersByAppAccess = async (appId: any, opts: any) => { } const resp = await directCouchFind(context.getGlobalDBName(), searchOptions) - return resp?.rows + return resp.rows } export const getGlobalUserByAppPage = (appId: string, user: User) => { diff --git a/packages/server/src/db/linkedRows/linkUtils.ts b/packages/server/src/db/linkedRows/linkUtils.ts index c74674a865..db9a0dc7d5 100644 --- a/packages/server/src/db/linkedRows/linkUtils.ts +++ b/packages/server/src/db/linkedRows/linkUtils.ts @@ -2,7 +2,12 @@ import { ViewName, getQueryIndex, isRelationshipColumn } from "../utils" import { FieldTypes } from "../../constants" import { createLinkView } from "../views/staticViews" import { context, logging } from "@budibase/backend-core" -import { LinkDocument, LinkDocumentValue, Table } from "@budibase/types" +import { + DatabaseQueryOpts, + LinkDocument, + LinkDocumentValue, + Table, +} from "@budibase/types" export { createLinkView } from "../views/staticViews" @@ -36,13 +41,13 @@ export async function getLinkDocuments(args: { }): Promise { const { tableId, rowId, fieldName, includeDocs } = args const db = context.getAppDB() - let params: any + let params: DatabaseQueryOpts if (rowId) { params = { key: [tableId, rowId] } } // only table is known else { - params = { startKey: [tableId], endKey: [tableId, {}] } + params = { startkey: [tableId], endkey: [tableId, {}] } } if (includeDocs) { params.include_docs = true diff --git a/packages/server/src/migrations/functions/backfill/global/configs.ts b/packages/server/src/migrations/functions/backfill/global/configs.ts index 1b76727bbe..04eb9caff2 100644 --- a/packages/server/src/migrations/functions/backfill/global/configs.ts +++ b/packages/server/src/migrations/functions/backfill/global/configs.ts @@ -11,10 +11,11 @@ import { isOIDCConfig, isSettingsConfig, ConfigType, + DatabaseQueryOpts, } from "@budibase/types" import env from "./../../../../environment" -export const getConfigParams = () => { +export function getConfigParams(): DatabaseQueryOpts { return { include_docs: true, startkey: `${DocumentType.CONFIG}${SEPARATOR}`, diff --git a/packages/server/src/sdk/app/links/links.ts b/packages/server/src/sdk/app/links/links.ts index 7f3dcbd4ac..fda07568f9 100644 --- a/packages/server/src/sdk/app/links/links.ts +++ b/packages/server/src/sdk/app/links/links.ts @@ -1,6 +1,10 @@ import { context } from "@budibase/backend-core" import { isTableId } from "@budibase/backend-core/src/docIds" -import { LinkDocument, LinkDocumentValue } from "@budibase/types" +import { + DatabaseQueryOpts, + LinkDocument, + LinkDocumentValue, +} from "@budibase/types" import { ViewName, getQueryIndex } from "../../../../src/db/utils" export async function fetch(tableId: string): Promise { @@ -9,7 +13,10 @@ export async function fetch(tableId: string): Promise { } const db = context.getAppDB() - const params: any = { startkey: [tableId], endkey: [tableId, {}] } + const params: DatabaseQueryOpts = { + startkey: [tableId], + endkey: [tableId, {}], + } const linkRows = (await db.query(getQueryIndex(ViewName.LINK), params)).rows return linkRows.map(row => row.value as LinkDocumentValue) } @@ -22,7 +29,7 @@ export async function fetchWithDocument( } const db = context.getAppDB() - const params: any = { + const params: DatabaseQueryOpts = { startkey: [tableId], endkey: [tableId, {}], include_docs: true, diff --git a/packages/types/src/sdk/db.ts b/packages/types/src/sdk/db.ts index 36141cc15b..d3a6396128 100644 --- a/packages/types/src/sdk/db.ts +++ b/packages/types/src/sdk/db.ts @@ -54,15 +54,17 @@ export type DatabaseDeleteIndexOpts = { type?: string | undefined } +export type DatabaseKey = string | number | {} | DatabaseKey[] + export type DatabaseQueryOpts = { include_docs?: boolean - startkey?: string - endkey?: string + startkey?: DatabaseKey + endkey?: DatabaseKey limit?: number skip?: number descending?: boolean - key?: string - keys?: string[] + key?: DatabaseKey + keys?: DatabaseKey[] group?: boolean startkey_docid?: string }