Add remove overload to allow for doc removal
This commit is contained in:
parent
e6589fd4c1
commit
6cb71b5272
|
@ -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) {
|
||||
|
|
|
@ -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<boolean>
|
||||
checkSetup(): Promise<Nano.DocumentScope<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(
|
||||
document: AnyDocument,
|
||||
opts?: DatabasePutOpts
|
||||
|
|
Loading…
Reference in New Issue