budibase/packages/backend-core/src/db/index.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

import env from "../environment"
import { CouchFindOptions } from "@budibase/types"
import { directCouchQuery, PouchLike } from "./couch"
export { init, PouchLike, getPouch, getPouchDB } from "./couch"
let initialised = false
const dbList = new Set()
const checkInitialised = () => {
if (!initialised) {
throw new Error("init has not been called")
}
}
export function getDB(dbName: string, opts?: any): PouchLike {
if (env.isTest()) {
dbList.add(dbName)
}
return new PouchLike(dbName, opts)
}
// we have to use a callback for this so that we can close
// the DB when we're done, without this manual requests would
// need to close the database when done with it to avoid memory leaks
export async function doWithDB(dbName: string, cb: any, opts = {}) {
const db = getDB(dbName, opts)
// need this to be async so that we can correctly close DB after all
// async operations have been completed
return await cb(db)
}
export function allDbs() {
if (!env.isTest()) {
throw new Error("Cannot be used outside test environment.")
}
checkInitialised()
return [...dbList]
2021-05-14 17:31:07 +02:00
}
export async function directCouchAllDbs(queryString?: string) {
let couchPath = "/_all_dbs"
if (queryString) {
couchPath += `?${queryString}`
}
return await directCouchQuery(couchPath)
}
export async function directCouchFind(dbName: string, opts: CouchFindOptions) {
const json = await directCouchQuery(`${dbName}/_find`, "POST", opts)
return { rows: json.docs, bookmark: json.bookmark }
}