Skip to main content

Using CRUD Interface

Often times, there is a well-defined data model declared in the server logic that may allow users to do a simple CRUD operations without mounting all HTTP methods and paths (e.g. GET, POST, PATCH, DELETE, etc.).

To integrate HttpServerShim with upstream data model, you can simply decorate with the convenience decorator HTTP.CRUD to handle all http methods with a single standalone handler.

import { httpOp, httpParamCrud, httpReturnCrud, Upstream } from 'ts-basis/nodejs'

export class MyAppServer extends HttpServerShim {
...

@HTTP.CRUD(`/my-object-path/:objectId`)
myCrudFunction = myCrudFunction

...
}

// One function to handle all CRUD op
export const myCrudFunction = httpOp(
{
params: httpParamCrud(MyModelClass),
returns: httpReturnCrud<{ data: string }>(),
},
async (op, env) => {
return await Upstream.httpCrud(
op.method,
MyModelClass,
'objectId', // CRUD primary key (e.g. unique id,)
{ ...op.params },
)
},
)