|
| 1 | +# Dojo.js Migration Guide - Query System Changes |
| 2 | + |
| 3 | +## Major Changes Overview |
| 4 | + |
| 5 | +- Default query system now uses `ToriiQueryBuilder` instead of the legacy `QueryBuilder` |
| 6 | +- Schema is no longer required as a parameter when initializing the SDK |
| 7 | +- Subscription APIs now return initial data along with the subscription |
| 8 | +- The `getEntities` and `getEventMessages` APIs have been simplified |
| 9 | +- Cartridge connector has been removed from the example |
| 10 | + |
| 11 | +## Initialization Changes |
| 12 | + |
| 13 | +### Before |
| 14 | + |
| 15 | +```typescript |
| 16 | +const sdk = await init<SchemaType>( |
| 17 | + { |
| 18 | + client: { |
| 19 | + rpcUrl: getRpcUrl(), |
| 20 | + toriiUrl: env.VITE_TORII_URL, |
| 21 | + // ... |
| 22 | + }, |
| 23 | + domain: { |
| 24 | + /* ... */ |
| 25 | + }, |
| 26 | + }, |
| 27 | + schema |
| 28 | +); |
| 29 | +``` |
| 30 | + |
| 31 | +### After |
| 32 | + |
| 33 | +```typescript |
| 34 | +const sdk = await init<SchemaType>({ |
| 35 | + client: { |
| 36 | + rpcUrl: getRpcUrl(), |
| 37 | + toriiUrl: env.VITE_TORII_URL, |
| 38 | + // ... |
| 39 | + }, |
| 40 | + domain: { |
| 41 | + /* ... */ |
| 42 | + }, |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +## Query System Changes |
| 47 | + |
| 48 | +### Entity Queries |
| 49 | + |
| 50 | +#### Before |
| 51 | + |
| 52 | +```typescript |
| 53 | +const query = new QueryBuilder<SchemaType>() |
| 54 | + .namespace("onchain_dash", (n) => |
| 55 | + n.entity("CallerCounter", (e) => |
| 56 | + e.eq("caller", addAddressPadding(address)) |
| 57 | + ) |
| 58 | + ) |
| 59 | + .build(); |
| 60 | +``` |
| 61 | + |
| 62 | +#### After |
| 63 | + |
| 64 | +```typescript |
| 65 | +const query = new ToriiQueryBuilder() |
| 66 | + .withClause( |
| 67 | + KeysClause( |
| 68 | + ["onchain_dash-CallerCounter"], |
| 69 | + [addAddressPadding(address)], |
| 70 | + "FixedLen" |
| 71 | + ).build() |
| 72 | + ) |
| 73 | + .includeHashedKeys(); |
| 74 | +``` |
| 75 | + |
| 76 | +## Subscription API Changes |
| 77 | + |
| 78 | +### Before |
| 79 | + |
| 80 | +```typescript |
| 81 | +const sub = await sdk.subscribeEntityQuery({ |
| 82 | + query: query, |
| 83 | + callback: ({ data, error }) => { |
| 84 | + // Handle updates |
| 85 | + }, |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +### After |
| 90 | + |
| 91 | +```typescript |
| 92 | +const [initialEntities, sub] = await sdk.subscribeEntityQuery({ |
| 93 | + query: query, |
| 94 | + callback: ({ data, error }) => { |
| 95 | + // Handle updates |
| 96 | + }, |
| 97 | +}); |
| 98 | + |
| 99 | +// initialEntities contains the first query result |
| 100 | +// sub is the subscription for future updates |
| 101 | +``` |
| 102 | + |
| 103 | +## Entity Query Examples |
| 104 | + |
| 105 | +### Querying Single Entity |
| 106 | + |
| 107 | +```typescript |
| 108 | +// New way using ToriiQueryBuilder |
| 109 | +const query = new ToriiQueryBuilder() |
| 110 | + // Model has only `key` in his models key |
| 111 | + .withClause(KeysClause(["namespace-Model"], [key], "FixedLen").build()) |
| 112 | + .includeHashedKeys(); |
| 113 | +``` |
| 114 | + |
| 115 | +### Querying Multiple Entities |
| 116 | + |
| 117 | +```typescript |
| 118 | +const query = new ToriiQueryBuilder() |
| 119 | + .withClause( |
| 120 | + KeysClause( |
| 121 | + ["namespace-Model1", "namespace-Model2"], |
| 122 | + [key], |
| 123 | + // Model has at least `key` in his models keys |
| 124 | + "VariableLen" |
| 125 | + ).build() |
| 126 | + ) |
| 127 | + .includeHashedKeys(); |
| 128 | +``` |
| 129 | + |
| 130 | +## Important Notes |
| 131 | + |
| 132 | +1. **Query Builder Migration** |
| 133 | + |
| 134 | + - The legacy `QueryBuilder` is now deprecated |
| 135 | + - Use `ToriiQueryBuilder` with `KeysClause` for better performance |
| 136 | + - Always call `.includeHashedKeys()` when using subscriptions to retrieve Torii internal entity ids |
| 137 | + |
| 138 | +2. **Subscription Handling** |
| 139 | + |
| 140 | + - Subscriptions now return a tuple: `[initialData, subscription]` |
| 141 | + - Handle initial data immediately instead of making a separate query |
| 142 | + - Use the subscription for subsequent updates |
| 143 | + |
| 144 | +3. **Key Types** |
| 145 | + |
| 146 | + - `FixedLen`: Use when querying exact key matches |
| 147 | + - `VariableLen`: Use when querying multiple entities or partial key matches |
| 148 | + |
| 149 | +4. **Breaking Changes** |
| 150 | + - Simplified API surface for entity queries |
| 151 | + - Changed return types for subscription methods |
| 152 | + |
| 153 | +## Performance Considerations |
| 154 | + |
| 155 | +- The new query system is more efficient for large-scale applications |
| 156 | +- Subscription initial data is returned immediately, reducing network requests |
| 157 | +- Key-based queries provide better performance than complex filters |
| 158 | + |
| 159 | +## Common Migration Patterns |
| 160 | + |
| 161 | +### Converting Complex Queries |
| 162 | + |
| 163 | +```typescript |
| 164 | +// Old way |
| 165 | +const query = new QueryBuilder() |
| 166 | + .namespace("game", (n) => |
| 167 | + n.entity("Player", (e) => e.eq("status", "active")) |
| 168 | + ) |
| 169 | + .build(); |
| 170 | + |
| 171 | +// New way |
| 172 | +const query = new ToriiQueryBuilder() |
| 173 | + .withClause(KeysClause(["game-Player"], [status], "FixedLen").build()) |
| 174 | + .includeHashedKeys(); |
| 175 | +``` |
| 176 | + |
| 177 | +### Handling Multiple Entities |
| 178 | + |
| 179 | +```typescript |
| 180 | +// New way to handle multiple entity types |
| 181 | +const query = new ToriiQueryBuilder() |
| 182 | + .withClause( |
| 183 | + KeysClause( |
| 184 | + [ModelsMapping.Entity1, ModelsMapping.Entity2], |
| 185 | + [commonKey], |
| 186 | + "VariableLen" |
| 187 | + ).build() |
| 188 | + ) |
| 189 | + .includeHashedKeys(); |
| 190 | +``` |
| 191 | + |
| 192 | +## Additional Resources |
| 193 | + |
| 194 | +- Check the official Dojo documentation for more details |
| 195 | +- Review the example applications in the repository |
| 196 | +- Join the Dojo Discord for migration support |
0 commit comments