Skip to content

Commit

Permalink
Merge pull request #372 from tigrisdata/main
Browse files Browse the repository at this point in the history
Merged by Reviewpad
  • Loading branch information
reviewpad[bot] authored May 17, 2023
2 parents 339c469 + f348df2 commit 7ad2e0d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 87 deletions.
31 changes: 8 additions & 23 deletions src/__tests__/tigris.readfields.spec.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
import { ReadFields, TigrisCollectionType } from "../types";
import { ReadFields } from "../types";
import { Utility } from "../utility";

export interface IBook1 extends TigrisCollectionType {
id?: number;
title: string;
author: Author;
tags?: string[];
}

export interface Author extends TigrisCollectionType {
firstName: string;
lastName: string;
}
describe("readFields tests", () => {
it("readFields1", () => {
const readFields: ReadFields<IBook1> = {
include: ["id", "title", "author.firstName", "author.lastName"],
const readFields: ReadFields = {
include: ["id", "title"],
};
expect(Utility.readFieldString<IBook1>(readFields)).toBe(
'{"id":true,"title":true,"author.firstName":true,"author.lastName":true}'
);
expect(Utility.readFieldString(readFields)).toBe('{"id":true,"title":true}');
});
it("readFields2", () => {
const readFields: ReadFields<IBook1> = {
const readFields: ReadFields = {
exclude: ["id", "title"],
};
expect(Utility.readFieldString<IBook1>(readFields)).toBe('{"id":false,"title":false}');
expect(Utility.readFieldString(readFields)).toBe('{"id":false,"title":false}');
});
it("readFields3", () => {
const readFields: ReadFields<IBook1> = {
const readFields: ReadFields = {
include: ["id", "title"],
exclude: ["author"],
};
expect(Utility.readFieldString<IBook1>(readFields)).toBe(
'{"id":true,"title":true,"author":false}'
);
expect(Utility.readFieldString(readFields)).toBe('{"id":true,"title":true,"author":false}');
});
});
28 changes: 8 additions & 20 deletions src/__tests__/tigris.utility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,11 @@ import {
SearchQueryOptions,
} from "../search";
import { SearchRequest as ProtoSearchRequest } from "../proto/server/v1/api_pb";
import { SortOrder, TigrisCollectionType } from "../types";
import { SortOrder } from "../types";
import { Field } from "../decorators/tigris-field";
import { TigrisCollection } from "../decorators/tigris-collection";
import { PrimaryKey } from "../decorators/tigris-primary-key";

interface ICollectionFields extends TigrisCollectionType {
field_1: string;
field_2: string;
field_3: string;
parent?: IParent;
}

interface IParent extends TigrisCollectionType {
field_1?: string;
field_2?: string;
field_3?: string;
}

describe("utility tests", () => {
it("base64encode", () => {
expect(Utility._base64Encode("hello world")).toBe("aGVsbG8gd29ybGQ=");
Expand All @@ -53,15 +40,15 @@ describe("utility tests", () => {
});

it("serializes FacetFields to string", () => {
const fields: FacetFields<ICollectionFields> = ["field_1", "field_2"];
const fields: FacetFields = ["field_1", "field_2"];
const serialized: string = Utility.facetQueryToString(fields);
expect(serialized).toBe(
'{"field_1":{"size":10,"type":"value"},"field_2":{"size":10,"type":"value"}}'
);
});

it("serializes FacetFieldOptions to string", () => {
const fields: FacetFieldOptions<ICollectionFields> = {
const fields: FacetFieldOptions = {
field_1: Utility.defaultFacetingOptions(),
field_2: { size: 10 },
};
Expand All @@ -72,16 +59,17 @@ describe("utility tests", () => {
});

it("equivalent serialization of FacetFieldsQuery", () => {
const facetFields: FacetFieldsQuery<ICollectionFields> = ["field_1", "field_2"];
const fieldOptions: FacetFieldsQuery<ICollectionFields> = {
const facetFields: FacetFieldsQuery = ["field_1", "field_2"];
const fieldOptions: FacetFieldsQuery = {
field_1: Utility.defaultFacetingOptions(),
field_2: { size: 10, type: "value" },
};
const serializedFields = Utility.facetQueryToString(facetFields);
expect(serializedFields).toBe(Utility.facetQueryToString(fieldOptions));
});

it.each<[string, SortOrder<ICollectionFields>, string]>([
it.each<[string, SortOrder, string]>([
["undefined", undefined, "[]"],
[
"multiple sort fields",
[
Expand All @@ -93,7 +81,7 @@ describe("utility tests", () => {
["single sort field", { field: "field_3", order: "$desc" }, '[{"field_3":"$desc"}]'],
["empty array", [], "[]"],
])("_sortOrderingToString() with '%s'", (testName, input, expected) => {
expect(Utility._sortOrderingToString<ICollectionFields>(input)).toBe(expected);
expect(Utility._sortOrderingToString(input)).toBe(expected);
});

describe("createProtoSearchRequest", () => {
Expand Down
6 changes: 2 additions & 4 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,13 +569,11 @@ export class Collection<T extends TigrisCollectionType> implements ICollection {
.setFilter(Utility.stringToUint8Array(Utility.filterToString(query.filter)));

if (query.readFields) {
readRequest.setFields(
Utility.stringToUint8Array(Utility.readFieldString<T>(query.readFields))
);
readRequest.setFields(Utility.stringToUint8Array(Utility.readFieldString(query.readFields)));
}

if (query.sort) {
readRequest.setSort(Utility.stringToUint8Array(Utility._sortOrderingToString<T>(query.sort)));
readRequest.setSort(Utility.stringToUint8Array(Utility._sortOrderingToString(query.sort)));
}

if (query.options) {
Expand Down
20 changes: 10 additions & 10 deletions src/search/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DocumentPaths, Filter, SortOrder, TigrisCollectionType } from "../types";
import { Filter, SortOrder, TigrisCollectionType } from "../types";
import { TigrisIndexType } from "./types";

export const MATCH_ALL_QUERY_STRING = "";
Expand All @@ -14,35 +14,35 @@ export interface SearchQuery<T extends TigrisCollectionType | TigrisIndexType> {
/**
* Fields to project search query on
*/
searchFields?: Array<DocumentPaths<T>>;
searchFields?: Array<string>;
/**
* Filter to further refine the search results
*/
filter?: Filter<T>;
/**
* Facet fields to categorically arrange indexed terms
*/
facets?: FacetFieldsQuery<T>;
facets?: FacetFieldsQuery;
/**
* Perform a nearest neighbor search to find closest documents
*/
vectorQuery?: VectorQuery;
/**
* Sort the search results in indicated order
*/
sort?: SortOrder<T>;
sort?: SortOrder;
/**
* Group by single or multiple fields in the index
*/
groupBy?: Array<string>;
/**
* Document fields to include when returning search results
*/
includeFields?: Array<DocumentPaths<T>>;
includeFields?: Array<string>;
/**
* Document fields to exclude when returning search results
*/
excludeFields?: Array<DocumentPaths<T>>;
excludeFields?: Array<string>;
/**
* Maximum number of search hits (matched documents) to fetch per page
*/
Expand All @@ -63,19 +63,19 @@ export interface SearchQueryOptions {
collation?: Collation;
}

export type FacetFieldsQuery<T> = FacetFieldOptions<T> | FacetFields<T>;
export type FacetFieldsQuery = FacetFieldOptions | FacetFields;

/**
* Map of collection field names and faceting options to include facet results in search response
*/
export type FacetFieldOptions<T> = {
[K in DocumentPaths<T>]?: FacetQueryOptions;
export type FacetFieldOptions = {
[key: string]: FacetQueryOptions;
};

/**
* Array of field names to include facet results for in search response
*/
export type FacetFields<T> = Array<DocumentPaths<T>>;
export type FacetFields = Array<string>;

/**
* Information to build facets in search results
Expand Down
42 changes: 20 additions & 22 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ export interface TigrisCollectionType {
export type NumericType = number | bigint;
export type FieldTypes = string | boolean | NumericType | BigInteger | Date | object;

export type ReadFields<T> = {
include?: Array<DocumentPaths<T>>;
exclude?: Array<DocumentPaths<T>>;
export type ReadFields = {
include?: Array<string>;
exclude?: Array<string>;
};

type DocumentFields<T, V> = Partial<{
Expand All @@ -518,13 +518,13 @@ export type UpdateFields<T> =
/**
* List of fields and their corresponding sort order to order the search results.
*/
export type SortOrder<T> = SortField<T> | Array<SortField<T>>;
export type SortOrder = SortField | Array<SortField>;

/**
* Collection field name and sort order
*/
export type SortField<T> = {
field: DocumentPaths<T>;
export type SortField = {
field: string;
order: "$asc" | "$desc";
};

Expand All @@ -549,12 +549,12 @@ export interface FindQuery<T> {
* Field projection to allow returning only specific document fields. By default
* all document fields are returned.
*/
readFields?: ReadFields<T>;
readFields?: ReadFields;

/**
* Sort the query results as per indicated order
*/
sort?: SortOrder<T>;
sort?: SortOrder;

/**
* Optional params
Expand Down Expand Up @@ -751,19 +751,17 @@ export type Selector<T> = Partial<{
[K in string]: unknown;
}>;

/**
* Compute all possible property combinations
*/
type normalTypes = PropertyKey | BigInt | Date | boolean | Array<unknown>;
export type DocumentPaths<T, Cache extends string = ""> = T extends normalTypes
? Cache
: {
[P in keyof T]: P extends string
? Cache extends ""
? DocumentPaths<T[P], `${P}`>
: Cache | DocumentPaths<T[P], `${Cache}.${P}`>
: `${Cache}${P & string}`;
}[keyof T];
type PathsForFilter<T, P extends string = ""> = {
[K in keyof T]: T[K] extends object
? T[K] extends unknown[]
? `${P}${K & string}`
: Paths<T[K], `${P}${K & string}.`> extends infer O
? T[K] extends Date | BigInt
? `${O & string}` | `${P}${K & string}`
: `${O & string}`
: never
: `${P}${K & string}`;
}[keyof T];

export type SelectorOperator =
| "$eq"
Expand All @@ -778,7 +776,7 @@ export type SelectorOperator =
export type LogicalOperator = "$or" | "$and";

export type SelectorFilter<T> = {
[K in DocumentPaths<T>]?: PathType<T, K> | { [P in SelectorOperator]?: PathType<T, K> };
[K in PathsForFilter<T>]?: PathType<T, K> | { [P in SelectorOperator]?: PathType<T, K> };
};

export type LogicalFilter<T> = {
Expand Down
13 changes: 5 additions & 8 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const Utility = {
_getRandomInt(upperBound: number): number {
return Math.floor(Math.random() * upperBound);
},
readFieldString<T>(readFields: ReadFields<T>): string {
readFieldString(readFields: ReadFields): string {
const include = readFields.include?.reduce((acc, field) => ({ ...acc, [field]: true }), {});
const exclude = readFields.exclude?.reduce((acc, field) => ({ ...acc, [field]: false }), {});

Expand Down Expand Up @@ -491,9 +491,8 @@ export const Utility = {
return { ...defaults, ...options };
},

facetQueryToString<T>(facets: FacetFieldsQuery<T>): string {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const optionsMap: any = {};
facetQueryToString(facets: FacetFieldsQuery): string {
const optionsMap = {};
if (Array.isArray(facets)) {
for (const f of facets) {
optionsMap[f] = this.defaultFacetingOptions();
Expand All @@ -513,7 +512,7 @@ export const Utility = {
return this.objToJsonString(q);
},

_sortOrderingToString<T>(ordering: SortOrder<T>): string {
_sortOrderingToString(ordering: SortOrder): string {
if (typeof ordering === "undefined") {
return "[]";
}
Expand Down Expand Up @@ -568,9 +567,7 @@ export const Utility = {
}

if (query.sort !== undefined) {
searchRequest.setSort(
Utility.stringToUint8Array(Utility._sortOrderingToString<T>(query.sort))
);
searchRequest.setSort(Utility.stringToUint8Array(Utility._sortOrderingToString(query.sort)));
}

if (query.groupBy !== undefined) {
Expand Down

0 comments on commit 7ad2e0d

Please sign in to comment.