X
Xata10mo ago
Kaligraphy

Field Names that are all caps cause Typescript Error.

While working with my database I noticed that , having a record with all caps caused Typescript error
No overload matches this call.
The last overload gave the following error.
Argument of type 'string' is not assignable to parameter of type '(Partial<EditableData<UsersRecord>> & Identifiable)[]
No overload matches this call.
The last overload gave the following error.
Argument of type 'string' is not assignable to parameter of type '(Partial<EditableData<UsersRecord>> & Identifiable)[]
Removing the OTP field works removed the Typescript error. The error does not appear in Xata playground so I was oblivious to it. Is this a Typescript thing ? Can I safely suppress the warning since it works, at least in the Playground? Below are images and videos if it helps
No description
14 Replies
Kaligraphy
KaligraphyOP10mo ago
I can provide the schema if needed
kostas
kostas10mo ago
Hi, was the OTP field newly introduced maybe? Could it be that it is not included in the generated file in use (src/xata.ts)? In case the client file was generated before the OTP field was added to the schema, the command "xata codegen" or "xata pull main" (where main, the branch in use) needs to be issued on the terminal in order to pull the latest changes from Xata upstream and generate an up to date client file. Note the Xata Playground uses the current db schema at the moment of page load, so it's up to date (unless the schema is edited on another tab after loading the Playground!), which likely hints why you didn't have this error there
Kaligraphy
KaligraphyOP10mo ago
Hi, sorry for taking too long. The OTP field was introduced less than 20 minutes before I started having this issue. I ran xata pull main before trying to test it locally. trying to pull confirms that there are no new migrations to pull Should o provide my Xata.ts file?
kostas
kostas10mo ago
The client file would probably tell half the story, we should check what's up with the migration history of this field instead and make sure it's printed out in the client file. If you'd like you can toggle "Allow support to view your workspace" in your workspace settings, let me know of the db:branch so I can investigate this On your end, you can also try "xata codegen" too. Check if the OTP field exists in the client.ts. Note the default location is src/xata.ts, if you have customized the location of the file where your project uses it from, you'll need to add the -o /path/to/xata.ts parameter
Kaligraphy
KaligraphyOP10mo ago
Hi, I can confirm that OTP field exists in xata.ts. And one migration file. I went ahead to enable Support for Xata. Any other information I can provide?
Kaligraphy
KaligraphyOP10mo ago
Xata.ts is truncated
No description
No description
No description
No description
kostas
kostas10mo ago
Thank you for granting us access! I've double checked the database and the client file, all seems to be in place. I verified in my local env that the type for the OTP column does not error, see:
No description
kostas
kostas10mo ago
Can you show the part of the code where you import the xata client? Do you import from ./src/xata.ts relative to where your current file is, or from some other location?
Kaligraphy
KaligraphyOP10mo ago
hi, my apologies for the late replies. xata.ts is located at src/lib/xata.ts The file I am trying to call it from is at src/routes/api/testing/+server.ts So there might also be a User error from me. Copilot completed the initilization of xata leading me to start create it like this:
const xata = new XataClient({
apiKey: API_KEY,
branch: BRANCH
})
const xata = new XataClient({
apiKey: API_KEY,
branch: BRANCH
})
And I didn't not second guess it, because I could still use it to get data from the DB. while I was cross checking your example line by line with mine, i noticed my unusual initialization, I checked everywhere else in my codebase just to see I only made this mistake here. Hower correcting to const xata = getXataClient() gives a different error
Kaligraphy
KaligraphyOP10mo ago
No overload matches this call.
The last overload gave the following error.
Argument of type 'string' is not assignable to parameter of type '(Partial<EditableData<UsersRecord>> & Identifiable)[]'.
No overload matches this call.
The last overload gave the following error.
Argument of type 'string' is not assignable to parameter of type '(Partial<EditableData<UsersRecord>> & Identifiable)[]'.
No description
Kaligraphy
KaligraphyOP10mo ago
import { json } from '@sveltejs/kit';
import { XataClient } from '$lib/xata';
import { getXataClient } from '$lib/xata';


const API_KEY = import.meta.env.VITE_XATA_API_KEY
const BRANCH = import.meta.env.VITE_XATA_BRANCH

/** My old Initialization */
// const xata = new XataClient({
// apiKey: API_KEY,
// branch: BRANCH
// })
const xata = getXataClient()


export async function GET() {
// Fetch user
const user = await xata.db.Users.filter({email: "[email protected]"}).getFirst();
console.log(user);
if (user) {
const record = await xata.db.Users.update(user?.id, {email: "[email protected]", isSubscribed: true, OTP: "000000"});
}

return json({"hello": "world"});
}
import { json } from '@sveltejs/kit';
import { XataClient } from '$lib/xata';
import { getXataClient } from '$lib/xata';


const API_KEY = import.meta.env.VITE_XATA_API_KEY
const BRANCH = import.meta.env.VITE_XATA_BRANCH

/** My old Initialization */
// const xata = new XataClient({
// apiKey: API_KEY,
// branch: BRANCH
// })
const xata = getXataClient()


export async function GET() {
// Fetch user
const user = await xata.db.Users.filter({email: "[email protected]"}).getFirst();
console.log(user);
if (user) {
const record = await xata.db.Users.update(user?.id, {email: "[email protected]", isSubscribed: true, OTP: "000000"});
}

return json({"hello": "world"});
}
This is src/routes/api/testing/+server.ts (truncated foe easier replication)
// Generated by Xata Codegen 0.29.0. Please do not edit.
import { buildClient } from "@xata.io/client";
import type {
BaseClientOptions,
SchemaInference,
XataRecord,
} from "@xata.io/client";

const tables = [
{
name: "Users",
columns: [
{ name: "first_name", type: "string" },
{ name: "last_name", type: "string" },
{ name: "email", type: "email", unique: true },
{ name: "courses", type: "link", link: { table: "Course" } },
{ name: "isSubscribed", type: "bool", defaultValue: "false" },
{ name: "OTP", type: "string" },
],
},
{
name: "Course",
columns: [
{ name: "course_name", type: "string", notNull: true, defaultValue: "" },
{ name: "files", type: "file[]" },
],
revLinks: [{ column: "courses", table: "Users" }],
},
] as const;

export type SchemaTables = typeof tables;
export type InferredTypes = SchemaInference<SchemaTables>;

export type Users = InferredTypes["Users"];
export type UsersRecord = Users & XataRecord;

export type Course = InferredTypes["Course"];
export type CourseRecord = Course & XataRecord;

export type DatabaseSchema = {
Users: UsersRecord;
Course: CourseRecord;
};

const DatabaseClient = buildClient();

const defaultOptions = {
databaseURL: "https://Course-Companion-t0b82o.eu-central-1.xata.sh/db/main",
};

export class XataClient extends DatabaseClient<DatabaseSchema> {
constructor(options?: BaseClientOptions) {
super({ ...defaultOptions, ...options }, tables);
}
}

let instance: XataClient | undefined = undefined;

export const getXataClient = () => {
if (instance) return instance;

instance = new XataClient();
return instance;
};
// Generated by Xata Codegen 0.29.0. Please do not edit.
import { buildClient } from "@xata.io/client";
import type {
BaseClientOptions,
SchemaInference,
XataRecord,
} from "@xata.io/client";

const tables = [
{
name: "Users",
columns: [
{ name: "first_name", type: "string" },
{ name: "last_name", type: "string" },
{ name: "email", type: "email", unique: true },
{ name: "courses", type: "link", link: { table: "Course" } },
{ name: "isSubscribed", type: "bool", defaultValue: "false" },
{ name: "OTP", type: "string" },
],
},
{
name: "Course",
columns: [
{ name: "course_name", type: "string", notNull: true, defaultValue: "" },
{ name: "files", type: "file[]" },
],
revLinks: [{ column: "courses", table: "Users" }],
},
] as const;

export type SchemaTables = typeof tables;
export type InferredTypes = SchemaInference<SchemaTables>;

export type Users = InferredTypes["Users"];
export type UsersRecord = Users & XataRecord;

export type Course = InferredTypes["Course"];
export type CourseRecord = Course & XataRecord;

export type DatabaseSchema = {
Users: UsersRecord;
Course: CourseRecord;
};

const DatabaseClient = buildClient();

const defaultOptions = {
databaseURL: "https://Course-Companion-t0b82o.eu-central-1.xata.sh/db/main",
};

export class XataClient extends DatabaseClient<DatabaseSchema> {
constructor(options?: BaseClientOptions) {
super({ ...defaultOptions, ...options }, tables);
}
}

let instance: XataClient | undefined = undefined;

export const getXataClient = () => {
if (instance) return instance;

instance = new XataClient();
return instance;
};
this is xata.ts at src/lib/xata.ts this could so be user error on my end. Any help is greatly appreciated.
kostas
kostas10mo ago
This looks good to me. Your initial config /** My old Initialization */ is correct actually for SvelteKit, that is how we recommend it in our guide: https://xata.io/docs/getting-started/sveltekit#query-and-list-the-posts At this point I think it is possible something is cached in your IDE and it doesn't "see" updates with new columns to the xata client file. How about you try copying src/lib/xata.ts to src/lib/xata2.ts and in +server.ts you switch to import { XataClient } from '$lib/xata2'; does your IDE "see" the OTP field then?
Kaligraphy
KaligraphyOP10mo ago
Hi, I tried everything you suggested above in no particular order and it just worked 🤷‍♂️ The type errors are gone and it works. Thanks for your help.
kostas
kostas10mo ago
That's good news! I must've been an IDE glitch, sometimes cached builds do things like that.
Want results from more Discord servers?
Add your server