Skip to content

Commit 6a98b79

Browse files
committed
feat: add examples
1 parent f9b283a commit 6a98b79

File tree

4 files changed

+80
-103
lines changed

4 files changed

+80
-103
lines changed

packages/sdk/CHANGELOG.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,11 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
77

88
**Note:** Version bump only for package @dojoengine/sdk
99

10-
11-
12-
13-
1410
# [1.0.0-alpha.30](https://github.com/dojoengine/sdk/compare/v1.0.0-alpha.29...v1.0.0-alpha.30) (2024-11-09)
1511

16-
1712
### Features
1813

19-
* docs ([1bf47fa](https://github.com/dojoengine/sdk/commit/1bf47fae3afef5ab7f02f9ed288141e735ab2788))
20-
21-
22-
23-
14+
- docs ([1bf47fa](https://github.com/dojoengine/sdk/commit/1bf47fae3afef5ab7f02f9ed288141e735ab2788))
2415

2516
# [1.0.0-alpha.29](https://github.com/dojoengine/sdk/compare/v1.0.0-alpha.28...v1.0.0-alpha.29) (2024-11-08)
2617

packages/sdk/src/__example__/index.ts

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// EXAMPLE FOR NOW
22

33
import * as torii from "@dojoengine/torii-client";
4-
import { init } from "..";
4+
import { init, QueryBuilder } from "..";
55
import { SchemaType } from "../types";
66

77
export interface PlayerModel {
@@ -111,73 +111,49 @@ async function exampleUsage() {
111111
invalidMessage // TypeScript Error
112112
);
113113

114-
db.subscribeEntityQuery(
115-
{
116-
world: {
117-
player: {
118-
$: {
119-
where: {
120-
name: { $is: "Alice" },
121-
score: { $is: 10 },
122-
},
123-
},
124-
},
125-
},
126-
},
127-
(resp) => {
114+
const query = new QueryBuilder<MockSchemaType>().namespace("world", (n) =>
115+
n.entity("player", (e) => e.eq("name", "Alice"))
116+
);
117+
118+
db.subscribeEntityQuery(query.build(), (resp) => {
119+
if (resp.error) {
120+
console.error(
121+
"Error querying todos and goals:",
122+
resp.error.message
123+
);
124+
return;
125+
}
126+
if (resp.data) {
127+
console.log(
128+
"Queried todos and goals:",
129+
resp.data.map((a) => a.models.world)
130+
);
131+
}
132+
});
133+
// Example usage of getEntities with where clause
134+
try {
135+
const eq = new QueryBuilder<MockSchemaType>().namespace("world", (n) =>
136+
n
137+
.entity("item", (e) =>
138+
e.eq("type", "sword").lt("durability", 5)
139+
)
140+
.entity("game", (e) => e.eq("status", "completed"))
141+
);
142+
const entities = await db.getEntities(eq.build(), (resp) => {
128143
if (resp.error) {
129144
console.error(
130-
"Error querying todos and goals:",
145+
"Error querying completed important todos:",
131146
resp.error.message
132147
);
133148
return;
134149
}
135150
if (resp.data) {
136151
console.log(
137-
"Queried todos and goals:",
138-
resp.data.map((a) => a.models.world)
152+
"Completed important todos:",
153+
resp.data.map((a) => a.models)
139154
);
140155
}
141-
}
142-
);
143-
// Example usage of getEntities with where clause
144-
try {
145-
const entities = await db.getEntities(
146-
{
147-
world: {
148-
item: {
149-
$: {
150-
where: {
151-
type: { $eq: "sword" },
152-
durability: { $lt: 5 },
153-
},
154-
},
155-
},
156-
game: {
157-
$: {
158-
where: {
159-
status: { $eq: "completed" },
160-
},
161-
},
162-
},
163-
},
164-
},
165-
(resp) => {
166-
if (resp.error) {
167-
console.error(
168-
"Error querying completed important todos:",
169-
resp.error.message
170-
);
171-
return;
172-
}
173-
if (resp.data) {
174-
console.log(
175-
"Completed important todos:",
176-
resp.data.map((a) => a.models)
177-
);
178-
}
179-
}
180-
);
156+
});
181157
console.log("Queried entities:", entities);
182158
} catch (error) {
183159
console.error("Error querying entities:", error);

packages/sdk/src/__tests__/queryBuilder.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "vitest";
22
import { QueryBuilder } from "../queryBuilder";
3+
import { MockSchemaType } from "../__example__/index";
34

45
describe("QueryBuilder", () => {
56
it("should be implemented", () => {
@@ -8,7 +9,7 @@ describe("QueryBuilder", () => {
89
});
910

1011
it("should work with example", () => {
11-
const query = new QueryBuilder()
12+
const query = new QueryBuilder<MockSchemaType>()
1213
.namespace("world", (n) =>
1314
n.entity("player", (e) => e.eq("id", "1").eq("name", "Alice"))
1415
)

packages/sdk/src/queryBuilder.ts

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { QueryType, SchemaType } from "./types";
1+
import { QueryType, SchemaType, SubscriptionQueryType } from "./types";
2+
3+
type NestedKeyOf<ObjectType extends object> = {
4+
[Key in keyof ObjectType &
5+
(string | number)]: ObjectType[Key] extends object
6+
? `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key]>}`
7+
: `${Key}`;
8+
}[keyof ObjectType & (string | number)];
9+
210
export class QueryBuilder<T extends SchemaType> {
311
namespaces: Map<string, Namespace<T>>;
412

@@ -7,16 +15,16 @@ export class QueryBuilder<T extends SchemaType> {
715
}
816

917
public namespace(
10-
name: string,
18+
name: keyof T,
1119
cb: (ns: Namespace<T>) => void
1220
): Namespace<T> {
13-
const ns = new Namespace(this, name);
14-
this.namespaces.set(name, ns);
21+
const ns = new Namespace(this);
22+
this.namespaces.set(name as string, ns);
1523
cb(ns);
1624
return ns;
1725
}
1826

19-
public build(): QueryType<T> {
27+
public build(): SubscriptionQueryType<T> {
2028
const qt: Record<
2129
string,
2230
Record<
@@ -43,25 +51,22 @@ export class QueryBuilder<T extends SchemaType> {
4351
};
4452
}
4553
}
46-
return qt as QueryType<T>;
54+
return qt as SubscriptionQueryType<T>;
4755
}
4856
}
4957

5058
class Namespace<T extends SchemaType> {
5159
entities: Map<string, QueryEntity<T>>;
5260

53-
constructor(
54-
private parent: QueryBuilder<T>,
55-
private name: string
56-
) {
61+
constructor(private parent: QueryBuilder<T>) {
5762
this.entities = new Map<string, QueryEntity<T>>();
5863
}
5964

6065
public entity(
61-
name: string,
66+
name: NestedKeyOf<T>,
6267
cb: (entity: QueryEntity<T>) => void
6368
): QueryEntity<T> {
64-
const entity = new QueryEntity(this, name);
69+
const entity = new QueryEntity(this);
6570
this.entities.set(name, entity);
6671
cb(entity);
6772
return entity;
@@ -71,53 +76,58 @@ class Namespace<T extends SchemaType> {
7176
return this.parent.namespace(ns, cb);
7277
}
7378

74-
public build(): QueryType<T> {
79+
public build(): SubscriptionQueryType<T> {
7580
return this.parent.build();
7681
}
7782
}
7883

7984
class QueryEntity<T extends SchemaType> {
80-
constraints: Map<string, Constraint<T>>;
85+
constraints: Map<string, Constraint>;
8186

82-
constructor(
83-
private parent: Namespace<T>,
84-
private name: string
85-
) {
86-
this.constraints = new Map<string, Constraint<T>>();
87+
constructor(private parent: Namespace<T>) {
88+
this.constraints = new Map<string, Constraint>();
89+
}
90+
public entity(
91+
name: NestedKeyOf<T>,
92+
cb: (entity: QueryEntity<T>) => void
93+
): QueryEntity<T> {
94+
return this.parent.entity(name, cb);
8795
}
8896

8997
public is(field: string, value: any): QueryEntity<T> {
90-
this.constraints.set(field, new Constraint(this, Operator.is, value));
91-
return this;
98+
return this.addConstraint(field, value, Operator.is);
9299
}
93100

94101
public eq(field: string, value: any): QueryEntity<T> {
95-
this.constraints.set(field, new Constraint(this, Operator.eq, value));
96-
return this;
102+
return this.addConstraint(field, value, Operator.eq);
97103
}
98104

99105
public neq(field: string, value: any): QueryEntity<T> {
100-
this.constraints.set(field, new Constraint(this, Operator.neq, value));
101-
return this;
106+
return this.addConstraint(field, value, Operator.neq);
102107
}
103108

104109
public gt(field: string, value: any): QueryEntity<T> {
105-
this.constraints.set(field, new Constraint(this, Operator.gt, value));
106-
return this;
110+
return this.addConstraint(field, value, Operator.gt);
107111
}
108112

109113
public gte(field: string, value: any): QueryEntity<T> {
110-
this.constraints.set(field, new Constraint(this, Operator.gte, value));
111-
return this;
114+
return this.addConstraint(field, value, Operator.gte);
112115
}
113116

114117
public lt(field: string, value: any): QueryEntity<T> {
115-
this.constraints.set(field, new Constraint(this, Operator.lt, value));
116-
return this;
118+
return this.addConstraint(field, value, Operator.lt);
117119
}
118120

119121
public lte(field: string, value: any): QueryEntity<T> {
120-
this.constraints.set(field, new Constraint(this, Operator.lte, value));
122+
return this.addConstraint(field, value, Operator.lte);
123+
}
124+
125+
private addConstraint(
126+
field: string,
127+
value: any,
128+
op: Operator
129+
): QueryEntity<T> {
130+
this.constraints.set(field, new Constraint(op, value));
121131
return this;
122132
}
123133

@@ -126,9 +136,8 @@ class QueryEntity<T extends SchemaType> {
126136
}
127137
}
128138

129-
class Constraint<T extends SchemaType> {
139+
class Constraint {
130140
constructor(
131-
private parent: QueryEntity<T>,
132141
private _operator: Operator,
133142
private _value: any
134143
) {}

0 commit comments

Comments
 (0)