public async searchObjects()

in src/persistence/staticObjectStorage.ts [61:138]


    public async searchObjects<T>(path: string, query: Query<T>): Promise<Page<T>> {
        const searchObj = Objects.getObjectAt(path, this.storageDataObject);

        if (!searchObj) {
            return { value: [] };
        }

        let collection: any[] = Object.values(searchObj);

        if (query?.filters.length > 0) {
            collection = collection.filter(x => {
                let meetsCriteria = true;

                for (const filter of query.filters) {
                    let left = Objects.getObjectAt<any>(filter.left, x);
                    let right = filter.right;

                    if (left === undefined) {
                        meetsCriteria = false;
                        continue;
                    }

                    if (typeof left === "string") {
                        left = left.toUpperCase();
                    }

                    if (typeof right === "string") {
                        right = right.toUpperCase();
                    }

                    const operator = filter.operator;

                    switch (operator) {
                        case Operator.contains:
                            if (left && !left.includes(right)) {
                                meetsCriteria = false;
                            }
                            break;

                        case Operator.equals:
                            if (left !== right) {
                                meetsCriteria = false;
                            }
                            break;

                        default:
                            throw new Error(`Operator not supported.`);
                    }
                }

                return meetsCriteria;
            });
        }

        if (query?.orderingBy) {
            const property = query.orderingBy;

            collection = collection.sort((x, y) => {
                const a = Objects.getObjectAt<any>(property, x);
                const b = Objects.getObjectAt<any>(property, y);
                const modifier = query.orderDirection === OrderDirection.accending ? 1 : -1;

                if (a > b) {
                    return modifier;
                }

                if (a < b) {
                    return -modifier;
                }

                return 0;
            });
        }

        const value = collection.slice(0, pageSize);

        return new StaticPage(value, collection, pageSize);
    }