1
1
import * as torii from "@dojoengine/torii-client" ;
2
- import { SchemaType , QueryType } from "./types" ;
2
+ import { SchemaType , QueryType , SubscriptionQueryType } from "./types" ;
3
+ import { convertQueryToKeysClause } from "./convertQueryToEntityKeyClauses" ;
3
4
4
5
/**
5
6
* Converts a query object into a Torii clause.
@@ -26,7 +27,7 @@ import { SchemaType, QueryType } from "./types";
26
27
*/
27
28
export function convertQueryToClause < T extends SchemaType > (
28
29
query : QueryType < T > ,
29
- operator : torii . LogicalOperator = "And"
30
+ schema : T
30
31
) : torii . Clause {
31
32
const clauses : torii . Clause [ ] = [ ] ;
32
33
@@ -50,50 +51,137 @@ export function convertQueryToClause<T extends SchemaType>(
50
51
) {
51
52
const whereClause = conditions . where ;
52
53
if ( whereClause && typeof whereClause === "object" ) {
53
- for ( const [ member , memberValue ] of Object . entries (
54
- whereClause
55
- ) ) {
56
- if (
57
- typeof memberValue === "object" &&
58
- memberValue !== null
59
- ) {
60
- for ( const [ op , val ] of Object . entries (
61
- memberValue
62
- ) ) {
54
+ // Separate $is conditions from other conditions
55
+ const hasIsClause = Object . values ( whereClause ) . some (
56
+ ( condition : any ) => "$is" in condition
57
+ ) ;
58
+
59
+ if ( hasIsClause ) {
60
+ // Extract $is conditions for Keys clause
61
+ const isConditions : any = { } ;
62
+ const otherConditions : any = { } ;
63
+ for ( const [
64
+ member ,
65
+ memberValue ,
66
+ ] of Object . entries ( whereClause ) ) {
67
+ if (
68
+ typeof memberValue === "object" &&
69
+ memberValue !== null &&
70
+ "$is" in memberValue
71
+ ) {
72
+ isConditions [ member ] = memberValue ;
73
+ } else {
74
+ otherConditions [ member ] = memberValue ;
75
+ }
76
+ }
77
+
78
+ // Build Keys clause using existing function
79
+ const keyClauses = convertQueryToKeysClause (
80
+ {
81
+ [ namespace ] : {
82
+ [ model ] : {
83
+ $ : {
84
+ where : isConditions ,
85
+ } ,
86
+ } ,
87
+ } ,
88
+ } as Omit <
89
+ SubscriptionQueryType < T > ,
90
+ "entityIds"
91
+ > ,
92
+ schema
93
+ ) ;
94
+ clauses . push ( ...( keyClauses as any ) ) ;
95
+
96
+ // Process other conditions as Member clauses
97
+ for ( const [
98
+ member ,
99
+ memberValue ,
100
+ ] of Object . entries ( otherConditions ) ) {
101
+ if (
102
+ typeof memberValue === "object" &&
103
+ memberValue !== null
104
+ ) {
105
+ for ( const [ op , val ] of Object . entries (
106
+ memberValue
107
+ ) ) {
108
+ clauses . push ( {
109
+ Member : {
110
+ model : namespaceModel ,
111
+ member,
112
+ operator :
113
+ convertOperator ( op ) ,
114
+ value : convertToPrimitive (
115
+ val
116
+ ) ,
117
+ } ,
118
+ } ) ;
119
+ }
120
+ } else {
121
+ // Assume equality condition
63
122
clauses . push ( {
64
123
Member : {
65
124
model : namespaceModel ,
66
125
member,
67
- operator : convertOperator ( op ) ,
68
- value : convertToPrimitive ( val ) ,
126
+ operator : "Eq" ,
127
+ value : convertToPrimitive (
128
+ memberValue
129
+ ) ,
130
+ } ,
131
+ } ) ;
132
+ }
133
+ }
134
+ } else {
135
+ // No $is conditions, process all as Member clauses
136
+ for ( const [
137
+ member ,
138
+ memberValue ,
139
+ ] of Object . entries ( whereClause ) ) {
140
+ if (
141
+ typeof memberValue === "object" &&
142
+ memberValue !== null
143
+ ) {
144
+ for ( const [ op , val ] of Object . entries (
145
+ memberValue
146
+ ) ) {
147
+ clauses . push ( {
148
+ Member : {
149
+ model : namespaceModel ,
150
+ member,
151
+ operator :
152
+ convertOperator ( op ) ,
153
+ value : convertToPrimitive (
154
+ val
155
+ ) ,
156
+ } ,
157
+ } ) ;
158
+ }
159
+ } else {
160
+ // Assume equality condition
161
+ clauses . push ( {
162
+ Member : {
163
+ model : namespaceModel ,
164
+ member,
165
+ operator : "Eq" ,
166
+ value : convertToPrimitive (
167
+ memberValue
168
+ ) ,
69
169
} ,
70
170
} ) ;
71
171
}
72
- } else {
73
- clauses . push ( {
74
- Member : {
75
- model : namespaceModel ,
76
- member,
77
- operator : "Eq" , // Default to Eq
78
- value : convertToPrimitive (
79
- memberValue
80
- ) ,
81
- } ,
82
- } ) ;
83
172
}
84
173
}
85
174
}
86
175
}
87
176
} else {
88
177
// Handle the case where there are no conditions
89
-
90
- return {
178
+ clauses . push ( {
91
179
Keys : {
92
180
keys : [ undefined ] ,
93
181
pattern_matching : "FixedLen" ,
94
182
models : [ namespaceModel ] ,
95
183
} ,
96
- } ;
184
+ } ) ;
97
185
}
98
186
}
99
187
}
@@ -103,7 +191,7 @@ export function convertQueryToClause<T extends SchemaType>(
103
191
if ( clauses . length > 0 ) {
104
192
return {
105
193
Composite : {
106
- operator : operator ,
194
+ operator : "And" ,
107
195
clauses : clauses ,
108
196
} ,
109
197
} ;
@@ -112,7 +200,7 @@ export function convertQueryToClause<T extends SchemaType>(
112
200
// If there are no clauses, return an empty Composite
113
201
return {
114
202
Composite : {
115
- operator : operator ,
203
+ operator : "And" ,
116
204
clauses : [ ] ,
117
205
} ,
118
206
} ;
0 commit comments