@autofleet/sheilta 2.2.1-beta-576f10d3.4 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +1 -1
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +183 -87
- package/lib/index.d.ts +183 -87
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/package.json +4 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,112 +1,208 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { LoggerInstanceManager } from
|
|
1
|
+
import { Op, literal } from "sequelize";
|
|
2
|
+
import { LoggerInstanceManager } from "@autofleet/logger";
|
|
3
|
+
import { Handler } from "express";
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
//#region src/operators/index.d.ts
|
|
5
6
|
|
|
7
|
+
declare const formatOperators: (sequelize?: {
|
|
8
|
+
Op: typeof Op;
|
|
9
|
+
}) => {
|
|
10
|
+
[k: string]: symbol;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/formatter/jsonAttributesFormater.d.ts
|
|
14
|
+
declare const ComputedActions: {
|
|
15
|
+
readonly length: "length";
|
|
16
|
+
};
|
|
17
|
+
type JsonSelectAttribute = {
|
|
18
|
+
columnName: string;
|
|
19
|
+
keys: string[];
|
|
20
|
+
alias?: string;
|
|
21
|
+
};
|
|
22
|
+
type JsonComputedAttribute = {
|
|
23
|
+
columnName: string;
|
|
24
|
+
action: keyof typeof ComputedActions;
|
|
25
|
+
alias: string;
|
|
26
|
+
};
|
|
27
|
+
type JsonAttributes = {
|
|
28
|
+
select?: JsonSelectAttribute[];
|
|
29
|
+
computed?: JsonComputedAttribute[];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Return a single array combining selected and computed attributes.
|
|
33
|
+
* Format: [[literal, alias], [literal, alias], ...]
|
|
34
|
+
*/
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/formatter/index.d.ts
|
|
6
37
|
type OrderItem = string | [string, string];
|
|
7
38
|
type SequelizeOrder = string | OrderItem[];
|
|
8
39
|
type FormatPayloadOptions = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
40
|
+
includeRawPayload?: boolean;
|
|
41
|
+
literalAttributes?: LiteralAttribute[];
|
|
42
|
+
DBFormatter?: any;
|
|
43
|
+
skipSearchTermFormat?: boolean;
|
|
44
|
+
additionalAllowedAttributes?: string[];
|
|
14
45
|
};
|
|
15
46
|
type ConditionWithOperator = {
|
|
16
|
-
|
|
17
|
-
|
|
47
|
+
operator: string;
|
|
48
|
+
value: string;
|
|
18
49
|
};
|
|
19
50
|
type ConditionValue = ConditionWithOperator | ConditionWithOperator[] | string | string[];
|
|
20
51
|
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
52
|
+
* Generates replacements for the given conditions.
|
|
53
|
+
*
|
|
54
|
+
* @param conditions - The conditions to generate replacements for.
|
|
55
|
+
* @returns The replacements object.
|
|
56
|
+
*/
|
|
26
57
|
declare const generateFilterReplacements: (conditions: Record<string, ConditionValue>) => Record<string, string>;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
attributes?: any;
|
|
34
|
-
searchTerm?: any;
|
|
35
|
-
jsonAttributes?: {};
|
|
36
|
-
}, model?: any, options?: FormatPayloadOptions) => {
|
|
37
|
-
externalQueryValues: Record<string, unknown>;
|
|
38
|
-
attributes: any[] | {
|
|
39
|
-
include: any[];
|
|
40
|
-
};
|
|
41
|
-
query: Record<string, unknown>;
|
|
42
|
-
order: SequelizeOrder[];
|
|
43
|
-
page: number;
|
|
44
|
-
perPage: number;
|
|
45
|
-
include: any;
|
|
46
|
-
scopes: (string | {
|
|
47
|
-
method: (string | {
|
|
48
|
-
replacementsMap: Record<string, string>;
|
|
49
|
-
scopeValue: any;
|
|
50
|
-
})[];
|
|
51
|
-
})[];
|
|
52
|
-
};
|
|
58
|
+
/**
|
|
59
|
+
* Generates replacements for the given order array.
|
|
60
|
+
*
|
|
61
|
+
* @param order - The order array to generate replacements for.
|
|
62
|
+
* @returns The replacements object.
|
|
63
|
+
*/
|
|
53
64
|
|
|
54
|
-
|
|
55
|
-
|
|
65
|
+
interface Include {
|
|
66
|
+
association?: string;
|
|
67
|
+
model?: string;
|
|
68
|
+
required?: boolean;
|
|
69
|
+
include?: Include[];
|
|
70
|
+
}
|
|
71
|
+
interface FormatPayloadData {
|
|
72
|
+
order?: string[];
|
|
73
|
+
page?: number;
|
|
74
|
+
perPage?: number;
|
|
75
|
+
include?: (string | Include)[];
|
|
76
|
+
query?: Record<string, unknown>;
|
|
77
|
+
attributes?: string[];
|
|
78
|
+
searchTerm?: string;
|
|
79
|
+
jsonAttributes?: JsonAttributes;
|
|
80
|
+
}
|
|
81
|
+
type Literal = ReturnType<typeof literal>;
|
|
82
|
+
interface FormattedPayload {
|
|
83
|
+
query: Record<string, unknown>;
|
|
84
|
+
externalQueryValues: Record<string, unknown>;
|
|
85
|
+
attributes: (SequelizeOrder | (string | Literal)[])[] | {
|
|
86
|
+
include: (SequelizeOrder | (string | Literal)[])[];
|
|
87
|
+
};
|
|
88
|
+
order: SequelizeOrder[];
|
|
89
|
+
page: number;
|
|
90
|
+
perPage: number;
|
|
91
|
+
include: any;
|
|
92
|
+
scopes: (string | {
|
|
93
|
+
method: (string | {
|
|
94
|
+
replacementsMap: Record<string, string>;
|
|
95
|
+
scopeValue: any;
|
|
96
|
+
})[];
|
|
97
|
+
})[];
|
|
98
|
+
}
|
|
99
|
+
declare const formatPayload: ({
|
|
100
|
+
order,
|
|
101
|
+
page,
|
|
102
|
+
perPage,
|
|
103
|
+
include,
|
|
104
|
+
query,
|
|
105
|
+
attributes,
|
|
106
|
+
searchTerm,
|
|
107
|
+
jsonAttributes
|
|
108
|
+
}: FormatPayloadData, model?: any, options?: FormatPayloadOptions) => FormattedPayload;
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/middleware/index.d.ts
|
|
111
|
+
type literal$1 = any;
|
|
112
|
+
type LiteralQuery = (literal$1 | string)[] | literal$1;
|
|
56
113
|
type LiteralAttribute = {
|
|
57
|
-
|
|
58
|
-
|
|
114
|
+
attribute: string;
|
|
115
|
+
literal: LiteralQuery;
|
|
59
116
|
};
|
|
60
117
|
type MiddlewareValidationOption = {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
118
|
+
literalAttributes?: LiteralAttribute[];
|
|
119
|
+
enrichmentAttributes?: string[];
|
|
120
|
+
additionalAllowedAttributes?: string[];
|
|
121
|
+
logger?: LoggerInstanceManager;
|
|
65
122
|
};
|
|
66
|
-
type ReqKeys =
|
|
123
|
+
type ReqKeys = "body" | "query";
|
|
67
124
|
/** consider using @see {@link queryHandler} directly */
|
|
68
125
|
declare const queryValidationMiddleware: (model: any, options?: MiddlewareValidationOption, inner?: ReqKeys) => Handler;
|
|
69
126
|
/** consider using @see {@link queryHandler} directly */
|
|
70
127
|
declare const queryFormatMiddleware: (model: any, options?: FormatPayloadOptions, inner?: ReqKeys) => Handler;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/validations/index.d.ts
|
|
130
|
+
interface PayloadValidationData {
|
|
131
|
+
query?: object;
|
|
132
|
+
order?: string[];
|
|
133
|
+
attributes?: string[];
|
|
134
|
+
include?: any[];
|
|
135
|
+
page?: number;
|
|
136
|
+
perPage?: number;
|
|
137
|
+
enrichments?: string[] | {
|
|
138
|
+
[enrichmentName: string]: {
|
|
139
|
+
exclude?: string[];
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
group?: string[];
|
|
143
|
+
jsonAttributes?: {
|
|
144
|
+
select?: {
|
|
145
|
+
columnName: string;
|
|
146
|
+
keys: string[];
|
|
147
|
+
alias?: string;
|
|
148
|
+
}[];
|
|
149
|
+
computed?: {
|
|
150
|
+
columnName: string;
|
|
151
|
+
action: "length";
|
|
152
|
+
alias: string;
|
|
153
|
+
}[];
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
declare const validatePayload: ({
|
|
157
|
+
query,
|
|
158
|
+
order,
|
|
159
|
+
attributes,
|
|
160
|
+
include,
|
|
161
|
+
page,
|
|
162
|
+
perPage,
|
|
163
|
+
enrichments,
|
|
164
|
+
group,
|
|
165
|
+
jsonAttributes
|
|
166
|
+
}: PayloadValidationData, model?: any, options?: MiddlewareValidationOption) => boolean;
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/handler/index.d.ts
|
|
84
169
|
interface QueryValues extends ReturnType<typeof formatPayload> {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
170
|
+
enrichments?: string[] | Record<string, {
|
|
171
|
+
exclude: string[];
|
|
172
|
+
}>;
|
|
173
|
+
distinct: boolean;
|
|
174
|
+
searchTerm?: string;
|
|
89
175
|
}
|
|
90
176
|
interface QueryHandlerOptions {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
177
|
+
/** The sequelize model too which querying abilities are added. */
|
|
178
|
+
model: any;
|
|
179
|
+
/** Optional settings for validation. */
|
|
180
|
+
validationOptions?: Omit<MiddlewareValidationOption, "logger">;
|
|
181
|
+
/** Optional settings for payload formatting */
|
|
182
|
+
formatOptions?: FormatPayloadOptions;
|
|
183
|
+
logger: LoggerInstanceManager;
|
|
184
|
+
/** The name of model to be printed in logs. defaults to `model`s constructor name. */
|
|
185
|
+
modelName?: string;
|
|
186
|
+
/** Sequelize scopes of the model to be used within the query. @example ['userScope'] */
|
|
187
|
+
additionalScopes?: string[];
|
|
188
|
+
/** Callback to allow modifying the query values prior to querying the DB */
|
|
189
|
+
modifyQueryValues?: (queryValues: QueryValues) => QueryValues;
|
|
190
|
+
/** Optional callback to modify endpoint's response based on the DBs response. */
|
|
191
|
+
onRowsRetrieved?: (data: {
|
|
192
|
+
rows: any[];
|
|
193
|
+
count: number;
|
|
194
|
+
}, queryValues: QueryValues) => any;
|
|
109
195
|
}
|
|
110
|
-
declare const queryHandler: ({
|
|
111
|
-
|
|
196
|
+
declare const queryHandler: ({
|
|
197
|
+
model,
|
|
198
|
+
logger,
|
|
199
|
+
validationOptions,
|
|
200
|
+
formatOptions,
|
|
201
|
+
modelName,
|
|
202
|
+
additionalScopes,
|
|
203
|
+
modifyQueryValues,
|
|
204
|
+
onRowsRetrieved
|
|
205
|
+
}: QueryHandlerOptions) => Handler;
|
|
206
|
+
//#endregion
|
|
112
207
|
export { type LiteralAttribute, type MiddlewareValidationOption, formatOperators, generateFilterReplacements, queryFormatMiddleware, queryHandler, queryValidationMiddleware, validatePayload };
|
|
208
|
+
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import
|
|
1
|
+
import{Op as e,literal as t}from"sequelize";import n from"@autofleet/logger";import{BadRequest as r,UnexpectedError as i,handleError as a}from"@autofleet/errors";import o from"joi";import{customFields as s}from"@autofleet/common-types";import{randomInt as c}from"node:crypto";const l=[`eq`,`ne`,`gte`,`gt`,`lte`,`lt`,`not`,`in`,`notIn`,`is`,`like`,`iLike`,`notLike`,`between`,`and`,`or`,`overlap`,`contains`],u={$eq:`=`,$ne:`!=`,$gte:`>=`,$gt:`>`,$lte:`<=`,$lt:`<`,$not:`NOT`,$in:`IN`,$notIn:`NOT IN`,$is:`IS`,$like:`LIKE`,$iLike:`ILIKE`,$notLike:`NOT LIKE`,$and:`AND`,$or:`OR`},d=(t={Op:e})=>{let{Op:n}=t;return Object.fromEntries(l.map(e=>[`${`$`+e}`,n[e]]))},f=e=>`\$${e}\$`,p=(e,t)=>e.includes(`.`)&&t.includes(e.split(`.`,1)[0]),m=(e,t)=>{let n=e;return e.includes(`-`)&&([,n]=n.split(`-`,2)),p(n,t)&&([n]=n.split(`.`,1)),n},h=e=>e.includes(`-`),g=e=>{throw new r([Error(e)])},_=e=>e.split(`.`,2)[1],v=(e=5)=>Array.from({length:e},()=>`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`.charAt(c(52))).join(``),y=(e,t)=>Object.fromEntries(t.map(t=>[t,e[t]])),b={length:`length`};function x(e){return e.replace(/(?!^)[A-Z]/g,e=>`_${e.toLowerCase()}`)}function S(e){switch(e.action){case b.length:{let n=`jsonb_array_length(${x(e.columnName)})`;return[t(n),e.alias]}default:return e.action,[]}}function C(e){return e.map(e=>S(e))}function w(e){return e.map(e=>{let n=x(e.columnName),r=`json_build_object(${e.keys.map(e=>`'${e}', ${n} -> '${e}'`).join(`, `)})`,i=e.alias||e.columnName;return[t(r),i]})}function T({select:e=[],computed:t=[]}={}){let n=w(e),r=C(t);return[...n,...r]}const E=`DESC`,D=`customFields.`,{CUSTOM_FIELDS_FILTER_SCOPE:O,CUSTOM_FIELDS_SORT_SCOPE:k}=s,ee=e=>[`string`,`number`].includes(typeof e)||Array.isArray(e)?e:Object.entries(e).map(([e,t])=>({operator:u[e],value:t})),te=(e,t={})=>{let{literalAttributes:n=[],DBFormatter:r=void 0}=t,[i,a]=e.reduce((e,t)=>{let[i,a=`ASC`]=Array.isArray(t)?t:[t],o=n?.find(e=>e.attribute===i);return o?(e[1].push(o.literal),e[0].push([r?r(`"${o.attribute}" ${a}`):`${o.attribute} ${a}`])):e[0].push(t),e},[[],[]]);return[i,a]},A=e=>{let t={};return Object.entries(e).forEach(([e,n])=>{let r=v();if(t[r]=e.split(D,2)[1],Array.isArray(n))n.forEach(e=>{let n=v();t[n]=typeof e==`string`?e:e.value});else if(typeof n==`string`||typeof n==`number`){let e=v();t[e]=n}else if(n?.operator){let e=v();t[e]=n.value}}),t},ne=e=>{let t={};return e.forEach(e=>{if(e.startsWith(D)){let n=v();t[n]=e.split(D,2)[1]}else if(e.substring(1).startsWith(D)){let n=v();t[n]=e.substring(1).split(D,2)[1]}}),t},j=(e,t)=>({...ne(e),...A(t)}),M=({order:e,associationModels:t=[],replacementsMap:n={}})=>{let r=[],i=new Map;return e.forEach(e=>{if([e,e.substring(1)].some(e=>e.startsWith(D))){i.has(k)||i.set(k,{});let t=e.split(D,2)[1];i.get(k)[t]=h(e)?E:`ASC`;return}let n=[m(e,t)],a=h(e),o=p(a?e.split(`-`,2)[1]:e,t);o&&n.push(_(e)),a&&n.push(E),r.push(n)}),{formattedOrders:r,replacementsMap:n,orderScopes:Array.from(i.entries()).map(([e,t])=>t?{method:[e,{replacementsMap:n,scopeValue:t}]}:e)}},N=e=>e||1,P=e=>e||20,F=(e,t={})=>{let n=e.map(e=>{let n=t[typeof e==`string`?e:e.association||e.model];return{...typeof e!=`string`&&e,association:n,required:typeof e==`string`||e.required!==!1,...typeof e!=`string`&&e.include&&{include:F(e.include,n?.target?.associations)}}});return n=n.map(({model:e,...t})=>t),n},I=(e,t,n,r=[])=>{let i={},a={},o=new Map;Object.entries(e).forEach(([e,n])=>{if(e.startsWith(D)){o.has(O)||o.set(O,{});let t=e.split(D,2)[1];o.get(O)[t]=ee(n);return}if(r.includes(e)){a[e]=n;return}let s=p(e,t)?f(e):e;i[s]=n});let s=Array.from(o.entries()).map(([e,t])=>t?{method:[e,{replacementsMap:n,scopeValue:t}]}:e);return{formattedQuery:i,externalQueryValues:a,formattedScopes:s}},re=(e,t,n)=>({$and:e.split(` `).map(e=>({$or:t.filter(e=>n[e].type.key===`STRING`).map(t=>({[t]:{$iLike:`%${e}%`}}))}))}),ie=({order:e=[],page:t=1,perPage:n=20,include:r=[],query:i={},attributes:a=null,searchTerm:o=null,jsonAttributes:s={}},c,l)=>{let u=j(e,i),d=Object.keys(c?.associations||{}),{formattedOrders:f,orderScopes:p}=M({order:[...e,`id`],associationModels:d,replacementsMap:u}),[m,h]=te(f,l),g=T(s),_=[...h,...a||[],...g],v=a?.length?_:{include:_},y=F(r,c?.associations),b=N(t),x=P(n),S=I(i,d,u,l?.additionalAllowedAttributes),{formattedScopes:C,externalQueryValues:w}=S,{formattedQuery:E}=S;if(o&&!l?.skipSearchTermFormat){let e=a?.length?a:Object.keys(c.rawAttributes||{}),t=re(o,e,c.rawAttributes);E=!E||Object.keys(E).length===0?t:{$and:[E,t]}}return{query:E,order:m,page:b,perPage:x,include:y,scopes:[...C,...p],...v&&{attributes:v},...Object.keys(w).length>0&&{externalQueryValues:w}}};var ae=ie;const oe=e=>l.includes(e.split(`$`,2)[1]),L=(e,t=[],n=[],r=[])=>{let i=e.startsWith(`$`)&&e.endsWith(`$`)?e.slice(1,-1):e;return[...t,...n].includes(i.includes(`.`)?i.split(`.`,1)[0]:i)||r.includes(i)},R=(e,t,n,r={})=>{let i=h(e);i&&e[0]!==`-`&&g(`- must be only at the beginning of the word`);let a=i?e.split(`-`,2)[1]:e,o=p(a,n),s=m(e,n),c=r?.literalAttributes?.map(e=>e.attribute)?.includes(a);!o&&s.includes(`.`)&&([s]=s.split(`.`,1)),t.includes(s)||o||c||g(`${e} is invalid. isLiteralAttribute: ${c}`)},z=(e,t)=>{t.includes(e)||g(`${e} is invalid`)},B=(e,t,n=[],r={})=>{e.forEach(e=>R(e,t,n,r))},V=(e,t)=>{e.forEach(e=>z(e,t))},H=(e,t)=>{let n=[...e.select?.map(e=>e.columnName)||[],...e.computed?.map(e=>e.columnName)||[]];V(n,t)},U=(e,t)=>{let n=Array.isArray(e)?e:Object.keys(e);if(!n?.length)return;let r=n.find(e=>!t?.enrichmentAttributes?.includes(e));r&&g(`enrichment attribute ${r} is invalid`)},W=(e,t,n=[],r=[])=>{Object.entries(e).forEach(([e,i])=>{Array.isArray(i)?i[0]&&typeof i[0]==`object`&&i.map(e=>W(e,t,n,r)):oe(e)||L(e,t,n,r)?i&&typeof i==`object`&&W(i,t,[],r):g(`invalid key: ${e}`)})},G=({page:e,perPage:t})=>{e<1&&g(`Page must be greater than 0`),(t>100||t<1)&&g(`PerPage must be between 1 to 100`)},K=(e,t)=>{let n=Object.keys(t);e.forEach(e=>{L(e.model,n);let r=t[e.model]?.target;r||g(`model not found in associations`);let{rawAttributes:i}=r,a=Object.keys(i);e.where&&W(e.where,a),e.order&&B(e.order,a),e.attributes&&V(e.attributes,a),[null,void 0,!0,!1].includes(e.required)||g(`include.required must be a boolean`)})},q=({query:e={},order:t=[],attributes:n=[],include:r=[],page:i=1,perPage:a=20,enrichments:o=[],group:s=[],jsonAttributes:c={}},l,u={})=>{let d=Object.keys(l.rawAttributes),f=Object.keys(l?.associations||{});return!n||n.length===0?n=d:V(n,d),B(t,d,f,u),W(e,d,f,u.additionalAllowedAttributes),U(o,u),H(c,d),Array.isArray(s)||g(`group must be an array`),r.length&&typeof r==`object`?K(r,l?.associations):r&&typeof r!=`object`&&g(`include must be an array`),G({page:i,perPage:a}),!0},{object:J,string:Y,number:X,any:se,array:Z,alternatives:ce}=o.types(),le=n(),ue=J.keys({query:J,attributes:Z.items(Y),order:Z.items(Y),page:X,perPage:X,include:Z.items(se),searchTerm:Y,group:Z.items(Y),enrichments:ce.try(Z.items(Y),J.pattern(Y,{exclude:Z.items(Y)})),jsonAttributes:o.object({select:o.array().items(o.object({columnName:o.string().required(),keys:o.array().items(o.string().required()).required(),alias:o.string().optional()})).default([]),computed:o.array().items(o.object({columnName:o.string().required(),action:o.string().valid(...Object.values(b)).required(),alias:o.string().required()})).default([])}).default({})}),Q=(e,t,n={})=>{let{query:i,attributes:a,order:o,page:s,perPage:c,include:l,group:u,enrichments:d,jsonAttributes:f}=t,p=ue.validate(t);if(p.error)throw new r([p.error],null);q({query:i,attributes:a,order:o,page:s,perPage:c,include:l,enrichments:d,group:u,jsonAttributes:f},e,n)},de=(e,t={},n=`body`)=>(r,i,o)=>{try{Q(e,r[n],t),o()}catch(e){let{query:o,attributes:s,order:c}=r[n];a(e,i,{logger:t.logger||le,message:`error in query middleware`,payload:{error:e,query:o,attributes:s,order:c}})}},$=(e,t,n={})=>{let{order:r,page:i,perPage:a,include:o,query:s,attributes:c,searchTerm:l,jsonAttributes:u}=t,{query:d,externalQueryValues:f,order:p,page:m,perPage:h,include:g,scopes:_,attributes:v}=ae({query:s,order:r,page:i,perPage:a,include:o,attributes:c,searchTerm:l,jsonAttributes:u},e,n);t.query=d,t.externalQueryValues=f,t.order=p,t.attributes=v,t.page=m,t.perPage=h,t.include=g,t.scopes=_,n.includeRawPayload&&(t.rawPayload={order:r,page:i,perPage:a,include:o,query:s,attributes:c,searchTerm:l})},fe=(e,t={},n=`body`)=>(r,i,a)=>{$(e,r[n],t),a()},pe=({model:e,logger:t,validationOptions:n,formatOptions:r,modelName:o=e.constructor?.name,additionalScopes:s=[],modifyQueryValues:c,onRowsRetrieved:l})=>async(u,d)=>{try{Q(e,u.body,{...n,logger:t})}catch(e){let n=y(u.body,[`query`,`order`,`attributes`]);a(e,d,{logger:t,message:`error in query endpoint`,payload:n});return}try{$(e,u.body,r);let n=Object.assign(y(u.body,[`query`,`externalQueryValues`,`order`,`attributes`,`page`,`perPage`,`include`,`scopes`,`enrichments`]),{distinct:!0});t.info(`querying ${o}`,{queryValues:n});let i=c?.(n)??n,{scopes:a=[],query:f,perPage:p,page:m,enrichments:h,externalQueryValues:g,..._}=i,v=await e.scope([...s,...a]).findAndCountAll({where:f,limit:p,offset:(m-1)*p,..._});if(!v.rows.length||!l){d.json(v);return}let b=await l(v,i);d.json(b)}catch(e){a(new i(e),d,{logger:t,message:`Error while querying ${o}`,payload:{query:u.body}})}};export{d as formatOperators,A as generateFilterReplacements,fe as queryFormatMiddleware,pe as queryHandler,de as queryValidationMiddleware,q as validatePayload};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|