hans-sequelize-api
Advanced tools
| import { NextFunction, Request, Response } from 'express'; | ||
| import { Model, ModelCtor, ModelStatic, WhereOptions } from 'sequelize'; | ||
| export declare type Handler = (req: Request, res: Response, next?: NextFunction) => Promise<any>; | ||
| export declare type Controller = (req: Request, res: Response, next: NextFunction) => Promise<void>; | ||
| export declare type Method = 'get' | 'post' | 'put' | 'delete'; | ||
| export declare type ExtendedMethod = 'gets' | Method; | ||
| export declare type Path = string; | ||
| export declare type Controllers = Record<ExtendedMethod, Controller>; | ||
| export declare type ValidationRules = Record<string, string>; | ||
| export declare type InitializeAPIOptions = { | ||
| authMiddleware: Handler; | ||
| adminMiddleware: Handler; | ||
| validationMiddleware: (rules: ValidationRules) => Handler; | ||
| }; | ||
| export declare type SetAPIOptions<PostgreModelName extends string> = { | ||
| possibleMethods?: ExtendedMethod[]; | ||
| auth?: ExtendedMethod[]; | ||
| admin?: ExtendedMethod[]; | ||
| validation?: Partial<Record<ExtendedMethod, ValidationRules>>; | ||
| additionalMiddlewares?: { | ||
| middleware: Handler; | ||
| method: ExtendedMethod; | ||
| }[]; | ||
| defaultFields?: Partial<Record<ExtendedMethod, string[]>>; | ||
| defaultRelationFields?: Partial<Record<PostgreModelName, string[]>>; | ||
| afterMethods?: Partial<Record<ExtendedMethod, Handler | Handler[]>>; | ||
| } | void; | ||
| export declare type Sort = 'ASC' | 'DESC'; | ||
| export declare type IncludeItem = { | ||
| model: ModelStatic<any>; | ||
| attributes?: string[]; | ||
| where?: WhereOptions<any>; | ||
| order?: [string, Sort][]; | ||
| }; | ||
| export declare type GetRelationsIncludeOptions<PostgreModelName extends string> = { | ||
| relations?: PostgreModelName[]; | ||
| relationFields?: Partial<Record<PostgreModelName, string[]>>; | ||
| relationFilters?: Partial<Record<PostgreModelName, Filters>>; | ||
| relationSort?: Partial<Record<PostgreModelName, string>>; | ||
| }; | ||
| export declare type PostgreModel<ModelType extends Model = Model> = ModelCtor<ModelType>; | ||
| export declare type FilterBooleanOperator = 'and' | 'or'; | ||
| export declare type FilterEqualMethod = 'eq' | 'ne' | 'is' | 'not' | 'col'; | ||
| export declare type FilterNumericMethod = 'gt' | 'gte' | 'lt' | 'lte' | 'between' | 'notBetween'; | ||
| export declare type FilterArrayMethod = 'all' | 'in' | 'notIn'; | ||
| export declare type FilterStringMethod = 'like' | 'notLike' | 'startsWith' | 'endsWith' | 'substring' | 'iLike' | 'notILike' | 'regexp' | 'notRegexp' | 'iRegexp' | 'notIRegexp'; | ||
| export declare type FilterMatchMethod = 'any' | 'match'; | ||
| export declare type FilterAllowedMethod = FilterBooleanOperator | FilterEqualMethod | FilterNumericMethod | FilterArrayMethod | FilterStringMethod | FilterMatchMethod; | ||
| export declare type FiltersSingle = Record<string, Record<FilterAllowedMethod, string>>; | ||
| export declare type FiltersMultiply = Record<FilterBooleanOperator, Record<string, Record<FilterAllowedMethod, string>>[]>; | ||
| export declare type Filters = FiltersSingle | FiltersMultiply; | ||
| export declare type FilterOptions = { | ||
| filters?: Filters; | ||
| pageSize?: string; | ||
| page?: string; | ||
| }; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); |
| import {NextFunction, Request, Response} from 'express' | ||
| import {Model, ModelCtor, ModelStatic, WhereOptions} from 'sequelize' | ||
| export type Handler = (req: Request, res: Response, next?: NextFunction) => Promise<any> | ||
| export type Controller = (req: Request, res: Response, next: NextFunction) => Promise<void> | ||
| export type Method = | ||
| 'get' | ||
| | 'post' | ||
| | 'put' | ||
| | 'delete' | ||
| export type ExtendedMethod = | ||
| 'gets' | ||
| | Method | ||
| export type Path = string | ||
| export type Controllers = Record<ExtendedMethod, Controller> | ||
| export type ValidationRules = Record<string, string> | ||
| export type InitializeAPIOptions = { | ||
| authMiddleware: Handler | ||
| adminMiddleware: Handler | ||
| validationMiddleware: (rules: ValidationRules) => Handler | ||
| } | ||
| export type SetAPIOptions<PostgreModelName extends string> = { | ||
| possibleMethods?: ExtendedMethod[] | ||
| auth?: ExtendedMethod[] | ||
| admin?: ExtendedMethod[] | ||
| validation?: Partial<Record<ExtendedMethod, ValidationRules>> | ||
| additionalMiddlewares?: {middleware: Handler, method: ExtendedMethod}[] | ||
| defaultFields?: Partial<Record<ExtendedMethod, string[]>> | ||
| defaultRelationFields?: Partial<Record<PostgreModelName, string[]>> | ||
| afterMethods?: Partial<Record<ExtendedMethod, Handler | Handler[]>> | ||
| } | void | ||
| export type Sort = 'ASC' | 'DESC' | ||
| export type IncludeItem = { | ||
| model: ModelStatic<any> | ||
| attributes?: string[] | ||
| where?: WhereOptions<any> | ||
| order?: [string, Sort][] | ||
| } | ||
| export type GetRelationsIncludeOptions<PostgreModelName extends string> = { | ||
| relations?: PostgreModelName[] | ||
| relationFields?: Partial<Record<PostgreModelName, string[]>> | ||
| relationFilters?: Partial<Record<PostgreModelName, Filters>> | ||
| relationSort?: Partial<Record<PostgreModelName, string>> | ||
| } | ||
| export type PostgreModel<ModelType extends Model = Model> = ModelCtor<ModelType> | ||
| export type FilterBooleanOperator = | ||
| 'and' | ||
| | 'or' | ||
| export type FilterEqualMethod = | ||
| 'eq' | ||
| | 'ne' | ||
| | 'is' | ||
| | 'not' | ||
| | 'col' | ||
| export type FilterNumericMethod = | ||
| 'gt' | ||
| | 'gte' | ||
| | 'lt' | ||
| | 'lte' | ||
| | 'between' | ||
| | 'notBetween' | ||
| export type FilterArrayMethod = | ||
| 'all' | ||
| | 'in' | ||
| | 'notIn' | ||
| export type FilterStringMethod = | ||
| 'like' | ||
| | 'notLike' | ||
| | 'startsWith' | ||
| | 'endsWith' | ||
| | 'substring' | ||
| | 'iLike' | ||
| | 'notILike' | ||
| | 'regexp' | ||
| | 'notRegexp' | ||
| | 'iRegexp' | ||
| | 'notIRegexp' | ||
| export type FilterMatchMethod = | ||
| 'any' | ||
| | 'match' | ||
| export type FilterAllowedMethod = | ||
| FilterBooleanOperator | ||
| | FilterEqualMethod | ||
| | FilterNumericMethod | ||
| | FilterArrayMethod | ||
| | FilterStringMethod | ||
| | FilterMatchMethod | ||
| export type FiltersSingle = Record<string, Record<FilterAllowedMethod, string>> | ||
| export type FiltersMultiply = Record<FilterBooleanOperator, Record<string, Record<FilterAllowedMethod, string>>[]> | ||
| export type Filters = FiltersSingle | FiltersMultiply | ||
| export type FilterOptions = { | ||
| filters?: Filters | ||
| pageSize?: string | ||
| page?: string | ||
| } |
+2
-2
| import { IRouter } from 'express'; | ||
| import { InitializeAPIOptions, SetAPIOptions, PostgreModel } from './types'; | ||
| export * from './types'; | ||
| import { InitializeAPIOptions, SetAPIOptions, PostgreModel } from './interfaces'; | ||
| export * from './interfaces'; | ||
| export default class SequelizeAPI<PostgreModelName extends string> { | ||
@@ -5,0 +5,0 @@ private _postgreModels; |
+6
-1
@@ -29,3 +29,3 @@ "use strict"; | ||
| const sequelize_1 = require("sequelize"); | ||
| __exportStar(require("./types"), exports); | ||
| __exportStar(require("./interfaces"), exports); | ||
| class SequelizeAPI { | ||
@@ -65,2 +65,3 @@ constructor(postgreModels) { | ||
| middlewares.push(validationMiddleware(validation[myMethod])); | ||
| //@ts-ignore | ||
| const additionalMiddleware = additionalMiddlewares === null || additionalMiddlewares === void 0 ? void 0 : additionalMiddlewares.find(middleware => middleware.method === myMethod); | ||
@@ -77,2 +78,3 @@ if (additionalMiddleware) | ||
| } | ||
| //@ts-ignore | ||
| router[method](path, ...middlewares, controllers[myMethod], ...afterMethodsForAdd); | ||
@@ -330,2 +332,3 @@ } | ||
| const self = this; | ||
| //@ts-ignore | ||
| relations === null || relations === void 0 ? void 0 : relations.forEach(relation => { | ||
@@ -367,2 +370,3 @@ const includeItem = { model: this._postgreModels[relation] }; | ||
| obj[modelFieldName] = { | ||
| //@ts-ignore | ||
| [sequelize_1.Op[operatorName]]: filterSingle[modelFieldName][operatorName] | ||
@@ -381,2 +385,3 @@ }; | ||
| where[modelFieldName] = { | ||
| //@ts-ignore | ||
| [sequelize_1.Op[operatorName]]: filters[modelFieldName][operatorName] | ||
@@ -383,0 +388,0 @@ }; |
+1
-1
| { | ||
| "name": "hans-sequelize-api", | ||
| "version": "1.0.27", | ||
| "version": "1.0.28", | ||
| "description": "rest-api form sequelize-express stack", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
+7
-2
@@ -21,3 +21,3 @@ import {IRouter, NextFunction, Request, Response, Router} from 'express' | ||
| Sort | ||
| } from './types' | ||
| } from './interfaces' | ||
| import {error404, error500, status200, status201} from 'hans-http-handlers' | ||
@@ -27,3 +27,3 @@ import {Op, WhereOptions} from 'sequelize' | ||
| export * from './types' | ||
| export * from './interfaces' | ||
@@ -73,2 +73,3 @@ export default class SequelizeAPI<PostgreModelName extends string> { | ||
| if (validation[myMethod]) middlewares.push(validationMiddleware(validation[myMethod] as ValidationRules)) | ||
| //@ts-ignore | ||
| const additionalMiddleware = additionalMiddlewares?.find(middleware => middleware.method === myMethod) | ||
@@ -82,2 +83,3 @@ if (additionalMiddleware) middlewares.push(additionalMiddleware.middleware) | ||
| } | ||
| //@ts-ignore | ||
| router[method](path, ...middlewares, controllers[myMethod], ...afterMethodsForAdd) | ||
@@ -366,2 +368,3 @@ } | ||
| const self = this | ||
| //@ts-ignore | ||
| relations?.forEach(relation => { | ||
@@ -407,2 +410,3 @@ const includeItem: IncludeItem = {model: this._postgreModels[relation as PostgreModelName]} | ||
| obj[modelFieldName] = { | ||
| //@ts-ignore | ||
| [Op[operatorName as FilterAllowedMethod]]: (filterSingle as FiltersSingle)[modelFieldName][operatorName as FilterAllowedMethod] | ||
@@ -420,2 +424,3 @@ } | ||
| where[modelFieldName] = { | ||
| //@ts-ignore | ||
| [Op[operatorName as FilterAllowedMethod]]: (filters as FiltersSingle)[modelFieldName][operatorName as FilterAllowedMethod] | ||
@@ -422,0 +427,0 @@ } |
-120
| import {NextFunction, Request, Response} from 'express' | ||
| import {Model, ModelCtor, ModelStatic, WhereOptions} from 'sequelize' | ||
| export type Handler = (req: Request, res: Response, next?: NextFunction) => Promise<any> | ||
| export type Controller = (req: Request, res: Response, next: NextFunction) => Promise<void> | ||
| export type Method = | ||
| 'get' | ||
| | 'post' | ||
| | 'put' | ||
| | 'delete' | ||
| export type ExtendedMethod = | ||
| 'gets' | ||
| | Method | ||
| export type Path = string | ||
| export type Controllers = Record<ExtendedMethod, Controller> | ||
| export type ValidationRules = Record<string, string> | ||
| export type InitializeAPIOptions = { | ||
| authMiddleware: Handler | ||
| adminMiddleware: Handler | ||
| validationMiddleware: (rules: ValidationRules) => Handler | ||
| } | ||
| export type SetAPIOptions<PostgreModelName extends string> = { | ||
| possibleMethods?: ExtendedMethod[] | ||
| auth?: ExtendedMethod[] | ||
| admin?: ExtendedMethod[] | ||
| validation?: Partial<Record<ExtendedMethod, ValidationRules>> | ||
| additionalMiddlewares?: {middleware: Handler, method: ExtendedMethod}[] | ||
| defaultFields?: Partial<Record<ExtendedMethod, string[]>> | ||
| defaultRelationFields?: Partial<Record<PostgreModelName, string[]>> | ||
| afterMethods?: Partial<Record<ExtendedMethod, Handler | Handler[]>> | ||
| } | void | ||
| export type Sort = 'ASC' | 'DESC' | ||
| export type IncludeItem = { | ||
| model: ModelStatic<any> | ||
| attributes?: string[] | ||
| where?: WhereOptions<any> | ||
| order?: [string, Sort][] | ||
| } | ||
| export type GetRelationsIncludeOptions<PostgreModelName extends string> = { | ||
| relations?: PostgreModelName[] | ||
| relationFields?: Partial<Record<PostgreModelName, string[]>> | ||
| relationFilters?: Partial<Record<PostgreModelName, Filters>> | ||
| relationSort?: Partial<Record<PostgreModelName, string>> | ||
| } | ||
| export type PostgreModel<ModelType extends Model = Model> = ModelCtor<ModelType> | ||
| export type FilterBooleanOperator = | ||
| 'and' | ||
| | 'or' | ||
| export type FilterEqualMethod = | ||
| 'eq' | ||
| | 'ne' | ||
| | 'is' | ||
| | 'not' | ||
| | 'col' | ||
| export type FilterNumericMethod = | ||
| 'gt' | ||
| | 'gte' | ||
| | 'lt' | ||
| | 'lte' | ||
| | 'between' | ||
| | 'notBetween' | ||
| export type FilterArrayMethod = | ||
| 'all' | ||
| | 'in' | ||
| | 'notIn' | ||
| export type FilterStringMethod = | ||
| 'like' | ||
| | 'notLike' | ||
| | 'startsWith' | ||
| | 'endsWith' | ||
| | 'substring' | ||
| | 'iLike' | ||
| | 'notILike' | ||
| | 'regexp' | ||
| | 'notRegexp' | ||
| | 'iRegexp' | ||
| | 'notIRegexp' | ||
| export type FilterMatchMethod = | ||
| 'any' | ||
| | 'match' | ||
| export type FilterAllowedMethod = | ||
| FilterBooleanOperator | ||
| | FilterEqualMethod | ||
| | FilterNumericMethod | ||
| | FilterArrayMethod | ||
| | FilterStringMethod | ||
| | FilterMatchMethod | ||
| export type FiltersSingle = Record<string, Record<FilterAllowedMethod, string>> | ||
| export type FiltersMultiply = Record<FilterBooleanOperator, Record<string, Record<FilterAllowedMethod, string>>[]> | ||
| export type Filters = FiltersSingle | FiltersMultiply | ||
| export type FilterOptions = { | ||
| filters?: Filters | ||
| pageSize?: string | ||
| page?: string | ||
| } |
58357
6.33%11
22.22%1051
6.92%