diff --git a/lib/errors/aggregate-error.ts b/lib/errors/aggregate-error.ts index 59a69c13eaf6..d1e6fb1235de 100644 --- a/lib/errors/aggregate-error.ts +++ b/lib/errors/aggregate-error.ts @@ -3,10 +3,11 @@ import BaseError from './base-error'; /** * A wrapper for multiple Errors * - * @param errors Array of errors + * @param errors The aggregated errors that occurred */ class AggregateError extends BaseError { - errors: Array; + /** the aggregated errors that occurred */ + readonly errors: Array; constructor(errors: Array) { super(); diff --git a/lib/errors/base-error.ts b/lib/errors/base-error.ts index 74cca10f295a..3e36660d0711 100644 --- a/lib/errors/base-error.ts +++ b/lib/errors/base-error.ts @@ -14,11 +14,12 @@ export interface CommonErrorProperties { } /** + * The Base Error all Sequelize Errors inherit from. + * * Sequelize provides a host of custom error classes, to allow you to do easier debugging. All of these errors are exposed on the sequelize object and the sequelize constructor. * All sequelize errors inherit from the base JS error object. * * This means that errors can be accessed using `Sequelize.ValidationError` - * The Base Error all Sequelize Errors inherit from. */ abstract class BaseError extends Error { constructor(message?: string) { diff --git a/lib/errors/bulk-record-error.ts b/lib/errors/bulk-record-error.ts index ad5ede0b9cda..9951b8b5bd5e 100644 --- a/lib/errors/bulk-record-error.ts +++ b/lib/errors/bulk-record-error.ts @@ -1,5 +1,5 @@ +import type { Model } from '../..'; import BaseError from './base-error'; -import type { Model } from '../../types/lib/model'; /** * Thrown when bulk operation fails, it represent per record level error. diff --git a/lib/errors/database-error.ts b/lib/errors/database-error.ts index d4dd00a474ef..944052588ade 100644 --- a/lib/errors/database-error.ts +++ b/lib/errors/database-error.ts @@ -24,6 +24,10 @@ class DatabaseError sql: string; parameters: object; + /** + * @param parent The database specific error which triggered this one + * @param options + */ constructor(parent: DatabaseErrorParent, options: ErrorOptions = {}) { super(parent.message); this.name = 'SequelizeDatabaseError'; diff --git a/lib/errors/database/exclusion-constraint-error.ts b/lib/errors/database/exclusion-constraint-error.ts index 715bba245826..e2b575eae17f 100644 --- a/lib/errors/database/exclusion-constraint-error.ts +++ b/lib/errors/database/exclusion-constraint-error.ts @@ -1,21 +1,18 @@ import DatabaseError, { DatabaseErrorSubclassOptions } from '../database-error'; interface ExclusionConstraintErrorOptions { - constraint: string; - fields: Record; - table: string; + constraint?: string; + fields?: Record; + table?: string; } /** * Thrown when an exclusion constraint is violated in the database */ -class ExclusionConstraintError - extends DatabaseError - implements ExclusionConstraintErrorOptions -{ - constraint: string; - fields: Record; - table: string; +class ExclusionConstraintError extends DatabaseError implements ExclusionConstraintErrorOptions { + constraint: string | undefined; + fields: Record | undefined; + table: string | undefined; constructor( options: DatabaseErrorSubclassOptions & ExclusionConstraintErrorOptions diff --git a/lib/errors/database/foreign-key-constraint-error.ts b/lib/errors/database/foreign-key-constraint-error.ts index 8ffb38cfdc15..9b6a1335f972 100644 --- a/lib/errors/database/foreign-key-constraint-error.ts +++ b/lib/errors/database/foreign-key-constraint-error.ts @@ -6,22 +6,22 @@ export enum RelationshipType { } interface ForeignKeyConstraintErrorOptions { - table: string; - fields: { [field: string]: string }; - value: unknown; - index: string; - reltype: RelationshipType; + table?: string; + fields?: { [field: string]: string }; + value?: unknown; + index?: string; + reltype?: RelationshipType; } /** * Thrown when a foreign key constraint is violated in the database */ class ForeignKeyConstraintError extends DatabaseError { - table: string; - fields: { [field: string]: string }; + table: string | undefined; + fields: { [field: string]: string } | undefined; value: unknown; - index: string; - reltype: RelationshipType; + index: string | undefined; + reltype: RelationshipType | undefined; constructor( options: ForeignKeyConstraintErrorOptions & DatabaseErrorSubclassOptions diff --git a/lib/errors/database/unknown-constraint-error.ts b/lib/errors/database/unknown-constraint-error.ts index 69d1ff0c2b1b..517fd380db6f 100644 --- a/lib/errors/database/unknown-constraint-error.ts +++ b/lib/errors/database/unknown-constraint-error.ts @@ -1,21 +1,18 @@ import DatabaseError, { DatabaseErrorSubclassOptions } from '../database-error'; interface UnknownConstraintErrorOptions { - constraint: string; - fields: Record; - table: string; + constraint?: string; + fields?: Record; + table?: string; } /** * Thrown when constraint name is not found in the database */ -class UnknownConstraintError - extends DatabaseError - implements UnknownConstraintErrorOptions -{ - constraint: string; - fields: Record; - table: string; +class UnknownConstraintError extends DatabaseError implements UnknownConstraintErrorOptions { + constraint: string | undefined; + fields: Record | undefined; + table: string | undefined; constructor( options: UnknownConstraintErrorOptions & DatabaseErrorSubclassOptions diff --git a/lib/errors/optimistic-lock-error.ts b/lib/errors/optimistic-lock-error.ts index 17e6d025b52a..45aebb5ece07 100644 --- a/lib/errors/optimistic-lock-error.ts +++ b/lib/errors/optimistic-lock-error.ts @@ -15,9 +15,9 @@ interface OptimisticLockErrorOptions { * Thrown when attempting to update a stale model instance */ class OptimisticLockError extends BaseError { - modelName?: string; - values?: Record; - where?: Record; + modelName: string | undefined; + values: Record | undefined; + where: Record | undefined; constructor(options: OptimisticLockErrorOptions) { options = options || {}; diff --git a/lib/errors/validation-error.ts b/lib/errors/validation-error.ts index 70e788e82d9a..5fcc94200142 100644 --- a/lib/errors/validation-error.ts +++ b/lib/errors/validation-error.ts @@ -1,5 +1,6 @@ -import BaseError, { ErrorOptions } from './base-error'; -import type { Model } from '../../types/lib/model'; +import type { Model } from '../..'; +import type { ErrorOptions } from './base-error'; +import BaseError from './base-error'; /** * An enum that is used internally by the `ValidationErrorItem` class @@ -51,44 +52,44 @@ export class ValidationErrorItem { /** * An error message */ - message: string; + readonly message: string; /** * The type/origin of the validation error */ - type: keyof typeof ValidationErrorItemType | null; + readonly type: keyof typeof ValidationErrorItemType | null; /** * The field that triggered the validation error */ - path: string | null; + readonly path: string | null; /** * The value that generated the error */ - value: string | null; + readonly value: string | null; - origin: keyof typeof ValidationErrorItemOrigin | null; + readonly origin: keyof typeof ValidationErrorItemOrigin | null; /** * The DAO instance that caused the validation error */ - instance: Model | null; + readonly instance: Model | null; /** * A validation "key", used for identification */ - validatorKey: string | null; + readonly validatorKey: string | null; /** * Property name of the BUILT-IN validator function that caused the validation error (e.g. "in" or "len"), if applicable */ - validatorName: string | null; + readonly validatorName: string | null; /** * Parameters used with the BUILT-IN validator function, if applicable */ - validatorArgs: unknown[]; + readonly validatorArgs: unknown[]; /** * Creates a new ValidationError item. Instances of this class are included in the `ValidationError.errors` property. @@ -202,7 +203,8 @@ export class ValidationErrorItem { * @param errors Array of ValidationErrorItem objects describing the validation errors */ class ValidationError extends BaseError { - errors: ValidationErrorItem[]; + /** Array of ValidationErrorItem objects describing the validation errors */ + readonly errors: ValidationErrorItem[]; constructor( message: string, diff --git a/lib/errors/validation/unique-constraint-error.ts b/lib/errors/validation/unique-constraint-error.ts index 11b208031b9b..ef91c15cfd0c 100644 --- a/lib/errors/validation/unique-constraint-error.ts +++ b/lib/errors/validation/unique-constraint-error.ts @@ -5,7 +5,7 @@ interface UniqueConstraintErrorParent extends Error, Pick {} -interface UniqueConstraintErrorOptions extends ErrorOptions { +export interface UniqueConstraintErrorOptions extends ErrorOptions { parent?: UniqueConstraintErrorParent; original?: UniqueConstraintErrorParent; errors?: ValidationErrorItem[]; @@ -16,14 +16,11 @@ interface UniqueConstraintErrorOptions extends ErrorOptions { /** * Thrown when a unique constraint is violated in the database */ -class UniqueConstraintError - extends ValidationError - implements CommonErrorProperties -{ - parent: UniqueConstraintErrorParent; - original: UniqueConstraintErrorParent; - fields: Record; - sql: string; +class UniqueConstraintError extends ValidationError implements CommonErrorProperties { + readonly parent: UniqueConstraintErrorParent; + readonly original: UniqueConstraintErrorParent; + readonly fields: Record; + readonly sql: string; constructor(options: UniqueConstraintErrorOptions) { options = options ?? {}; @@ -34,7 +31,6 @@ class UniqueConstraintError super(options.message, options.errors, { stack: options.stack }); this.name = 'SequelizeUniqueConstraintError'; - this.errors = options.errors; this.fields = options.fields ?? {}; this.parent = options.parent; this.original = options.parent; diff --git a/types/index.d.ts b/types/index.d.ts index efb34b39dc8c..418bbfbf7d23 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -8,8 +8,8 @@ import Utils = require("./lib/utils"); export * from "./lib/associations/index"; export * from "./lib/data-types"; -export * from "./lib/errors"; -export { BaseError as Error } from "./lib/errors"; +export * from "../lib/errors"; +export { BaseError as Error } from "../lib/errors"; export * from "./lib/model"; export * from "./lib/query-interface"; export * from "./lib/sequelize"; diff --git a/types/lib/errors.d.ts b/types/lib/errors.d.ts deleted file mode 100644 index 2f8340b14113..000000000000 --- a/types/lib/errors.d.ts +++ /dev/null @@ -1,262 +0,0 @@ -import Model from "./model"; - -/** - * The Base Error all Sequelize Errors inherit from. - */ -export class BaseError extends Error { - public name: string; -} - -/** - * Scope Error. Thrown when the sequelize cannot query the specified scope. - */ -export class SequelizeScopeError extends BaseError {} - -export class ValidationError extends BaseError { - /** Array of ValidationErrorItem objects describing the validation errors */ - public readonly errors: ValidationErrorItem[]; - - /** - * Validation Error. Thrown when the sequelize validation has failed. The error contains an `errors` - * property, which is an array with 1 or more ValidationErrorItems, one for each validation that failed. - * - * @param message Error message - * @param errors Array of ValidationErrorItem objects describing the validation errors - */ - constructor(message: string, errors?: ValidationErrorItem[]); - - /** - * Gets all validation error items for the path / field specified. - * - * @param path The path to be checked for error items - */ - public get(path: string): ValidationErrorItem[]; -} - -/** - * An enum that is used internally by the `ValidationErrorItem` class - * that maps current `type` strings (as given to ValidationErrorItem.constructor()) to - * our new `origin` values. - */ -export enum ValidationErrorItemType { - 'notnull violation' = 'CORE', - 'string violation' = 'CORE', - 'unique violation' = 'DB', - 'validation error' = 'FUNCTION', -} - -/** - * An enum that defines valid ValidationErrorItem `origin` values - */ -export enum ValidationErrorItemOrigin { - /** - * specifies errors that originate from the sequelize "core" - */ - CORE = 'CORE', - - /** - * specifies validation errors that originate from the storage engine - */ - DB = 'DB', - - /** - * specifies validation errors that originate from validator functions (both built-in and custom) defined for a given attribute - */ - FUNCTION = 'FUNCTION', -} - -export class ValidationErrorItem { - /** An error message */ - public readonly message: string; - - /** The type/origin of the validation error */ - public readonly type: string | null; - - /** The field that triggered the validation error */ - public readonly path: string | null; - - /** The value that generated the error */ - public readonly value: string | null; - - /** The DAO instance that caused the validation error */ - public readonly instance: Model | null; - - /** A validation "key", used for identification */ - public readonly validatorKey: string | null; - - /** Property name of the BUILT-IN validator function that caused the validation error (e.g. "in" or "len"), if applicable */ - public readonly validatorName: string | null; - - /** Parameters used with the BUILT-IN validator function, if applicable */ - public readonly validatorArgs: unknown[]; - - public readonly original: Error; - - /** - * Creates a new ValidationError item. Instances of this class are included in the `ValidationError.errors` property. - * - * @param message An error message - * @param type The type/origin of the validation error - * @param path The field that triggered the validation error - * @param value The value that generated the error - * @param instance the DAO instance that caused the validation error - * @param validatorKey a validation "key", used for identification - * @param fnName property name of the BUILT-IN validator function that caused the validation error (e.g. "in" or "len"), if applicable - * @param fnArgs parameters used with the BUILT-IN validator function, if applicable - */ - constructor( - message?: string, - type?: string, - path?: string, - value?: string, - instance?: object, - validatorKey?: string, - fnName?: string, - fnArgs?: unknown[] - ); -} - -export interface CommonErrorProperties { - /** The database specific error which triggered this one */ - readonly parent: Error; - - /** The database specific error which triggered this one */ - readonly original: Error; - - /** The SQL that triggered the error */ - readonly sql: string; -} - -/** - * Thrown when a record was not found, Usually used with rejectOnEmpty mode (see message for details) - */ -export class EmptyResultError extends BaseError { -} - -export class DatabaseError extends BaseError implements CommonErrorProperties { - public readonly parent: Error; - public readonly original: Error; - public readonly sql: string; - public readonly parameters: Array; - /** - * A base class for all database related errors. - * @param parent The database specific error which triggered this one - */ - constructor(parent: Error); -} - -/** Thrown when a database query times out because of a deadlock */ -export class TimeoutError extends DatabaseError {} - -export interface UniqueConstraintErrorOptions { - parent?: Error; - message?: string; - errors?: ValidationErrorItem[]; - fields?: { [key: string]: unknown }; - original?: Error; -} - -/** - * Thrown when a unique constraint is violated in the database - */ -export class UniqueConstraintError extends ValidationError implements CommonErrorProperties { - public readonly parent: Error; - public readonly original: Error; - public readonly sql: string; - public readonly fields: { [key: string]: unknown }; - constructor(options?: UniqueConstraintErrorOptions); -} - -/** - * Thrown when a foreign key constraint is violated in the database - */ -export class ForeignKeyConstraintError extends DatabaseError { - public table: string; - public fields: { [field: string]: string }; - public value: unknown; - public index: string; - constructor(options: { parent?: Error; message?: string; index?: string; fields?: string[]; table?: string }); -} - -/** - * Thrown when an exclusion constraint is violated in the database - */ -export class ExclusionConstraintError extends DatabaseError { - public constraint: string; - public fields: { [field: string]: string }; - public table: string; - constructor(options: { parent?: Error; message?: string; constraint?: string; fields?: string[]; table?: string }); -} - -/** - * Thrown when constraint name is not found in the database - */ -export class UnknownConstraintError extends DatabaseError { - public constraint: string; - public fields: { [field: string]: string }; - public table: string; - constructor(options: { parent?: Error; message?: string; constraint?: string; fields?: string[]; table?: string }); -} - -/** - * Thrown when attempting to update a stale model instance - */ -export class OptimisticLockError extends BaseError { - constructor(options: { message?: string, modelName?: string, values?: { [key: string]: any }, where?: { [key: string]: any } }); -} - -/** - * A base class for all connection related errors. - */ -export class ConnectionError extends BaseError { - public parent: Error; - public original: Error; - constructor(parent: Error); -} - -/** - * Thrown when a connection to a database is refused - */ -export class ConnectionRefusedError extends ConnectionError {} - -/** - * Thrown when a connection to a database is refused due to insufficient privileges - */ -export class AccessDeniedError extends ConnectionError {} - -/** - * Thrown when a connection to a database has a hostname that was not found - */ -export class HostNotFoundError extends ConnectionError {} - -/** - * Thrown when a connection to a database has a hostname that was not reachable - */ -export class HostNotReachableError extends ConnectionError {} - -/** - * Thrown when a connection to a database has invalid values for any of the connection parameters - */ -export class InvalidConnectionError extends ConnectionError {} - -/** - * Thrown when a connection to a database times out - */ -export class ConnectionTimedOutError extends ConnectionError {} - -/** - * Thrown when queued operations were aborted because a connection was closed - */ -export class AsyncQueueError extends BaseError {} - -export class AggregateError extends BaseError { - /** - * AggregateError. A wrapper for multiple errors. - * - * @param {Error[]} errors The aggregated errors that occurred - */ - constructor(errors: Error[]); - - /** the aggregated errors that occurred */ - public readonly errors: Error[]; -} diff --git a/types/test/attributes.ts b/types/test/attributes.ts index c01ab6274af8..4c2e5b6f4439 100644 --- a/types/test/attributes.ts +++ b/types/test/attributes.ts @@ -1,4 +1,4 @@ -import { Model } from "sequelize/lib/model"; +import { Model } from "sequelize"; interface UserCreationAttributes { name: string; diff --git a/types/test/errors.ts b/types/test/errors.ts index 253a379b4d59..9cd292d3ce43 100644 --- a/types/test/errors.ts +++ b/types/test/errors.ts @@ -1,6 +1,5 @@ import { expectTypeOf } from "expect-type"; -import { BaseError, EmptyResultError, Error as AliasedBaseError, UniqueConstraintError } from 'sequelize'; -import { OptimisticLockError } from 'sequelize/lib/errors'; +import { BaseError, EmptyResultError, Error as AliasedBaseError, UniqueConstraintError, OptimisticLockError } from 'sequelize'; expectTypeOf().toEqualTypeOf(); expectTypeOf().toHaveProperty('sql').toBeString(); diff --git a/types/test/hooks.ts b/types/test/hooks.ts index 474c149b599c..a9d1820b2dba 100644 --- a/types/test/hooks.ts +++ b/types/test/hooks.ts @@ -1,9 +1,7 @@ import { expectTypeOf } from "expect-type"; -import { FindOptions, Model, QueryOptions, SaveOptions, Sequelize, UpsertOptions } from "sequelize"; +import { FindOptions, Model, QueryOptions, SaveOptions, Sequelize, UpsertOptions, Config, Utils } from "sequelize"; import { ModelHooks } from "sequelize/lib/hooks"; import { AbstractQuery } from "sequelize/lib/query"; -import { Config } from 'sequelize/lib/sequelize'; -import { DeepWriteable } from 'sequelize/lib/utils'; import { SemiDeepWritable } from "./type-helpers/deep-writable"; { @@ -84,7 +82,7 @@ import { SemiDeepWritable } from "./type-helpers/deep-writable"; } { - Sequelize.beforeConnect('name', config => expectTypeOf(config).toEqualTypeOf>()); - Sequelize.beforeConnect(config => expectTypeOf(config).toEqualTypeOf>()); - Sequelize.addHook('beforeConnect', (...args) => { expectTypeOf(args).toEqualTypeOf<[DeepWriteable]>(); }) + Sequelize.beforeConnect('name', config => expectTypeOf(config).toEqualTypeOf>()); + Sequelize.beforeConnect(config => expectTypeOf(config).toEqualTypeOf>()); + Sequelize.addHook('beforeConnect', (...args) => { expectTypeOf(args).toEqualTypeOf<[Utils.DeepWriteable]>(); }) } diff --git a/types/test/model.ts b/types/test/model.ts index 53294e19f1f3..20f94d6c3d02 100644 --- a/types/test/model.ts +++ b/types/test/model.ts @@ -1,6 +1,5 @@ import { expectTypeOf } from "expect-type"; -import { Association, BelongsToManyGetAssociationsMixin, DataTypes, HasOne, Model, Optional, Sequelize } from 'sequelize'; -import { ModelDefined } from 'sequelize/lib/model'; +import { Association, BelongsToManyGetAssociationsMixin, DataTypes, HasOne, Model, Optional, Sequelize, ModelDefined } from 'sequelize'; expectTypeOf().toMatchTypeOf(); class MyModel extends Model { diff --git a/types/test/query-interface.ts b/types/test/query-interface.ts index 58408c298add..ef634daab016 100644 --- a/types/test/query-interface.ts +++ b/types/test/query-interface.ts @@ -1,6 +1,4 @@ -import { DataTypes, Model, fn, literal, col } from 'sequelize'; -// tslint:disable-next-line:no-submodule-imports -import { QueryInterface } from 'sequelize/lib/query-interface'; +import { DataTypes, Model, fn, literal, col, QueryInterface } from 'sequelize'; declare let queryInterface: QueryInterface; diff --git a/types/test/sequelize.ts b/types/test/sequelize.ts index 6ab8df360112..6b17704a8154 100644 --- a/types/test/sequelize.ts +++ b/types/test/sequelize.ts @@ -1,5 +1,4 @@ -import { Config, Sequelize, Model, QueryTypes, ModelCtor, Op } from 'sequelize'; -import { Fn } from 'sequelize/lib/utils'; +import { Config, Sequelize, Model, QueryTypes, ModelCtor, Op, Utils } from 'sequelize'; Sequelize.useCLS({ }); @@ -70,7 +69,7 @@ Sequelize.afterConnect(() => { }); -const rnd: Fn = sequelize.random(); +const rnd: Utils.Fn = sequelize.random(); class Model1 extends Model{} class Model2 extends Model{}