Crudlify: simple CRUD REST API's for a NoSQL database
Tags: node, javascript, mongodb, typescript
Published: 2022-11-26 10:49:35

In database speak, create, read, update, and delete (CRUD) are the four basic operations of persistent storage ([Wikipedia](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete)).
In many application the CRUD are the baseline in your data backend, i.e. to get anything working in the front-end, you need a basic CRUD API backend.

NoSQL databases are popular for their schemaless and document oriented (JSON) way of working with data. [MongoDB](https://www.mongodb.com/) is perhaps the best known brand of all NoSQL document databases.

To help [codehooks.io](https://codehooks.io) JavaScript (and Typescript) developers kick-start their data backend, we created two open source packages that automagically creates a full CRUD REST API based on a data schema definition. The first one is based on Yup, a popular, fluent schema and validation library, the second is based on JSON schema, a standard approach to create schemas for JSON.

1. [Yup schema](https://www.npmjs.com/package/codehooks-crudlify-yup)
2. [JSON schema](https://www.npmjs.com/package/codehooks-crudlify-jsonschema)


## A code example with Yup

The code example below shows a [Yup](https://www.npmjs.com/package/yup) schema that defines two objects: `customer` and `product`.

```js
/* 
* schema.js
*/
import * as yup from 'yup';

// yup schemas per collection

// customer schema
let customer =
  yup.object().shape({
    name: yup.string().required(),
    status: yup.mixed().oneOf(['open', 'suspended', 'closed']).required(),
    balance: yup.number().required(),
    products: yup.array().of(
        yup.object({
          name: yup.string().required(),
          price: yup.number().required()
        })
      )
  })

  // product schema, any json is allowed
let product = yup.object().shape({
  json: yup.mixed()
})

export {customer, product}
```

The code example below shows how the `codehooks-crudlify-yup` package uses the data schema to create a CRUD REST API on top of codehooks' built-in NoSQL document database.

```js
/*
* index.js
*/
import app from 'codehooks-js'
import crudlify from 'codehooks-crudlify-yup'
import {customer, product} from './schema'

// Add CRUD routes with yup schema
crudlify(app, {customer, product})

// bind to serverless runtime
export default app.init()
```

When deploying this application to the cloud, the Crudlify library will effectively create CRUD REST API endpoints for `GET`, `POST`, `PUT`, `PATCH` and `DELETE`.


| Verb  | Codehooks.io route | Description  |
|:---|---|---|
| `GET`  | https://{projectid}.api.codehooks.io/:space/:collection[?field=value&...]  | Retrieve all objects or use API query |
| `GET`  | https://{projectid}.api.codehooks.io/:space/:collection/:ID  | Retrieve object by ID |
| `POST` | https://{projectid}.api.codehooks.io/:space/:collection  | Add object  | 
| `PUT`  | https://{projectid}.api.codehooks.io/:space/:collection/:ID  | Replace object by ID | 
| `PATCH`| https://{projectid}.api.codehooks.io/:space/:collection/:ID  | Update object by ID | 
|`DELETE`| https://{projectid}.api.codehooks.io/:space/:collection/:ID  | Delete object by ID | 

Happy CRUD API hacking!

> Full source code available at [GitHub](https://github.com/RestDB/codehooks-io-examples/tree/main/crud)

[codehooks.io](https://codehooks.io) is a new simple backend service for JavaScript (and Typescript) developers. It has a built in NoSQL document database with a subset of the MongoDB query language. It also has a key-value store, a message queue and a CRON job scheduling system built-in.