🚧 #46 Shared with me (#67)

* 🚧 #46 Shared with me
This commit is contained in:
Anton Shepilov
2023-06-07 10:48:44 +02:00
committed by GitHub
parent 49ec20d60e
commit f1bf507865
42 changed files with 1122 additions and 867 deletions
@@ -149,6 +149,10 @@ export class CrudException extends Error {
static badGateway(details: string): CrudException {
return new CrudException(details, 502);
}
static throwMe(cause: Error, newOne: CrudException): void {
throw cause instanceof CrudException ? cause : newOne;
}
}
export interface Paginable {
@@ -202,7 +206,7 @@ export interface CRUDService<Entity, PrimaryKey, Context extends ExecutionContex
* Save a resource.
* If the resource exists, it is updated, if it does not exists, it is created.
*
* @param itemOrItems
* @param item
* @param options
* @param context
*/
@@ -223,6 +227,8 @@ export interface CRUDService<Entity, PrimaryKey, Context extends ExecutionContex
/**
* List a resource
*
* @param pagination
* @param options
* @param context
*/
list<ListOptions>(
@@ -49,7 +49,7 @@ export function buildComparison(where: any, options: FindOptions = {}): string[]
if (operator === "$gt" || operator === "$gte" || operator === "$lt" || operator === "$lte") {
(options[operator] || []).forEach(element => {
if (!where[element[0]]) where[element[0]] = {};
where[element[0]][operator] = element[1];
where[element[0]][operator] = parseInt(element[1]);
});
}
});
@@ -16,9 +16,9 @@ export type FindFilter = { [key: string]: any };
export type RemoveFilter = { [key: string]: any };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type comparisonType = [string, any];
export type comparisonType = [string, any];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type inType = [string, Array<any>];
export type inType = [string, Array<any>];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type likeType = [string, any];
@@ -42,6 +42,9 @@ export default class ElasticSearch extends SearchAdapter implements SearchAdapte
try {
this.client = new Client({
node: this.configuration.endpoint,
ssl: {
rejectUnauthorized: false,
},
});
} catch (e) {
logger.error(`Unable to connect to ElasticSearch at ${this.configuration.endpoint}`);
@@ -3,6 +3,7 @@ import { TransportRequestOptions } from "@elastic/elasticsearch/lib/Transport";
import { logger } from "../../../../../../core/platform/framework/logger";
import { EntityTarget, FindFilter, FindOptions, getEntityDefinition } from "../../api";
import { asciiFold } from "../utils";
import { comparisonType } from "src/core/platform/services/database/services/orm/repository/repository";
export function buildSearchQuery<Entity>(
entityType: EntityTarget<Entity>,
@@ -45,7 +46,44 @@ export function buildSearchQuery<Entity>(
}
}
//TODO implement $gte, $lt, etc
function buildRangeQuery(
lteOperations: comparisonType[],
key: "gte" | "lte" | "lt" | "gt",
): void {
for (const lteOperation of lteOperations) {
if (lteOperation?.length == 2 && lteOperation[0]) {
const field_name = lteOperation[0];
esBody.query.bool.must.push({
range: {
[field_name]: {
[key]: lteOperation[1] ? parseInt(lteOperation[1]) : 0,
},
},
});
} else {
logger.warn(`Not enough data to include to the query: ${lteOperation}`);
}
}
}
if (options.$lte?.length) {
esBody.query.bool.must = esBody.query.bool.must || [];
buildRangeQuery(options.$lte, "lte");
}
if (options.$gte?.length) {
esBody.query.bool.must = esBody.query.bool.must || [];
buildRangeQuery(options.$gte, "gte");
}
if (options.$lt?.length) {
esBody.query.bool.must = esBody.query.bool.must || [];
buildRangeQuery(options.$lt, "lt");
}
if (options.$gt?.length) {
esBody.query.bool.must = esBody.query.bool.must || [];
buildRangeQuery(options.$gt, "gt");
}
if (options.$text) {
esBody.query.bool.minimum_should_match = 1;
@@ -84,7 +84,9 @@ export default class MongoSearch extends SearchAdapter implements SearchAdapterI
//Create one index for each type of indexes ["text"]
Object.keys(indexedFields).forEach(k => {
collection.createIndex(indexedFields[k], { default_language: "none" });
collection.createIndex(indexedFields[k], {
default_language: "none",
});
});
}
@@ -58,6 +58,10 @@ export function buildSearchQuery<Entity>(
});
}
if (options.$sort) {
sort = options.$sort;
}
return {
project,
query,
@@ -26,7 +26,7 @@ type TextType = {
$diacriticSensitive?: boolean; //Default false
};
type SortType = {
export type SortType = {
[key: string]: "asc" | "desc";
};