@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,695 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @imqueue/pg-sequelize - Sequelize ORM refines for @imqueue
|
|
3
|
+
*
|
|
4
|
+
* I'm Queue Software Project
|
|
5
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
6
|
+
*
|
|
7
|
+
* This program is free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the GNU General Public License as published by
|
|
9
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
* (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* This program is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
* GNU General Public License for more details.
|
|
16
|
+
*
|
|
17
|
+
* You should have received a copy of the GNU General Public License
|
|
18
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
|
+
*
|
|
20
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
21
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
22
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
23
|
+
*/
|
|
24
|
+
import { Graph } from './Graph.js';
|
|
25
|
+
export type * from 'sequelize-typescript';
|
|
26
|
+
export { AfterBulkCreate, AfterBulkDestroy, AfterBulkRestore, AfterBulkSync, AfterBulkUpdate, AfterConnect, AfterCreate, AfterDefine, AfterDestroy, AfterFind, AfterInit, AfterRestore, AfterSave, AfterSync, AfterUpdate, AfterUpsert, AfterValidate, AllowNull, Association, AutoIncrement, BaseAssociation, BeforeBulkCreate, BeforeBulkDestroy, BeforeBulkRestore, BeforeBulkSync, BeforeBulkUpdate, BeforeConnect, BeforeCount, BeforeCreate, BeforeDefine, BeforeDestroy, BeforeFind, BeforeFindAfterExpandIncludeAll, BeforeFindAfterOptions, BeforeInit, BeforeRestore, BeforeSave, BeforeSync, BeforeUpdate, BeforeUpsert, BeforeValidate, BelongsTo, BelongsToAssociation, BelongsToMany, BelongsToManyAssociation, Column, Comment, Contains, CreatedAt, DataType, Default, DefaultScope, DeletedAt, Equals, ForeignKey, HasAssociation, HasMany, HasOne, INFER_ALIAS_MAP, Index, Is, IsAfter, IsAlpha, IsAlphanumeric, IsArray, IsBefore, IsCreditCard, IsDate, IsDecimal, IsEmail, IsFloat, IsIP, IsIPv4, IsIPv6, IsIn, IsInt, IsLowercase, IsNull, IsNumeric, IsUUID, IsUppercase, IsUrl, Length, Max, Min, Model, Not, NotContains, NotEmpty, NotIn, NotNull, PrimaryKey, Scopes, Table, Unique, UpdatedAt, Validate, ValidationFailed, Validator, addAssociation, addAttribute, addAttributeOptions, addFieldToIndex, addHook, addOptions, addScopeOptions, addScopeOptionsGetter, annotateModelWithIndex, createIndexDecorator, getAssociations, getAssociationsByRelation, getAttributes, getHooks, getIndexes, getModelName, getModels, getOptions, getPreparedAssociationOptions, getScopeOptions, getScopeOptionsGetters, getSequelizeTypeByDesignType, implementHookDecorator, inferDataType, installHooks, isDataType, prepareArgs, prepareOptions, resolveModelGetter, resolveScope, resolveScopes, resolvesDeprecatedScopes, setAssociations, setAttributes, setHooks, setIndexes, setModelName, setOptions, setScopeOptionsGetters, verbose, } from 'sequelize-typescript';
|
|
27
|
+
import { type BuildOptions as BuildOptionsOrigin, type BulkCreateOptions as BulkCreateOptionsOrigin, type CreateOptions as CreateOptionsOrigin, type DropOptions, type FindOptions as FindOptionsOrigin, type Identifier, type IncludeOptions, type InitOptions as InitOptionsOrigin, type ModelAttributes, type ModelOptions, QueryInterface as QueryInterfaceOrigin, type QueryOptions as QueryOptionsOrigin, type QueryOptionsWithType, type SaveOptions as InstanceSaveOptionsOrigin, type SyncOptions as SyncOptionsOrigin, type UpdateOptions as UpdateOptionsOrigin, type UpsertOptions as UpsertOptionsOrigin } from 'sequelize';
|
|
28
|
+
import { Model, Sequelize as SequelizeOrigin } from 'sequelize-typescript';
|
|
29
|
+
import type * as QueryTypes from 'sequelize/types/query-types';
|
|
30
|
+
import { type ColumnIndexOptions, type IDynamicViewDefineOptions, type ViewParams } from './decorators/index.js';
|
|
31
|
+
/**
|
|
32
|
+
* Replaces every member of `T` that `R` also declares with `R`'s version of it.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* The mechanism behind each widened option type in this module. Sequelize types
|
|
36
|
+
* `returning` as a boolean; this package needs it to accept a column list as well,
|
|
37
|
+
* and intersecting the two would leave a member that has to be both. Removing the
|
|
38
|
+
* keys `R` redeclares before intersecting is what lets the override win instead.
|
|
39
|
+
*
|
|
40
|
+
* `T` is the type to start from and `R` the members to override it with, so the
|
|
41
|
+
* widened save options are Sequelize's own with only `returning` replaced.
|
|
42
|
+
*/
|
|
43
|
+
export type Modify<T, R> = Pick<T, Exclude<keyof T, keyof R>> & R;
|
|
44
|
+
/**
|
|
45
|
+
* Sequelize's own sync options, extended for views and column indices.
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* Passing nothing is the common case: tables, then views, then indices. The two
|
|
49
|
+
* additions worth knowing are `withNoViews`, for a deployment where views are
|
|
50
|
+
* managed outside the application, and `withoutDrop`, for views that depend on each
|
|
51
|
+
* other.
|
|
52
|
+
*/
|
|
53
|
+
export interface SyncOptions extends SyncOptionsOrigin {
|
|
54
|
+
/**
|
|
55
|
+
* Has no effect when passed to a sync call.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* Whether a model is a view is decided by the model, not by the caller: the
|
|
59
|
+
* `View` and `DynamicView` decorators set this flag in the model's own options,
|
|
60
|
+
* and that is the only place it is ever read from. It is declared here because
|
|
61
|
+
* a model's options and a sync call's options are the same type, not because
|
|
62
|
+
* passing it does anything.
|
|
63
|
+
*
|
|
64
|
+
* @deprecated Decorate the model instead; passing this is ignored.
|
|
65
|
+
*/
|
|
66
|
+
treatAsView?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Syncs tables and indices only, leaving every view untouched.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* For a database where views are owned by migrations or by a DBA rather than by
|
|
72
|
+
* the model definitions, and for start-up paths that should not drop and rebuild
|
|
73
|
+
* every view.
|
|
74
|
+
*/
|
|
75
|
+
withNoViews?: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Replaces each view in place instead of dropping it first.
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* The default drops a view and creates it again, which fails as soon as another
|
|
81
|
+
* view selects from it — Postgres will not drop a view something depends on.
|
|
82
|
+
* With this set, only the model's own create statement runs, so it has to be
|
|
83
|
+
* written as `CREATE OR REPLACE VIEW` to succeed against a view that already
|
|
84
|
+
* exists. Postgres will still refuse a replacement whose output columns differ
|
|
85
|
+
* in name, type or order, so a column added to the middle of a view needs the
|
|
86
|
+
* drop.
|
|
87
|
+
*/
|
|
88
|
+
withoutDrop?: boolean;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Sequelize's own find options, plus the parameters of a dynamic view.
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* Accepted by every finder on `BaseModel`. The single addition is ignored unless the
|
|
95
|
+
* model was declared with `DynamicView`.
|
|
96
|
+
*/
|
|
97
|
+
export interface FindOptions extends FindOptionsOrigin {
|
|
98
|
+
/**
|
|
99
|
+
* Values for the placeholders in a dynamic view's definition.
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
102
|
+
* Merged over the defaults the decorator supplied, then substituted into the view
|
|
103
|
+
* SQL before the statement runs — which is how one model can be selected from
|
|
104
|
+
* with different parameters. Values are escaped on the way in, and one reached
|
|
105
|
+
* through an `include` is honoured too.
|
|
106
|
+
*/
|
|
107
|
+
viewParams?: ViewParams;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* What a model's `options` actually holds on a model of this package.
|
|
111
|
+
*
|
|
112
|
+
* @remarks
|
|
113
|
+
* Sequelize's own init options together with the view fields the decorators write
|
|
114
|
+
* into them, which is why reading `Model.options` for `treatAsView`,
|
|
115
|
+
* `viewDefinition` or `viewParams` type-checks here and does not in plain sequelize.
|
|
116
|
+
*/
|
|
117
|
+
export interface InitOptions extends InitOptionsOrigin, IDynamicViewDefineOptions {
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Widens `returning` so it can name the columns to fetch back, not just ask whether
|
|
121
|
+
* to fetch any.
|
|
122
|
+
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* Sequelize types `returning` as a boolean: every column, or none. Postgres can
|
|
125
|
+
* return a subset, and a service usually wants exactly the fields its caller asked
|
|
126
|
+
* for — so this package accepts a list of column names and rewrites the
|
|
127
|
+
* `RETURNING *` in the generated statement to name just those.
|
|
128
|
+
*
|
|
129
|
+
* Two things follow. Less data crosses both hops: the database returns fewer
|
|
130
|
+
* columns, and the instance remembers the list, so serializing it emits only those
|
|
131
|
+
* properties. And because the option is widened rather than replaced, `true` and
|
|
132
|
+
* `false` keep working exactly as before. An empty array is treated as `false`,
|
|
133
|
+
* since a statement returning no columns is not what anyone means by it.
|
|
134
|
+
*
|
|
135
|
+
* The cost is a cast at the call site, and the mimicked option types in this module
|
|
136
|
+
* are what to cast to — they exist for this and nothing else. Whenever TypeScript
|
|
137
|
+
* objects to a `returning` array, import the type it names from `@imqueue/pg-sequelize`
|
|
138
|
+
* rather than from `sequelize`. Use `restoreSerialization()` to forget the list
|
|
139
|
+
* again.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* const scope = new Scope({ name: 'test', description: 'Test', schema: {} });
|
|
144
|
+
*
|
|
145
|
+
* await scope.save({ returning: ['id', 'name'] } as SaveOptions);
|
|
146
|
+
* console.log(JSON.stringify(scope)); // {"id":2,"name":"test"}
|
|
147
|
+
*
|
|
148
|
+
* const [count, scopes] = await Scope.update({ name: 'TEST' }, {
|
|
149
|
+
* where: { id: 2 },
|
|
150
|
+
* returning: ['id', 'name'],
|
|
151
|
+
* } as UpdateOptions);
|
|
152
|
+
* console.log(JSON.stringify(scopes[0])); // {"id":2,"name":"TEST"}
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
export interface ReturningOptions {
|
|
156
|
+
/**
|
|
157
|
+
* `true` for every column, `false` for none, or the names of the columns to
|
|
158
|
+
* fetch back.
|
|
159
|
+
*/
|
|
160
|
+
returning?: boolean | string[];
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The shape sequelize's resolved include tree actually has, as this module reads it.
|
|
164
|
+
*
|
|
165
|
+
* @remarks
|
|
166
|
+
* Sequelize builds this while working out an `include`, and does not declare it in
|
|
167
|
+
* its public types. It is declared here because the select-query override walks the
|
|
168
|
+
* tree looking for dynamic views nested inside a join, and needs each node's alias to
|
|
169
|
+
* rewrite the right part of the statement. Of no use to application code.
|
|
170
|
+
*/
|
|
171
|
+
export interface WithIncludeMap extends InitOptions {
|
|
172
|
+
/** The resolved options of each included association, keyed by property name. */
|
|
173
|
+
includeMap?: {
|
|
174
|
+
[propertyName: string]: WithIncludeMap & IncludeOptions & FindOptions;
|
|
175
|
+
};
|
|
176
|
+
/** The property names present in `includeMap`. */
|
|
177
|
+
includeNames?: string[];
|
|
178
|
+
/** The node this one was included from. */
|
|
179
|
+
parent: WithIncludeMap;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Names a model class by the instance type it produces.
|
|
183
|
+
*
|
|
184
|
+
* @remarks
|
|
185
|
+
* Used where a helper takes a model class rather than an instance. The constructor is
|
|
186
|
+
* declared without arguments because the type is there to identify a class, not to
|
|
187
|
+
* build anything with it — sequelize instantiates models itself.
|
|
188
|
+
*/
|
|
189
|
+
export type IModelClass<T extends BaseModel<T>> = new () => T;
|
|
190
|
+
/**
|
|
191
|
+
* Options for `upsert()`, with `returning` widened to accept a column list.
|
|
192
|
+
*
|
|
193
|
+
* @remarks
|
|
194
|
+
* Cast to this at the call site when TypeScript objects to a `returning`
|
|
195
|
+
* column list; {@link ReturningOptions} explains what the list does.
|
|
196
|
+
*/
|
|
197
|
+
export type UpsertOptions = Modify<UpsertOptionsOrigin, ReturningOptions>;
|
|
198
|
+
/**
|
|
199
|
+
* Options for `build()`, with `returning` widened to accept a column list.
|
|
200
|
+
*
|
|
201
|
+
* @remarks
|
|
202
|
+
* Cast to this at the call site when TypeScript objects to a `returning`
|
|
203
|
+
* column list; {@link ReturningOptions} explains what the list does.
|
|
204
|
+
*/
|
|
205
|
+
export type BuildOptions = Modify<BuildOptionsOrigin, ReturningOptions>;
|
|
206
|
+
/**
|
|
207
|
+
* Options for `bulkCreate()`, with `returning` widened to accept a column list.
|
|
208
|
+
*
|
|
209
|
+
* @remarks
|
|
210
|
+
* Cast to this at the call site when TypeScript objects to a `returning`
|
|
211
|
+
* column list; {@link ReturningOptions} explains what the list does.
|
|
212
|
+
*/
|
|
213
|
+
export type BulkCreateOptions = Modify<BulkCreateOptionsOrigin, ReturningOptions>;
|
|
214
|
+
/**
|
|
215
|
+
* Options for a raw `query()`, with `returning` widened to accept a column list.
|
|
216
|
+
*
|
|
217
|
+
* @remarks
|
|
218
|
+
* Cast to this at the call site when TypeScript objects to a `returning`
|
|
219
|
+
* column list; {@link ReturningOptions} explains what the list does.
|
|
220
|
+
*/
|
|
221
|
+
export type QueryOptions = Modify<QueryOptionsOrigin, ReturningOptions>;
|
|
222
|
+
/**
|
|
223
|
+
* Options for the static `update()`, with `returning` widened to accept a column
|
|
224
|
+
* list.
|
|
225
|
+
*
|
|
226
|
+
* @remarks
|
|
227
|
+
* Cast to this at the call site when TypeScript objects to a `returning`
|
|
228
|
+
* column list; {@link ReturningOptions} explains what the list does.
|
|
229
|
+
*/
|
|
230
|
+
export type UpdateOptions = Modify<UpdateOptionsOrigin, ReturningOptions>;
|
|
231
|
+
/**
|
|
232
|
+
* Options for `create()`, with `returning` widened to accept a column list.
|
|
233
|
+
*
|
|
234
|
+
* @remarks
|
|
235
|
+
* Cast to this at the call site when TypeScript objects to a `returning`
|
|
236
|
+
* column list; {@link ReturningOptions} explains what the list does.
|
|
237
|
+
*/
|
|
238
|
+
export type CreateOptions = Modify<CreateOptionsOrigin, ReturningOptions>;
|
|
239
|
+
/**
|
|
240
|
+
* Options for an instance's `save()`, with `returning` widened to accept a column
|
|
241
|
+
* list.
|
|
242
|
+
*
|
|
243
|
+
* @remarks
|
|
244
|
+
* Cast to this at the call site when TypeScript objects to a `returning`
|
|
245
|
+
* column list; {@link ReturningOptions} explains what the list does.
|
|
246
|
+
*/
|
|
247
|
+
export type SaveOptions = Modify<InstanceSaveOptionsOrigin, ReturningOptions>;
|
|
248
|
+
/**
|
|
249
|
+
* Sequelize's query interface, extended with view creation and removal.
|
|
250
|
+
*
|
|
251
|
+
* @remarks
|
|
252
|
+
* The type a migration works against, and the most imported symbol in this package.
|
|
253
|
+
* Everything sequelize's own interface offers is here unchanged; `createView` and
|
|
254
|
+
* `dropView` are the additions, since sequelize has no equivalent.
|
|
255
|
+
*
|
|
256
|
+
* The instance handed out by `Sequelize.getQueryInterface()` is also wrapped in three
|
|
257
|
+
* ways that do not show up in this type: every write method treats an empty
|
|
258
|
+
* `returning` array as `false`, `query()` rewrites `RETURNING *` when given a column
|
|
259
|
+
* list, and the select-query generator substitutes a dynamic view's definition into
|
|
260
|
+
* the statement.
|
|
261
|
+
*/
|
|
262
|
+
export interface QueryInterface extends QueryInterfaceOrigin {
|
|
263
|
+
/** The connection this interface belongs to. */
|
|
264
|
+
sequelize: Sequelize;
|
|
265
|
+
/**
|
|
266
|
+
* Drops a view, if it exists.
|
|
267
|
+
*
|
|
268
|
+
* @remarks
|
|
269
|
+
* Issues `DROP VIEW IF EXISTS`, so it is safe to call for a view that was never
|
|
270
|
+
* created. A materialized view needs a different statement and is not covered.
|
|
271
|
+
*
|
|
272
|
+
* @param viewName - Name of the view to drop.
|
|
273
|
+
* @param options - `cascade` also drops whatever depends on the view.
|
|
274
|
+
* @returns The result of the drop statement.
|
|
275
|
+
*/
|
|
276
|
+
dropView(viewName: string, options?: DropOptions): Promise<any>;
|
|
277
|
+
/**
|
|
278
|
+
* Creates a view from a complete SQL definition.
|
|
279
|
+
*
|
|
280
|
+
* @remarks
|
|
281
|
+
* The definition is executed exactly as given, so it carries its own `CREATE VIEW`
|
|
282
|
+
* or `CREATE OR REPLACE VIEW` keywords. The name is checked against the one the
|
|
283
|
+
* statement declares before anything runs, which catches registering one view's
|
|
284
|
+
* SQL under another view's name — a copy-paste mistake that would otherwise
|
|
285
|
+
* create the wrong relation and leave the model pointing at nothing.
|
|
286
|
+
*
|
|
287
|
+
* The check recognises a plain or temporary view. A `CREATE MATERIALIZED VIEW`
|
|
288
|
+
* definition is rejected by it, and would not survive the drop-and-create cycle
|
|
289
|
+
* either, so materialized views are outside what this supports today.
|
|
290
|
+
*
|
|
291
|
+
* @param viewName - The name the definition is expected to declare.
|
|
292
|
+
* @param viewDefinition - The complete create statement.
|
|
293
|
+
* @returns The result of executing the definition.
|
|
294
|
+
* @throws TypeError when the definition does not declare `viewName`.
|
|
295
|
+
*/
|
|
296
|
+
createView(viewName: string, viewDefinition: string): Promise<any>;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Sequelize's own connection class, taught about views, column indices and the
|
|
300
|
+
* widened `returning` option.
|
|
301
|
+
*
|
|
302
|
+
* @remarks
|
|
303
|
+
* Returned by `database()`, which is how a service normally gets one. Four things
|
|
304
|
+
* differ from the class it extends.
|
|
305
|
+
*
|
|
306
|
+
* Views are first-class: a model declared with `View` or `DynamicView` is created as
|
|
307
|
+
* a view after every table exists, dropped as a view, and skipped by the table sync.
|
|
308
|
+
* A dynamic view goes further — its definition carries `@{name}` placeholders, and
|
|
309
|
+
* the select-query generator substitutes them into the statement at query time, so
|
|
310
|
+
* one model can be read with different parameters, including when it is reached
|
|
311
|
+
* through a join.
|
|
312
|
+
*
|
|
313
|
+
* Column indices declared with `ColumnIndex` or `NullableIndex` are created as part
|
|
314
|
+
* of the same sync, which sequelize has no notion of at all.
|
|
315
|
+
*
|
|
316
|
+
* `returning` may name columns rather than being a boolean, both on the write methods
|
|
317
|
+
* and on a raw `query()`.
|
|
318
|
+
*
|
|
319
|
+
* @example
|
|
320
|
+
* ```typescript
|
|
321
|
+
* const orm = database(dbConfig); // a Sequelize, already connected
|
|
322
|
+
*
|
|
323
|
+
* await orm.sync(); // tables, then views, then indices
|
|
324
|
+
* await orm.sync({ withNoViews: true }); // tables and indices only
|
|
325
|
+
* ```
|
|
326
|
+
*/
|
|
327
|
+
export declare class Sequelize extends SequelizeOrigin {
|
|
328
|
+
/**
|
|
329
|
+
* The query interface for this connection, with view support and the widened
|
|
330
|
+
* `returning` option.
|
|
331
|
+
*
|
|
332
|
+
* @remarks
|
|
333
|
+
* Sequelize's own interface is wrapped once, the first time this is called, and
|
|
334
|
+
* the same wrapped object is returned afterwards. The wrapper adds `createView`
|
|
335
|
+
* and `dropView`, makes every write method treat an empty `returning` array as
|
|
336
|
+
* `false`, and teaches the select-query generator to substitute a dynamic view's
|
|
337
|
+
* definition into the statement.
|
|
338
|
+
*
|
|
339
|
+
* @returns The wrapped query interface.
|
|
340
|
+
*/
|
|
341
|
+
getQueryInterface(): QueryInterface;
|
|
342
|
+
/**
|
|
343
|
+
* Defines a model from an attribute map rather than from a decorated class.
|
|
344
|
+
*
|
|
345
|
+
* @remarks
|
|
346
|
+
* Sequelize's own `define()` builds on its `Model`; this one builds on
|
|
347
|
+
* `BaseModel`, so a model defined this way gets the view handling, the widened
|
|
348
|
+
* `returning` and the serialization of this package. Decorated classes registered
|
|
349
|
+
* through `addModels()` are the usual route, and the one `database()` takes.
|
|
350
|
+
*
|
|
351
|
+
* @param modelName - Name to register the model under.
|
|
352
|
+
* @param attributes - Column definitions, as sequelize's own `define` takes them.
|
|
353
|
+
* @param options - Model options. `modelName` and `sequelize` are filled in.
|
|
354
|
+
* @returns The generated model class.
|
|
355
|
+
*/
|
|
356
|
+
define<TInstance, _TAttributes>(modelName: string, attributes: ModelAttributes, options?: ModelOptions): any;
|
|
357
|
+
/**
|
|
358
|
+
* Creates every table, then every view, then every column index.
|
|
359
|
+
*
|
|
360
|
+
* @remarks
|
|
361
|
+
* Sequelize's own sync knows about tables only. This one runs it first, then
|
|
362
|
+
* replaces the views declared with `View` or `DynamicView`, then creates the
|
|
363
|
+
* indices declared with `ColumnIndex` or `NullableIndex`. Views come before
|
|
364
|
+
* indices so an index declared on a materialized view has something to attach to,
|
|
365
|
+
* and both come after the tables a view selects from.
|
|
366
|
+
*
|
|
367
|
+
* The three passes used to overlap. The index pass was started but never waited
|
|
368
|
+
* for and its result was discarded, so this resolved while indices were still
|
|
369
|
+
* being created, and a failure in that pass became an unhandled rejection —
|
|
370
|
+
* fatal on any current Node — instead of rejecting here. The options were not
|
|
371
|
+
* forwarded to the view pass either, which left `withoutDrop` doing nothing. All
|
|
372
|
+
* three are fixed.
|
|
373
|
+
*
|
|
374
|
+
* @param options - Sequelize's own sync options, plus `withNoViews` and
|
|
375
|
+
* `withoutDrop`.
|
|
376
|
+
* @returns The connection, once every pass has finished.
|
|
377
|
+
*/
|
|
378
|
+
sync(options?: SyncOptions): Promise<any>;
|
|
379
|
+
/**
|
|
380
|
+
* Creates the column indices declared across every registered model.
|
|
381
|
+
*
|
|
382
|
+
* @remarks
|
|
383
|
+
* Run by `sync()` once the tables and views are in place. Only models that
|
|
384
|
+
* declare at least one index are visited, and the models are done concurrently.
|
|
385
|
+
*
|
|
386
|
+
* @param options - Passed to each model, which does not currently read it.
|
|
387
|
+
* @returns Resolves once every index of every model exists.
|
|
388
|
+
*/
|
|
389
|
+
syncIndices(options?: SyncOptions): Promise<any>;
|
|
390
|
+
/**
|
|
391
|
+
* Replaces every model that is declared as a view.
|
|
392
|
+
*
|
|
393
|
+
* @remarks
|
|
394
|
+
* Each view is dropped and created again unless `withoutDrop` is set, and the
|
|
395
|
+
* views are done concurrently — so a view that selects from another view has no
|
|
396
|
+
* ordering guarantee, and the drop of the one it depends on would fail anyway.
|
|
397
|
+
* That combination is what `withoutDrop` is for.
|
|
398
|
+
*
|
|
399
|
+
* @param options - `withoutDrop` skips the drop; nothing else is read.
|
|
400
|
+
* @returns Resolves once every view has been replaced.
|
|
401
|
+
*/
|
|
402
|
+
syncViews(options?: SyncOptions): Promise<any>;
|
|
403
|
+
/**
|
|
404
|
+
* The registered models that declare at least one column index.
|
|
405
|
+
*
|
|
406
|
+
* @returns Model classes with a non-empty `indices` option, in registration
|
|
407
|
+
* order.
|
|
408
|
+
*/
|
|
409
|
+
getModelsWithIndices(): (typeof BaseModel)[];
|
|
410
|
+
/**
|
|
411
|
+
* The registered models that are declared as views.
|
|
412
|
+
*
|
|
413
|
+
* @remarks
|
|
414
|
+
* A model counts as a view once `View` or `DynamicView` has put `treatAsView` in
|
|
415
|
+
* its options, which is the same flag that makes the table sync skip it and
|
|
416
|
+
* `drop()` issue `DROP VIEW`.
|
|
417
|
+
*
|
|
418
|
+
* @returns Model classes flagged as views, in registration order.
|
|
419
|
+
*/
|
|
420
|
+
getViews(): (typeof BaseModel)[];
|
|
421
|
+
/**
|
|
422
|
+
* Runs a raw statement, honouring a `returning` column list.
|
|
423
|
+
*
|
|
424
|
+
* @remarks
|
|
425
|
+
* Given a non-empty `returning` array, the `RETURNING *` in the statement is
|
|
426
|
+
* rewritten to name exactly those columns, and every returned model instance
|
|
427
|
+
* remembers the list, so serializing it emits only those properties. Everything
|
|
428
|
+
* else is sequelize's own `query()`.
|
|
429
|
+
*
|
|
430
|
+
* The rewrite is textual and looks for `RETURNING *` specifically, so a statement
|
|
431
|
+
* that already names its columns is left alone — and one that has no `RETURNING`
|
|
432
|
+
* at all is not given one.
|
|
433
|
+
*
|
|
434
|
+
* @param sqlQuery - The statement, or a statement with its bind values.
|
|
435
|
+
* @param options - Query options, where `returning` may be a column list.
|
|
436
|
+
* @returns Whatever sequelize's own `query()` returns for these options.
|
|
437
|
+
*/
|
|
438
|
+
query(sqlQuery: string | {
|
|
439
|
+
query: string;
|
|
440
|
+
values: any[];
|
|
441
|
+
}, options?: QueryOptions | QueryOptionsWithType<QueryTypes.RAW>): Promise<any>;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* The class every model in an `@imqueue` service extends.
|
|
445
|
+
*
|
|
446
|
+
* @remarks
|
|
447
|
+
* Sequelize's own `Model` with four additions.
|
|
448
|
+
*
|
|
449
|
+
* Views: a model declared with `View` or `DynamicView` is created and dropped as a
|
|
450
|
+
* view, skipped by the table sync, and has its numeric columns cast back to numbers
|
|
451
|
+
* after every finder, since a view returns them as strings.
|
|
452
|
+
*
|
|
453
|
+
* Indices: `ColumnIndex` and `NullableIndex` declarations become `CREATE INDEX`
|
|
454
|
+
* statements at sync time, which plain sequelize cannot express.
|
|
455
|
+
*
|
|
456
|
+
* Serialization: `toJSON()` honours the `returning` column list left behind by the
|
|
457
|
+
* last write, and picks up associated instances that the loaded attributes do not
|
|
458
|
+
* already cover.
|
|
459
|
+
*
|
|
460
|
+
* Associations as a graph: `toGraph()` walks them transitively, so a cycle can be
|
|
461
|
+
* found before a query walks into it.
|
|
462
|
+
*
|
|
463
|
+
* @example
|
|
464
|
+
* ```typescript
|
|
465
|
+
* @Table
|
|
466
|
+
* export class Lead extends BaseModel<Lead> {
|
|
467
|
+
* @PrimaryKey
|
|
468
|
+
* @AutoIncrement
|
|
469
|
+
* @Column(DataType.BIGINT)
|
|
470
|
+
* public readonly id: number;
|
|
471
|
+
*
|
|
472
|
+
* @AllowNull(false)
|
|
473
|
+
* @Column(DataType.STRING)
|
|
474
|
+
* public name: string;
|
|
475
|
+
*
|
|
476
|
+
* @CreatedBy()
|
|
477
|
+
* @Column(DataType.BIGINT)
|
|
478
|
+
* public createdBy: number;
|
|
479
|
+
* }
|
|
480
|
+
* ```
|
|
481
|
+
*/
|
|
482
|
+
export declare abstract class BaseModel<T> extends Model<BaseModel<T>> {
|
|
483
|
+
/**
|
|
484
|
+
* Drops this model's relation: `DROP VIEW` for a view, `DROP TABLE` otherwise.
|
|
485
|
+
*
|
|
486
|
+
* @param options - Sequelize's own drop options; `cascade` applies to both.
|
|
487
|
+
* @returns The result of the drop statement.
|
|
488
|
+
*/
|
|
489
|
+
static drop(options?: DropOptions): Promise<any>;
|
|
490
|
+
/**
|
|
491
|
+
* Creates this model's table, or does nothing when the model is a view.
|
|
492
|
+
*
|
|
493
|
+
* @remarks
|
|
494
|
+
* A view is skipped deliberately: it normally selects from tables that do not
|
|
495
|
+
* exist yet, so views are left to the second pass `Sequelize.sync()` runs once
|
|
496
|
+
* every table is there.
|
|
497
|
+
*
|
|
498
|
+
* @param options - Sequelize's own sync options.
|
|
499
|
+
* @returns Resolves when the table exists, or immediately for a view.
|
|
500
|
+
*/
|
|
501
|
+
static sync(options?: SyncOptions): Promise<any>;
|
|
502
|
+
/**
|
|
503
|
+
* Creates this view, replacing whatever definition is in the database.
|
|
504
|
+
*
|
|
505
|
+
* @remarks
|
|
506
|
+
* Drops the view first unless `withoutDrop` is set. That drop is why a view
|
|
507
|
+
* another view selects from cannot be replaced this way, and why `withoutDrop`
|
|
508
|
+
* exists.
|
|
509
|
+
*
|
|
510
|
+
* Only meaningful on a model declared with `View` or `DynamicView` — anything
|
|
511
|
+
* else has no definition to create.
|
|
512
|
+
*
|
|
513
|
+
* @param options - `withoutDrop` skips the drop.
|
|
514
|
+
* @returns The result of the create statement.
|
|
515
|
+
*/
|
|
516
|
+
static syncView(options?: SyncOptions): Promise<any>;
|
|
517
|
+
/**
|
|
518
|
+
* This view's SQL, with any dynamic parameters substituted in.
|
|
519
|
+
*
|
|
520
|
+
* @remarks
|
|
521
|
+
* Parameters are merged in two layers: the defaults given to `DynamicView`, then
|
|
522
|
+
* whatever is passed here. Each `@{name}` placeholder in the definition is
|
|
523
|
+
* replaced with the escaped value, which is what makes it safe to pass a caller's
|
|
524
|
+
* input — though only numbers and strings render as values, and everything else,
|
|
525
|
+
* including a missing parameter, becomes `NULL`.
|
|
526
|
+
*
|
|
527
|
+
* @param viewParams - Values overriding the decorated defaults.
|
|
528
|
+
* @param asQuery - Strips the leading create-view clause, leaving a statement
|
|
529
|
+
* that can be embedded as a subquery. This is how a dynamic view is spliced
|
|
530
|
+
* into a `FROM` or a join.
|
|
531
|
+
* @returns The definition, whitespace-normalised and ending in a semicolon.
|
|
532
|
+
*/
|
|
533
|
+
static getViewDefinition(viewParams?: ViewParams, asQuery?: boolean): string;
|
|
534
|
+
/**
|
|
535
|
+
* Creates every column index declared on this model.
|
|
536
|
+
*
|
|
537
|
+
* @remarks
|
|
538
|
+
* Reads the declarations left behind by `ColumnIndex` and `NullableIndex` and
|
|
539
|
+
* creates them concurrently. A model that declares none resolves immediately —
|
|
540
|
+
* it used to throw, which made this unsafe to call on anything but a model
|
|
541
|
+
* `Sequelize.getModelsWithIndices()` had already picked out.
|
|
542
|
+
*
|
|
543
|
+
* @param _options - Accepted for symmetry with the other sync methods, not read.
|
|
544
|
+
* @returns Resolves once every index of this model exists.
|
|
545
|
+
*/
|
|
546
|
+
static syncIndices(_options?: SyncOptions): Promise<any>;
|
|
547
|
+
/**
|
|
548
|
+
* Builds and runs the statements that create one column index.
|
|
549
|
+
*
|
|
550
|
+
* @remarks
|
|
551
|
+
* Each `ColumnIndexOptions` field maps to a clause of the `CREATE INDEX`. The
|
|
552
|
+
* index is dropped first unless `safe` is set, so the declaration in the code
|
|
553
|
+
* always wins over what is in the database; with `safe`, an existing index is
|
|
554
|
+
* left exactly as it is and only a missing one is created.
|
|
555
|
+
*
|
|
556
|
+
* Five of those options used to be placed where Postgres does not accept them —
|
|
557
|
+
* `USING` ahead of `ON`, and `COLLATE`, the operator class, the sort order and
|
|
558
|
+
* the nulls position after the closing parenthesis rather than inside it — so
|
|
559
|
+
* `method`, `collation`, `opClass`, `order` and `nullsFirst` could not be used at
|
|
560
|
+
* all. `include` was declared and never emitted. Both are fixed, and the reason
|
|
561
|
+
* neither was noticed is the promise bug below: the statement failed, and the
|
|
562
|
+
* rejection went nowhere.
|
|
563
|
+
*
|
|
564
|
+
* The two statements used to be attached to the same already-resolved promise
|
|
565
|
+
* rather than chained onto each other, so they were issued together and this
|
|
566
|
+
* returned before either had run — the create could reach the server ahead of the
|
|
567
|
+
* drop, and a failure of either became an unhandled rejection rather than
|
|
568
|
+
* rejecting here. They now run in order, and the returned promise waits for them.
|
|
569
|
+
*
|
|
570
|
+
* @param column - Column the index is declared on.
|
|
571
|
+
* @param options - The declared index options.
|
|
572
|
+
* @param position - Ordinal used to name the index when `name` is not given,
|
|
573
|
+
* which is what keeps two indices on one column from colliding.
|
|
574
|
+
* @returns Resolves once the index exists.
|
|
575
|
+
*/
|
|
576
|
+
static syncIndex(column: string, options: ColumnIndexOptions, position: number): Promise<any>;
|
|
577
|
+
/**
|
|
578
|
+
* Search for multiple instances.
|
|
579
|
+
*
|
|
580
|
+
* @remarks
|
|
581
|
+
* Delegates to Sequelize's own `findAll`, then re-maps numeric columns, which
|
|
582
|
+
* a view returns as strings.
|
|
583
|
+
*
|
|
584
|
+
* @param options - Find options; `viewParams` applies to a dynamic view.
|
|
585
|
+
* @returns The matching instances.
|
|
586
|
+
*/
|
|
587
|
+
static findAll<M>(options?: FindOptions): Promise<M[]>;
|
|
588
|
+
/**
|
|
589
|
+
* Finds one instance by primary key.
|
|
590
|
+
*
|
|
591
|
+
* @remarks
|
|
592
|
+
* Sequelize's own `findByPk` with one addition: on a model declared as a view, the
|
|
593
|
+
* numeric columns of the result are cast back to numbers.
|
|
594
|
+
*
|
|
595
|
+
* @param identifier - Primary key value to look for.
|
|
596
|
+
* @param options - Find options, minus `where`, which the key supplies.
|
|
597
|
+
* @returns The instance, or `null` when no row matches.
|
|
598
|
+
*/
|
|
599
|
+
static findByPk<M>(identifier?: Identifier, options?: Omit<FindOptions, 'where'>): Promise<M | null>;
|
|
600
|
+
/**
|
|
601
|
+
* Finds the first instance matching the options.
|
|
602
|
+
*
|
|
603
|
+
* @remarks
|
|
604
|
+
* Sequelize's own `findOne` with one addition: on a model declared as a view, the
|
|
605
|
+
* numeric columns of the result are cast back to numbers.
|
|
606
|
+
*
|
|
607
|
+
* @param options - Find options; `viewParams` applies to a dynamic view.
|
|
608
|
+
* @returns The instance, or `null` when no row matches.
|
|
609
|
+
*/
|
|
610
|
+
static findOne<M>(options?: FindOptions): Promise<M | null>;
|
|
611
|
+
/**
|
|
612
|
+
* Forgets the `returning` column list, so serializing emits every loaded column
|
|
613
|
+
* again.
|
|
614
|
+
*
|
|
615
|
+
* @remarks
|
|
616
|
+
* A write that named its returning columns leaves that list on the instance, and
|
|
617
|
+
* `toJSON()` honours it from then on. Call this when the same instance is reused
|
|
618
|
+
* for something that should serialize in full.
|
|
619
|
+
*
|
|
620
|
+
* @returns This instance, for chaining.
|
|
621
|
+
*/
|
|
622
|
+
restoreSerialization(): this;
|
|
623
|
+
/**
|
|
624
|
+
* Attaches related data to this instance as though it had been joined in.
|
|
625
|
+
*
|
|
626
|
+
* @remarks
|
|
627
|
+
* Sets the property, and when a `returning` list is in force adds the name to it
|
|
628
|
+
* so the property survives serialization. That is the point of it: data fetched
|
|
629
|
+
* by a second query would otherwise be dropped by `toJSON()`.
|
|
630
|
+
*
|
|
631
|
+
* @param name - Property to set, normally an association name.
|
|
632
|
+
* @param data - What to attach: an instance, an array of them, or anything else.
|
|
633
|
+
* @returns This instance, for chaining.
|
|
634
|
+
*/
|
|
635
|
+
appendChild(name: string, data: any): this;
|
|
636
|
+
/**
|
|
637
|
+
* Serializes this instance, honouring the `returning` column list.
|
|
638
|
+
*
|
|
639
|
+
* @remarks
|
|
640
|
+
* Sequelize's own `toJSON()` emits the loaded attributes. This one also picks up
|
|
641
|
+
* an associated instance, or an array of them, that those attributes do not
|
|
642
|
+
* already cover, and then — if the last write named the columns to return — drops
|
|
643
|
+
* every property that was not named.
|
|
644
|
+
*
|
|
645
|
+
* @returns A plain object, ready to send over the wire.
|
|
646
|
+
*/
|
|
647
|
+
toJSON(): any;
|
|
648
|
+
/**
|
|
649
|
+
* Casts this instance's numeric columns back to numbers.
|
|
650
|
+
*
|
|
651
|
+
* @remarks
|
|
652
|
+
* Numeric columns selected through a view arrive as strings, so the finders on
|
|
653
|
+
* this class re-cast them after every select on a view, and this is the method
|
|
654
|
+
* they use. It is public for the cases they do not cover, a raw query being the
|
|
655
|
+
* common one.
|
|
656
|
+
*
|
|
657
|
+
* Only the columns the model declares are looked at, and the instance is changed
|
|
658
|
+
* in place.
|
|
659
|
+
*
|
|
660
|
+
* @returns This instance, for chaining.
|
|
661
|
+
*/
|
|
662
|
+
fixNumbers(): BaseModel<T>;
|
|
663
|
+
/**
|
|
664
|
+
* Serializes one property that sequelize's own `toJSON()` left out.
|
|
665
|
+
*
|
|
666
|
+
* @param prop - Property to check.
|
|
667
|
+
* @param serialized - Result being built, added to in place.
|
|
668
|
+
*/
|
|
669
|
+
private verifyProperty;
|
|
670
|
+
/**
|
|
671
|
+
* Serializes an array property element by element.
|
|
672
|
+
*
|
|
673
|
+
* @param arr - Array to serialize.
|
|
674
|
+
* @param prop - Property it belongs to.
|
|
675
|
+
* @param serialized - Result being built, added to in place.
|
|
676
|
+
*/
|
|
677
|
+
private verifyArray;
|
|
678
|
+
/**
|
|
679
|
+
* Builds a graph of this model's associations, following them transitively.
|
|
680
|
+
*
|
|
681
|
+
* @remarks
|
|
682
|
+
* Every association becomes an edge, and a many-to-many contributes two: this
|
|
683
|
+
* model to the through-model, and the through-model to the target. Each target is
|
|
684
|
+
* then walked in turn, and an association already present as an edge is not
|
|
685
|
+
* followed again, which is what stops a cycle from recursing forever.
|
|
686
|
+
*
|
|
687
|
+
* The reason to have it is to find those cycles before a query does — a cycle in
|
|
688
|
+
* the graph is a query that can be asked to include itself.
|
|
689
|
+
*
|
|
690
|
+
* @param graph - Graph to add to. A fresh one by default, and passing the same
|
|
691
|
+
* one is what makes the recursive calls accumulate into a single graph.
|
|
692
|
+
* @returns The graph, so the outermost call can use it.
|
|
693
|
+
*/
|
|
694
|
+
static toGraph(graph?: Graph<typeof BaseModel>): Graph<typeof BaseModel>;
|
|
695
|
+
}
|