Skip to content

Commit 8d531c0

Browse files
committed
Readme overhaul, bump to 4.2.0
1 parent d9ee4dc commit 8d531c0

2 files changed

Lines changed: 90 additions & 42 deletions

File tree

README.md

Lines changed: 89 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,43 @@
44

55
# scrud [![NPM version](https://badge.fury.io/js/scrud.svg)](https://npmjs.org/package/scrud) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) [![Dependency Status](https://dependencyci.com/github/doesdev/scrud/badge)](https://dependencyci.com/github/doesdev/scrud)
66

7-
> Super opinionated, minimalistic, PG centric API fabric
7+
> SCRUD API server, fast, light, capable
88
9-
# what it is
9+
# what is SCRUD
1010

11-
- a collection of helpers that allow you to stand up APIs rapidly
12-
- extremely opinionated
13-
- driven by PostgreSQL functions
14-
- all APIs revolve around [SCRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) actions
15-
- all resource actions have default handlers but can be individually overridden
11+
It's just [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) with a search action included in the package.
12+
- search
13+
- create
14+
- read
15+
- update
16+
- delete
17+
18+
Every resource has corresponding SCRUD actions which are expressed in the HTTP methods and path (resource ids and query params).
19+
20+
# what is this module
21+
22+
- a blazing fast API server
23+
- a resourceful SCRUD based router
24+
- a collection of tools that allow you to stand up APIs rapidly
25+
- opinionated
26+
- full featured (PG, gzip, CORS, and JWT fully integrated)
27+
- a wire between your SCRUD PostgreSQL functions and the client
1628

1729
# who is it for
1830

1931
- me mostly
20-
- organizations / individuals comfortable working with business logic in Postgres
32+
- folks who need a fast and effective resource driven API server
33+
- organizations / individuals who like PostgreSQL and need a solid frontend
2134

2235
# is it fast
2336

24-
Glad you asked. Yes, it is. As always, take benches with a grain of salt. The point is we care about speed and optimize for it. To see benchmarking details check out the bench directory. The gist is this, all libs return a simple JSON response and the proper content-type headers. Each server is run in their own forked process and gets a warm up run for 3 seconds before we start tracking the results.
37+
Glad you asked. Yes, it is.
38+
39+
# benchmarks
40+
41+
As always, take benches with a grain of salt. The point is that we care about performance and optimize for it. To see benchmarking details check out the bench directory. The gist is this, all libs return a simple JSON response and the proper content-type headers. Each server is run in their own forked process and gets a warm up run for 3 seconds before we start tracking the results.
42+
43+
Node's `http` built-in is given the advantage of pre-rendered JSON, as it is the benchmark. SCRUD and Polka bounce back and forth in second position. To be clear SCRUD only handles resourceful SCRUD routes, Polka packs a more capable router which handles less standardized routes and an Express compatible middleware handler. SCRUD is focused on APIs and microservices.
2544

2645
<div align="center">
2746
<img src="https://github.com/doesdev/scrud/raw/master/assets/bench.png" alt="SCRUD" />
@@ -34,51 +53,85 @@ Glad you asked. Yes, it is. As always, take benches with a grain of salt. The po
3453
$ npm install --save scrud
3554
```
3655

56+
# usage
57+
58+
```js
59+
const scrud = require('scrud')
60+
const config = require('./secrets.json')
61+
62+
async function main () {
63+
let memberHandler = {
64+
update: (req, res) => scrud.sendData(res, 'not updated, goteem'),
65+
delete: (req, res) => scrud.sendErr(res, new Error('Resource cannot be deleted')),
66+
beforeQuery: {
67+
search: (req, res) => {
68+
delete req.params.someProperty
69+
return Promise.resolve()
70+
}
71+
},
72+
beforeSend: {
73+
read: (req, res, data) => {
74+
data.meaningOfLife = 42
75+
return Promise.resolve(data)
76+
}
77+
}
78+
}
79+
await scrud.register('member', memberHandler)
80+
await scrud.start(config)
81+
}
82+
```
83+
3784
# api
3885

39-
### scrud.register(name, options)
86+
### scrud.register(resourceName, actionHandlers)
4087
- Registers a resource as an API, enabling it to respond to SCRUD actions against it
4188
- A common pattern would be registering resources that correlate to database tables
4289
- Does not have to correlate to a database table
43-
- If actions are not being overridden requires matching PG functions for each action
90+
- If actions handlers are not specified then matching PG functions for each action are required
4491
- Each PG function matches pattern `${namespace}_${resource}_${action}(IN jsonb, OUT jsonb)`
4592
- PG functions receive `JSONB` object containing the following
4693
- `auth` - auth object (i.e. JWT payload)
47-
- `ip` - client IP address
94+
- `ip` - **(if `getIp` enabled)** client IP address
4895
- `id` - resource id for `READ`, `UPDATE`, `DELETE`
4996
- `id_array` - array of single resource id for `READ`
5097
- anything passed as querystring or in JSON body will be passed along and should be validated / escaped within PG functions
51-
- PG functions are expected to reply with an array of record in `JSONB` format
98+
- PG functions are expected to reply with an array of records in `JSONB` format
5299
- All PG functions should reply with array, even if they act on single resource
53100

54101
***Returns:*** Promise which with resolves with resource object
55102

56103
***Arguments:***
57-
- **name** *(String)* - *required*
58-
- **options** *(Object)* - *optional*
59-
- **search** - *optional*
60-
- type: `Function` - receives (http.ClientRequest, http.ServerResponse)
61-
- default: calls `${namespace}_${resource}_search(IN jsonb, OUT jsonb)` PG function and responds to client with array of records or error
62-
- **create** - *optional*
63-
- type: `Function` - receives (http.ClientRequest, http.ServerResponse)
64-
- default: calls `${namespace}_${resource}_create(IN jsonb, OUT jsonb)` PG function and responds to client with created record or error
65-
- **read** - *optional*
66-
- type: `Function` - receives (http.ClientRequest, http.ServerResponse)
67-
- default: calls `${namespace}_${resource}_read(IN jsonb, OUT jsonb)` PG function and responds to client with record or error
68-
- **update** - *optional*
69-
- type: `Function` - receives (http.ClientRequest, http.ServerResponse)
70-
- default: calls `${namespace}_${resource}_update(IN jsonb, OUT jsonb)` PG function and responds to client with updated record or error
71-
- **delete** - *optional*
72-
- type: `Function` - receives (http.ClientRequest, http.ServerResponse)
73-
- default: calls `${namespace}_${resource}_delete(IN jsonb, OUT jsonb)` PG function and responds to client with data (success) or error
104+
- **resourceName** - `String` - *required* - example: `'user'`
105+
- **actionHandlers** - `Object` - *optional*
106+
- example: `{read: (req, res) => scrud.sendData(res, req.params.id)}`
107+
- **search**, **create**, **read**, **update**, **delete** - *optional*
108+
- type: `Function`
109+
- receives: (http.ClientRequest, http.ServerResponse)
110+
- default: calls `${namespace}_${resource}_${action}(IN jsonb, OUT jsonb)` PG function and responds to client with array of records or error
111+
- **beforeQuery** - `Object` - optional
112+
- example: `{beforeQuery: {delete: (req, res) => scrud.logIt(req.ip, 'debug')}}`
113+
- **search**, **create**, **read**, **update**, **delete** - *optional*
114+
- type: `Function`
115+
- receives: (http.ClientRequest, http.ServerResponse)
116+
- should return: `Promise`
117+
- default: `null`
118+
- **IMPORTANT:** If a Promise isn't returned or doesn't resolve or reject and the function doesn't close the response it will be hung
119+
- **beforeSend** - `Object` - optional
120+
- example: `{beforeSend: {read: (req, res, data) => Promise.resolve('hello')}}`
121+
- **search**, **create**, **read**, **update**, **delete** - *optional*
122+
- type: `Function`
123+
- receives: (http.ClientRequest, http.ServerResponse, data)
124+
- should return: `Promise` that resolves with data to be sent to client
125+
- default: `null`
126+
- **IMPORTANT:** If a Promise isn't returned or doesn't resolve or reject and the function doesn't close the response it will be hung. Also, the data that is sent in the promise resolution will be passed to the client, be sure it's what you intend to send.
74127

75128
### scrud.start(options)
76129
Set global options and start API server
77130

78131
***Returns:*** Promise which with resolves with http.Server
79132

80133
***Arguments:***
81-
- **options** *(Object)* - *required*
134+
- **options** - `Object` - *required*
82135
- **basePath** - *optional* - base path for APIs (i.e. `https://host.com/${basePath}/resource`)
83136
- type: `String`
84137
- default: `null`
@@ -97,6 +150,9 @@ Set global options and start API server
97150
- **logger** - *optional* - callback that will get called with any errors encountered
98151
- type: `Function` - receives (Error, String[loglevel])
99152
- default: `console.log`
153+
- **getIp** - *optional* - should client IP be added to request object
154+
- type: `Boolean`
155+
- default: `false`
100156
- **authTrans** - *optional* - Synchronous function that transforms the passed in auth object before proceeding with processing
101157
- type: `Function` - receives (Object[auth / JWT payload)])
102158
- default: `1e6`
@@ -128,17 +184,9 @@ Set global options and start API server
128184
- `update`(resource, request) - run registered update handler for resource
129185
- `delete`(resource, request) - run registered delete handler for resource
130186

131-
# usage
187+
## related
132188

133-
```js
134-
const scrud = require('scrud')
135-
const config = require('./secrets.json')
136-
137-
async function main () {
138-
await scrud.register('member')
139-
await scrud.start(config)
140-
}
141-
```
189+
[get-scrud](https://github.com/doesdev/get-scrud) - A handy client for SCRUD APIs, backed by `axios`
142190

143191
# license
144192

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scrud",
3-
"version": "4.1.4",
3+
"version": "4.2.0",
44
"description": "Super opinionated, minimalistic, PG centric API fabric",
55
"engines": {
66
"node": ">=6.0.0"

0 commit comments

Comments
 (0)