procrastinator_bas
procrastinator_bas
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Accessing/changing another entity when performing a db action
Haha, thanks. I was able to resolve it. I gave the bot the final correct code, but I'm not sure it's gonna do something with it 😉 This issue is resolved. 👍
37 replies
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Accessing/changing another entity when performing a db action
@kapa.ai , Almost correct. 🙂 I can't return the prsima.transaction, it does not fit the correct return type. This altered code does work:
const [updatedClass, newStudent] = await prisma.$transaction([
context.entities.Class.update({
where: { teacherId: context.user.id },
data: { lastAssignedNumber: newStudentNumber }
}),
context.entities.Student.create({
data: {
name: args.name,
number: newStudentNumber,
class: { connect: { teacherId: context.user.id }}
}
})
]);

return newStudent;
const [updatedClass, newStudent] = await prisma.$transaction([
context.entities.Class.update({
where: { teacherId: context.user.id },
data: { lastAssignedNumber: newStudentNumber }
}),
context.entities.Student.create({
data: {
name: args.name,
number: newStudentNumber,
class: { connect: { teacherId: context.user.id }}
}
})
]);

return newStudent;
37 replies
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Accessing/changing another entity when performing a db action
@kapa.ai Is import prisma from '@wasp/dbClient' correct? I get this error: Cannot find module '@wasp/dbClient' or its corresponding type declarations. Where should prisma be located? I'm using wasp 0.14.
37 replies
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Getting the user in the rootcomponent
I'm sorry, I meant in the props for the Header function. Here is my code:
import React from "react"
import '@mantine/core/styles.css';
import { MantineProvider, Button } from '@mantine/core';
import { Container, Group, Burger } from '@mantine/core';
import classes from './HeaderSimple.module.css';
import { logout, useAuth } from "wasp/client/auth";
import { Routes, routes } from "wasp/client/router";
import { NavLink } from "react-router-dom";
import { AuthUser } from "wasp/auth";

const links: { link: Routes, label: string }[] = [
{ link: routes.RootRoute, label: 'Home' },
{ link: routes.ClassRoute, label: 'Class' },
{ link: routes.CourseRoute, label: 'Course' },
];

export default function Root({ children }: { children: React.ReactNode }) {
const { data: user } = useAuth();

return (
<MantineProvider>
<Header user={user} />
{children}
</MantineProvider>)
}

function Header({ user }: { user: AuthUser | null | undefined }) {
const items = links.map((link) => (
<NavLink to={link.link.to}>
<Button key={link.label}>{link.label}</Button>
</NavLink>
));

return (
<header className={classes.header}>
<Container size="md" className={classes.inner}>
<h1>Teachers tools</h1>
<Group gap={5} visibleFrom="xs">
{user && items}
{user && <Button size="s" onClick={logout}>Logout</Button>}
{!user && <NavLink to='/login'><Button size="s">Login</Button></NavLink>}
</Group>
</Container>
</header>
);
}
import React from "react"
import '@mantine/core/styles.css';
import { MantineProvider, Button } from '@mantine/core';
import { Container, Group, Burger } from '@mantine/core';
import classes from './HeaderSimple.module.css';
import { logout, useAuth } from "wasp/client/auth";
import { Routes, routes } from "wasp/client/router";
import { NavLink } from "react-router-dom";
import { AuthUser } from "wasp/auth";

const links: { link: Routes, label: string }[] = [
{ link: routes.RootRoute, label: 'Home' },
{ link: routes.ClassRoute, label: 'Class' },
{ link: routes.CourseRoute, label: 'Course' },
];

export default function Root({ children }: { children: React.ReactNode }) {
const { data: user } = useAuth();

return (
<MantineProvider>
<Header user={user} />
{children}
</MantineProvider>)
}

function Header({ user }: { user: AuthUser | null | undefined }) {
const items = links.map((link) => (
<NavLink to={link.link.to}>
<Button key={link.label}>{link.label}</Button>
</NavLink>
));

return (
<header className={classes.header}>
<Container size="md" className={classes.inner}>
<h1>Teachers tools</h1>
<Group gap={5} visibleFrom="xs">
{user && items}
{user && <Button size="s" onClick={logout}>Logout</Button>}
{!user && <NavLink to='/login'><Button size="s">Login</Button></NavLink>}
</Group>
</Container>
</header>
);
}
26 replies
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Accessing/changing another entity when performing a db action
@kapa.ai My IDE says context.entities.$transaction does not exist. Is that syntax correct? Or should I directly use prisma? If so, how do I import it?
37 replies
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Getting the user in the rootcomponent
@sodic I am using Typescript. If I hover over useAuth it shows me the UseQueryResult<AuthUser | null> type. I'm guessing AuthUser will be undefined at first render? @kapa.ai Your code gives me a typescript error on this part: <Header user={user} /> I needed to change the type of user to AuthUser | null | undefined to get it to work.
26 replies
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Accessing/changing another entity when performing a db action
@kapa.ai Is this all in 1 transaction? If any part of these database calls fail, I would want all of it to roll back, so I don't skip numbers unnecessarily.
37 replies
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Accessing/changing another entity when performing a db action
It is a working solution to my question, but after analyzing further the requirements have changed (don't they always?). If I have a class of 10 students, delete student 3 and add a new one, that student will get a number that is already assigned. Numbers do not need to be re-used after deleting a student and a number opens up.. So I do believe I need to store the latest assigned number in the class table and get that number when adding a new student. @kapa.ai , do you want to give it another try with the new requirements?
37 replies
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Getting the user in the rootcomponent
@kapa.ai If the user is not loggedIn, what will be the value of user?
26 replies
WWasp-lang
Created by procrastinator_bas on 8/25/2024 in #🙋questions
Accessing/changing another entity when performing a db action
Good bot. 🙂
37 replies