X
Xata2mo ago
_4kb

Unable to get query using dynamic table

I want to retrieve data with the filter userId is this.userId. but when I do it with dynamic table name I get an error:
This expression is not callable.
Each member of the union type '{ <F extends FilterColumns<AgentRecord> | JSONFilterColumns<AgentRecord>>(column: F, value: FilterValueAtColumn<AgentRecord, F>): Query<...>; (filter?: BaseApiFilter<...> | ... 1 more ... | undefined): Query<...>; } | { ...; } | { ...; } | { ...; }' has signatures, but none of those signatures are compatible with each other.ts(2349)
This expression is not callable.
Each member of the union type '{ <F extends FilterColumns<AgentRecord> | JSONFilterColumns<AgentRecord>>(column: F, value: FilterValueAtColumn<AgentRecord, F>): Query<...>; (filter?: BaseApiFilter<...> | ... 1 more ... | undefined): Query<...>; } | { ...; } | { ...; } | { ...; }' has signatures, but none of those signatures are compatible with each other.ts(2349)
this is the example code in my case:
import { DatabaseSchema, getXataClient, XataClient } from "../../xata";

class AiDB {
private xata: XataClient;
private userId: string;
private agentName: string;
private table: keyof DatabaseSchema;

constructor(userId: string, agentName: string) {
this.xata = getXataClient();
this.userId = userId;
this.agentName = agentName;
this.table = `${agentName}_AIDB` as keyof DatabaseSchema;
}

public async get() {
return await this.xata.db[this.table].filter({ userId: { $is: this.userId } }).getFirst();
}
}
import { DatabaseSchema, getXataClient, XataClient } from "../../xata";

class AiDB {
private xata: XataClient;
private userId: string;
private agentName: string;
private table: keyof DatabaseSchema;

constructor(userId: string, agentName: string) {
this.xata = getXataClient();
this.userId = userId;
this.agentName = agentName;
this.table = `${agentName}_AIDB` as keyof DatabaseSchema;
}

public async get() {
return await this.xata.db[this.table].filter({ userId: { $is: this.userId } }).getFirst();
}
}
7 Replies
_4kb
_4kbOP2mo ago
any suggestion or advice guys?
tsg
tsg2mo ago
can you try it by running in the Queries (Typescript) with a snippet like this:
// Generated with CLI
import { getXataClient } from "./xata";
const xata = getXataClient();

const table = "test";
const userId = "hi";
const result = await xata.db[table].filter({ userId: { $is: userId } }).getFirst();
console.log(result);
// Generated with CLI
import { getXataClient } from "./xata";
const xata = getXataClient();

const table = "test";
const userId = "hi";
const result = await xata.db[table].filter({ userId: { $is: userId } }).getFirst();
console.log(result);
tsg
tsg2mo ago
the above seems fine for me:
No description
tsg
tsg2mo ago
I'm thinking that the issue for you might be that userId is of type string @SferaDev is the expert here
SferaDev
SferaDev2mo ago
When using a dynamic table, the schemas become a union of all tables and type safety might be lost, you can attempt to cast the table name property to one that you know the schema is correct to get proper types.
_4kb
_4kbOP2mo ago
I tried that way and it works, the problem is when the dynamic table variable is a prop but i have unpredictable and scalable client, which is 1 client 1 table
SferaDev
SferaDev2mo ago
Instead of getXataClient() you can create the BaseClient and pass the tables as parameter in the client constructor, but you'll loose all type safety.
Want results from more Discord servers?
Add your server