Adding search index for user.
This commit is contained in:
parent
495c8f4b0e
commit
900e6c8129
|
@ -1,6 +1,7 @@
|
||||||
import fetch from "node-fetch"
|
import fetch from "node-fetch"
|
||||||
import { getCouchInfo } from "./couch"
|
import { getCouchInfo } from "./couch"
|
||||||
import { SearchFilters, Row } from "@budibase/types"
|
import { SearchFilters, Row } from "@budibase/types"
|
||||||
|
import { createUserIndex } from "./searchIndexes/searchIndexes"
|
||||||
|
|
||||||
const QUERY_START_REGEX = /\d[0-9]*:/g
|
const QUERY_START_REGEX = /\d[0-9]*:/g
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
import { User, SearchIndex } from "@budibase/types"
|
||||||
|
import { getGlobalDB } from "../../context"
|
||||||
|
|
||||||
|
export async function createUserIndex() {
|
||||||
|
const db = getGlobalDB()
|
||||||
|
let designDoc
|
||||||
|
try {
|
||||||
|
designDoc = await db.get("_design/database")
|
||||||
|
} catch (err: any) {
|
||||||
|
if (err.status === 404) {
|
||||||
|
designDoc = { _id: "_design/database" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// this is a very specific function given that it is only for audit logs
|
||||||
|
const fn = function (user: User) {
|
||||||
|
if (user._id && !user._id.startsWith("us_")) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const ignoredFields = ["_id", "_rev", "password"]
|
||||||
|
function idx(input: Record<string, any>, prev?: string) {
|
||||||
|
for (let key of Object.keys(input)) {
|
||||||
|
if (ignoredFields.includes(key)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let idxKey = prev != null ? `${prev}.${key}` : key
|
||||||
|
if (typeof input[key] === "string") {
|
||||||
|
// eslint-disable-next-line no-undef
|
||||||
|
// @ts-ignore
|
||||||
|
index(idxKey, input[key].toLowerCase(), { facet: true })
|
||||||
|
} else if (typeof input[key] !== "object") {
|
||||||
|
// eslint-disable-next-line no-undef
|
||||||
|
// @ts-ignore
|
||||||
|
index(idxKey, input[key], { facet: true })
|
||||||
|
} else {
|
||||||
|
idx(input[key], idxKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
idx(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
designDoc.indexes = {
|
||||||
|
[SearchIndex.USER]: {
|
||||||
|
index: fn.toString(),
|
||||||
|
analyzer: {
|
||||||
|
default: "keyword",
|
||||||
|
name: "perfield",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
await db.put(designDoc)
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import { Writable } from "stream"
|
||||||
export enum SearchIndex {
|
export enum SearchIndex {
|
||||||
ROWS = "rows",
|
ROWS = "rows",
|
||||||
AUDIT = "audit",
|
AUDIT = "audit",
|
||||||
|
USER = "user",
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PouchOptions = {
|
export type PouchOptions = {
|
||||||
|
|
Loading…
Reference in New Issue