From 95abb831ae41861713e14489e73af0c4ef940e40 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 15 Dec 2023 18:42:14 +0100 Subject: [PATCH] Fix type returned by DatabaseTable.with method (#30) --- src/database_table.ts | 5 +++-- tests/spacetimedb_client.test.ts | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/database_table.ts b/src/database_table.ts index 29b5f37..9c7cc76 100644 --- a/src/database_table.ts +++ b/src/database_table.ts @@ -19,9 +19,10 @@ export class DatabaseTable { public static tableName: string; public static with( + this: T, client: SpacetimeDBClient - ): ThisType { - return _tableProxy(this, client) as unknown as ThisType; + ): T { + return _tableProxy(this, client) as unknown as T; } public static getDB(): ClientDB { diff --git a/tests/spacetimedb_client.test.ts b/tests/spacetimedb_client.test.ts index ff5b9bb..7060fc2 100644 --- a/tests/spacetimedb_client.test.ts +++ b/tests/spacetimedb_client.test.ts @@ -508,4 +508,23 @@ describe("SpacetimeDBClient", () => { expect(updates[1]["oldUser"].username).toBe("jaime"); expect(updates[1]["newUser"].username).toBe("kingslayer"); }); + + test("Filtering works", async () => { + const client = new SpacetimeDBClient( + "ws://127.0.0.1:1234", + "db", + undefined, + "json" + ); + const db = client.db; + const user1 = new User(new Identity("bobs-idenitty"), "bob"); + const user2 = new User(new Identity("sallys-identity"), "sally"); + const users = db.getTable("User").instances; + users.set("abc123", user1); + users.set("def456", user2); + + const filteredUsers = User.with(client).filterByUsername("sally"); + expect(filteredUsers).toHaveLength(1); + expect(filteredUsers[0].username).toBe("sally"); + }); });