Apick.JS is a versatile API construction toolkit designed to facilitate the quick and efficient development of scalable and maintainable APIs. Leveraging the power of Koa, a modern and lightweight framework for Node.js, Apick.JS offers a streamlined and intuitive approach to API development. Its flexible architecture is further enhanced by the Adapter pattern, enabling support for a wide array of databases. This adaptability makes Apick.JS an ideal choice for projects that demand rapid development, deployment, and the flexibility to work with various types of databases. This README provides a detailed guide on setting up, features, and how to effectively use Apick.JS, complete with original examples to kickstart your API development.
Ensure Node.js is installed on your system before proceeding. Follow these steps to set up Apick.JS:
git clone git@github.com:vivmagarwal/apickjs.git
cd apickjs
npm install
To start your APICK server, you need to initialize it with a database adapter. The following example uses LowDBAdapter for simplicity. It ships with APICK JS.
import Apickjs from './index.js';
import LowDBAdapter from './src/database/lowDbAdapter.js';
let db = new LowDBAdapter('db.json');
let app = await Apickjs.create(db);
app.listen(3000, () => {
console.log(`APICK Server is running on port ${app.server.address().port}`);
});
You can add custom middleware to your server. Here's an example of a simple logging middleware:
export default function logger() {
return async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
}
}
To use this middleware, add it to your server setup:
app.use(logHelloWorldMiddleware());
APICK automatically generates CRUD (Create, Read, Update, Delete) routes for your database collections. For example, if you have a posts
collection in your db.json
, the following routes are automatically created:
GET /posts
- Fetch all postsGET /posts/:id
- Fetch a single post by IDPOST /posts
- Create a new postPUT /posts/:id
- Update a post by IDDELETE /posts/:id
- Delete a post by ID
You can define custom routes in addition to the auto-generated CRUD routes. Here's how you can add a custom route for restarting the server:
router.post("/__restart-server", async (ctx) => {
ctx.body = { message: "Server restarting..." };
setTimeout(() => {
apickApp.server.close(async () => {
console.log('Server closed.');
apickApp.listen(3000);
console.log('Server restarted.');
});
}, 1000);
});
APICK uses a JSON file (db.json
) as the database by default. You can perform operations on this database using the provided utility functions.
Apick.JS auto-generates routes based on your database structure. For a db.json
resembling:
{
"posts": [...],
"comments": [...],
"profile": {...}
}
You'll have routes like:
- Posts:
GET /posts
,GET /posts/:id
,POST /posts
,PUT /posts/:id
,PATCH /posts/:id
,DELETE /posts/:id
- Comments: Similar structure to posts.
- Profile:
GET /profile
,PUT /profile
,PATCH /profile
- Filtering:
GET /posts?views_gt=9000
to get posts with more than 9000 views. - Range:
GET /posts?_start=10&_end=20
fetches posts 11 through 20. - Pagination:
GET /posts?_page=1&_per_page=25
for pagination support. - Sorting:
GET /posts?_sort=id,-views
sorts byid
asc andviews
desc. - Nested Fields: Query by nested fields using dot notation:
GET /foo?a.b=bar
. - Embedding: Include related resources:
GET /posts?_embed=comments
.
To interact with your API, you can use tools like curl
, Postman, or any HTTP client. Here are some example requests:
GET http://localhost:3000/posts
GET http://localhost:3000/posts/1
POST http://localhost:3000/posts
Content-Type: application/json
{
"title": "New Post",
"userId": 1
}
PUT http://localhost:3000/posts/1
Content-Type: application/json
{
"title": "Updated Post Title",
"userId": 1
}
PATCH http://localhost:3000/posts/1
Content-Type: application/json
{
"title": "Partially Updated Post Title"
}
DELETE http://localhost:3000/posts/1
Apick.JS is designed to be flexible. You can add custom middleware for authentication, logging, or any other functionality your API might need.
We welcome contributions to Apick.JS! Please submit pull requests or issues on our GitHub repository. For support, refer to the documentation or open an issue.
To read data from a collection:
async get(collectionName, ctx) {
await this.db.read();
let data = this.db.data[collectionName];
return data;
}
To insert a new document into a collection:
async insert(collectionName, doc) {
await this.db.read();
this.db.data[collectionName].push(doc);
await this.db.write();
return doc;
}
To update an existing document in a collection:
async update(collectionName, id, doc) {
await this.db.read();
const index = this.db.data[collectionName].findIndex(({ id: currentId }) => currentId == id);
if (index !== -1) {
this.db.data[collectionName][index] = { ...doc, id };
await this.db.write();
}
return { id, ...doc };
}
Apick.JS is released under the MIT License. See the LICENSE file for more details.