1
0
Fork 0
mirror of synced 2024-06-28 02:50:50 +12:00

Some minor updates based on getting the worker using the new pouchlike impl.

This commit is contained in:
mike12345567 2022-11-09 17:53:35 +00:00
parent bbc8965d55
commit 1ee6761a69
4 changed files with 11 additions and 12 deletions

View file

@ -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))

View file

@ -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") {

View file

@ -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> {

View file

@ -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 => ({