Express plugin
Express plugin
Section titled “Express plugin”Autogenerate REST APIs for the entities with Express
Section titled “Autogenerate REST APIs for the entities with Express”The @uql/core package includes an express plugin to automatically generate REST APIs for your entities. It is accessible via the @uql/core/express sub-path.
- Initialize the
querierMiddlewaremiddleware in your server code to generate REST APIs for your entities
import * as express from 'express';import { querierMiddleware } from '@uql/core/express';import { User, Product, Category } from './shared/models/index.js';
const app = express();
app .use( '/api', // this will generate REST APIs for the entities. querierMiddleware({ /** * allow specify which entities will be used by the middleware * (all of them are used by default) */ include: [User, Product, Category], /** * allow excluding some entities of being used by the middleware * (all of them are used by default) */ exclude: [User], /** * Allow augment any kind of request before it runs */ pre(req, meta) { console.log(`${req.method} ${req.url} ${meta.name}`); if (req.method === 'POST' && meta.entity === Product) { console.log(`A new product is going to be created!`); } }, /** * Allow augment a saving request (POST, PATCH, PUT) before it runs */ preSave(req, meta) { req.body.creatorId = req.identify.userId; }, /** * Allow augment a filtering request (GET, DELETE) before it runs */ preFilter(req, meta) { req.query.$where.creatorId = req.identify.userId; }, }) );