1
0
Fork 0
mirror of synced 2024-08-17 02:51:55 +12:00

Merge pull request #8742 from Budibase/fix/table-delete

Require params on db.remove
This commit is contained in:
Rory Powell 2022-11-21 12:04:32 +00:00 committed by GitHub
commit 9d721cd196
3 changed files with 29 additions and 9 deletions

View file

@ -6,6 +6,8 @@ import {
DatabaseOpts, DatabaseOpts,
DatabaseQueryOpts, DatabaseQueryOpts,
DatabasePutOpts, DatabasePutOpts,
Document,
isDocument,
} from "@budibase/types" } from "@budibase/types"
import { getCouchInfo } from "./connections" import { getCouchInfo } from "./connections"
import { directCouchCall } from "./utils" import { directCouchCall } from "./utils"
@ -77,12 +79,23 @@ export class DatabaseImpl implements Database {
return this.updateOutput(() => db.get(id)) return this.updateOutput(() => db.get(id))
} }
async remove(id?: string, rev?: string) { async remove(idOrDoc: string | Document, rev?: string) {
const db = await this.checkSetup() const db = await this.checkSetup()
if (!id || !rev) { let _id: string
let _rev: string
if (isDocument(idOrDoc)) {
_id = idOrDoc._id!
_rev = idOrDoc._rev!
} else {
_id = idOrDoc
_rev = rev!
}
if (!_id || !_rev) {
throw new Error("Unable to remove doc without a valid _id and _rev.") throw new Error("Unable to remove doc without a valid _id and _rev.")
} }
return this.updateOutput(() => db.destroy(id, rev)) return this.updateOutput(() => db.destroy(_id, _rev))
} }
async put(document: AnyDocument, opts?: DatabasePutOpts) { async put(document: AnyDocument, opts?: DatabasePutOpts) {

View file

@ -2,7 +2,7 @@ import { updateLinks, EventType } from "../../../db/linkedRows"
import { getRowParams, generateTableID } from "../../../db/utils" import { getRowParams, generateTableID } from "../../../db/utils"
import { FieldTypes } from "../../../constants" import { FieldTypes } from "../../../constants"
import { TableSaveFunctions, hasTypeChanged, handleDataImport } from "./utils" import { TableSaveFunctions, hasTypeChanged, handleDataImport } from "./utils"
const { getAppDB } = require("@budibase/backend-core/context") import { context } from "@budibase/backend-core"
import { isTest } from "../../../environment" import { isTest } from "../../../environment"
import { import {
cleanupAttachments, cleanupAttachments,
@ -35,7 +35,7 @@ function checkAutoColumns(table: Table, oldTable: Table) {
} }
export async function save(ctx: any) { export async function save(ctx: any) {
const db = getAppDB() const db = context.getAppDB()
const { dataImport, ...rest } = ctx.request.body const { dataImport, ...rest } = ctx.request.body
let tableToSave = { let tableToSave = {
type: "table", type: "table",
@ -138,7 +138,7 @@ export async function save(ctx: any) {
} }
export async function destroy(ctx: any) { export async function destroy(ctx: any) {
const db = getAppDB() const db = context.getAppDB()
const tableToDelete = await db.get(ctx.params.tableId) const tableToDelete = await db.get(ctx.params.tableId)
// Delete all rows for that table // Delete all rows for that table
@ -161,7 +161,7 @@ export async function destroy(ctx: any) {
}) })
// don't remove the table itself until very end // don't remove the table itself until very end
await db.remove(tableToDelete) await db.remove(tableToDelete._id, tableToDelete._rev)
// remove table search index // remove table search index
if (!isTest() || env.COUCH_DB_URL) { if (!isTest() || env.COUCH_DB_URL) {

View file

@ -1,6 +1,6 @@
import PouchDB from "pouchdb" import PouchDB from "pouchdb"
import Nano from "nano" import Nano from "nano"
import { AllDocsResponse, AnyDocument } from "../" import { AllDocsResponse, AnyDocument, Document } from "../"
export type PouchOptions = { export type PouchOptions = {
inMemory?: boolean inMemory?: boolean
@ -44,13 +44,20 @@ export type DatabaseQueryOpts = {
keys?: string[] keys?: string[]
} }
export const isDocument = (doc: any): doc is Document => {
return typeof doc === "object" && doc._id && doc._rev
}
export interface Database { export interface Database {
name: string name: string
exists(): Promise<boolean> exists(): Promise<boolean>
checkSetup(): Promise<Nano.DocumentScope<any>> checkSetup(): Promise<Nano.DocumentScope<any>>
get<T>(id?: string): Promise<T | any> get<T>(id?: string): Promise<T | any>
remove(id?: string, rev?: string): Promise<Nano.DocumentDestroyResponse> remove(
id: string | Document,
rev?: string
): Promise<Nano.DocumentDestroyResponse>
put( put(
document: AnyDocument, document: AnyDocument,
opts?: DatabasePutOpts opts?: DatabasePutOpts