next-json-server
v0.2.1
Published
Next.js App Router based lightweight JSON Server implementation
Maintainers
Readme
Next JSON Server
A lightweight JSON Server implementation based on Next.js App Router. Inspired by json-server, it provides a simple RESTful API based on JSON files.
Features
- 💡 Next.js App Router support
- 🚀 Simple setup
- 📝 JSON file based data management
- 🔄 RESTful API support
- 🛠 Customizable endpoints
- 🛢️ PostgreSQL support with Drizzle ORM
Getting Started
Using npm package
- Install the package
npm install next-json-server- Create a
db.jsonin your project root
{
"posts": [
{ "id": "1", "title": "a title", "views": 100 },
{ "id": "2", "title": "another title", "views": 200 }
],
"comments": [
{ "id": "1", "text": "a comment about post 1", "postsId": "1" },
{ "id": "2", "text": "another comment about post 1", "postsId": "1" }
],
"users": [{ "id": "1", "name": "yuyakinjo" }]
}- Generate JSON API route
npx next-json-server generate jsonThis will create /app/json/[...api]/internal.ts and /app/json/[...api]/route.ts files in your project.
- Start your Next.js development server
npm run dev- Access your API at
http://localhost:3000/json/posts
Using with PostgreSQL (Drizzle ORM)
- Generate PostgreSQL API route
npx next-json-server generate db/pgThis will create the following files:
/app/db/pg/[...api]/internal.ts- Internal utility functions/app/db/pg/[...api]/route.ts- API route handler/app/db/pg/schema/index.ts- Sample Drizzle schema file
- Configure your PostgreSQL connection in
.env.local
DATABASE_URL=postgresql://user:password@localhost:5432/dbnameCustomize your schema in
/app/db/pg/schema/index.tsas neededStart your Next.js development server
npm run devThe server will automatically run migrations and seed data on startup
Access your API at
http://localhost:3000/db/pg/users(or any other table name defined in your schema)
Clone and Run
- Clone the repository
git clone https://github.com/yuyakinjo/next-json-server.git- Install dependencies
cd next-json-server
bun install- Start the development server
bun devAPI Endpoints
The following RESTful API endpoints are available:
Retrieving Resources
Navigate by path
GET /json/posts- Get all postsGET /json/posts/1- Get post with ID:1GET /json/posts/1/comments- Get comments for post with ID:1GET /json/posts/1/comments/1- Get comment with ID:1 for post with ID:1
Using queries
GET /json/posts?id=1- Get post with ID:1GET /json/posts?id=1&id=2- Get posts with ID:1 and ID:2GET /json/posts?title=starwars- Get posts with title "starwars"GET /json/posts?gt_views=100- Get posts with views greater than 100GET /json/posts?lt_views=100- Get posts with views less than 100GET /json/posts?gte_views=100- Get posts with views greater than or equal to 100GET /json/posts?lte_views=100- Get posts with views less than or equal to 100GET /json/posts?ne_views=100- Get posts with views not equal to 100GET /json/posts?in_views=100,200- Get posts with views equal to 100 or 200
Creating Resources
POST /json/posts- Create a new post
{
"title": "New post",
"views": 0
}Updating Resources
PUT /json/posts/1- Update post with ID:1
{
"title": "Updated post",
"views": 150
}Deleting Resources
DELETE /json/posts/1- Delete post with ID:1
PostgreSQL API Endpoints
The following RESTful API endpoints are available when using the PostgreSQL integration:
Retrieving Resources
GET /db/pg/users- Get all usersGET /db/pg/users/1- Get user with ID:1GET /db/pg/posts- Get all postsGET /db/pg/posts/1- Get post with ID:1
Creating Resources
POST /db/pg/users- Create a new user
{
"name": "New User",
"email": "[email protected]"
}Updating Resources
PUT /db/pg/users/1- Update user with ID:1
{
"name": "Updated User",
"email": "[email protected]"
}Deleting Resources
DELETE /db/pg/users/1- Delete user with ID:1
Filtering and Pagination
GET /db/pg/users?limit=10&offset=0- Paginate usersGET /db/pg/users?name=John- Filter users by nameGET /db/pg/posts?authorId=1- Get posts with authorId:1
Response Examples
GET /json/posts
[
{
"id": "1",
"title": "a title",
"views": 100
},
{
"id": "2",
"title": "another title",
"views": 200
}
]Status Codes
200- Request successful201- Resource created successfully204- Resource deleted successfully404- Resource not found
CLI Commands
npx next-json-server generate json- Generate JSON API route filesnpx next-json-server generate db/pg- Generate PostgreSQL API route files (with Drizzle ORM)npx next-json-server help- Show help message
Usage in Development Environment
- Clone the repository
git clone https://github.com/yuyakinjo/next-json-server.git- Install dependencies
cd next-json-server
bun install- Start the development server
bun devRunning with Docker
You can also run it using Docker:
docker compose up -dLicense
Released under the MIT License. See LICENSE for details.
