From 626576dbee0da2f0cd9c026dc7f28766ab1c6667 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Mon, 21 Nov 2022 10:07:51 +0000 Subject: [PATCH 1/2] Require params on db.remove --- packages/server/src/api/controllers/table/internal.ts | 8 ++++---- packages/types/src/sdk/db.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/server/src/api/controllers/table/internal.ts b/packages/server/src/api/controllers/table/internal.ts index a50009b1f6..ee98852a8f 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -2,7 +2,7 @@ import { updateLinks, EventType } from "../../../db/linkedRows" import { getRowParams, generateTableID } from "../../../db/utils" import { FieldTypes } from "../../../constants" import { TableSaveFunctions, hasTypeChanged, handleDataImport } from "./utils" -const { getAppDB } = require("@budibase/backend-core/context") +import { context } from "@budibase/backend-core" import { isTest } from "../../../environment" import { cleanupAttachments, @@ -35,7 +35,7 @@ function checkAutoColumns(table: Table, oldTable: Table) { } export async function save(ctx: any) { - const db = getAppDB() + const db = context.getAppDB() const { dataImport, ...rest } = ctx.request.body let tableToSave = { type: "table", @@ -138,7 +138,7 @@ export async function save(ctx: any) { } export async function destroy(ctx: any) { - const db = getAppDB() + const db = context.getAppDB() const tableToDelete = await db.get(ctx.params.tableId) // 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 - await db.remove(tableToDelete) + await db.remove(tableToDelete._id, tableToDelete._rev) // remove table search index if (!isTest() || env.COUCH_DB_URL) { diff --git a/packages/types/src/sdk/db.ts b/packages/types/src/sdk/db.ts index 44b4735882..df873911c8 100644 --- a/packages/types/src/sdk/db.ts +++ b/packages/types/src/sdk/db.ts @@ -50,7 +50,7 @@ export interface Database { exists(): Promise checkSetup(): Promise> get(id?: string): Promise - remove(id?: string, rev?: string): Promise + remove(id: string, rev: string): Promise put( document: AnyDocument, opts?: DatabasePutOpts From 523368655d6676ee1e38093feb67821d22d66b3e Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Mon, 21 Nov 2022 11:38:37 +0000 Subject: [PATCH 2/2] Add remove overload to allow for doc removal --- .../backend-core/src/db/couch/DatabaseImpl.ts | 19 ++++++++++++++++--- packages/types/src/sdk/db.ts | 11 +++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/backend-core/src/db/couch/DatabaseImpl.ts b/packages/backend-core/src/db/couch/DatabaseImpl.ts index d7220e845c..d4b7918ff1 100644 --- a/packages/backend-core/src/db/couch/DatabaseImpl.ts +++ b/packages/backend-core/src/db/couch/DatabaseImpl.ts @@ -6,6 +6,8 @@ import { DatabaseOpts, DatabaseQueryOpts, DatabasePutOpts, + Document, + isDocument, } from "@budibase/types" import { getCouchInfo } from "./connections" import { directCouchCall } from "./utils" @@ -77,12 +79,23 @@ export class DatabaseImpl implements Database { return this.updateOutput(() => db.get(id)) } - async remove(id?: string, rev?: string) { + async remove(idOrDoc: string | Document, rev?: string) { 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.") } - return this.updateOutput(() => db.destroy(id, rev)) + return this.updateOutput(() => db.destroy(_id, _rev)) } async put(document: AnyDocument, opts?: DatabasePutOpts) { diff --git a/packages/types/src/sdk/db.ts b/packages/types/src/sdk/db.ts index df873911c8..8979c1ba38 100644 --- a/packages/types/src/sdk/db.ts +++ b/packages/types/src/sdk/db.ts @@ -1,6 +1,6 @@ import PouchDB from "pouchdb" import Nano from "nano" -import { AllDocsResponse, AnyDocument } from "../" +import { AllDocsResponse, AnyDocument, Document } from "../" export type PouchOptions = { inMemory?: boolean @@ -44,13 +44,20 @@ export type DatabaseQueryOpts = { keys?: string[] } +export const isDocument = (doc: any): doc is Document => { + return typeof doc === "object" && doc._id && doc._rev +} + export interface Database { name: string exists(): Promise checkSetup(): Promise> get(id?: string): Promise - remove(id: string, rev: string): Promise + remove( + id: string | Document, + rev?: string + ): Promise put( document: AnyDocument, opts?: DatabasePutOpts