Skip to content

Commit

Permalink
fix: change operator to or
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Oct 23, 2024
1 parent 2e6a4aa commit 0b76d37
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/sdk/src/__example__/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// EXAMPLE FOR NOW

import * as torii from "@dojoengine/torii-client";
import { init } from "..";
import { SchemaType } from "../types";

Expand Down
16 changes: 9 additions & 7 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 } from "../types";
import { QueryType, SchemaType } from "../types";

describe("convertQueryToClause", () => {
it("should convert a single model query with conditions", () => {
Expand All @@ -27,7 +27,7 @@ describe("convertQueryToClause", () => {

expect(result).toEqual({
Composite: {
operator: "And",
operator: "Or",
clauses: [
{
Member: {
Expand Down Expand Up @@ -81,7 +81,7 @@ describe("convertQueryToClause", () => {

expect(result).toEqual({
Composite: {
operator: "And",
operator: "Or",
clauses: [
{
Member: {
Expand Down Expand Up @@ -110,10 +110,10 @@ describe("convertQueryToClause", () => {
player: {
$: {
where: {
AND: [
And: [
{ score: { $gt: 100 } },
{
OR: [
Or: [
{ name: { $eq: "Alice" } },
{ name: { $eq: "Bob" } },
],
Expand All @@ -125,7 +125,7 @@ describe("convertQueryToClause", () => {
item: {
$: {
where: {
AND: [{ durability: { $lt: 50 } }],
And: [{ durability: { $lt: 50 } }],
},
},
},
Expand All @@ -134,10 +134,12 @@ describe("convertQueryToClause", () => {

const result = convertQueryToClause(query, schema);

console.log("result", result);

// Updated expectation to match the actual output
expect(result).toEqual({
Composite: {
operator: "And",
operator: "Or",
clauses: [
{
Member: {
Expand Down
7 changes: 4 additions & 3 deletions packages/sdk/src/convertQuerytoClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function convertQueryToClause<T extends SchemaType>(
if (clauses.length > 1) {
return {
Composite: {
operator: "And",
operator: "Or",
clauses: clauses,
},
};
Expand Down Expand Up @@ -158,8 +158,8 @@ function buildWhereClause(
): torii.Clause | undefined {
// Define logical operator mapping
const logicalOperators: Record<string, torii.LogicalOperator> = {
AND: "And",
OR: "Or",
And: "And",
Or: "Or",
};

// Check for logical operators first
Expand Down Expand Up @@ -299,6 +299,7 @@ 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
2 changes: 1 addition & 1 deletion packages/sdk/src/getEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function getEntities<T extends SchemaType>(
limit: limit,
offset: cursor,
clause,
dont_include_hashed_keys: query.entityIds ? false : true,
dont_include_hashed_keys: false,
};

try {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/getEventMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function getEventMessages<T extends SchemaType>(
limit: limit,
offset: cursor,
clause,
dont_include_hashed_keys: true,
dont_include_hashed_keys: false,
};

try {
Expand Down
18 changes: 12 additions & 6 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@ export type QueryOptions = {
/**
* Logical operators for combining multiple conditions
*/
export type LogicalOperator = "AND" | "OR";

/**
* Recursively defines the conditions for the `where` clause.
*/
export type WhereCondition<TModel> =
| {
[key in LogicalOperator]?: Array<WhereCondition<TModel>>;
[key in torii.LogicalOperator]?: Array<WhereCondition<TModel>>;
}
| {
[P in keyof TModel]?: {
Expand Down Expand Up @@ -156,12 +155,14 @@ export type SubscriptionQueryType<T extends SchemaType> = {
};
};

export type BaseQueryType = {
entityIds?: string[];
};

/**
* QueryType for queries, using QueryWhereOptions
* Query type with model conditions
*/
export type QueryType<T extends SchemaType> = {
entityIds?: string[];
} & {
export type ModelQueryType<T extends SchemaType> = {
[K in keyof T]?: {
[L in keyof T[K]]?:
| AtLeastOne<{
Expand All @@ -171,6 +172,11 @@ export type QueryType<T extends SchemaType> = {
};
};

/**
* Combined QueryType using union of base and model types
*/
export type QueryType<T extends SchemaType> = BaseQueryType | ModelQueryType<T>;

/**
* Result of a query
*/
Expand Down

0 comments on commit 0b76d37

Please sign in to comment.