0.0.7 • Published 4 years ago

express-json-file-crud v0.0.7

Weekly downloads
3
License
BSD-2-Clause
Repository
github
Last release
4 years ago

This package helps to quickly setup a CRUD REST service using Express: You can create, read, update and delete objects using REST.

It stores data in plain JSON files. No database is required, which makes setup rapid compared to similar solutions, and therefore perfectly suitable for quickly prototyping solutions.

This package should probably not be used for productive applications, as the underlying JSON-file-based storage engine is very basic and not a replacement for a proper database.

Setup

In your express project, run npm install --save express-json-file-crud.

Then mount a CRUD route under any endpoint, e.g.:

const makeCrud = require('express-json-file-crud').makeCrud;
const carCrud = makeCrud('cars', './storage');
app.use('/cars', carCrud);

makeCrud(entityName: string, storagePath: string) takes two arguments:

  • entityName: The type of entity that will be stored using this end point.
  • storagePath: The (relative or absolute) path to a directory where one file per entity will be stored.

All entities will be stored in [storagePath]/[entityName].json. The individual [entityName].json files and storagePath will be created if they don't exist.

IDs

Objects are identified by a numeric ID. When an object is created that has no ID set or that has an ID that conflicts with an existing ID, the id property will automatically be filled (by incrementing the highest stored ID so far for the given entity).

The IDs are used to read, update and delete specific objects.

REST usage

ActionPathHTTP ActionDescription
Create/POST
Read (list all objects)/GETReturns an array of all stored entities
Read (single object)/:idGETReturns the object with the given ID
Update (update existing object)/:idPUTUpdates the object with the given ID
Delete/:idDELETEdeletes the object with the given ID

When you start your express app, the cars route is now ready to be used.

POST and PUT need to contain a request header Content-Type: application/json to work properly.

For example, to create a new car, you can call

POST /cars

with a car object as the request body.

To list all cars, you can call

GET /cars

and get an array with cars back as the response body.

To get the car with the ID 1, you can call

GET /cars/1

To update it, call

PUT /cars/1

with the updated car object in the request body.

To delete the car with the ID 1:

DELETE /cars/1
0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago