⨯ FetcherError: dbname: invalid branch name when using client@next ts sdk

Hi, I'm trying to use Xata with postgres enabled via the @next ts sdk. Firstly, the client generation seems broken, as mentioned in my previous post. But secondly, when I try and make a request I'm getting: ⨯ FetcherError: mydbname: invalid branch name However when I inspect the client config, the config via await xata.getConfig();, correct branch name seems to be present... Any help with this would be much appreciated.
4 Replies
emily
emily3mo ago
Hey Adam, I'm looking into this now. Can you paste the code for the query you're running and also how you're initialising the client? I haven't been able to reproduce locally with xata.io/client@^0.0.0-next.veed33633c02b9411aecf8f69d8c94071c9257c12 and xata.io/cli/0.0.0-next.veed33633c02b9411aecf8f69d8c94071c9257c12 on a Postgres enabled branch. Also, we just deployed a fix for some issues around our feature flags and Postgres branches, I think this might have resolved your issue (🤞) so if you could give it another try that would be awesome
Adam Harris
Adam HarrisOP3mo ago
Hi @emily thanks for the response. I'm instantiating the client via:
import { XataClient } from "@/xata";

let instance: XataClient | undefined = undefined;

// Seems to fix a bug in the xata client where apiKey and branch are not set
export const getXataClient = () => {
if (instance) return instance;
const apiKey = process.env.XATA_API_KEY;
if (!apiKey) {
throw new Error("XATA_API_KEY not set");
}
const branch = process.env.XATA_BRANCH;
if (!branch) {

throw new Error("XATA_BRANCH not set");
}
const databaseURL = process.env.XATA_DATABASE_URL;
if (!databaseURL) {

throw new Error("XATA_DATABASE_URL not set");
}
instance = new XataClient({
apiKey,
branch,
databaseURL,
});
return instance;
};
import { XataClient } from "@/xata";

let instance: XataClient | undefined = undefined;

// Seems to fix a bug in the xata client where apiKey and branch are not set
export const getXataClient = () => {
if (instance) return instance;
const apiKey = process.env.XATA_API_KEY;
if (!apiKey) {
throw new Error("XATA_API_KEY not set");
}
const branch = process.env.XATA_BRANCH;
if (!branch) {

throw new Error("XATA_BRANCH not set");
}
const databaseURL = process.env.XATA_DATABASE_URL;
if (!databaseURL) {

throw new Error("XATA_DATABASE_URL not set");
}
instance = new XataClient({
apiKey,
branch,
databaseURL,
});
return instance;
};
and accessing the data via a data access layer in a nextjs app. Here is the fn in the data access layer:
import { getXataClient } from "@/data/helpers";
import { TenantsRecord } from "@/xata";
import { SelectedPick } from "@xata.io/client";

const xata = getXataClient();

export async function getTenantBySlug(
slug: string
): Promise<SelectedPick<TenantsRecord, ["*"]> | null> {
const result = await xata.db.tenants.filter({ slug }).getFirst();
return result;
}
import { getXataClient } from "@/data/helpers";
import { TenantsRecord } from "@/xata";
import { SelectedPick } from "@xata.io/client";

const xata = getXataClient();

export async function getTenantBySlug(
slug: string
): Promise<SelectedPick<TenantsRecord, ["*"]> | null> {
const result = await xata.db.tenants.filter({ slug }).getFirst();
return result;
}
@emily how do I check if postgres is enabled on a branch? I definitely enabled it when I initially created the db, but since then I created a new dev branch to rule out a bug due to a branch name containing hyphens. Now I can't see any flags in the UI indicating whether postgres is enabled...
tsg
tsg3mo ago
If it shows the elephant icon it is Also if you tell me the name I can check yeah, all the branches in your workspace are Postgres enabled, so that's not the issue ah, I think I know. How does the XATA_DATABASE_URL look for you? It should not contain the branch name. For example, this works for me:
XATA_DATABASE_URL='https://demo-uni3q8.us-east-1.xata.sh/db/xata-nextjs'
XATA_DATABASE_URL='https://demo-uni3q8.us-east-1.xata.sh/db/xata-nextjs'
if I add :main to it, I get a similar error as you do
Adam Harris
Adam HarrisOP3mo ago
@tsg yes, that was it - thank you!
Want results from more Discord servers?
Add your server