@imqueue/pg-sequelize 4.2.0
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/CHANGELOG.md +50 -0
- package/CONTRIBUTING.md +58 -0
- package/CONTRIBUTION-TERMS.md +79 -0
- package/LICENSE +585 -0
- package/README.md +94 -0
- package/SECURITY.md +41 -0
- package/index.d.ts +86 -0
- package/index.js +87 -0
- package/package.json +75 -0
- package/src/BaseModel.d.ts +695 -0
- package/src/BaseModel.js +917 -0
- package/src/Graph.d.ts +215 -0
- package/src/Graph.js +257 -0
- package/src/decorators/AssociatedWith.d.ts +94 -0
- package/src/decorators/AssociatedWith.js +71 -0
- package/src/decorators/ColumnIndex.d.ts +206 -0
- package/src/decorators/ColumnIndex.js +98 -0
- package/src/decorators/CreatedBy.d.ts +27 -0
- package/src/decorators/CreatedBy.js +84 -0
- package/src/decorators/DeletedBy.d.ts +30 -0
- package/src/decorators/DeletedBy.js +89 -0
- package/src/decorators/DynamicView.d.ts +124 -0
- package/src/decorators/DynamicView.js +113 -0
- package/src/decorators/Emittable.d.ts +39 -0
- package/src/decorators/Emittable.js +42 -0
- package/src/decorators/NullableIndex.d.ts +77 -0
- package/src/decorators/NullableIndex.js +64 -0
- package/src/decorators/UpdatedBy.d.ts +27 -0
- package/src/decorators/UpdatedBy.js +105 -0
- package/src/decorators/View.d.ts +87 -0
- package/src/decorators/View.js +93 -0
- package/src/decorators/index.d.ts +32 -0
- package/src/decorators/index.js +33 -0
- package/src/helpers/index.d.ts +24 -0
- package/src/helpers/index.js +25 -0
- package/src/helpers/js.d.ts +61 -0
- package/src/helpers/js.js +88 -0
- package/src/helpers/query.d.ts +445 -0
- package/src/helpers/query.js +1095 -0
- package/src/index.d.ts +162 -0
- package/src/index.js +223 -0
- package/src/types/DataPage.d.ts +52 -0
- package/src/types/DataPage.js +2 -0
- package/src/types/FieldsInput.d.ts +41 -0
- package/src/types/FieldsInput.js +75 -0
- package/src/types/FilterInput.d.ts +136 -0
- package/src/types/FilterInput.js +291 -0
- package/src/types/JsonObject.d.ts +16 -0
- package/src/types/JsonObject.js +50 -0
- package/src/types/OrderByInput.d.ts +45 -0
- package/src/types/OrderByInput.js +80 -0
- package/src/types/PaginationInput.d.ts +44 -0
- package/src/types/PaginationInput.js +90 -0
- package/src/types/index.d.ts +30 -0
- package/src/types/index.js +31 -0
- package/src/types/ranges/DateRange.d.ts +27 -0
- package/src/types/ranges/DateRange.js +69 -0
- package/src/types/ranges/IRange.d.ts +47 -0
- package/src/types/ranges/IRange.js +2 -0
- package/src/types/ranges/NumericRange.d.ts +19 -0
- package/src/types/ranges/NumericRange.js +61 -0
- package/src/types/ranges/index.d.ts +26 -0
- package/src/types/ranges/index.js +27 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import { type CountOptions, Transaction } from 'sequelize';
|
|
2
|
+
import { type FindOptions, type IncludeOptions, type ModelAttributes } from 'sequelize';
|
|
3
|
+
import { Model } from 'sequelize-typescript';
|
|
4
|
+
import type { Literal } from 'sequelize/types/utils';
|
|
5
|
+
import type { BaseModel } from '../BaseModel.js';
|
|
6
|
+
import { FieldsInput, OrderByInput, PaginationInput } from '../types/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Turns a caller's serialized query into Sequelize options, and back out as SQL.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* The reason this package exists. A GraphQL API receives a filter, a page, an order
|
|
12
|
+
* and the set of fields the query selected — all of it plain JSON, none of it in a
|
|
13
|
+
* shape Sequelize accepts. These helpers translate: `autoQuery` is the one that
|
|
14
|
+
* composes the others, `toWhereOptions` turns a serialized filter into a `where` with
|
|
15
|
+
* the joins its nested parts imply, and `toLimitOptions` and `toOrderOptions` cover
|
|
16
|
+
* paging and ordering.
|
|
17
|
+
*
|
|
18
|
+
* The efficiency comes from the fields map. A column nobody asked for is not selected
|
|
19
|
+
* and a relation nobody reached into is not joined, so the statement narrows as the
|
|
20
|
+
* caller's selection narrows rather than being fixed by the resolver.
|
|
21
|
+
*
|
|
22
|
+
* Below that sit the escape hatches — `sql`, `L` and `E` — for the cases no option
|
|
23
|
+
* object can express.
|
|
24
|
+
*/
|
|
25
|
+
export declare namespace query {
|
|
26
|
+
/**
|
|
27
|
+
* The two shapes {@link query.pureData} accepts: one record, or many.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* An overloaded call signature rather than a union, so the return type follows the
|
|
31
|
+
* argument — pass an array and an array comes back.
|
|
32
|
+
*/
|
|
33
|
+
interface PureDataFunction {
|
|
34
|
+
<_M extends Model<_M>, T>(model: typeof Model, input: T, attributes?: string[]): ModelAttributes;
|
|
35
|
+
<_M extends Model<_M>, T>(model: typeof Model, input: T[], attributes?: string[]): ModelAttributes[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Collapses whitespace outside quoted literals, so a statement fits on one line.
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* Walks the string tracking whether a single quote is open, so runs of
|
|
42
|
+
* whitespace inside a literal are preserved and only structural whitespace is
|
|
43
|
+
* collapsed. Quote tracking is a simple toggle: it does not understand escaped
|
|
44
|
+
* or doubled quotes, so a literal containing one can throw the parity off.
|
|
45
|
+
*
|
|
46
|
+
* @param input - Statement to normalise.
|
|
47
|
+
* @returns The statement with structural whitespace collapsed to single spaces.
|
|
48
|
+
*/
|
|
49
|
+
export function safeSqlSpaceCleanup(input: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* Normalises a SQL string: one-lined, whitespace collapsed, ending in a single
|
|
52
|
+
* semicolon.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Usable as a plain function or as a template tag, the tag form being there so
|
|
56
|
+
* an editor highlights the SQL. It does NOT interpolate: a tag carrying
|
|
57
|
+
* substitutions throws, because the substituted values cannot be reached from
|
|
58
|
+
* here and the statement would come out mangled: tagging
|
|
59
|
+
* `SELECT ... WHERE id = ${id}` used to yield `... id = ,`. Bind parameters are
|
|
60
|
+
* the answer, and they are also the only safe one.
|
|
61
|
+
*
|
|
62
|
+
* Whitespace inside single-quoted literals is preserved; only whitespace
|
|
63
|
+
* outside them is collapsed.
|
|
64
|
+
*
|
|
65
|
+
* @param sqlQuery - A complete statement, or a template with no substitutions.
|
|
66
|
+
* @param values - Template substitutions, which are not supported.
|
|
67
|
+
* @returns The statement on one line, terminated with exactly one `;`.
|
|
68
|
+
* @throws TypeError when used as a template tag with substitutions.
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const statement = sql`
|
|
72
|
+
* SELECT id, name
|
|
73
|
+
* FROM "Lead"
|
|
74
|
+
* WHERE status = $1
|
|
75
|
+
* `;
|
|
76
|
+
* // SELECT id, name FROM "Lead" WHERE status = $1;
|
|
77
|
+
* await database().query(statement, { bind: [status] });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export function sql(sqlQuery: string | TemplateStringsArray, ...values: any[]): string;
|
|
81
|
+
/**
|
|
82
|
+
* Keeps only the properties a model actually declares.
|
|
83
|
+
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* The filter for input arriving from outside: anything the model does not declare
|
|
86
|
+
* as an attribute is dropped rather than passed to Sequelize, so a caller cannot
|
|
87
|
+
* set a column by sending an unexpected property. Arrays are mapped element by
|
|
88
|
+
* element, and relations are dropped along with everything else — this looks at
|
|
89
|
+
* `rawAttributes` only.
|
|
90
|
+
*
|
|
91
|
+
* @param model - Model whose attributes define what survives.
|
|
92
|
+
* @param input - One object, or an array of them.
|
|
93
|
+
* @param attributes - Attribute names to allow, defaulting to all the model's.
|
|
94
|
+
* @returns A new object (or array) carrying only the allowed properties.
|
|
95
|
+
*/
|
|
96
|
+
export const pureData: PureDataFunction;
|
|
97
|
+
/**
|
|
98
|
+
* Narrows a requested fields map to the model's own columns.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* Relations and unknown names are dropped, and the primary keys are then added
|
|
102
|
+
* back whether or not they were asked for — a deliberate trade so domain logic
|
|
103
|
+
* always has a key to work with, at the cost of returning a column the caller
|
|
104
|
+
* did not request.
|
|
105
|
+
*
|
|
106
|
+
* @param model - Model to narrow against.
|
|
107
|
+
* @param fields - Requested fields map, or a falsy value for "everything".
|
|
108
|
+
* @returns The surviving column names, or `true` meaning no restriction.
|
|
109
|
+
*/
|
|
110
|
+
export function pureFields(model: typeof BaseModel, fields: any): string[] | true;
|
|
111
|
+
/**
|
|
112
|
+
* Whether a fields map asks for any of the model's relations.
|
|
113
|
+
*
|
|
114
|
+
* @remarks
|
|
115
|
+
* The cheap test for "does this query need joins at all", used to avoid building
|
|
116
|
+
* an `include` when the caller only wants columns.
|
|
117
|
+
*
|
|
118
|
+
* @param model - Model whose associations to check against.
|
|
119
|
+
* @param fields - Requested fields map.
|
|
120
|
+
* @returns `true` when at least one key names an association.
|
|
121
|
+
*/
|
|
122
|
+
export function needNesting(model: typeof Model, fields: any): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Intersects a set of attributes with the names a caller asked for.
|
|
125
|
+
*
|
|
126
|
+
* @remarks
|
|
127
|
+
* With a `model` given, an empty intersection falls back to that model's primary
|
|
128
|
+
* keys rather than to nothing — so a fields list that matches no column selects
|
|
129
|
+
* the keys instead of every column, which is the safer failure but is not what
|
|
130
|
+
* "no matches" might suggest. Without a `model`, an empty result stays empty.
|
|
131
|
+
*
|
|
132
|
+
* @param attributes - Object whose keys are the available names.
|
|
133
|
+
* @param fields - Names the caller asked for.
|
|
134
|
+
* @param model - Model to take primary keys from when nothing matched.
|
|
135
|
+
* @returns The matching names, or the primary keys, or an empty array.
|
|
136
|
+
*/
|
|
137
|
+
export function filtered(attributes: any, fields: string[], model?: typeof BaseModel): string[];
|
|
138
|
+
/**
|
|
139
|
+
* Foreign-key column names on a model for the given relations.
|
|
140
|
+
*
|
|
141
|
+
* @remarks
|
|
142
|
+
* These have to be selected even when the caller did not ask for them, or
|
|
143
|
+
* Sequelize cannot attach the joined rows — which is why `autoQuery` merges them
|
|
144
|
+
* into the attribute list.
|
|
145
|
+
*
|
|
146
|
+
* @param model - Model holding the foreign keys.
|
|
147
|
+
* @param relations - Association names to collect keys for.
|
|
148
|
+
* @returns The foreign-key column names.
|
|
149
|
+
*/
|
|
150
|
+
export function foreignKeys(model: typeof BaseModel, relations: string[]): string[];
|
|
151
|
+
/**
|
|
152
|
+
* Merges query-option fragments into one options object.
|
|
153
|
+
*
|
|
154
|
+
* @remarks
|
|
155
|
+
* Per property: an absent property is taken as-is, arrays are unioned with
|
|
156
|
+
* duplicates dropped, objects are shallow-assigned, and anything else is
|
|
157
|
+
* overwritten by the later value. A fragment whose property type disagrees with
|
|
158
|
+
* what is already there — a scalar where an array sits, say — throws rather than
|
|
159
|
+
* guessing.
|
|
160
|
+
*
|
|
161
|
+
* MUTATES and returns `queryOptions`, so pass a fresh object unless sharing is
|
|
162
|
+
* what you want.
|
|
163
|
+
*
|
|
164
|
+
* @param queryOptions - Target, mutated in place.
|
|
165
|
+
* @param merge - Fragments to merge, in order; falsy ones are skipped.
|
|
166
|
+
* @returns The same `queryOptions` object.
|
|
167
|
+
* @throws TypeError when a fragment's property type conflicts with the target's.
|
|
168
|
+
*/
|
|
169
|
+
export function mergeQuery(queryOptions?: any, ...merge: any[]): any;
|
|
170
|
+
/**
|
|
171
|
+
* Builds Sequelize find options from a requested fields map, joins included.
|
|
172
|
+
*
|
|
173
|
+
* @remarks
|
|
174
|
+
* The helper the others compose into. It turns a {@link FieldsInput} into
|
|
175
|
+
* `attributes` plus a nested `include` for every relation the map mentions,
|
|
176
|
+
* recursing into each level, and adds the foreign keys a join needs whether or
|
|
177
|
+
* not they were requested. Its rest parameter then merges the fragments the
|
|
178
|
+
* sibling helpers return, which is the intended shape of a paginated read.
|
|
179
|
+
*
|
|
180
|
+
* A value in the map that is not `false` doubles as a filter for that column, so
|
|
181
|
+
* a fields map can carry a where clause; see {@link FieldsInput} for why presence
|
|
182
|
+
* rather than value is what selects.
|
|
183
|
+
*
|
|
184
|
+
* Two things to know. It MUTATES the `fields` map, adding any column named in a
|
|
185
|
+
* merged `order` that the map omits, so ordering cannot break selection. And
|
|
186
|
+
* `fields` may also be a plain array of names, which selects those columns and
|
|
187
|
+
* builds no joins at all.
|
|
188
|
+
*
|
|
189
|
+
* @param model - Model to build the query for.
|
|
190
|
+
* @param fields - Requested fields map, or an array of column names.
|
|
191
|
+
* @param merge - Option fragments to merge in, typically from the helpers below.
|
|
192
|
+
* @returns Find options, typed as the caller asks.
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const where = toWhereOptions(withRangeFilters(filter));
|
|
196
|
+
* const rows = await LeadModel.findAll(autoQuery<FindOptions>(
|
|
197
|
+
* LeadModel,
|
|
198
|
+
* fields,
|
|
199
|
+
* where,
|
|
200
|
+
* toLimitOptions(pageOptions),
|
|
201
|
+
* toOrderOptions(orderBy),
|
|
202
|
+
* ));
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
export function autoQuery<T>(model: any, fields?: any, ...merge: (Partial<T> | undefined)[]): T;
|
|
206
|
+
/**
|
|
207
|
+
* Return names of primary key fields for a given model.
|
|
208
|
+
*
|
|
209
|
+
* @param model - Model to read the keys of.
|
|
210
|
+
* @returns The primary key attribute names, in declaration order.
|
|
211
|
+
*/
|
|
212
|
+
export function primaryKeys(model: typeof BaseModel): string[];
|
|
213
|
+
/**
|
|
214
|
+
* Foreign key map representation, where related property name references
|
|
215
|
+
* parent property name.
|
|
216
|
+
*/
|
|
217
|
+
interface ForeignKeyMap {
|
|
218
|
+
[property: string]: string;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Returns foreign key map for a given pair of parent model and related
|
|
222
|
+
* model.
|
|
223
|
+
*
|
|
224
|
+
* @param parent - Model on the referenced side.
|
|
225
|
+
* @param model - Model whose foreign keys are wanted.
|
|
226
|
+
*/
|
|
227
|
+
export function foreignKeysMap(parent: typeof BaseModel, model: typeof BaseModel): ForeignKeyMap | null;
|
|
228
|
+
/**
|
|
229
|
+
* Recursively creates entity and all it's relations from a given input
|
|
230
|
+
* using a given model.
|
|
231
|
+
*
|
|
232
|
+
* @param model - model class to map entity to
|
|
233
|
+
* @param input - data input object related to a given model
|
|
234
|
+
* @param fields - fields map to return on created entity
|
|
235
|
+
* @param transaction - transaction
|
|
236
|
+
*/
|
|
237
|
+
export function createEntity<T extends BaseModel<T>, I>(model: typeof BaseModel, input: I, fields?: FieldsInput, transaction?: Transaction): Promise<Partial<T>>;
|
|
238
|
+
/**
|
|
239
|
+
* The counting counterpart of {@link query.autoQuery}, for the same fields and filter.
|
|
240
|
+
*
|
|
241
|
+
* @remarks
|
|
242
|
+
* Builds the same query, then drops `attributes` and counts distinct primary keys
|
|
243
|
+
* instead — `distinct` matters because the joins `autoQuery` adds would otherwise
|
|
244
|
+
* multiply a row once per joined record and inflate the total.
|
|
245
|
+
*
|
|
246
|
+
* Pass it the same `fields` and filter as the data query, or the two disagree.
|
|
247
|
+
*
|
|
248
|
+
* @param model - Model to count rows of.
|
|
249
|
+
* @param fields - The same fields map used for the data query.
|
|
250
|
+
* @param merge - The same filter fragments, minus limit and order.
|
|
251
|
+
* @returns Count options ready for `Model.count()`.
|
|
252
|
+
*/
|
|
253
|
+
export function autoCountQuery(model: any, fields?: any, ...merge: (Partial<CountOptions> | undefined)[]): CountOptions;
|
|
254
|
+
/**
|
|
255
|
+
* Builds proper paging options query part
|
|
256
|
+
*
|
|
257
|
+
* @param pageOptions - obtained pagination input
|
|
258
|
+
* from remote
|
|
259
|
+
* @returns pagination part of the query
|
|
260
|
+
*/
|
|
261
|
+
export function toLimitOptions<_T>(pageOptions?: PaginationInput): FindOptions;
|
|
262
|
+
/**
|
|
263
|
+
* Turns a serialized order into Sequelize's `order` option.
|
|
264
|
+
*
|
|
265
|
+
* @remarks
|
|
266
|
+
* One entry per property, in the order the object lists them, so the caller
|
|
267
|
+
* controls precedence. Directions are normalised rather than trusted — anything
|
|
268
|
+
* that does not read as descending becomes ascending, which is what keeps a value
|
|
269
|
+
* off the wire out of the statement. An empty or absent order yields no `order`
|
|
270
|
+
* at all rather than an empty one.
|
|
271
|
+
*
|
|
272
|
+
* @param orderBy - Property-to-direction map from the caller.
|
|
273
|
+
* @returns Options carrying `order`, or empty options.
|
|
274
|
+
*/
|
|
275
|
+
export function toOrderOptions<_T>(orderBy?: OrderByInput): FindOptions;
|
|
276
|
+
/**
|
|
277
|
+
* Matches a value, or no value at all.
|
|
278
|
+
*
|
|
279
|
+
* @remarks
|
|
280
|
+
* For the filter that means "these, and the rows where it is not set": the result
|
|
281
|
+
* is an `OR` over `null` and what you pass. Without it a `null` in a where clause
|
|
282
|
+
* compares rather than tests, and matches nothing.
|
|
283
|
+
*
|
|
284
|
+
* @param value - Value, or values, to accept alongside `null`.
|
|
285
|
+
* @returns A where fragment for one column.
|
|
286
|
+
*/
|
|
287
|
+
export function orNull(value: string | string[]): Partial<FindOptions>;
|
|
288
|
+
/**
|
|
289
|
+
* Turns a serializable filter into Sequelize `where` options.
|
|
290
|
+
*
|
|
291
|
+
* @remarks
|
|
292
|
+
* Each property is dispatched on its shape: a `$`-prefixed key becomes the
|
|
293
|
+
* matching Sequelize operator (see {@link FILTER_OPS}), an object with `start`
|
|
294
|
+
* and `end` becomes a `BETWEEN`, any other object is walked recursively as a
|
|
295
|
+
* nested filter, an array becomes an OR of its values, and a scalar is compared
|
|
296
|
+
* directly.
|
|
297
|
+
*
|
|
298
|
+
* A STRING value is inspected before it is compared, which is convenient and
|
|
299
|
+
* occasionally surprising. A `%` anywhere in it makes the comparison a
|
|
300
|
+
* case-insensitive `ILIKE`, and a leading `<=`, `>=`, `<`, `>` or `=` becomes
|
|
301
|
+
* that operator with the rest as the value, coerced to a number or a `Date` when
|
|
302
|
+
* it parses as one. So `'>=10'` and `'%abc%'` work with no operator key — and a
|
|
303
|
+
* literal value that happens to contain `%`, such as `'50% off'`, becomes a
|
|
304
|
+
* pattern match rather than an equality test. Use an explicit `$eq` where that
|
|
305
|
+
* matters.
|
|
306
|
+
*
|
|
307
|
+
* Empty is treated as absent, not as a contradiction: an empty array is skipped
|
|
308
|
+
* and a single-element array is unwrapped to the value. Given a falsy filter it
|
|
309
|
+
* returns `{}`, which means "no restriction" — so a caller cannot accidentally
|
|
310
|
+
* filter everything out by passing nothing.
|
|
311
|
+
*
|
|
312
|
+
* With `inputType`, a property matching one of that type's own properties is
|
|
313
|
+
* turned into a required `include` on the related model rather than a column
|
|
314
|
+
* comparison, which is how a filter reaches across a relation.
|
|
315
|
+
*
|
|
316
|
+
* @param filter - Filter from a caller. Modified in place as empties are cleared.
|
|
317
|
+
* @param inputType - Constructor describing which properties are relations.
|
|
318
|
+
* @returns Options carrying `where` and, where relations were filtered, `include`.
|
|
319
|
+
*/
|
|
320
|
+
export function toWhereOptions<T>(filter?: T, inputType?: new () => T): any;
|
|
321
|
+
/**
|
|
322
|
+
* ORs an array of filter values into one condition.
|
|
323
|
+
*
|
|
324
|
+
* @remarks
|
|
325
|
+
* Plain values are gathered into a single `IN`, while values carrying their own
|
|
326
|
+
* operator prefix become separate conditions, and the two groups are then ORed —
|
|
327
|
+
* so `['a', 'b', '>=10']` becomes `IN (a, b) OR >= 10` rather than three
|
|
328
|
+
* unrelated comparisons.
|
|
329
|
+
*
|
|
330
|
+
* @param data - Values to combine.
|
|
331
|
+
* @returns A `where` fragment for one column.
|
|
332
|
+
*/
|
|
333
|
+
export function buildWhereFromArray(data: any[]): any;
|
|
334
|
+
/**
|
|
335
|
+
* Rewrites `<column>Range` filter properties onto the columns they belong to.
|
|
336
|
+
*
|
|
337
|
+
* @remarks
|
|
338
|
+
* The convention that lets a range be filtered over RPC: a caller sends
|
|
339
|
+
* `durationRange: { start, end }` and this moves it to `duration`, where
|
|
340
|
+
* {@link query.toWhereOptions} turns it into a `BETWEEN`. Recognition is strict — the
|
|
341
|
+
* property name must end in `Range` and the value must have exactly the keys
|
|
342
|
+
* `start` and `end`, in either order. Anything else is left untouched and
|
|
343
|
+
* filtered as an ordinary value, and nested objects are walked so a range on a
|
|
344
|
+
* related model works too.
|
|
345
|
+
*
|
|
346
|
+
* Sending both `duration` and `durationRange` throws rather than choosing one.
|
|
347
|
+
*
|
|
348
|
+
* MUTATES and returns the filter it is given.
|
|
349
|
+
*
|
|
350
|
+
* @param filter - Filter to rewrite in place.
|
|
351
|
+
* @returns The same filter, with ranges moved onto their columns.
|
|
352
|
+
*/
|
|
353
|
+
export function withRangeFilters(filter: any): any;
|
|
354
|
+
/**
|
|
355
|
+
* Finds the include options for a model reached through a chain of includes.
|
|
356
|
+
*
|
|
357
|
+
* @remarks
|
|
358
|
+
* The way to reach into a query that is already built — to add a where clause to a
|
|
359
|
+
* join, say, without rebuilding the whole thing. The path is walked one model at a
|
|
360
|
+
* time, and it is CONSUMED as that happens, so pass a copy if the caller still
|
|
361
|
+
* needs it.
|
|
362
|
+
*
|
|
363
|
+
* @param queryOptions - Query to search.
|
|
364
|
+
* @param path - Models to follow, outermost first.
|
|
365
|
+
* @returns The matching include options, or `null` when the path does not resolve.
|
|
366
|
+
*/
|
|
367
|
+
export function getInclude(queryOptions: FindOptions, path: (typeof Model)[]): IncludeOptions | null;
|
|
368
|
+
/**
|
|
369
|
+
* Builds a Sequelize literal from a string or a template — the escape hatch for
|
|
370
|
+
* SQL that no query option can express.
|
|
371
|
+
*
|
|
372
|
+
* @remarks
|
|
373
|
+
* A literal is spliced into the statement exactly as given, so nothing about it
|
|
374
|
+
* is parsed, checked or escaped. That is the whole point of it, and the reason to
|
|
375
|
+
* keep each one as small as the job allows: a correlated subquery in a `where`, a
|
|
376
|
+
* window function in an `order`, an operator Sequelize has no name for. Runtime
|
|
377
|
+
* values belong in {@link query.E} rather than in the text.
|
|
378
|
+
*
|
|
379
|
+
* Used as a template tag, the substitutions used to be dropped and the literal
|
|
380
|
+
* parts joined with commas, so the example below produced
|
|
381
|
+
* `(SELECT COUNT(*) FROM "SomeTable" WHERE owner = ,) = 0` — accepted by the
|
|
382
|
+
* template, rejected by Postgres. They are now interpolated in order.
|
|
383
|
+
*
|
|
384
|
+
* {@link query.sql} refuses substitutions rather than interpolating them, and the
|
|
385
|
+
* difference is deliberate: a complete statement can carry bind parameters, so
|
|
386
|
+
* interpolating into one is a choice to avoid. A fragment handed to Sequelize as
|
|
387
|
+
* a literal has no bind channel, which leaves escaping as the only option.
|
|
388
|
+
*
|
|
389
|
+
* @param str - The SQL text, or the literal parts when used as a template tag.
|
|
390
|
+
* @param values - The substitutions, when used as a template tag.
|
|
391
|
+
* @returns The text as a Sequelize literal, ready to use as a query option value.
|
|
392
|
+
* @example
|
|
393
|
+
* ```typescript
|
|
394
|
+
* const owner = 3;
|
|
395
|
+
* const query = {
|
|
396
|
+
* where: L`(SELECT COUNT(*) FROM "SomeTable" WHERE owner = ${E(owner)}) = 0`,
|
|
397
|
+
* };
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
export function L(str: TemplateStringsArray | string, ...values: any[]): Literal;
|
|
401
|
+
/**
|
|
402
|
+
* Renders a value as a SQL constant: a number as itself, a string quoted and
|
|
403
|
+
* escaped, anything else as `NULL`.
|
|
404
|
+
*
|
|
405
|
+
* @remarks
|
|
406
|
+
* The companion to {@link query.L} and the only safe way to get a runtime value
|
|
407
|
+
* into a literal. Single quotes inside a string are doubled, which is what
|
|
408
|
+
* Postgres requires — and what this did not do: a value of `O'Brien` came out as
|
|
409
|
+
* a broken string constant, and a value chosen deliberately came out as SQL. The
|
|
410
|
+
* same path escapes a dynamic view's parameters, so a view selected with
|
|
411
|
+
* caller-supplied `viewParams` was open the same way.
|
|
412
|
+
*
|
|
413
|
+
* Only numbers and strings render as values. Booleans, dates, objects, `null` and
|
|
414
|
+
* `undefined` all become `NULL` — so format a date as a string before passing it,
|
|
415
|
+
* and do not reach for this to render a boolean.
|
|
416
|
+
*
|
|
417
|
+
* @param input - Value to render.
|
|
418
|
+
* @returns The number itself, a quoted and escaped string, or `NULL`.
|
|
419
|
+
*/
|
|
420
|
+
export function E(input: any): string | number;
|
|
421
|
+
/**
|
|
422
|
+
* Deletes properties from an object.
|
|
423
|
+
*
|
|
424
|
+
* @remarks
|
|
425
|
+
* MUTATES what it is given and hands it back, so it composes into a call chain.
|
|
426
|
+
* A falsy argument is returned untouched, which makes it safe on an optional
|
|
427
|
+
* value.
|
|
428
|
+
*
|
|
429
|
+
* @param obj - Object to strip.
|
|
430
|
+
* @param props - Property names to delete.
|
|
431
|
+
* @returns The same object.
|
|
432
|
+
*/
|
|
433
|
+
export function skip(obj: any, ...props: string[]): any;
|
|
434
|
+
/**
|
|
435
|
+
* Traverses given query object, lookups for includes matching
|
|
436
|
+
* the given arguments of include options and overrides those are matching
|
|
437
|
+
* by model and alias with the provided option.
|
|
438
|
+
*
|
|
439
|
+
* @param queryOptions - Query whose includes are to be overridden.
|
|
440
|
+
* @param options - Include options to apply, matched by model and alias.
|
|
441
|
+
* @returns The same query options.
|
|
442
|
+
*/
|
|
443
|
+
export function overrideJoin(queryOptions: FindOptions | CountOptions, ...options: IncludeOptions[]): FindOptions | CountOptions;
|
|
444
|
+
export {};
|
|
445
|
+
}
|