This is an example how you can implement fully type-safe entity model using prisma.
- End-to-End type safety
- Before and After hooks for each operation
- Completely extensible
- User Entity (src/entities/User.ts): Before and After hooks
- Post Entity (src/entities/Post.ts): Data validation using zod
git clone [email protected]:salmannotkhan/prisma-entity.git
Install npm dependencies:
cd prisma-entity
npm install
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.
npm run dev
The server is now running on http://localhost:3000
. You can now run the API requests, e.g. http://localhost:3000/users
.
You can access the REST API of the server using the following endpoints:
/users
: Fetch all users/users/:id
: Fetch user by theirid
/users/:id/posts
: Fetch all posts by userid
/users/:id/posts/:postId
: Fetch post by userid
and postid
/users
: Create a new Useremail: String
(required): The email address of the username: String
(optional): The name of the user
/users/:id/posts
: Create new posts for the userid
title: String
(required): The title for the postcontent: String
(optoinal): The content of the postpublished: Boolean
(optional): The content of the postviewCount: Number
(optoinal): The view count of the post
/users/:id
: Update the user details by using it'sid
/users/:id/posts/:postId
: Update post for the userid
and postid
/users/:id
: Delete a user by itsid
/users/:id/posts/:postId
: Delete a post by userid
and postid