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