From f49233d8834bb9a2fcfc75602a2eedd8320464c0 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 21 Nov 2022 14:41:19 +0000 Subject: [PATCH] Fix for table deletion, updating DatabaseImpl to define pouch functions directly. --- .../backend-core/src/db/couch/DatabaseImpl.ts | 39 ++++++++++--------- packages/types/src/sdk/db.ts | 15 +++++++ 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/packages/backend-core/src/db/couch/DatabaseImpl.ts b/packages/backend-core/src/db/couch/DatabaseImpl.ts index d4b7918ff1..de06b4e8ee 100644 --- a/packages/backend-core/src/db/couch/DatabaseImpl.ts +++ b/packages/backend-core/src/db/couch/DatabaseImpl.ts @@ -6,12 +6,15 @@ import { DatabaseOpts, DatabaseQueryOpts, DatabasePutOpts, + DatabaseCreateIndexOpts, + DatabaseDeleteIndexOpts, Document, isDocument, } from "@budibase/types" import { getCouchInfo } from "./connections" import { directCouchCall } from "./utils" import { getPouchDB } from "./pouchDB" +import { WriteStream, ReadStream } from "fs" export class DatabaseImpl implements Database { public readonly name: string @@ -159,34 +162,32 @@ export class DatabaseImpl implements Database { return this.updateOutput(() => db.compact()) } - private doWithPouchDB(func: string) { - const dbName = this.name - return async (args: any[]) => { - const pouch = getPouchDB(dbName) - // @ts-ignore - return pouch[func](...args) - } - } - // All below functions are in-frequently called, just utilise PouchDB // for them as it implements them better than we can - async dump(...args: any[]) { - return this.doWithPouchDB("dump")(args) + async dump(stream: WriteStream, opts?: { filter?: any }) { + const pouch = getPouchDB(this.name) + // @ts-ignore + return pouch.dump(stream, opts) } - async load(...args: any[]) { - return this.doWithPouchDB("load")(args) + async load(stream: ReadStream) { + const pouch = getPouchDB(this.name) + // @ts-ignore + return pouch.load(stream) } - async createIndex(...args: any[]) { - return this.doWithPouchDB("createIndex")(args) + async createIndex(opts: DatabaseCreateIndexOpts) { + const pouch = getPouchDB(this.name) + return pouch.createIndex(opts) } - async deleteIndex(...args: any[]) { - return this.doWithPouchDB("createIndex")(args) + async deleteIndex(opts: DatabaseDeleteIndexOpts) { + const pouch = getPouchDB(this.name) + return pouch.deleteIndex(opts) } - async getIndexes(...args: any[]) { - return this.doWithPouchDB("createIndex")(args) + async getIndexes() { + const pouch = getPouchDB(this.name) + return pouch.getIndexes() } } diff --git a/packages/types/src/sdk/db.ts b/packages/types/src/sdk/db.ts index 8979c1ba38..200b25c501 100644 --- a/packages/types/src/sdk/db.ts +++ b/packages/types/src/sdk/db.ts @@ -33,6 +33,21 @@ export type DatabasePutOpts = { force?: boolean } +export type DatabaseCreateIndexOpts = { + index: { + fields: string[] + name?: string | undefined + ddoc?: string | undefined + type?: string | undefined + } +} + +export type DatabaseDeleteIndexOpts = { + name: string + ddoc: string + type?: string | undefined +} + export type DatabaseQueryOpts = { include_docs?: boolean startkey?: string