DEPRECATED! DOCS WILL BE UPDATED SOON
Express train to the response
Skip writing code for CRUD operations while using sequelize
or
sequelize-typescript
Abbreviations
req.body
refers to Request objectsbody
property that is passed to the middlewaresreq.params
refers to Request objectsparams
property that is passed to the middlewaresreq.query
refers to Request objectsquery
property that is passed to the middlewarespk
refers to primary keys in databasesfk
refers to foreign keys in the databases
Design
Before using this package
Make sure you follow semantic structure of a RESTful API
e.g
PUT /api/users/:userId
if you were to use updateRecord
for such route to
handle it. updateRecord
relies on :userId
param.
It can accept the key name but there is no way to
pass the value for userId
directly.
another example
POST /api/users
It's intuitive to assume that this method on this
route will create a user, createRecord
would
rely on req.body
to create the record.
Function dependencies for each function are explained in detail below
createRecord()
Creates a record with the passed model in the target table
Signature
createRecord =
Depends On
req.body
pass the required fields via body of the response
Passes
res.locals[LOCAL_CREATED]
newRecord: Record<string, any>;
getPaginatedResults()
Tries to paginate the records if params provided else does nothing
Signature
getPaginatedResults = M extends TSSequelizeModel, K extends SequelizeModel model: // Model instance to pass | & typeof TSSequelizeModel | & typeof SequelizeModel, }:
Depends On
req.query
page size and page number values are obtained from the query object, if custom names are passed it'll try to find them too it won't look for them in the req.body ever
Passes
res.locals[LOCAL_PAGINATED]
Record<string, any>[]
updateRecord()
See notice first, isn't safe on its own
Signature
// Updates the record, relies on `req.params` and// `req.body` updateRecord =: )
Depends On
req.body
where new fields must be provided to update th existing record.req.params
where foreign and / or primary keys are passed in order to find the target record
Passes
res.local[LOCAL_UPDATED]
updatedRecord: Record<string, any> | null;
affectedRowCount: number | undefined;
updatedRecordId: number | string | undefined;
deleteRecord()
See notice first, isn't safe on its own
DELETE /api/users/:userId/jobs/:jobId
This method with this url speaks the following words
Hello, Imma delete job that is identied with :jobId
that belongs to user that is identified with :userId
deleteRecordJobModel /* Some sequelize model that represents Jobs table */, ;
if it was like the following uri with the DELETE method
DELETE /api/users/:userId/jobs/:id
deleteRecordJobModel /* Some sequelize model that represents Jobs table */, ;
Has two modes either completely deletes the record
-- which is undoable -- or updates the status of the
record, former one is called hard
, latter is called soft
defaults to soft
Signature
deleteRecord =: )
Depends On
req.params
to obtain the foreign and / or primary keys
Passes
res.local[LOCAL_DELETED]
deletedRecord: Record<string, any> | null;
affectedRowCount: number | undefined;
deletedRecordId: string | number | undefined;
Notice {#notice}
DOT! Do one thing, I am lazy and dumb to find the way to make sure that indeed user is deleting or updating their own records, I encourage you to apply DOT rule so you should handle the authorization in a seperate middleware to confirm that user can indeed reach to this middleware