Some of the new table getters did not account for table IDs not being found, adding a new function which properly accounts for IDs being missing and manages it correctly, rather than leaving tables in an undefined state.
This commit is contained in:
parent
7afcfbac93
commit
a26f2e83e4
|
@ -109,7 +109,7 @@ export class DatabaseImpl implements Database {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async get<T>(id?: string): Promise<T | any> {
|
async get<T extends Document>(id?: string): Promise<T> {
|
||||||
const db = await this.checkSetup()
|
const db = await this.checkSetup()
|
||||||
if (!id) {
|
if (!id) {
|
||||||
throw new Error("Unable to get doc without a valid _id.")
|
throw new Error("Unable to get doc without a valid _id.")
|
||||||
|
@ -117,6 +117,17 @@ export class DatabaseImpl implements Database {
|
||||||
return this.updateOutput(() => db.get(id))
|
return this.updateOutput(() => db.get(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getMultiple<T extends Document>(ids: string[]): Promise<T[]> {
|
||||||
|
// get unique
|
||||||
|
ids = [...new Set(ids)]
|
||||||
|
const response = await this.allDocs<T>({
|
||||||
|
keys: ids,
|
||||||
|
include_docs: true,
|
||||||
|
})
|
||||||
|
const rows = response.rows.filter(row => row.error !== "not_found")
|
||||||
|
return rows.map(row => row.doc!)
|
||||||
|
}
|
||||||
|
|
||||||
async remove(idOrDoc: string | Document, rev?: string) {
|
async remove(idOrDoc: string | Document, rev?: string) {
|
||||||
const db = await this.checkSetup()
|
const db = await this.checkSetup()
|
||||||
let _id: string
|
let _id: string
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
import * as linkRows from "../../../db/linkedRows"
|
import * as linkRows from "../../../db/linkedRows"
|
||||||
import {
|
import { generateRowID, InternalTables } from "../../../db/utils"
|
||||||
generateRowID,
|
|
||||||
getMultiIDParams,
|
|
||||||
InternalTables,
|
|
||||||
} from "../../../db/utils"
|
|
||||||
import * as userController from "../user"
|
import * as userController from "../user"
|
||||||
import {
|
import {
|
||||||
cleanupAttachments,
|
cleanupAttachments,
|
||||||
|
@ -240,8 +236,9 @@ export async function fetchEnrichedRow(ctx: UserCtx) {
|
||||||
const linkVals = links as LinkDocumentValue[]
|
const linkVals = links as LinkDocumentValue[]
|
||||||
|
|
||||||
// look up the actual rows based on the ids
|
// look up the actual rows based on the ids
|
||||||
const params = getMultiIDParams(linkVals.map(linkVal => linkVal.id))
|
let linkedRows = await db.getMultiple<Row>(
|
||||||
let linkedRows = (await db.allDocs<Row>(params)).rows.map(row => row.doc!)
|
linkVals.map(linkVal => linkVal.id)
|
||||||
|
)
|
||||||
|
|
||||||
// get the linked tables
|
// get the linked tables
|
||||||
const linkTableIds = getLinkedTableIDs(table as Table)
|
const linkTableIds = getLinkedTableIDs(table as Table)
|
||||||
|
|
|
@ -1,21 +1,9 @@
|
||||||
import { InternalTables } from "../../../db/utils"
|
import { InternalTables } from "../../../db/utils"
|
||||||
import * as userController from "../user"
|
import * as userController from "../user"
|
||||||
import { context } from "@budibase/backend-core"
|
import { context } from "@budibase/backend-core"
|
||||||
import {
|
import { Ctx, Row, UserCtx } from "@budibase/types"
|
||||||
Ctx,
|
|
||||||
FieldType,
|
|
||||||
ManyToOneRelationshipFieldMetadata,
|
|
||||||
OneToManyRelationshipFieldMetadata,
|
|
||||||
Row,
|
|
||||||
SearchFilters,
|
|
||||||
Table,
|
|
||||||
UserCtx,
|
|
||||||
} from "@budibase/types"
|
|
||||||
import { FieldTypes, NoEmptyFilterStrings } from "../../../constants"
|
|
||||||
import sdk from "../../../sdk"
|
|
||||||
|
|
||||||
import validateJs from "validate.js"
|
import validateJs from "validate.js"
|
||||||
import { cloneDeep } from "lodash/fp"
|
|
||||||
|
|
||||||
validateJs.extend(validateJs.validators.datetime, {
|
validateJs.extend(validateJs.validators.datetime, {
|
||||||
parse: function (value: string) {
|
parse: function (value: string) {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import {
|
||||||
getLinkedTable,
|
getLinkedTable,
|
||||||
} from "./linkUtils"
|
} from "./linkUtils"
|
||||||
import flatten from "lodash/flatten"
|
import flatten from "lodash/flatten"
|
||||||
import { getMultiIDParams, USER_METDATA_PREFIX } from "../utils"
|
import { USER_METDATA_PREFIX } from "../utils"
|
||||||
import partition from "lodash/partition"
|
import partition from "lodash/partition"
|
||||||
import { getGlobalUsersFromMetadata } from "../../utilities/global"
|
import { getGlobalUsersFromMetadata } from "../../utilities/global"
|
||||||
import { processFormulas } from "../../utilities/rowProcessor"
|
import { processFormulas } from "../../utilities/rowProcessor"
|
||||||
|
@ -79,9 +79,7 @@ async function getFullLinkedDocs(links: LinkDocumentValue[]) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const linkedRowIds = links.map(link => link.id)
|
const linkedRowIds = links.map(link => link.id)
|
||||||
const uniqueRowIds = [...new Set(linkedRowIds)]
|
const uniqueRowIds = [...new Set(linkedRowIds)]
|
||||||
let dbRows = (await db.allDocs<Row>(getMultiIDParams(uniqueRowIds))).rows.map(
|
let dbRows = await db.getMultiple<Row>(uniqueRowIds)
|
||||||
row => row.doc!
|
|
||||||
)
|
|
||||||
// convert the unique db rows back to a full list of linked rows
|
// convert the unique db rows back to a full list of linked rows
|
||||||
const linked = linkedRowIds
|
const linked = linkedRowIds
|
||||||
.map(id => dbRows.find(row => row && row._id === id))
|
.map(id => dbRows.find(row => row && row._id === id))
|
||||||
|
|
|
@ -283,16 +283,6 @@ export function generatePluginID(name: string) {
|
||||||
return `${DocumentType.PLUGIN}${SEPARATOR}${name}`
|
return `${DocumentType.PLUGIN}${SEPARATOR}${name}`
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This can be used with the db.allDocs to get a list of IDs
|
|
||||||
*/
|
|
||||||
export function getMultiIDParams(ids: string[]) {
|
|
||||||
return {
|
|
||||||
keys: ids,
|
|
||||||
include_docs: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a new view ID.
|
* Generates a new view ID.
|
||||||
* @returns The new view ID which the view doc can be stored under.
|
* @returns The new view ID which the view doc can be stored under.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { context } from "@budibase/backend-core"
|
import { context } from "@budibase/backend-core"
|
||||||
import { getMultiIDParams, getTableParams } from "../../../db/utils"
|
import { getTableParams } from "../../../db/utils"
|
||||||
import {
|
import {
|
||||||
breakExternalTableId,
|
breakExternalTableId,
|
||||||
isExternalTableID,
|
isExternalTableID,
|
||||||
|
@ -17,6 +17,9 @@ import datasources from "../datasources"
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
|
|
||||||
export function processTable(table: Table): Table {
|
export function processTable(table: Table): Table {
|
||||||
|
if (!table) {
|
||||||
|
return table
|
||||||
|
}
|
||||||
if (table._id && isExternalTableID(table._id)) {
|
if (table._id && isExternalTableID(table._id)) {
|
||||||
return {
|
return {
|
||||||
...table,
|
...table,
|
||||||
|
@ -73,6 +76,9 @@ export async function getExternalTable(
|
||||||
tableName: string
|
tableName: string
|
||||||
): Promise<Table> {
|
): Promise<Table> {
|
||||||
const entities = await getExternalTablesInDatasource(datasourceId)
|
const entities = await getExternalTablesInDatasource(datasourceId)
|
||||||
|
if (!entities[tableName]) {
|
||||||
|
throw new Error(`Unable to find table named "${tableName}"`)
|
||||||
|
}
|
||||||
return processTable(entities[tableName])
|
return processTable(entities[tableName])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,10 +130,8 @@ export async function getTables(tableIds: string[]): Promise<Table[]> {
|
||||||
}
|
}
|
||||||
if (internalTableIds.length) {
|
if (internalTableIds.length) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const internalTableDocs = await db.allDocs<Table>(
|
const internalTables = await db.getMultiple<Table>(internalTableIds)
|
||||||
getMultiIDParams(internalTableIds)
|
tables = tables.concat(internalTables)
|
||||||
)
|
|
||||||
tables = tables.concat(internalTableDocs.rows.map(row => row.doc!))
|
|
||||||
}
|
}
|
||||||
return processTables(tables)
|
return processTables(tables)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { getMultiIDParams, getGlobalIDFromUserMetadataID } from "../db/utils"
|
import { getGlobalIDFromUserMetadataID } from "../db/utils"
|
||||||
import {
|
import {
|
||||||
roles,
|
roles,
|
||||||
db as dbCore,
|
db as dbCore,
|
||||||
|
@ -96,9 +96,7 @@ export async function getRawGlobalUsers(userIds?: string[]): Promise<User[]> {
|
||||||
const db = tenancy.getGlobalDB()
|
const db = tenancy.getGlobalDB()
|
||||||
let globalUsers: User[]
|
let globalUsers: User[]
|
||||||
if (userIds) {
|
if (userIds) {
|
||||||
globalUsers = (await db.allDocs<User>(getMultiIDParams(userIds))).rows.map(
|
globalUsers = await db.getMultiple<User>(userIds)
|
||||||
row => row.doc!
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
globalUsers = (
|
globalUsers = (
|
||||||
await db.allDocs<User>(
|
await db.allDocs<User>(
|
||||||
|
|
|
@ -122,7 +122,8 @@ export interface Database {
|
||||||
|
|
||||||
exists(): Promise<boolean>
|
exists(): Promise<boolean>
|
||||||
checkSetup(): Promise<Nano.DocumentScope<any>>
|
checkSetup(): Promise<Nano.DocumentScope<any>>
|
||||||
get<T>(id?: string): Promise<T>
|
get<T extends Document>(id?: string): Promise<T>
|
||||||
|
getMultiple<T extends Document>(ids: string[]): Promise<T[]>
|
||||||
remove(
|
remove(
|
||||||
id: string | Document,
|
id: string | Document,
|
||||||
rev?: string
|
rev?: string
|
||||||
|
|
Loading…
Reference in New Issue