Some minor updates based on getting the worker using the new pouchlike impl.
This commit is contained in:
parent
c744d23832
commit
9e01a9d1be
|
@ -1,5 +1,5 @@
|
|||
import Nano from "nano"
|
||||
import { AnyDocument } from "@budibase/types"
|
||||
import { AllDocsResponse, AnyDocument } from "@budibase/types"
|
||||
import { getCouchInfo } from "./couch"
|
||||
import { directCouchCall } from "./utils"
|
||||
import { getPouchDB } from "./pouchDB"
|
||||
|
@ -23,10 +23,6 @@ export type QueryOpts = {
|
|||
keys?: string[]
|
||||
}
|
||||
|
||||
type QueryResp<T> = Promise<{
|
||||
rows: { doc?: T | any; value?: any }[]
|
||||
}>
|
||||
|
||||
export class PouchLike {
|
||||
public readonly name: string
|
||||
private static nano: Nano.ServerScope
|
||||
|
@ -127,12 +123,15 @@ export class PouchLike {
|
|||
return this.updateOutput(() => db.bulk({ docs: documents }))
|
||||
}
|
||||
|
||||
async allDocs<T>(params: QueryOpts): QueryResp<T> {
|
||||
async allDocs<T>(params: QueryOpts): Promise<AllDocsResponse<T>> {
|
||||
const db = await this.checkSetup()
|
||||
return this.updateOutput(() => db.list(params))
|
||||
}
|
||||
|
||||
async query<T>(viewName: string, params: QueryOpts): QueryResp<T> {
|
||||
async query<T>(
|
||||
viewName: string,
|
||||
params: QueryOpts
|
||||
): Promise<AllDocsResponse<T>> {
|
||||
const db = await this.checkSetup()
|
||||
const [database, view] = viewName.split("/")
|
||||
return this.updateOutput(() => db.view(database, view, params))
|
||||
|
|
|
@ -131,16 +131,16 @@ export const queryView = async <T>(
|
|||
opts?: QueryViewOptions
|
||||
): Promise<T[] | T | undefined> => {
|
||||
try {
|
||||
let response = await db.query(`database/${viewName}`, params)
|
||||
let response = await db.query<T>(`database/${viewName}`, params)
|
||||
const rows = response.rows
|
||||
const docs = rows.map(row => (params.include_docs ? row.doc : row.value))
|
||||
|
||||
// if arrayResponse has been requested, always return array regardless of length
|
||||
if (opts?.arrayResponse) {
|
||||
return docs
|
||||
return docs as T[]
|
||||
} else {
|
||||
// return the single document if there is only one
|
||||
return docs.length <= 1 ? docs[0] : docs
|
||||
return docs.length <= 1 ? (docs[0] as T) : (docs as T[])
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (err != null && err.name === "not_found") {
|
||||
|
|
|
@ -8,7 +8,7 @@ export interface RowResponse<T> {
|
|||
key: string
|
||||
error: string
|
||||
value: RowValue
|
||||
doc: T
|
||||
doc?: T
|
||||
}
|
||||
|
||||
export interface AllDocsResponse<T> {
|
||||
|
|
|
@ -479,7 +479,7 @@ export const bulkDelete = async (
|
|||
(user: RowResponse<User>) => {
|
||||
return user.doc
|
||||
}
|
||||
)
|
||||
) as User[]
|
||||
|
||||
// Delete from DB
|
||||
const toDelete = usersToDelete.map(user => ({
|
||||
|
|
Loading…
Reference in New Issue