Egose Documentation
website·Indexed 30 days ago
https://egose.github.io/Documentation for the Egose ecosystem, including egose-acl for access control and CRUD lifecycle management, egose-adapter-js for model service interactions, and egose-swagger and egose-deco for backend configuration and installation.
What's inside Egose
- @egose/acl is a package that provides REST API endpoints for Mongoose data models in Express routes. It secures backend databases by decorating Mongoose queries with access control lists (ACLs) to restrict access to sensitive data. It also supports dynamic, Mongoose-like query options from the frontend, allowing developers to manipulate and query data using familiar syntax while maintaining security boundaries.
Overview of @egose/adapter-js
The@egose/adapter-jspackage is a TypeScript/JavaScript client adapter designed to facilitate communication with Egose backend API endpoints. It acts as the bridge between the frontend application and the Egose backend services.Overview of @egose/deco Annotations
The@egose/decopackage provides TypeScript Decorators used to define@egose/aclconfigurations and options. These annotations are applied to classes, methods, parameters, and properties to streamline authorization logic and improve the organization and maintainability of backend codebases.Overview of Model Router Middleware Functions
Middleware functions (hooks) in egose-acl execute during the lifecycle of asynchronous data model CRUD operations. They allow developers to intercept and modify requests and responses for tasks like validation and data transformation without altering core application logic.
Critical Implementation Note: To access the
Express Request Objectvia thethiskeyword inside a middleware function, you must use regular functions (function() {}) instead of arrow functions, as arrow functions do not have their ownthiscontext.Overview of @egose/swagger
The@egose/swaggerpackage provides support for the OpenAPI Specification (formerly known as Swagger Specification) to describe REST APIs. It is specifically designed to describe the API endpoints generated by the@egose/aclmodel routers.Limit blog post length in list views using truncate comments
To limit the amount of content displayed for a blog post in the list view, insert the<!-- truncate -->HTML comment within the post content. Content appearing after this comment will be hidden in the list view and only visible on the full post page.This text will be visible in the list view. <!-- truncate --> This text will only be visible on the full blog post page.Simplify authorization logic using TypeScript decorators
Use TypeScript decorators to improve code organization and readability when defining configurations and options for authorization logic. This approach allows for a more declarative way to handle permissions and access rules.Bind Model Router to Express
After configuring the model router, bind itsroutesproperty to an Express router to expose the CRUDL API endpoints.import express from 'express'; const app = express(); const router = express.Router(); // Create a model router for the User model const userRouter = egose.createRouter('User', { basePath: '/users' }); // Bind the User model router routes to the Express router router.use('/', userRouter.routes); app.use('/api', router);Bootstrap @egose/deco in an Express server
To activate the defined router modules and expose the API endpoints, use theEgoseFactory.bootstrapmethod, passing the module class and the Express application instance.import express from 'express'; import { EgoseFactory } from '@egose/deco'; import { RoutersModule } from './routers.module'; const app = express(); EgoseFactory.bootstrap(RoutersModule, app);Install @egose/adapter-js
Install the @egose/adapter-js package using npm, Yarn, or pnpm.npm install @egose/adapter-jsInitialize a sub-document reference in egose-adapter-js
To perform CRUD operations on sub-documents, use theid()method on a model service to target a specific parent document, and then call thesubs()method specifying the name of the sub-document collection (array) you wish to manipulate.import egoseAdapter from '@egose/adapter-js'; const adapter = egoseAdapter.createAdapter({ baseURL: 'http://127.0.0.1:3000/api' }); interface Status { name: string; active: boolean; } interface User { name: string; statusHistory: Status[]; [key: string]: any; } const userService = adapter.createModelService<User>({ modelName: 'User', basePath: 'users' }); // Target a specific user by ID and access the 'statusHistory' sub-document collection const sub = userService.id('79c8c567b05e986509d55733a').subs('statusHistory');Initialize egose-adapter-js and create a Model Service
The@egose/adapter-jslibrary allows developers to create a service that binds toegose-aclModel API endpoints. To set this up, first create an adapter instance by specifying thebaseURLof the API. Then, use thecreateModelServicemethod, providing themodelNameand thebasePathfor the specific resource you wish to manage.import egoseAdapter from '@egose/adapter-js'; // Initialize the adapter with the API base URL const adapter = egoseAdapter.createAdapter({ baseURL: 'http://127.0.0.1:3000/api' }); // Define the interface for the model interface User { name: string; role: string; public: boolean; [key: string]: any; } // Create a model service for the 'User' model at the 'users' path const userService = adapter.createModelService<User>({ modelName: 'User', basePath: 'users' });