1.0.2 • Published 9 days ago

identity-services v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
9 days ago

Identity Admin Services

A package for some general-purpose services

How to install

npm i identity-services

Features

  1. Easy and configurable way for caching any endpoint responses

1. Caching Service

Usage

  • Use the notation @cached above any endpoint you need the response to be cached

Example

// Just add the notation above the HTTP method
@cached()
@httpGet("/")
 public async index(@request() req: IRequest, @response() res: Response) {
  /*
  .. The endpoint body will be executed only when the data is not found in the cache
  */
  return ResponseUtils.send(res, 200, "OK", data);
 }

Add this at the top of your file

import { cached } from "identity-services/lib/cache/decorator";

cached = function (duration, async (cachedData, req) => modified cachedData)

@cached() can be passed 2 optional parameters.

1. duration (number): The validation period of the cached data in milliseconds.

  • After the given period is over, the cached data is cleared for this endpoint and the data will be fetched for the first upcoming request. Hence, it will be cached again.
  • The data will be cached forever if you pass nothing or undefined.

2. async (cachedData, req) => modified cachedData: An optional function that gives you the ability to change any value in the cached data before returning it to the user.

  • The function passes the cached data along with the request as parameters. The function should return the data to be sent to the user.
  • Real example: If you are developing an e-commerce app, and the home page is some products that are the same for all the users, except that each user has his favorite products. So the cached data is the same, but you need to alter some values in it. This can save much time instead of re-fetching the whole data from scratch.
@cached(undefined, async (cachedData, req) => {
 // Apply any updates here on the cached data.
 // It should return the data to be sent to the user.
return cachedData;
})

Cache reset

You don't have to wait for the validation period to reset a cache entry. You can delete a cache entry manually after some action is happened.

  • Real example: If you have an endpoint with /v1/videos path that fetch some videos. You cached the data forever without passing any value for the duration and you only need to clear this cache entry and fetch the data again from the database when creating any new video or updating an existing one.

VideoController.ts

@controller("/v1/videos", isAuth, defaultHeaders)
export default class VideoController extends BaseController {
 /* ..
 ..
 */

 @cached()
 @httpGet("/")
 public async index(@request() req: IRequest, @response() res: Response) {
  /*
   .. The endpoint body will be executed only when the data is not found in the cache
   */
 }
}
  • You can delete the cache entry in the resource file of this model in the after handler of the create and edit, or any customized place that makes any change in the data.

videoResource.ts

import VideoController from "@pbb/api/v1/controllers/VideoController"; 

import { deleteAllCacheEntriesByPathName } from "identity-services/lib/cache/deletionMethods";
import { CacheHelper } from "identity-services/lib/cache/pathHelper";


// Returns the path of the controller, for the above example, will return '/v1/videos'
// You can put that as a static value for example path = '/v1/videos', but it is more dynamic this way
const path = CacheHelper.getControllerPath(VideoController);

const VideoResource: IResourceFile = {
 properties: {
  /*
  ..
  */
  crudOperations: {
   create: {
    async after(req, document, currentUser, params) {
     deleteAllCacheEntriesByPathName(path); // deletes the cached data for any endpoint in the video controller
     return document;
    },
   },
   update: {
    async after(req, document, params, currentUser) {
     deleteAllCacheEntriesByPathName(path);
     return document;
    },
   },
  },
 },
};

export default VideoResource;

deleteAllCacheEntriesByPathName: Passed the path of the controller as an argument, and it deletes the cache entries for any cache entry that has the prefix /v1/videos

1.0.2

9 days ago

1.0.1

18 days ago

1.0.0

20 days ago