Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import elastic from '@elastic/elasticsearch' import { v4 as uuid } from 'uuid' import { AuthInput, ContractType, Implementations, AuthenticationDefinition, ManageableFields, HandleResult } from './globalTypes.js' import { mapFilter } from 'microtil' import { AbstractBackend, filterToAccess, authorizedByPermission, forbidden, notFound } from './backendAbstract.js' type Client = elastic.Client const Client = elastic.Client let clientInstance: Client | undefined export const client = () => clientInstance || init() export const destroyClient = () => { clientInstance = undefined } export const init = () => { const node = process.env.ELASTIC_HOST const username = process.env.ELASTIC_USER_NAME const password = process.env.ELASTIC_PASSWORD const apiKey = process.env.ELASTIC_API_KEY const apiId = process.env.ELASTIC_API_ID const unauthenticated = process.env.ELASTIC_UNAUTHENTICATED const setup: any = { node, requestTimeout: 90000 } if (username && password) { setup.auth = { username, password } } else if (apiKey) { setup.auth = apiId ? { apiKey: { id: apiId, api_key: apiKey } } : { apiKey } } else if (!unauthenticated) { console.warn('Elasticsearch api credentials are not set') } clientInstance = new Client(setup) return clientInstance } export const info = () => client().info() export const createIndex = (name:string) => client().indices.create({ index: name }) export const defaultSize = 64 type ES = Implementations.elasticsearch const getById = async (index:string, id:string) => { const { body: { _source } } = await client().get({ index, id }) return _source } const getByIdChecked = async ( index:string, id:string, authentication:AuthenticationDefinition, auth:AuthInput, manageFields:ManageableFields ) => filterToAccess([await getById(index, id)], authentication, auth, manageFields) export const get = async <IN, OUT>( contract: ContractType<'GET', ES, IN, OUT>, auth: AuthInput, idIn: string | string[] | undefined, input?:IN ): Promise<HandleResult<OUT>> => { const index = contract.implementation.index.toLowerCase() const { manageFields, authentication: authDef } = contract const userIdFilter: any = { match: { createdBy: auth.sub || '' } } const id:string | string[] = idIn || (input as any)?.id const search:string = (input as any)?.search if (Array.isArray(id)) { if (id.length === 0) return { result: ([] as any) } const { body: { docs } } = await client().mget({ index, body: { ids: id } }) return { result: filterToAccess(mapFilter(docs, (x: any) => x._source), authDef, auth, manageFields) as any } } else if (id) { try { const got = await getByIdChecked(index, id, authDef, auth, manageFields) as any[] if (got.length === 0) { return forbidden(id) } return { result: (got as any) } } catch (got) { if (got.meta.statusCode === 404) return notFound({ id, input }) throw got } } else if (search) { const queryString = { query: { bool: { must: [{ simple_query_string: { query: search } }] } } } if (!authorizedByPermission(authDef, auth)) queryString.query.bool.must.push(userIdFilter) const all = await client().search({ index, body: queryString, size: contract.implementation.maxResults || defaultSize }) return { result: new Array(all.body.hits.hits).flatMap((y: any) => y.map((x: any) => x._source)) as any } } const searchAll:any = { index, size: contract.implementation.maxResults || defaultSize } if (!authorizedByPermission(authDef, auth)) { searchAll.body = { query: { bool: { must: [userIdFilter] } } } } const all = await client().search(searchAll) const result = new Array(all.body.hits.hits).flatMap((y: any) => y.map((x: any) => x._source)) // console.log('IN THE END', result) return { result: result as any } } export const post = async <IN, OUT>( contract: ContractType<'POST', ES, IN, OUT>, auth:AuthInput, id: string|undefined, body: IN): Promise<HandleResult<OUT>> => { const idNew = id || uuid() const newBody: any = { ...body } if (contract.manageFields.id === true) { newBody.id = idNew } if (contract.manageFields.createdBy === true) { newBody.createdBy = auth.sub } try { await client().create({ id: idNew, index: contract.implementation.index.toLowerCase(), refresh: 'wait_for', body: newBody }) } catch (e) { if (e.meta.statusCode === 409) return { errorType: 'conflict', data: body, status: 409, errors: [] } throw e } return { result: newBody } } export const del = async <IN, OUT>( contract: ContractType<'DELETE', ES, IN, OUT>, auth:AuthInput, id: string|string[] ): Promise<HandleResult<OUT>> => { const index = contract.implementation.index.toLowerCase() if (Array.isArray(id)) { const data = await Promise.all( id.map(async (x) => ({ id, result: await del(contract, auth, x) }))) const errors = data.reduce( (p, c) => p.concat(Array.isArray(c.result.errors) && c.result.errors.length ? c.result.errors : []), [] as (string[])) if (errors.length) { return { errorType: 'forbidden', data, status: 403, errors } } return { result: {} as any } } try { const result = await getByIdChecked( index, id, contract.authentication, auth, contract.manageFields) if (!result || result.length === 0) { return forbidden(id, ['Can\'t delete, unauthorized']) } await client().delete({ index, id, refresh: 'wait_for' }) } catch (error) { if (error.meta.statusCode === 404) return { errorType: 'notFound', status: 404, data: id, errors: [] } throw error } return { result: {} as any } } export const patch = async <IN, OUT>( contract: ContractType<'PATCH', ES, IN, OUT>, auth:AuthInput, id: string, body: IN ): Promise<HandleResult<OUT>> => { const index = contract.implementation.index.toLowerCase() try { const result = await getByIdChecked(index, id, contract.authentication, auth, contract.manageFields) if (!result || result.length === 0) return forbidden({ id, body }) let newBody:any = body if (contract.manageFields.createdBy === true) { newBody = { ...(body as any) } newBody.createdBy = result[0].createdBy } await client().update({ index, refresh: 'wait_for', id, body: { doc: newBody } }) } catch (error) { if (error.meta.statusCode === 404) return notFound({ id, body }) throw error } return { result: {} as any } } export const put = async <IN, OUT>( contract: ContractType<'PUT', ES, IN, OUT>, auth:AuthInput, id: string, body: IN ): Promise<HandleResult<OUT>> => { const index = contract.implementation.index.toLowerCase() try { const result = await getByIdChecked(index, id, contract.authentication, auth, contract.manageFields) if (!result || result.length === 0) return forbidden({ id, body }) let newBody:any = body if (contract.manageFields.createdBy === true) { newBody = { ...(body as any) } newBody.createdBy = result[0].createdBy } await client().index( { index, refresh: 'wait_for', id, body: newBody }) return { result: {} as any } } catch (error) { if (error.meta.statusCode === 404) return notFound({ id, body }) throw error } } export const getElasticsearchProvider = ():AbstractBackend<ES> => ({ get, post, put, patch, delete: del }) |