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

[ FIX ] Support top-level And Condition in where query #335

Closed
Closed
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
100 changes: 89 additions & 11 deletions packages/sdk/src/__tests__/convertQueryToClause.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, expect, it } from "vitest";

import { MockSchemaType, schema } from "../__example__/index";
import { convertQueryToClause } from "../convertQuerytoClause";
import { QueryType, SchemaType } from "../types";
import { QueryType } from "../types";

describe("convertQueryToClause", () => {
it("should convert a single model query with conditions", () => {
Expand Down Expand Up @@ -134,12 +134,90 @@ describe("convertQueryToClause", () => {

const result = convertQueryToClause(query, schema);

console.log("result", result);

// Updated expectation to match the actual output
expect(result).toEqual({
Composite: {
operator: "Or",
clauses: [
{
Composite: {
operator: "And",
clauses: [
{
Member: {
model: "world-player",
member: "score",
operator: "Gt",
value: { Primitive: { U32: 100 } },
},
},
{
Composite: {
operator: "Or",
clauses: [
{
Member: {
model: "world-player",
member: "name",
operator: "Eq",
value: { String: "Alice" },
},
},
{
Member: {
model: "world-player",
member: "name",
operator: "Eq",
value: { String: "Bob" },
},
},
],
},
},
],
},
},
{
Member: {
model: "world-item",
member: "durability",
operator: "Lt",
value: { Primitive: { U32: 50 } },
},
},
],
},
});
});

it("should handle top level AND conditions", () => {
const query: QueryType<MockSchemaType> = {
world: {
player: {
$: {
where: {
And: [
{ score: { $gt: 100 } },
{ name: { $eq: "Charlie" } },
{
Or: [
{ name: { $eq: "Alice" } },
{ name: { $eq: "Bob" } },
],
},
],
},
},
},
},
};

const result = convertQueryToClause(query, schema);

// Updated expectation to match the actual output
expect(result).toEqual({
Composite: {
operator: "And",
clauses: [
{
Member: {
Expand All @@ -149,6 +227,14 @@ describe("convertQueryToClause", () => {
value: { Primitive: { U32: 100 } },
},
},
{
Member: {
model: "world-player",
member: "name",
operator: "Eq",
value: { String: "Charlie" },
},
},
{
Composite: {
operator: "Or",
Expand All @@ -172,14 +258,6 @@ describe("convertQueryToClause", () => {
],
},
},
{
Member: {
model: "world-item",
member: "durability",
operator: "Lt",
value: { Primitive: { U32: 50 } },
},
},
],
},
});
Expand Down
124 changes: 41 additions & 83 deletions packages/sdk/src/convertQuerytoClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,34 @@ export function convertQueryToClause<T extends SchemaType>(
schema: T
): torii.Clause | undefined {
const clauses: torii.Clause[] = [];
let hasOnlyAndCondition = true;

for (const [namespace, models] of Object.entries(query)) {
if (namespace === "entityIds") continue; // Skip entityIds
if (namespace === "entityIds") continue; // Skip entityIds;

if (models && typeof models === "object") {
const modelClauses = processModels(namespace, models, schema);
if (modelClauses.length > 0) {
if (
!modelClauses.every(
(clause) =>
clause &&
"Composite" in clause &&
clause.Composite.operator === "And"
)
) {
hasOnlyAndCondition = false;
}
clauses.push(...modelClauses);
}
}
}

// If there are clauses, combine them under a single Composite clause
// If there are multiple clauses, wrap them in a Composite clause
if (clauses.length > 1) {
return {
Composite: {
operator: "Or",
operator: hasOnlyAndCondition ? "And" : "Or",
clauses: clauses,
},
};
Expand Down Expand Up @@ -73,7 +84,6 @@ function processModels<T extends SchemaType>(
) {
const whereClause = conditions.where;
if (whereClause && typeof whereClause === "object") {
// Iterate over each member in the whereClause to handle $is
for (const [member, memberConditions] of Object.entries(
whereClause
)) {
Expand Down Expand Up @@ -103,7 +113,7 @@ function processModels<T extends SchemaType>(
);
clauses.push(...(isClauses as any));

// Remove $is from memberConditions to prevent further processing
// Remove $is from the where clause
const { $is, ...remainingConditions } =
memberConditions;
(whereClause as Record<string, unknown>)[member] =
Expand All @@ -117,14 +127,9 @@ function processModels<T extends SchemaType>(
whereClause
);
if (clause) {
if (
"Composite" in clause &&
clause.Composite.operator === "And"
) {
// If the composite operator is "And", flatten the clauses
clauses.push(...clause.Composite.clauses);
if (Array.isArray(clause)) {
clauses.push(...clause);
} else {
// Otherwise, keep the composite as is to preserve logical structure
clauses.push(clause);
}
}
Expand All @@ -150,13 +155,12 @@ function processModels<T extends SchemaType>(
*
* @param {string} namespaceModel - The namespaced model identifier.
* @param {Record<string, any>} where - The where clause conditions.
* @returns {torii.Clause | undefined} - The constructed Torii clause or undefined.
* @returns {torii.Clause | torii.Clause[] | undefined} - The constructed Torii clause or undefined.
*/
function buildWhereClause(
namespaceModel: string,
where: Record<string, any>
): torii.Clause | undefined {
// Define logical operator mapping
): torii.Clause | torii.Clause[] | undefined {
const logicalOperators: Record<string, torii.LogicalOperator> = {
And: "And",
Or: "Or",
Expand All @@ -167,21 +171,24 @@ function buildWhereClause(
const logicalKey = keys.find((key) => key in logicalOperators);

if (logicalKey) {
// Handle explicit And/Or conditions
const operator = logicalOperators[logicalKey];
const conditions = where[logicalKey] as Array<Record<string, any>>;

const subClauses: torii.Clause[] = [];

for (const condition of conditions) {
const clause = buildWhereClause(namespaceModel, condition);
if (clause) {
subClauses.push(clause);
if (Array.isArray(clause)) {
subClauses.push(...clause);
} else {
subClauses.push(clause);
}
}
}

if (subClauses.length === 1) {
return subClauses[0];
}
if (subClauses.length === 0) return undefined;
if (subClauses.length === 1) return subClauses[0];

return {
Composite: {
Expand All @@ -196,73 +203,25 @@ function buildWhereClause(

for (const [member, memberValue] of Object.entries(where)) {
if (typeof memberValue === "object" && memberValue !== null) {
const memberKeys = Object.keys(memberValue);
// Check if memberValue contains logical operators
const memberLogicalKey = memberKeys.find(
(key) => key in logicalOperators
);
if (memberLogicalKey) {
const operator = logicalOperators[memberLogicalKey];
const conditions = memberValue[memberLogicalKey] as Array<
Record<string, any>
>;

const nestedClauses: torii.Clause[] = [];
for (const condition of conditions) {
const clause = buildWhereClause(namespaceModel, condition);
if (clause) {
nestedClauses.push(clause);
}
}

if (nestedClauses.length === 1) {
memberClauses.push(nestedClauses[0]);
} else {
memberClauses.push({
Composite: {
operator: operator,
clauses: nestedClauses,
},
});
}
} else {
// Process operators like $eq, $gt, etc
for (const [op, val] of Object.entries(memberValue)) {
memberClauses.push({
Member: {
model: namespaceModel,
member,
operator: convertOperator(op),
value: convertToPrimitive(val),
},
});
}
for (const [op, val] of Object.entries(memberValue)) {
if (!op.startsWith("$")) continue;
memberClauses.push({
Member: {
model: namespaceModel,
member,
operator: convertOperator(op),
value: convertToPrimitive(val),
},
});
}
} else {
// Assume equality condition
memberClauses.push({
Member: {
model: namespaceModel,
member,
operator: "Eq",
value: convertToPrimitive(memberValue),
},
});
}
}

if (memberClauses.length === 1) {
return memberClauses[0];
} else if (memberClauses.length > 1) {
return {
Composite: {
operator: "And",
clauses: memberClauses,
},
};
}
if (memberClauses.length === 0) return undefined;
if (memberClauses.length === 1) return memberClauses[0];

return undefined;
// Return array of Member clauses directly
return memberClauses;
}

/**
Expand Down Expand Up @@ -299,7 +258,6 @@ function convertToPrimitive(value: any): torii.MemberValue {
* @throws {Error} - If the operator is unsupported.
*/
function convertOperator(operator: string): torii.ComparisonOperator {
console.log(operator);
switch (operator) {
case "$eq":
return "Eq";
Expand Down