Skip to content

Commit

Permalink
feat: add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGreed committed Nov 21, 2024
1 parent f9b283a commit 2438663
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 98 deletions.
11 changes: 1 addition & 10 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

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





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


### Features

* docs ([1bf47fa](https://github.com/dojoengine/sdk/commit/1bf47fae3afef5ab7f02f9ed288141e735ab2788))




- docs ([1bf47fa](https://github.com/dojoengine/sdk/commit/1bf47fae3afef5ab7f02f9ed288141e735ab2788))

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

Expand Down
92 changes: 34 additions & 58 deletions packages/sdk/src/__example__/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// EXAMPLE FOR NOW

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

export interface PlayerModel {
Expand Down Expand Up @@ -111,73 +111,49 @@ async function exampleUsage() {
invalidMessage // TypeScript Error
);

db.subscribeEntityQuery(
{
world: {
player: {
$: {
where: {
name: { $is: "Alice" },
score: { $is: 10 },
},
},
},
},
},
(resp) => {
const query = new QueryBuilder<MockSchemaType>().namespace("world", (n) =>
n.entity("player", (e) => e.eq("name", "Alice"))
);

db.subscribeEntityQuery(query.build(), (resp) => {
if (resp.error) {
console.error(
"Error querying todos and goals:",
resp.error.message
);
return;
}
if (resp.data) {
console.log(
"Queried todos and goals:",
resp.data.map((a) => a.models.world)
);
}
});
// Example usage of getEntities with where clause
try {
const eq = new QueryBuilder<MockSchemaType>().namespace("world", (n) =>
n
.entity("item", (e) =>
e.eq("type", "sword").lt("durability", 5)
)
.entity("game", (e) => e.eq("status", "completed"))
);
const entities = await db.getEntities(eq.build(), (resp) => {
if (resp.error) {
console.error(
"Error querying todos and goals:",
"Error querying completed important todos:",
resp.error.message
);
return;
}
if (resp.data) {
console.log(
"Queried todos and goals:",
resp.data.map((a) => a.models.world)
"Completed important todos:",
resp.data.map((a) => a.models)
);
}
}
);
// Example usage of getEntities with where clause
try {
const entities = await db.getEntities(
{
world: {
item: {
$: {
where: {
type: { $eq: "sword" },
durability: { $lt: 5 },
},
},
},
game: {
$: {
where: {
status: { $eq: "completed" },
},
},
},
},
},
(resp) => {
if (resp.error) {
console.error(
"Error querying completed important todos:",
resp.error.message
);
return;
}
if (resp.data) {
console.log(
"Completed important todos:",
resp.data.map((a) => a.models)
);
}
}
);
});
console.log("Queried entities:", entities);
} catch (error) {
console.error("Error querying entities:", error);
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/__tests__/queryBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { QueryBuilder } from "../queryBuilder";
import { MockSchemaType } from "../__example__/index";

describe("QueryBuilder", () => {
it("should be implemented", () => {
Expand All @@ -8,9 +9,9 @@ describe("QueryBuilder", () => {
});

it("should work with example", () => {
const query = new QueryBuilder()
const query = new QueryBuilder<MockSchemaType>()
.namespace("world", (n) =>
n.entity("player", (e) => e.eq("id", "1").eq("name", "Alice"))
n.entity("test", (e) => e.eq("id", "1").eq("name", "Alice"))
)
.namespace("universe", (n) =>
n.entity("galaxy", (e) => e.is("name", "Milky Way"))
Expand Down
63 changes: 35 additions & 28 deletions packages/sdk/src/queryBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { QueryType, SchemaType } from "./types";
import { QueryType, SchemaType, SubscriptionQueryType } from "./types";

type NestedKeyOf<ObjectType extends object> = {
[Key in keyof ObjectType &
(string | number)]: ObjectType[Key] extends object
? `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key]>}`
: `${Key}`;
}[keyof ObjectType & (string | number)];

export class QueryBuilder<T extends SchemaType> {
namespaces: Map<string, Namespace<T>>;

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

public namespace(
name: string,
name: keyof T,
cb: (ns: Namespace<T>) => void
): Namespace<T> {
const ns = new Namespace(this, name);
this.namespaces.set(name, ns);
const ns = new Namespace(this);
this.namespaces.set(name as string, ns);
cb(ns);
return ns;
}

public build(): QueryType<T> {
public build(): SubscriptionQueryType<T> {
const qt: Record<
string,
Record<
Expand All @@ -43,25 +51,22 @@ export class QueryBuilder<T extends SchemaType> {
};
}
}
return qt as QueryType<T>;
return qt as SubscriptionQueryType<T>;
}
}

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

constructor(
private parent: QueryBuilder<T>,
private name: string
) {
constructor(private parent: QueryBuilder<T>) {
this.entities = new Map<string, QueryEntity<T>>();
}

public entity(
name: string,
name: NestedKeyOf<T>,
cb: (entity: QueryEntity<T>) => void
): QueryEntity<T> {
const entity = new QueryEntity(this, name);
const entity = new QueryEntity(this);
this.entities.set(name, entity);
cb(entity);
return entity;
Expand All @@ -71,53 +76,56 @@ class Namespace<T extends SchemaType> {
return this.parent.namespace(ns, cb);
}

public build(): QueryType<T> {
public build(): SubscriptionQueryType<T> {
return this.parent.build();
}
}

class QueryEntity<T extends SchemaType> {
constraints: Map<string, Constraint<T>>;
constraints: Map<string, Constraint>;

constructor(
private parent: Namespace<T>,
private name: string
) {
this.constraints = new Map<string, Constraint<T>>();
constructor(private parent: Namespace<T>) {
this.constraints = new Map<string, Constraint>();
}
public entity(
name: NestedKeyOf<T>,
cb: (entity: QueryEntity<T>) => void
): QueryEntity<T> {
return this.parent.entity(name, cb);
}

public is(field: string, value: any): QueryEntity<T> {
this.constraints.set(field, new Constraint(this, Operator.is, value));
this.constraints.set(field, new Constraint(Operator.is, value));
return this;
}

public eq(field: string, value: any): QueryEntity<T> {
this.constraints.set(field, new Constraint(this, Operator.eq, value));
this.constraints.set(field, new Constraint(Operator.eq, value));
return this;
}

public neq(field: string, value: any): QueryEntity<T> {
this.constraints.set(field, new Constraint(this, Operator.neq, value));
this.constraints.set(field, new Constraint(Operator.neq, value));
return this;
}

public gt(field: string, value: any): QueryEntity<T> {
this.constraints.set(field, new Constraint(this, Operator.gt, value));
this.constraints.set(field, new Constraint(Operator.gt, value));
return this;
}

public gte(field: string, value: any): QueryEntity<T> {
this.constraints.set(field, new Constraint(this, Operator.gte, value));
this.constraints.set(field, new Constraint(Operator.gte, value));
return this;
}

public lt(field: string, value: any): QueryEntity<T> {
this.constraints.set(field, new Constraint(this, Operator.lt, value));
this.constraints.set(field, new Constraint(Operator.lt, value));
return this;
}

public lte(field: string, value: any): QueryEntity<T> {
this.constraints.set(field, new Constraint(this, Operator.lte, value));
this.constraints.set(field, new Constraint(Operator.lte, value));
return this;
}

Expand All @@ -126,9 +134,8 @@ class QueryEntity<T extends SchemaType> {
}
}

class Constraint<T extends SchemaType> {
class Constraint {
constructor(
private parent: QueryEntity<T>,
private _operator: Operator,
private _value: any
) {}
Expand Down

0 comments on commit 2438663

Please sign in to comment.