forked from tsai/budibase
12 changed files with 310 additions and 33 deletions
@ -1,9 +1,45 @@ |
|||
exports.search = () => {} |
|||
const { search } = require("./utils") |
|||
const { getAllApps } = require("@budibase/backend-core/db") |
|||
const { updateAppId } = require("@budibase/backend-core/context") |
|||
const controller = require("../application") |
|||
|
|||
exports.create = () => {} |
|||
async function setResponseApp(ctx) { |
|||
if (ctx.body && ctx.body.appId && (!ctx.params || !ctx.params.appId)) { |
|||
ctx.params = { appId: ctx.body.appId } |
|||
} |
|||
await controller.fetchAppPackage(ctx) |
|||
} |
|||
|
|||
exports.read = () => {} |
|||
exports.search = async ctx => { |
|||
const { name } = ctx.request.body |
|||
const apps = await getAllApps({ all: true }) |
|||
ctx.body = { |
|||
applications: search(apps, "name", name), |
|||
} |
|||
} |
|||
|
|||
exports.update = () => {} |
|||
exports.create = async ctx => { |
|||
await controller.create(ctx) |
|||
await setResponseApp(ctx) |
|||
} |
|||
|
|||
exports.delete = () => {} |
|||
exports.read = async ctx => { |
|||
updateAppId(ctx.params.appId) |
|||
await setResponseApp(ctx) |
|||
} |
|||
|
|||
exports.update = async ctx => { |
|||
updateAppId(ctx.params.appId) |
|||
await controller.update(ctx) |
|||
await setResponseApp(ctx) |
|||
} |
|||
|
|||
exports.delete = async ctx => { |
|||
updateAppId(ctx.params.appId) |
|||
// get the app before deleting it
|
|||
await setResponseApp(ctx) |
|||
const body = ctx.body |
|||
await controller.delete(ctx) |
|||
// overwrite the body again
|
|||
ctx.body = body |
|||
} |
|||
|
|||
@ -1,3 +1,14 @@ |
|||
exports.search = () => {} |
|||
const { searchDocs } = require("./utils") |
|||
const { DocumentTypes } = require("../../../db/utils") |
|||
const queryController = require("../query") |
|||
|
|||
exports.execute = () => {} |
|||
exports.search = async ctx => { |
|||
const { name } = ctx.request.body |
|||
ctx.body = { |
|||
queries: await searchDocs(DocumentTypes.QUERY, "name", name), |
|||
} |
|||
} |
|||
|
|||
exports.execute = async ctx => { |
|||
await queryController.executeV2(ctx) |
|||
} |
|||
|
|||
@ -0,0 +1,47 @@ |
|||
const { getAppDB } = require("@budibase/backend-core/context") |
|||
const { getDocParams } = require("@budibase/backend-core/db") |
|||
|
|||
exports.addRev = async body => { |
|||
if (!body._id) { |
|||
return body |
|||
} |
|||
const db = getAppDB() |
|||
const dbDoc = await db.get(body._id) |
|||
body._rev = dbDoc._rev |
|||
return body |
|||
} |
|||
|
|||
exports.search = (docs, key, value) => { |
|||
if (!value || typeof value !== "string") { |
|||
return docs |
|||
} |
|||
value = value.toLowerCase() |
|||
const filtered = [] |
|||
for (let doc of docs) { |
|||
if (typeof doc[key] !== "string") { |
|||
continue |
|||
} |
|||
const toTest = doc[key].toLowerCase() |
|||
if (toTest.startsWith(value)) { |
|||
filtered.push(doc) |
|||
} |
|||
} |
|||
return filtered |
|||
} |
|||
|
|||
/** |
|||
* Performs a case insensitive search on a document type, using the |
|||
* provided key and value. This will be a string based search, |
|||
* using the startsWith function. |
|||
*/ |
|||
exports.searchDocs = async (docType, key, value) => { |
|||
const db = getAppDB() |
|||
const docs = ( |
|||
await db.allDocs( |
|||
getDocParams(docType, null, { |
|||
include_docs: true, |
|||
}) |
|||
) |
|||
).rows.map(row => row.doc) |
|||
return exports.search(docs, key, value) |
|||
} |
|||
Loading…
Reference in new issue