Merge pull request #12185 from Budibase/feature/budi-7607-migrate-user-relationship-columns-to-the-new-user-column-6

Replace several instances of `any` with `DatabaseQueryOpts`.
This commit is contained in:
Sam Rose 2023-10-27 10:24:55 +01:00 committed by GitHub
commit 731ff06be4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 63 additions and 34 deletions

View File

@ -21,7 +21,7 @@
"test:watch": "jest --watchAll" "test:watch": "jest --watchAll"
}, },
"dependencies": { "dependencies": {
"@budibase/nano": "10.1.2", "@budibase/nano": "10.1.3",
"@budibase/pouchdb-replication-stream": "1.2.10", "@budibase/pouchdb-replication-stream": "1.2.10",
"@budibase/shared-core": "0.0.0", "@budibase/shared-core": "0.0.0",
"@budibase/types": "0.0.0", "@budibase/types": "0.0.0",

View File

@ -6,6 +6,7 @@ import {
ViewName, ViewName,
} from "../constants" } from "../constants"
import { getProdAppID } from "./conversions" 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 * 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( export function getDocParams(
docType: string, docType: string,
docId?: string | null, docId?: string | null,
otherProps: any = {} otherProps: Partial<DatabaseQueryOpts> = {}
) { ): DatabaseQueryOpts {
if (docId == null) { if (docId == null) {
docId = "" docId = ""
} }
@ -45,8 +46,8 @@ export function getDocParams(
export function getRowParams( export function getRowParams(
tableId?: string | null, tableId?: string | null,
rowId?: string | null, rowId?: string | null,
otherProps = {} otherProps: Partial<DatabaseQueryOpts> = {}
) { ): DatabaseQueryOpts {
if (tableId == null) { if (tableId == null) {
return getDocParams(DocumentType.ROW, null, otherProps) return getDocParams(DocumentType.ROW, null, otherProps)
} }
@ -88,7 +89,10 @@ export const isDatasourceId = (id: string) => {
/** /**
* Gets parameters for retrieving workspaces. * Gets parameters for retrieving workspaces.
*/ */
export function getWorkspaceParams(id = "", otherProps = {}) { export function getWorkspaceParams(
id = "",
otherProps: Partial<DatabaseQueryOpts> = {}
): DatabaseQueryOpts {
return { return {
...otherProps, ...otherProps,
startkey: `${DocumentType.WORKSPACE}${SEPARATOR}${id}`, startkey: `${DocumentType.WORKSPACE}${SEPARATOR}${id}`,
@ -99,7 +103,10 @@ export function getWorkspaceParams(id = "", otherProps = {}) {
/** /**
* Gets parameters for retrieving users. * Gets parameters for retrieving users.
*/ */
export function getGlobalUserParams(globalId: any, otherProps: any = {}) { export function getGlobalUserParams(
globalId: any,
otherProps: Partial<DatabaseQueryOpts> = {}
): DatabaseQueryOpts {
if (!globalId) { if (!globalId) {
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. * 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> = {}
): DatabaseQueryOpts {
return getRowParams(InternalTable.USER_METADATA, userId, otherProps) return getRowParams(InternalTable.USER_METADATA, userId, otherProps)
} }
export function getUsersByAppParams(appId: any, otherProps: any = {}) { export function getUsersByAppParams(
appId: any,
otherProps: Partial<DatabaseQueryOpts> = {}
): DatabaseQueryOpts {
const prodAppId = getProdAppID(appId) const prodAppId = getProdAppID(appId)
return { return {
...otherProps, ...otherProps,

View File

@ -160,13 +160,9 @@ export class UserDB {
} }
static async getUsersByAppAccess(opts: { appId?: string; limit?: number }) { static async getUsersByAppAccess(opts: { appId?: string; limit?: number }) {
const params: any = {
include_docs: true,
limit: opts.limit || 50,
}
let response: User[] = await usersCore.searchGlobalUsersByAppAccess( let response: User[] = await usersCore.searchGlobalUsersByAppAccess(
opts.appId, opts.appId,
params { limit: opts.limit || 50 }
) )
return response return response
} }

View File

@ -20,6 +20,7 @@ import {
SearchUsersRequest, SearchUsersRequest,
User, User,
DatabaseQueryOpts, DatabaseQueryOpts,
CouchFindOptions,
} from "@budibase/types" } from "@budibase/types"
import { getGlobalDB } from "../context" import { getGlobalDB } from "../context"
import * as context from "../context" import * as context from "../context"
@ -140,7 +141,7 @@ export const getGlobalUserByEmail = async (
export const searchGlobalUsersByApp = async ( export const searchGlobalUsersByApp = async (
appId: any, appId: any,
opts: any, opts: DatabaseQueryOpts,
getOpts?: GetOpts getOpts?: GetOpts
) => { ) => {
if (typeof appId !== "string") { if (typeof appId !== "string") {
@ -166,7 +167,10 @@ export const searchGlobalUsersByApp = async (
Return any user who potentially has access to the application Return any user who potentially has access to the application
Admins, developers and app users with the explicitly role. 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}` const roleSelector = `roles.${appId}`
let orQuery: any[] = [ let orQuery: any[] = [
@ -187,7 +191,7 @@ export const searchGlobalUsersByAppAccess = async (appId: any, opts: any) => {
orQuery.push(roleCheck) orQuery.push(roleCheck)
} }
let searchOptions = { let searchOptions: CouchFindOptions = {
selector: { selector: {
$or: orQuery, $or: orQuery,
_id: { _id: {
@ -198,7 +202,7 @@ export const searchGlobalUsersByAppAccess = async (appId: any, opts: any) => {
} }
const resp = await directCouchFind(context.getGlobalDBName(), searchOptions) const resp = await directCouchFind(context.getGlobalDBName(), searchOptions)
return resp?.rows return resp.rows
} }
export const getGlobalUserByAppPage = (appId: string, user: User) => { export const getGlobalUserByAppPage = (appId: string, user: User) => {

View File

@ -2,7 +2,12 @@ import { ViewName, getQueryIndex, isRelationshipColumn } from "../utils"
import { FieldTypes } from "../../constants" import { FieldTypes } from "../../constants"
import { createLinkView } from "../views/staticViews" import { createLinkView } from "../views/staticViews"
import { context, logging } from "@budibase/backend-core" 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" export { createLinkView } from "../views/staticViews"
@ -36,13 +41,13 @@ export async function getLinkDocuments(args: {
}): Promise<LinkDocumentValue[] | LinkDocument[]> { }): Promise<LinkDocumentValue[] | LinkDocument[]> {
const { tableId, rowId, fieldName, includeDocs } = args const { tableId, rowId, fieldName, includeDocs } = args
const db = context.getAppDB() const db = context.getAppDB()
let params: any let params: DatabaseQueryOpts
if (rowId) { if (rowId) {
params = { key: [tableId, rowId] } params = { key: [tableId, rowId] }
} }
// only table is known // only table is known
else { else {
params = { startKey: [tableId], endKey: [tableId, {}] } params = { startkey: [tableId], endkey: [tableId, {}] }
} }
if (includeDocs) { if (includeDocs) {
params.include_docs = true params.include_docs = true

View File

@ -11,10 +11,11 @@ import {
isOIDCConfig, isOIDCConfig,
isSettingsConfig, isSettingsConfig,
ConfigType, ConfigType,
DatabaseQueryOpts,
} from "@budibase/types" } from "@budibase/types"
import env from "./../../../../environment" import env from "./../../../../environment"
export const getConfigParams = () => { export function getConfigParams(): DatabaseQueryOpts {
return { return {
include_docs: true, include_docs: true,
startkey: `${DocumentType.CONFIG}${SEPARATOR}`, startkey: `${DocumentType.CONFIG}${SEPARATOR}`,

View File

@ -1,6 +1,10 @@
import { context } from "@budibase/backend-core" import { context } from "@budibase/backend-core"
import { isTableId } from "@budibase/backend-core/src/docIds" 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" import { ViewName, getQueryIndex } from "../../../../src/db/utils"
export async function fetch(tableId: string): Promise<LinkDocumentValue[]> { export async function fetch(tableId: string): Promise<LinkDocumentValue[]> {
@ -9,7 +13,10 @@ export async function fetch(tableId: string): Promise<LinkDocumentValue[]> {
} }
const db = context.getAppDB() 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 const linkRows = (await db.query(getQueryIndex(ViewName.LINK), params)).rows
return linkRows.map(row => row.value as LinkDocumentValue) return linkRows.map(row => row.value as LinkDocumentValue)
} }
@ -22,7 +29,7 @@ export async function fetchWithDocument(
} }
const db = context.getAppDB() const db = context.getAppDB()
const params: any = { const params: DatabaseQueryOpts = {
startkey: [tableId], startkey: [tableId],
endkey: [tableId, {}], endkey: [tableId, {}],
include_docs: true, include_docs: true,

View File

@ -15,7 +15,7 @@
}, },
"jest": {}, "jest": {},
"devDependencies": { "devDependencies": {
"@budibase/nano": "10.1.2", "@budibase/nano": "10.1.3",
"@types/koa": "2.13.4", "@types/koa": "2.13.4",
"@types/node": "18.17.0", "@types/node": "18.17.0",
"@types/pouchdb": "6.4.0", "@types/pouchdb": "6.4.0",

View File

@ -54,15 +54,18 @@ export type DatabaseDeleteIndexOpts = {
type?: string | undefined type?: string | undefined
} }
type DBPrimitiveKey = string | number | {}
export type DatabaseKey = DBPrimitiveKey | DBPrimitiveKey[]
export type DatabaseQueryOpts = { export type DatabaseQueryOpts = {
include_docs?: boolean include_docs?: boolean
startkey?: string startkey?: DatabaseKey
endkey?: string endkey?: DatabaseKey
limit?: number limit?: number
skip?: number skip?: number
descending?: boolean descending?: boolean
key?: string key?: DatabaseKey
keys?: string[] keys?: DatabaseKey[]
group?: boolean group?: boolean
startkey_docid?: string startkey_docid?: string
} }

View File

@ -2099,10 +2099,10 @@
striptags "^3.1.1" striptags "^3.1.1"
to-gfm-code-block "^0.1.1" to-gfm-code-block "^0.1.1"
"@budibase/nano@10.1.2": "@budibase/nano@10.1.3":
version "10.1.2" version "10.1.3"
resolved "https://registry.yarnpkg.com/@budibase/nano/-/nano-10.1.2.tgz#10fae5a1ab39be6a81261f40e7b7ec6d21cbdd4a" resolved "https://registry.yarnpkg.com/@budibase/nano/-/nano-10.1.3.tgz#81b99d76b5c256a393e6ee0e284a6aecc517e4b8"
integrity sha512-1w+YN2n/M5aZ9hBKCP4NEjdQbT8BfCLRizkdvm0Je665eEHw3aE1hvo8mon9Ro9QuDdxj1DfDMMFnym6/QUwpQ== integrity sha512-UuhwjKCfVO+oVB0dbKpssZfTfb5k3CTrbrjqdx0kd971zzSRMFJ0TwvBB/2Z7kgOOA+Evoq4BSd747meEz21YA==
dependencies: dependencies:
"@types/tough-cookie" "^4.0.2" "@types/tough-cookie" "^4.0.2"
axios "^1.1.3" axios "^1.1.3"