IMPORTANT: For all the tasks, focus on best practices. Aim for solutions that not only work but that you'd be happy to push to a production app.
After running this app, you'll notice that the backend crashes if you try adding a new draft post with an email address that doesn't belong to any existing user. To reproduce this issue:
- Click "Create draft"
- Enter a title
- Enter an email address that doesn't belong to any user, for example 'test@hellopolygon.com'
- Enter content
- Click "Create"
- You'll see the server crash with the following error message:
/Users/polygon/Documents/development.nosync/recruitment-task/backend/node_modules/@prisma/client/runtime/index.js:45405
throw new PrismaClientKnownRequestError(message, e.code, this.client._clientVersion, e.meta);
^
PrismaClientKnownRequestError:
Invalid `prisma.post.create()` invocation in
/Users/polygon/Documents/development.nosync/recruitment-task/backend/src/index.ts:50:36
47
48 app.post(`/post`, async (req, res) => {
49 const { title, content, authorEmail } = req.body
→ 50 const result = await prisma.post.create(
An operation failed because it depends on one or more records that were required but not found. No 'User' record(s) (needed to inline the relation on 'Post' record(s)) was found for a nested connect on one-to-many relation 'PostToUser'.
at Object.request (/Users/polygon/Documents/development.nosync/recruitment-task/backend/node_modules/@prisma/client/runtime/index.js:45405:15)
at async PrismaClient._request (/Users/polygon/Documents/development.nosync/recruitment-task/backend/node_modules/@prisma/client/runtime/index.js:46301:18) {
code: 'P2025',
clientVersion: '3.14.0',
meta: {
cause: "No 'User' record(s) (needed to inline the relation on 'Post' record(s)) was found for a nested connect on one-to-many relation 'PostToUser'."
}
}
There are many ways to address this bug, please address it using a solution of your choice. Remember that a good solution is not only technically sound but should also result in good user experience.
Make it possible for users to leave comments under published posts.
You're probaly familiar with typing indicators such as this one.
Now that users can add comments, add a typing indicator to show others that someone is writing a comment.
During the technical interview, among other things, we're going to discuss how you approached the tasks. We're probably going to ask you about why you decided on a specific solution, pros and cons of different ways of addressing the same problem etc.
This example shows how to implement a fullstack app in TypeScript with Next.js using React (frontend), Express and Prisma Client (backend). It uses a SQLite database file with some initial dummy data which you can find at ./backend/prisma/dev.db
.
Install dependencies for your backend
. Open a terminal window and install the backend
's dependencies
cd backend
npm install
Open a separate terminal window and navigate to your frontend
directory and install its dependencies
cd frontend
npm install
On the terminal window used to install the backend npm dependencies, run the following command to create your SQLite database file. This also creates the User
and Post
tables that are defined in prisma/schema.prisma
:
npx prisma migrate dev --name init
When npx prisma migrate dev
is executed against a newly created database, seeding is also triggered. The seed file in prisma/seed.ts
will be executed and your database will be populated with the sample data.
On the same terminal used in step 2, run the following command to start the server:
npm run dev
The server is now running at http://localhost:3001/
.
On the terminal window used to install frontend npm dependencies, run the following command to start the app:
npm run dev
The app is now running, navigate to http://localhost:3000/
in your browser to explore its UI.
Expand for a tour through the UI of the app
Blog (located in ./pages/index.tsx
)
Signup (located in ./pages/signup.tsx
)
Create post (draft) (located in ./pages/create.tsx
)
Drafts (located in ./pages/drafts.tsx
)
View post (located in ./pages/p/[id].tsx
) (delete or publish here)
You can also access the REST API of the API server directly. It is running localhost:3001
(so you can e.g. reach the API with localhost:3000/feed
).
/api/post/:id
: Fetch a single post by itsid
/api/feed
: Fetch all published posts/api/filterPosts?searchString={searchString}
: Filter posts bytitle
orcontent
/api/post
: Create a new post- Body:
title: String
(required): The title of the postcontent: String
(optional): The content of the postauthorEmail: String
(required): The email of the user that creates the post
- Body:
/api/user
: Create a new user- Body:
email: String
(required): The email address of the username: String
(optional): The name of the user
- Body:
/api/publish/:id
: Publish a post by itsid
/api/post/:id
: Delete a post by itsid
Evolving the application typically requires three steps:
- Migrate your database using Prisma Migrate
- Update your server-side application code
- Build new UI features in React
For the following example scenario, assume you want to add a "profile" feature to the app where users can create a profile and write a short bio about themselves.
The first step is to add a new table, e.g. called Profile
, to the database. You can do this by adding a new model to your Prisma schema file file and then running a migration afterwards:
// schema.prisma
model Post {
id Int @default(autoincrement()) @id
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int
}
model User {
id Int @default(autoincrement()) @id
name String?
email String @unique
posts Post[]
+ profile Profile?
}
+model Profile {
+ id Int @default(autoincrement()) @id
+ bio String?
+ userId Int @unique
+ user User @relation(fields: [userId], references: [id])
+}
Once you've updated your data model, you can execute the changes against your database with the following command:
npx prisma migrate dev
You can now use your PrismaClient
instance to perform operations against the new Profile
table. Here are some examples:
const profile = await prisma.profile.create({
data: {
bio: "Hello World",
user: {
connect: { email: "alice@prisma.io" },
},
},
});
const user = await prisma.user.create({
data: {
email: "john@prisma.io",
name: "John",
profile: {
create: {
bio: "Hello World",
},
},
},
});
const userWithUpdatedProfile = await prisma.user.update({
where: { email: "alice@prisma.io" },
data: {
profile: {
update: {
bio: "Hello Friends",
},
},
},
});
Once you have added a new endpoint to the API (e.g. /api/profile
with /POST
, /PUT
and GET
operations), you can start building a new UI component in React. It could e.g. be called profile.tsx
and would be located in the pages
directory.
In the application code, you can access the new endpoint via fetch
operations and populate the UI with the data you receive from the API calls.