ansa
ansa3y ago

Schemas vs Schemaless

So I have a conceptual question about convex! I was doing the counter/messages tutorial and adding the code for the messages portion seems to break the counter portion (I haven't updated App.tsx yet, only listMessage and sendMessage)
convex/incrementCounter.ts:4:37 - error TS2345: Argument of type '"counter_table"' is not assignable to parameter of type '"messages"'.
4 const counterDoc = await db.query("counter_table").first();
~~~~~~~~~~~~~~~
convex/incrementCounter.ts:6:21 - error TS2345: Argument of type '"counter_table"' is not assignable to parameter of type '"messages"'.
6 await db.insert("counter_table", {
~~~~~~~~~~~~~~~
convex/incrementCounter.ts:12:16 - error TS2339: Property 'counter' does not exist on type '{ _id: GenericId<"messages">; _creationTime: number; author: string; body: string; }'.

12 counterDoc.counter += increment;
convex/incrementCounter.ts:4:37 - error TS2345: Argument of type '"counter_table"' is not assignable to parameter of type '"messages"'.
4 const counterDoc = await db.query("counter_table").first();
~~~~~~~~~~~~~~~
convex/incrementCounter.ts:6:21 - error TS2345: Argument of type '"counter_table"' is not assignable to parameter of type '"messages"'.
6 await db.insert("counter_table", {
~~~~~~~~~~~~~~~
convex/incrementCounter.ts:12:16 - error TS2339: Property 'counter' does not exist on type '{ _id: GenericId<"messages">; _creationTime: number; author: string; body: string; }'.

12 counterDoc.counter += increment;
is it possible to leave both of these parts intact, or can you only interact with one db table at a time?
4 Replies
alexcole
alexcole3y ago
Do you have a convex/schema.ts file? The type of db is based on the schema you defined. It seems like maybe your schema only includes messages and not counter_table? If thats the case you can either: - Update the schema to get the TypeScript types for both - Or delete the schema.ts file to get permissive types that will work for all tables/documents More info at https://docs.convex.dev/using/schemas
Defining a Schema | Convex Developer Hub
A schema is a description of
ansa
ansaOP3y ago
so I just created the convex/schema.ts as part of this tutorial https://docs.convex.dev/getting-started/basics/chat-app. i don't think it existed before this. how was the original counter_table running (as part of the first tutorial https://docs.convex.dev/getting-started/hello-convex as I don't think there was a schema.ts during this part?
Hello Convex | Convex Developer Hub
This tutorial will teach you how to set up your first Convex app. Let's get
alexcole
alexcole3y ago
If you don't have a schema, Convex's code generation will allow any string as a table name, documents are typed as any, etc. Once you define a schema Convex expects your code to match it. The first tutorial works because it is schemaless (we wanted to put off introducing schemas until the chat app to keep the intro simple) But yeah, sorry about the confusing tutorial here! We're working on streamlining it to one app and I think that'll make this less confusing
ansa
ansaOP3y ago
got it, that makes sense!! thank you!!

Did you find this page helpful?