igor9silva
igor9silva•2mo ago

Splitting up schema debugging

Idk what is wrong
17 Replies
ballingt
ballingt•2mo ago
@igor9silva what's your schema look like? It'll be easier to debug if you can share your code as code instead of screenshots. That ...taskEventTable line looks wrong but I can't see what's around it
igor9silva
igor9silvaOP•2mo ago
ofc, sorry
export const actionResultErrorTaskEventSchema = v.object({
kind: v.literal('actionResult'),
taskId: v.id('tasks'),
author: authorSchema,
actionId: v.id('taskActions'),
actionKind: taskActionKinds,
error: v.string(),
result: v.null(),
});

export const actionResultSuccessTaskEventSchema = v.object({
kind: v.literal('actionResult'),
taskId: v.id('tasks'),
author: authorSchema,
actionId: v.id('taskActions'),
actionKind: taskActionKinds,
result: v.string(),
error: v.null(),
});

export const actionRequestTaskEventSchema = v.object({
kind: v.literal('actionRequest'),
taskId: v.id('tasks'),
author: authorSchema,
actionId: v.id('taskActions'),
actionKind: taskActionKinds,
});

export const messageTaskEventSchema = v.object({
kind: v.literal('message'),
taskId: v.id('tasks'),
author: authorSchema,
message: v.string(),
});

export const addTaskEventSchema = v.object({
kind: v.literal('add'),
taskId: v.id('tasks'),
author: authorSchema,
});

export const taskEventSchema = v.union(
actionRequestTaskEventSchema,
actionResultErrorTaskEventSchema,
actionResultSuccessTaskEventSchema,
messageTaskEventSchema,
addTaskEventSchema,
);
export const actionResultErrorTaskEventSchema = v.object({
kind: v.literal('actionResult'),
taskId: v.id('tasks'),
author: authorSchema,
actionId: v.id('taskActions'),
actionKind: taskActionKinds,
error: v.string(),
result: v.null(),
});

export const actionResultSuccessTaskEventSchema = v.object({
kind: v.literal('actionResult'),
taskId: v.id('tasks'),
author: authorSchema,
actionId: v.id('taskActions'),
actionKind: taskActionKinds,
result: v.string(),
error: v.null(),
});

export const actionRequestTaskEventSchema = v.object({
kind: v.literal('actionRequest'),
taskId: v.id('tasks'),
author: authorSchema,
actionId: v.id('taskActions'),
actionKind: taskActionKinds,
});

export const messageTaskEventSchema = v.object({
kind: v.literal('message'),
taskId: v.id('tasks'),
author: authorSchema,
message: v.string(),
});

export const addTaskEventSchema = v.object({
kind: v.literal('add'),
taskId: v.id('tasks'),
author: authorSchema,
});

export const taskEventSchema = v.union(
actionRequestTaskEventSchema,
actionResultErrorTaskEventSchema,
actionResultSuccessTaskEventSchema,
messageTaskEventSchema,
addTaskEventSchema,
);
thats the whole table definition (the last union) on the same file works fine if I just move them to another file (any file inside convex/) and import in schema.ts, it breaks tried both by calling defineTable(taskEventSchema) inside defineSchema() or in the 2nd file
ballingt
ballingt•2mo ago
What does convex/schema.ts look like?
igor9silva
igor9silvaOP•2mo ago
one sec
No description
igor9silva
igor9silvaOP•2mo ago
import { authTables } from '@convex-dev/auth/server';
import { defineSchema, defineTable } from 'convex/server';
import { v } from 'convex/values';

// #region Global -------------------------------------
export const authorSchema = v.union(
v.id('users'), //
v.literal('meseeks'),
);
// #endregion

// #region Task Actions -------------------------------------
export const taskActionStatuses = v.union(
v.literal('pending'),
v.literal('running'),
v.literal('succeeded'),
v.literal('failed'),
v.literal('skipped'),
);

export const taskActionKinds = v.union(
v.literal('fill'),
v.literal('minify'),
v.literal('scrape'),
v.literal('factCheck'),
// v.literal('learn'),
// v.literal('suggest'),
);
// #endregion

// #region Task Events -------------------------------------
// previously shared taskEventSchema here
// #endregion

export default defineSchema({
...authTables,
tasks: defineTable({
owner: v.id('users'),
title: v.string(),
body: v.optional(v.string()),
}).index('by_owner', ['owner']),
taskActions: defineTable({
taskId: v.id('tasks'),
kind: taskActionKinds,
status: taskActionStatuses,
isDone: v.boolean(),
errorMessage: v.optional(v.string()),
}).index('by_task', ['taskId']),
taskEvents: defineTable(
taskEventSchema, //
).index('by_task', ['taskId']),
});
import { authTables } from '@convex-dev/auth/server';
import { defineSchema, defineTable } from 'convex/server';
import { v } from 'convex/values';

// #region Global -------------------------------------
export const authorSchema = v.union(
v.id('users'), //
v.literal('meseeks'),
);
// #endregion

// #region Task Actions -------------------------------------
export const taskActionStatuses = v.union(
v.literal('pending'),
v.literal('running'),
v.literal('succeeded'),
v.literal('failed'),
v.literal('skipped'),
);

export const taskActionKinds = v.union(
v.literal('fill'),
v.literal('minify'),
v.literal('scrape'),
v.literal('factCheck'),
// v.literal('learn'),
// v.literal('suggest'),
);
// #endregion

// #region Task Events -------------------------------------
// previously shared taskEventSchema here
// #endregion

export default defineSchema({
...authTables,
tasks: defineTable({
owner: v.id('users'),
title: v.string(),
body: v.optional(v.string()),
}).index('by_owner', ['owner']),
taskActions: defineTable({
taskId: v.id('tasks'),
kind: taskActionKinds,
status: taskActionStatuses,
isDone: v.boolean(),
errorMessage: v.optional(v.string()),
}).index('by_task', ['taskId']),
taskEvents: defineTable(
taskEventSchema, //
).index('by_task', ['taskId']),
});
i'm doing #regions like in the good old days 😅 any insights?
ballingt
ballingt•2mo ago
Can you share more of the code? What errors are you getting? I don't see any issues here. You might try starting small, and building up your schema piece by piece until you don't get the error. Are there any type errors, are you using TypeScript?
igor9silva
igor9silvaOP•2mo ago
No description
ballingt
ballingt•2mo ago
@igor9silva are you still stuck on this, could you share your code? frustrating that there's not a better error here, maybe we can improve this Do you have any type errors? Are you using TypeScript?
igor9silva
igor9silvaOP•2mo ago
hi, yes
igor9silva
igor9silvaOP•2mo ago
I tried cloning this but I cannot reproduce that behavior there
GitHub
GitHub - get-convex/convex-tanstack-start
Contribute to get-convex/convex-tanstack-start development by creating an account on GitHub.
igor9silva
igor9silvaOP•2mo ago
I do use typescript, but no type error this is the full log
No description
igor9silva
igor9silvaOP•2mo ago
I had taskEventSchema declared in schema.ts and working as expected
No description
No description
igor9silva
igor9silvaOP•2mo ago
but if I move it to a different file, it breaks with the above error only (no extra info)
No description
No description
ballingt
ballingt•2mo ago
sounds like it could be circular imports, are you importing something from schema.ts into another file?
igor9silva
igor9silvaOP•2mo ago
bingo!!! thank you so much @ballingt I was exporting authorSchema from schema.ts and importing it in the 2nd file (which was naturally imported in schema.ts) a proper error message would be nice ofc also updated everything to use Zod and it looks fantastic now, I'm truly hyped with Convex
ballingt
ballingt•2mo ago
We've been kicking around the idea of a Convex-specific linter, but this is a general thing you can use something like https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-cycle.md#importno-cycle for. We probably can't make it an error like https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-cycle.md does because it's a common structure and hard to statically detect when it works

Did you find this page helpful?