edproton
edproton
CCConvex Community
Created by FleetAdmiralJakob 🗕 🗗 🗙 on 11/20/2024 in #support-community
Bug: Can't preload a page with pagination
Hello @jamwt , I’m a big fan of TanStack tools, and Tanner’s vision (even the React community reveres him—who is this wizard, haha) is truly mind-blowing! I’m curious, what led you to prefer TanStack Start? By the way, I discovered Convex through TanStack Start’s sponsorship—such a great connection there!
18 replies
CCConvex Community
Created by FleetAdmiralJakob 🗕 🗗 🗙 on 11/20/2024 in #support-community
Bug: Can't preload a page with pagination
18 replies
CCConvex Community
Created by FleetAdmiralJakob 🗕 🗗 🗙 on 11/20/2024 in #support-community
Bug: Can't preload a page with pagination
If you create something please share with us :convexspin:
18 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
I have "bundler" too. Thanks @TripleSpeeder @erquhart and @ballingt
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
I believe @ballingt is the maintainer of convex/auth, but I'm not entirely sure if the issue is with convex/auth or convex/test.
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
Good. Let me know if you need any additional input or further details @erquhart
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
Haha, alright, give it a shot, mate—appreciate it!
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
is the same, sorry for "stealing the thread" @TripleSpeeder
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
No description
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
No description
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
// vitest.config.mts
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
environment: "edge-runtime",
server: { deps: { inline: ["convex-test"] } },
},
});
// vitest.config.mts
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
environment: "edge-runtime",
server: { deps: { inline: ["convex-test"] } },
},
});
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
I will provide an example
// subjects.test.ts
import { convexTest } from "convex-test";
import { expect, test } from "vitest";
import { api } from "./_generated/api";
import schema from "./schema";

test("creating and retrieving subjects", async () => {
const t = convexTest(schema);

// Setup admin user
const asAdmin = t.withIdentity({
name: "Admin User",
role: "admin",
});

// Test creating a subject
const mathId = await asAdmin.mutation(api.subjects.createSubject, {
name: "Mathematics",
});

// Test getting all subjects
const allSubjects = await t.query(api.subjects.getAllSubjects);
expect(allSubjects).toHaveLength(1);
expect(allSubjects[0]).toMatchObject({
name: "Mathematics",
});

// Test getting subject by ID
const mathSubject = await t.query(api.subjects.getSubjectById, {
id: mathId,
});
expect(mathSubject).toMatchObject({
name: "Mathematics",
});
});

// subjects.ts
...
// Mutation to create a new subject
export const createSubject = mutation({
args: {
name: v.string(),
},
handler: async (ctx, args) => {
await requireRole(ctx, ROLES.ADMIN);

const subjectId = await ctx.db.insert("subjects", {
name: args.name,
});

return subjectId;
},
});

// schemas.ts
import { defineSchema, defineTable } from "convex/server";
import { authTables } from "@convex-dev/auth/server";
import { v } from "convex/values";
import { ROLES } from "./lib/permissions";

const schema = defineSchema({
...authTables,

/**
* Users table
*/
users: defineTable({
name: v.optional(v.string()),
...
role: v.optional(
v.union(
v.literal(ROLES.ADMIN),
v.literal(ROLES.TUTOR),
v.literal(ROLES.STUDENT)
)
),
})
.index("email", ["email"])
.index("phone", ["phone"])
.index("role", ["role"]),

/**
* Subjects table
*/
subjects: defineTable({
name: v.string(),
}),

/**
* Levels table
*/
levels: defineTable({
name: v.string(),
subjectId: v.id("subjects"),
}).index("by_subjectId", ["subjectId"]),
});

export default schema;

// permissions.ts
export const ROLES = {
STUDENT: "student",
TUTOR: "tutor",
ADMIN: "admin",
} as const;

export async function checkRole(
ctx: QueryCtx | MutationCtx,
requiredRole: Role
): Promise<boolean> {
const user = await ctx.auth.getUserIdentity();

// If the user doesn't exist or doesn't have a role, return false
if (!user || !user.roles) return false;

// Check if the user has the required role
return user.roles === requiredRole;
}

export async function requireRole(
ctx: QueryCtx | MutationCtx,
requiredRole: Role
): Promise<void> {
const hasRole = await checkRole(ctx, requiredRole);
if (!hasRole) {
throw new Error(`Access denied. Required role: ${requiredRole}`);
}
}
// subjects.test.ts
import { convexTest } from "convex-test";
import { expect, test } from "vitest";
import { api } from "./_generated/api";
import schema from "./schema";

test("creating and retrieving subjects", async () => {
const t = convexTest(schema);

// Setup admin user
const asAdmin = t.withIdentity({
name: "Admin User",
role: "admin",
});

// Test creating a subject
const mathId = await asAdmin.mutation(api.subjects.createSubject, {
name: "Mathematics",
});

// Test getting all subjects
const allSubjects = await t.query(api.subjects.getAllSubjects);
expect(allSubjects).toHaveLength(1);
expect(allSubjects[0]).toMatchObject({
name: "Mathematics",
});

// Test getting subject by ID
const mathSubject = await t.query(api.subjects.getSubjectById, {
id: mathId,
});
expect(mathSubject).toMatchObject({
name: "Mathematics",
});
});

// subjects.ts
...
// Mutation to create a new subject
export const createSubject = mutation({
args: {
name: v.string(),
},
handler: async (ctx, args) => {
await requireRole(ctx, ROLES.ADMIN);

const subjectId = await ctx.db.insert("subjects", {
name: args.name,
});

return subjectId;
},
});

// schemas.ts
import { defineSchema, defineTable } from "convex/server";
import { authTables } from "@convex-dev/auth/server";
import { v } from "convex/values";
import { ROLES } from "./lib/permissions";

const schema = defineSchema({
...authTables,

/**
* Users table
*/
users: defineTable({
name: v.optional(v.string()),
...
role: v.optional(
v.union(
v.literal(ROLES.ADMIN),
v.literal(ROLES.TUTOR),
v.literal(ROLES.STUDENT)
)
),
})
.index("email", ["email"])
.index("phone", ["phone"])
.index("role", ["role"]),

/**
* Subjects table
*/
subjects: defineTable({
name: v.string(),
}),

/**
* Levels table
*/
levels: defineTable({
name: v.string(),
subjectId: v.id("subjects"),
}).index("by_subjectId", ["subjectId"]),
});

export default schema;

// permissions.ts
export const ROLES = {
STUDENT: "student",
TUTOR: "tutor",
ADMIN: "admin",
} as const;

export async function checkRole(
ctx: QueryCtx | MutationCtx,
requiredRole: Role
): Promise<boolean> {
const user = await ctx.auth.getUserIdentity();

// If the user doesn't exist or doesn't have a role, return false
if (!user || !user.roles) return false;

// Check if the user has the required role
return user.roles === requiredRole;
}

export async function requireRole(
ctx: QueryCtx | MutationCtx,
requiredRole: Role
): Promise<void> {
const hasRole = await checkRole(ctx, requiredRole);
if (!hasRole) {
throw new Error(`Access denied. Required role: ${requiredRole}`);
}
}
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
just kidding.
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
What, we're in the same repo 😂
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
😂
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
in the same second.
37 replies
CCConvex Community
Created by TripleSpeeder on 12/14/2024 in #support-community
After moving from Clerk to Convex-Auth my convex tests fail
I'm having the same problem haha WHAT
37 replies
CCConvex Community
Created by edproton on 11/30/2024 in #support-community
Dynamic query builder in convex
You could @lee, it would be appreciate it.
14 replies
CCConvex Community
Created by h4nto on 11/21/2024 in #support-community
current user in middleware
Thanks it helped a lot @sshader
7 replies
CCConvex Community
Created by Doogibo on 11/29/2024 in #support-community
Conditionally Building Queries
I'm checking this @djbalin, thanks.
10 replies