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

feat: create migration guide to update to v1.1 #394

Merged
merged 1 commit into from
Feb 10, 2025
Merged
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
196 changes: 196 additions & 0 deletions migration/v1-0-v1-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# Dojo.js Migration Guide - Query System Changes

## Major Changes Overview

- Default query system now uses `ToriiQueryBuilder` instead of the legacy `QueryBuilder`
- Schema is no longer required as a parameter when initializing the SDK
- Subscription APIs now return initial data along with the subscription
- The `getEntities` and `getEventMessages` APIs have been simplified
- Cartridge connector has been removed from the example

## Initialization Changes

### Before

```typescript
const sdk = await init<SchemaType>(
{
client: {
rpcUrl: getRpcUrl(),
toriiUrl: env.VITE_TORII_URL,
// ...
},
domain: {
/* ... */
},
},
schema
);
```

### After

```typescript
const sdk = await init<SchemaType>({
client: {
rpcUrl: getRpcUrl(),
toriiUrl: env.VITE_TORII_URL,
// ...
},
domain: {
/* ... */
},
});
```

## Query System Changes

### Entity Queries

#### Before

```typescript
const query = new QueryBuilder<SchemaType>()
.namespace("onchain_dash", (n) =>
n.entity("CallerCounter", (e) =>
e.eq("caller", addAddressPadding(address))
)
)
.build();
```

#### After

```typescript
const query = new ToriiQueryBuilder()
.withClause(
KeysClause(
["onchain_dash-CallerCounter"],
[addAddressPadding(address)],
"FixedLen"
).build()
)
.includeHashedKeys();
```

## Subscription API Changes

### Before

```typescript
const sub = await sdk.subscribeEntityQuery({
query: query,
callback: ({ data, error }) => {
// Handle updates
},
});
```

### After

```typescript
const [initialEntities, sub] = await sdk.subscribeEntityQuery({
query: query,
callback: ({ data, error }) => {
// Handle updates
},
});

// initialEntities contains the first query result
// sub is the subscription for future updates
```

## Entity Query Examples

### Querying Single Entity

```typescript
// New way using ToriiQueryBuilder
const query = new ToriiQueryBuilder()
// Model has only `key` in his models key
.withClause(KeysClause(["namespace-Model"], [key], "FixedLen").build())
.includeHashedKeys();
```

### Querying Multiple Entities

```typescript
const query = new ToriiQueryBuilder()
.withClause(
KeysClause(
["namespace-Model1", "namespace-Model2"],
[key],
// Model has at least `key` in his models keys
"VariableLen"
).build()
)
.includeHashedKeys();
```

## Important Notes

1. **Query Builder Migration**

- The legacy `QueryBuilder` is now deprecated
- Use `ToriiQueryBuilder` with `KeysClause` for better performance
- Always call `.includeHashedKeys()` when using subscriptions to retrieve Torii internal entity ids

2. **Subscription Handling**

- Subscriptions now return a tuple: `[initialData, subscription]`
- Handle initial data immediately instead of making a separate query
- Use the subscription for subsequent updates

3. **Key Types**

- `FixedLen`: Use when querying exact key matches
- `VariableLen`: Use when querying multiple entities or partial key matches

4. **Breaking Changes**
- Simplified API surface for entity queries
- Changed return types for subscription methods

## Performance Considerations

- The new query system is more efficient for large-scale applications
- Subscription initial data is returned immediately, reducing network requests
- Key-based queries provide better performance than complex filters

## Common Migration Patterns

### Converting Complex Queries

```typescript
// Old way
const query = new QueryBuilder()
.namespace("game", (n) =>
n.entity("Player", (e) => e.eq("status", "active"))
)
.build();

// New way
const query = new ToriiQueryBuilder()
.withClause(KeysClause(["game-Player"], [status], "FixedLen").build())
.includeHashedKeys();
```

### Handling Multiple Entities

```typescript
// New way to handle multiple entity types
const query = new ToriiQueryBuilder()
.withClause(
KeysClause(
[ModelsMapping.Entity1, ModelsMapping.Entity2],
[commonKey],
"VariableLen"
).build()
)
.includeHashedKeys();
```

## Additional Resources

- Check the official Dojo documentation for more details
- Review the example applications in the repository
- Join the Dojo Discord for migration support