This week, your task is to rework task number 6 to use Server actions instead of API endpoints and asynchronous React Server Components to retrieve data instead of useQuery. You are provided with a correct solution of task number 6. The final functionality required is exactly the same as in problem number 6, except:
-
the logged-in user must be stored in the database, not just in the application Context. The user must remain logged in after reloading the entire application. To retrieve the logged in user, we recommend creating a custom asynchronous function that will read from the database which user is logged in.
- there is no need to create a separate table for the logged-in user. The
isLoggedInflag in theuserstable is sufficient. Suppose that only one user can be logged in at a time. There is no need to include this condition in the code, it is a general assumption.
- there is no need to create a separate table for the logged-in user. The
-
the application will allow users to log out.
-
All functionality that is available in task 6 must be available in this task.
-
The resulting application will not use any API endpoints, but only Server actions (for logging in, logging out, creating a new gift, and updating the gift delivery status) and asynchronous React Server Components (for retrieving data from the database).
- The
src/app/apifolder should be completely deleted.
- The
-
The
src/modules/*/schema.tsshould be removed. All schemas should be replaced by schemas in the database. -
The
src/modules/*/server.tsfolder should be removed. Create a local sqlite database using Turso and use Drizzle ORM to mutate and query data. -
The
src/contextfolder should be removed. The user should be stored in the database.
Tips:
-
When implementing server actions and creating a database, read our materials.
-
Use the local database with
tursoduring development.- You need to create a
.envfile with two variables -DATABASE_URLandAUTH_TOKEN.
- You need to create a
-
Take inspiration from the code structure presented in the lecture.
-
The database schema does not differ from the currently defined schemas in
src/modules/*/schema.ts. The only change is the addition of theisLoggedIncolumn to theuserstable. -
There is a
users 1:M giftsrelationship between the two tables. In thegiftstable, there is a columncreatedBywhich refers to theidof the user who created the gift. To create a relationship, use therelationsfunction from thedrizzle-orm.- Be sure to include this session in the schema in your database instance
const db = drizzle(client, { schema: { users, gifts, // relations usersRelations, giftsRelations } });
-
To create a user
rolein the database so that the value can only contain "user" or "santa", we can add additionaloptionsto thetextfunction, specifically as follows:
const roleSchema = z.enum(['santa', 'user']);
const users = sqliteTable('users', {
...
role: text('role', { enum: roleSchema.options }),
})- First group 1.12.2025
- Second group 2.12.2025