Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct search (where clauses) #1078

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/dull-kids-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@osdk/client": patch
"@osdk/api": patch
---

Add support for search on struct properties.
12 changes: 12 additions & 0 deletions packages/api/src/aggregate/WhereClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import type {
CompileTimeMetadata,
ObjectMetadata,
} from "../ontology/ObjectTypeDefinition.js";
import type { SimpleWirePropertyTypes } from "../ontology/WirePropertyTypes.js";
import type { IsNever } from "../OsdkObjectFrom.js";
import type { ArrayFilter } from "./ArrayFilter.js";
import type { BaseFilter } from "./BaseFilter.js";
import type { BooleanFilter } from "./BooleanFilter.js";
import type { DatetimeFilter } from "./DatetimeFilter.js";
import type { GeoFilter } from "./GeoFilter.js";
import type { Just } from "./Just.js";
import type { NumberFilter } from "./NumberFilter.js";
import type { StringFilter } from "./StringFilter.js";

Expand Down Expand Up @@ -149,6 +151,9 @@ type FilterFor<PD extends ObjectMetadata.Property> = PD["multiplicity"] extends
? ArrayFilter<string>
: (PD["type"] extends boolean ? ArrayFilter<boolean>
: ArrayFilter<number>))
: PD["type"] extends Record<string, SimpleWirePropertyTypes> ?
| StructFilter<PD["type"]>
| BaseFilter<string>
: (PD["type"] extends "string" ? StringFilter
: PD["type"] extends "geopoint" | "geoshape" ? GeoFilter
: PD["type"] extends "datetime" | "timestamp" ? DatetimeFilter
Expand All @@ -158,6 +163,13 @@ type FilterFor<PD extends ObjectMetadata.Property> = PD["multiplicity"] extends
? NumberFilter
: BaseFilter<string>); // FIXME we need to represent all types

type StructFilterOpts<ST extends Record<string, SimpleWirePropertyTypes>> = {
[K in keyof ST]?: FilterFor<{ "type": ST[K] }>;
};
type StructFilter<ST extends Record<string, SimpleWirePropertyTypes>> = {
[K in keyof ST]: Just<K, StructFilterOpts<ST>>;
}[keyof ST];

export interface AndWhereClause<
T extends ObjectOrInterfaceDefinition,
> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/

import type { ObjectOrInterfaceDefinition, WhereClause } from "@osdk/api";
import { objectTypeWithAllPropertyTypes } from "@osdk/client.test.ontology";
import {
BgaoNflPlayer,
objectTypeWithAllPropertyTypes,
} from "@osdk/client.test.ontology";
import type { Point } from "geojson";
import { expectType } from "ts-expect";
import { describe, expect, it } from "vitest";
import { modernToLegacyWhereClause } from "./modernToLegacyWhereClause.js";

type ObjAllProps = objectTypeWithAllPropertyTypes;
type structObj = BgaoNflPlayer;
describe(modernToLegacyWhereClause, () => {
describe("api namespaces", () => {
describe("interfaces", () => {
Expand All @@ -31,7 +35,12 @@ describe(modernToLegacyWhereClause, () => {
apiName: "a.Foo",
__DefinitionMetadata: {
type: "interface",
properties: { "prop": { type: "integer" } },
properties: {
"prop": { type: "integer" },
"prop2": {
type: { "innerProp1": "string", "innerProp2": "float" },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defined a struct prop on interface

},
},
apiName: "a.Foo",
displayName: "",
links: {},
Expand All @@ -40,14 +49,32 @@ describe(modernToLegacyWhereClause, () => {
} as const satisfies ObjectOrInterfaceDefinition;

const r = modernToLegacyWhereClause({
prop: 5,
$and: [
{ prop: { $eq: 5 } },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to a $ filter, because this broke the old code, so want to make sure we can handle that case

{ prop2: { innerProp1: { $eq: "myProp" } } },
],
}, T);

expect(r).toMatchInlineSnapshot(`
{
"field": "a.prop",
"type": "eq",
"value": 5,
"type": "and",
"value": [
{
"field": "a.prop",
"type": "eq",
"value": 5,
},
{
"field": undefined,
"propertyIdentifier": {
"propertyApiName": "a.prop2",
"structFieldApiName": "innerProp1",
"type": "structField",
},
"type": "eq",
"value": "myProp",
},
],
}
`);
});
Expand All @@ -62,6 +89,9 @@ describe(modernToLegacyWhereClause, () => {
"foo": { type: "integer" },
"b.prop": { type: "integer" },
"prop": { type: "integer" },
"c.prop2": {
type: { "innerProp1": "string", "innerProp2": "float" },
},
},
apiName: "Foo",
displayName: "",
Expand All @@ -73,6 +103,7 @@ describe(modernToLegacyWhereClause, () => {
const r = modernToLegacyWhereClause({
"b.prop": 5,
foo: 6,
"c.prop2": { innerProp1: { $eq: "myProp" } },
}, T);

expect(r).toMatchInlineSnapshot(`
Expand All @@ -89,6 +120,16 @@ describe(modernToLegacyWhereClause, () => {
"type": "eq",
"value": 6,
},
{
"field": undefined,
"propertyIdentifier": {
"propertyApiName": "c.prop2",
"structFieldApiName": "innerProp1",
"type": "structField",
},
"type": "eq",
"value": "myProp",
},
],
}
`);
Expand All @@ -104,6 +145,9 @@ describe(modernToLegacyWhereClause, () => {
"a.foo": { type: "integer" },
"b.prop": { type: "integer" },
"prop": { type: "integer" },
"c.prop2": {
type: { "innerProp1": "string", "innerProp2": "float" },
},
},
apiName: "a.Foo",
displayName: "",
Expand All @@ -115,6 +159,7 @@ describe(modernToLegacyWhereClause, () => {
const r = modernToLegacyWhereClause({
"b.prop": 5,
"a.foo": 6,
"c.prop2": { innerProp1: { $eq: "myProp" } },
}, T);

expect(r).toMatchInlineSnapshot(`
Expand All @@ -131,6 +176,16 @@ describe(modernToLegacyWhereClause, () => {
"type": "eq",
"value": 6,
},
{
"field": undefined,
"propertyIdentifier": {
"propertyApiName": "c.prop2",
"structFieldApiName": "innerProp1",
"type": "structField",
},
"type": "eq",
"value": "myProp",
},
],
}
`);
Expand Down Expand Up @@ -705,6 +760,96 @@ describe(modernToLegacyWhereClause, () => {
}
`);
});
it("converts struct where clauses correctly", () => {
expect(modernToLegacyWhereClause<structObj>({
address: { state: { $eq: "NJ" } },
}, BgaoNflPlayer)).toMatchInlineSnapshot(`
{
"field": undefined,
"propertyIdentifier": {
"propertyApiName": "address",
"structFieldApiName": "state",
"type": "structField",
},
"type": "eq",
"value": "NJ",
}
`);

expect(modernToLegacyWhereClause<structObj>({
$and: [
{ address: { state: { $eq: "NJ" } } },
{ address: { city: { $containsAnyTerm: "N" } } },
],
}, BgaoNflPlayer)).toMatchInlineSnapshot(`
{
"type": "and",
"value": [
{
"field": undefined,
"propertyIdentifier": {
"propertyApiName": "address",
"structFieldApiName": "state",
"type": "structField",
},
"type": "eq",
"value": "NJ",
},
{
"field": undefined,
"fuzzy": false,
"propertyIdentifier": {
"propertyApiName": "address",
"structFieldApiName": "city",
"type": "structField",
},
"type": "containsAnyTerm",
"value": "N",
},
],
}
`);

expect(modernToLegacyWhereClause<structObj>({
$or: [
{ address: { state: { $eq: "NJ" } } },
{ address: { city: { $containsAnyTerm: "N" } } },
{ gamesPlayed: { $gt: 5 } },
],
}, BgaoNflPlayer)).toMatchInlineSnapshot(`
{
"type": "or",
"value": [
{
"field": undefined,
"propertyIdentifier": {
"propertyApiName": "address",
"structFieldApiName": "state",
"type": "structField",
},
"type": "eq",
"value": "NJ",
},
{
"field": undefined,
"fuzzy": false,
"propertyIdentifier": {
"propertyApiName": "address",
"structFieldApiName": "city",
"type": "structField",
},
"type": "containsAnyTerm",
"value": "N",
},
{
"field": "gamesPlayed",
"type": "gt",
"value": 5,
},
],
}
`);
});
});

describe("multiple checks", () => {
Expand Down
Loading
Loading