objection 3.9.1 → 4.0.0-rc.2
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/dbErrors/LICENSE +21 -0
- package/lib/dbErrors/errorCodes/mssql.js +19 -0
- package/lib/dbErrors/errorCodes/mysql.js +805 -0
- package/lib/dbErrors/errorCodes/postgres.js +241 -0
- package/lib/dbErrors/errors/CheckViolationError.js +10 -0
- package/lib/dbErrors/errors/ConstraintViolationError.js +3 -0
- package/lib/dbErrors/errors/DBError.js +9 -0
- package/lib/dbErrors/errors/DataError.js +3 -0
- package/lib/dbErrors/errors/ForeignKeyViolationError.js +11 -0
- package/lib/dbErrors/errors/NotNullViolationError.js +12 -0
- package/lib/dbErrors/errors/UniqueViolationError.js +12 -0
- package/lib/dbErrors/index.js +59 -0
- package/lib/dbErrors/parsers/mssql/DBError/CheckViolationError/parser.js +28 -0
- package/lib/dbErrors/parsers/mssql/DBError/DataError/parser.js +25 -0
- package/lib/dbErrors/parsers/mssql/DBError/ForeignKeyViolationError/parser.js +39 -0
- package/lib/dbErrors/parsers/mssql/DBError/NotNullViolationError/parser.js +30 -0
- package/lib/dbErrors/parsers/mssql/DBError/UniqueViolationError/parser.js +53 -0
- package/lib/dbErrors/parsers/mssql/DBError/parser.js +51 -0
- package/lib/dbErrors/parsers/mssql/DBError/util.js +16 -0
- package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +43 -0
- package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/NotNullViolationError/parser.js +29 -0
- package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/UniqueViolationError/parser.js +27 -0
- package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/parser.js +23 -0
- package/lib/dbErrors/parsers/mysql/DBError/DataError/parser.js +24 -0
- package/lib/dbErrors/parsers/mysql/DBError/parser.js +28 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/CheckViolationError/parser.js +18 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +18 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/NotNullViolationError/parser.js +18 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/UniqueViolationError/parser.js +40 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/parser.js +25 -0
- package/lib/dbErrors/parsers/postgres/DBError/DataError/parser.js +16 -0
- package/lib/dbErrors/parsers/postgres/DBError/parser.js +27 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/CheckViolationError/parser.js +20 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +20 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/NotNullViolationError/parser.js +27 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/UniqueViolationError/parser.js +28 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/parser.js +24 -0
- package/lib/dbErrors/parsers/sqlite/DBError/parser.js +23 -0
- package/lib/dbErrors/utils/sqlState.js +3 -0
- package/lib/initialize.js +1 -7
- package/lib/model/AjvValidator.js +51 -117
- package/lib/model/Model.js +84 -59
- package/lib/model/ModifierNotFoundError.js +1 -7
- package/lib/model/NotFoundError.js +1 -7
- package/lib/model/RelationDoesNotExistError.js +1 -7
- package/lib/model/ValidationError.js +3 -10
- package/lib/model/Validator.js +1 -7
- package/lib/model/getModel.js +7 -6
- package/lib/model/inheritModel.js +1 -7
- package/lib/model/modelBindKnex.js +4 -10
- package/lib/model/modelClone.js +5 -11
- package/lib/model/modelColPropMap.js +3 -10
- package/lib/model/modelId.js +3 -10
- package/lib/model/modelJsonAttributes.js +35 -16
- package/lib/model/modelParseRelations.js +2 -8
- package/lib/model/modelQueryProps.js +11 -23
- package/lib/model/modelSet.js +89 -41
- package/lib/model/modelTableMetadata.js +4 -11
- package/lib/model/modelToJson.js +6 -13
- package/lib/model/modelUtils.js +14 -13
- package/lib/model/modelValidate.js +3 -9
- package/lib/model/modelValues.js +5 -14
- package/lib/model/modelVisitor.js +1 -7
- package/lib/objection.js +33 -71
- package/lib/queryBuilder/FunctionBuilder.js +4 -11
- package/lib/queryBuilder/InternalOptions.js +4 -9
- package/lib/queryBuilder/JoinBuilder.js +5 -8
- package/lib/queryBuilder/QueryBuilder.js +126 -95
- package/lib/queryBuilder/QueryBuilderBase.js +41 -123
- package/lib/queryBuilder/QueryBuilderContext.js +2 -8
- package/lib/queryBuilder/QueryBuilderContextBase.js +2 -8
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +7 -21
- package/lib/queryBuilder/QueryBuilderUserContext.js +1 -7
- package/lib/queryBuilder/RawBuilder.js +5 -13
- package/lib/queryBuilder/ReferenceBuilder.js +20 -48
- package/lib/queryBuilder/RelationExpression.js +18 -16
- package/lib/queryBuilder/StaticHookArguments.js +74 -16
- package/lib/queryBuilder/ValueBuilder.js +11 -13
- package/lib/queryBuilder/getJoinBuilder.js +10 -0
- package/lib/queryBuilder/graph/GraphLoader.js +314 -0
- package/lib/queryBuilder/graph/GraphNode.js +241 -0
- package/lib/queryBuilder/graph/GraphOptions.js +55 -248
- package/lib/queryBuilder/graph/GraphPlan.js +431 -0
- package/lib/queryBuilder/graph/GraphSchedule.js +513 -0
- package/lib/queryBuilder/graph/GraphUpsert.js +519 -412
- package/lib/queryBuilder/graph/GraphWriter.js +508 -0
- package/lib/queryBuilder/graph/graphKeys.js +929 -0
- package/lib/queryBuilder/join/JoinResultColumn.js +1 -7
- package/lib/queryBuilder/join/JoinResultParser.js +6 -11
- package/lib/queryBuilder/join/RelationJoiner.js +66 -84
- package/lib/queryBuilder/join/TableNode.js +2 -8
- package/lib/queryBuilder/join/TableTree.js +3 -9
- package/lib/queryBuilder/join/utils.js +5 -15
- package/lib/queryBuilder/jsonDialects/JsonDialect.js +51 -0
- package/lib/queryBuilder/jsonDialects/MySqlJsonDialect.js +81 -0
- package/lib/queryBuilder/jsonDialects/PostgresJsonDialect.js +45 -0
- package/lib/queryBuilder/jsonDialects/SqliteJsonDialect.js +67 -0
- package/lib/queryBuilder/jsonDialects/index.js +44 -0
- package/lib/queryBuilder/operations/DelegateOperation.js +2 -8
- package/lib/queryBuilder/operations/DeleteOperation.js +10 -39
- package/lib/queryBuilder/operations/FindByIdOperation.js +7 -12
- package/lib/queryBuilder/operations/FindByIdsOperation.js +4 -8
- package/lib/queryBuilder/operations/FindOperation.js +14 -34
- package/lib/queryBuilder/operations/FirstOperation.js +6 -10
- package/lib/queryBuilder/operations/FromOperation.js +3 -9
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +13 -12
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +4 -10
- package/lib/queryBuilder/operations/InsertGraphOperation.js +7 -18
- package/lib/queryBuilder/operations/InsertOperation.js +118 -45
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +5 -12
- package/lib/queryBuilder/operations/InstanceFindOperation.js +4 -11
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +2 -8
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +7 -14
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +5 -11
- package/lib/queryBuilder/operations/KnexOperation.js +2 -8
- package/lib/queryBuilder/operations/LimitPerParentOperation.js +7 -69
- package/lib/queryBuilder/operations/MergeOperation.js +5 -11
- package/lib/queryBuilder/operations/NoneOperation.js +2 -8
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +20 -49
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +2 -8
- package/lib/queryBuilder/operations/OnBuildOperation.js +2 -8
- package/lib/queryBuilder/operations/OnErrorOperation.js +2 -8
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +1 -7
- package/lib/queryBuilder/operations/RangeOperation.js +11 -26
- package/lib/queryBuilder/operations/RelateOperation.js +2 -8
- package/lib/queryBuilder/operations/ReturningOperation.js +3 -9
- package/lib/queryBuilder/operations/RunAfterOperation.js +2 -12
- package/lib/queryBuilder/operations/RunBeforeOperation.js +2 -8
- package/lib/queryBuilder/operations/UnrelateOperation.js +2 -8
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +14 -17
- package/lib/queryBuilder/operations/UpdateOperation.js +32 -132
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +8 -11
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +6 -12
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +3 -9
- package/lib/queryBuilder/operations/WhereGroupOperation.js +2 -8
- package/lib/queryBuilder/operations/eager/EagerOperation.js +3 -9
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +72 -10
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +2 -8
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +23 -44
- package/lib/queryBuilder/operations/fieldExpressions.js +122 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +20 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +44 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonTypeOperation.js +14 -0
- package/lib/queryBuilder/operations/select/SelectOperation.js +11 -21
- package/lib/queryBuilder/operations/select/Selection.js +19 -17
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +4 -10
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +4 -10
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +4 -10
- package/lib/queryBuilder/operations/windowOrder.js +56 -0
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +3 -7
- package/lib/queryBuilder/parsers/relationExpressionParser.js +42 -43
- package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +39 -36
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +2 -8
- package/lib/queryBuilder/transformations/QueryTransformation.js +1 -7
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +3 -9
- package/lib/queryBuilder/transformations/index.js +3 -11
- package/lib/queryBuilder/whereGroups.js +9 -24
- package/lib/queryBuilder/withoutOrder.js +4 -11
- package/lib/relations/Relation.js +47 -28
- package/lib/relations/RelationDeleteOperation.js +4 -14
- package/lib/relations/RelationFindOperation.js +119 -90
- package/lib/relations/RelationInsertOperation.js +2 -8
- package/lib/relations/RelationOwner.js +5 -11
- package/lib/relations/RelationProperty.js +4 -10
- package/lib/relations/RelationUpdateOperation.js +4 -14
- package/lib/relations/RelationWriteResultOperation.js +15 -0
- package/lib/relations/belongsToMany/BelongsToManyDeleteOperation.js +23 -0
- package/lib/relations/belongsToMany/BelongsToManyFindOperation.js +35 -0
- package/lib/relations/belongsToMany/BelongsToManyInsertOperation.js +28 -0
- package/lib/relations/belongsToMany/BelongsToManyRelateOperation.js +16 -0
- package/lib/relations/belongsToMany/BelongsToManyRelation.js +276 -0
- package/lib/relations/belongsToMany/BelongsToManyUnrelateOperation.js +11 -0
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +2 -8
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +6 -11
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +3 -9
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +8 -19
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +53 -27
- package/lib/relations/hasMany/HasManyInsertOperation.js +5 -10
- package/lib/relations/hasMany/HasManyRelateOperation.js +5 -11
- package/lib/relations/hasMany/HasManyRelation.js +9 -11
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +3 -9
- package/lib/relations/hasOne/HasOneRelation.js +2 -8
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +2 -8
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +10 -15
- package/lib/relations/manyToMany/ManyToManyMySqlModifyMixin.js +2 -8
- package/lib/relations/manyToMany/ManyToManyRelation.js +82 -50
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +2 -8
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +5 -9
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +2 -8
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -9
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +20 -59
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +6 -11
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +4 -10
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateMySqlOperation.js +3 -9
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +5 -9
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +3 -9
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +3 -9
- package/lib/relations/manyToMany/update/ManyToManyUpdateMySqlOperation.js +3 -9
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +5 -9
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +64 -72
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -9
- package/lib/transaction.js +3 -9
- package/lib/utils/assert.js +3 -11
- package/lib/utils/buildUtils.js +2 -8
- package/lib/utils/classUtils.js +1 -7
- package/lib/utils/createModifier.js +2 -8
- package/lib/utils/identifierMapping.js +23 -27
- package/lib/utils/internalPropUtils.js +1 -7
- package/lib/utils/knexUtils.js +84 -40
- package/lib/utils/mixin.js +3 -10
- package/lib/utils/normalizeIds.js +2 -8
- package/lib/utils/objectUtils.js +41 -60
- package/lib/utils/parseFieldExpression.js +16 -16
- package/lib/utils/promiseUtils/after.js +2 -8
- package/lib/utils/promiseUtils/afterReturn.js +2 -8
- package/lib/utils/promiseUtils/index.js +7 -16
- package/lib/utils/promiseUtils/isPromise.js +2 -8
- package/lib/utils/promiseUtils/map.js +2 -8
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +2 -8
- package/lib/utils/promiseUtils/try.js +2 -8
- package/lib/utils/resolveModel.js +10 -17
- package/lib/utils/tmpColumnUtils.js +2 -9
- package/lib/utils/warnings.js +2 -14
- package/package.json +25 -17
- package/typings/objection/index.d.ts +3217 -2179
- package/lib/.eslintrc.json +0 -8
- package/lib/model/graph/ModelGraph.js +0 -80
- package/lib/model/graph/ModelGraphBuilder.js +0 -241
- package/lib/model/graph/ModelGraphEdge.js +0 -52
- package/lib/model/graph/ModelGraphNode.js +0 -238
- package/lib/queryBuilder/graph/GraphAction.js +0 -69
- package/lib/queryBuilder/graph/GraphData.js +0 -53
- package/lib/queryBuilder/graph/GraphFetcher.js +0 -197
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +0 -184
- package/lib/queryBuilder/graph/GraphOperation.js +0 -27
- package/lib/queryBuilder/graph/delete/GraphDelete.js +0 -73
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +0 -88
- package/lib/queryBuilder/graph/insert/DeferredRelationGraphInsertAction.js +0 -58
- package/lib/queryBuilder/graph/insert/GraphInsert.js +0 -296
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +0 -233
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +0 -92
- package/lib/queryBuilder/graph/patch/GraphPatch.js +0 -20
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +0 -289
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +0 -31
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +0 -126
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasPostgresOperation.js +0 -30
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectPostgresOperation.js +0 -37
- package/lib/queryBuilder/operations/jsonApi/WhereJsonPostgresOperation.js +0 -62
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +0 -192
- package/lib/utils/wrapDbError.js +0 -64
package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/UniqueViolationError/parser.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { UniqueViolationError } from '../../../../../errors/UniqueViolationError.js';
|
|
2
|
+
|
|
3
|
+
const CODES = ['ER_DUP_ENTRY', 'ER_DUP_ENTRY_WITH_KEY_NAME'];
|
|
4
|
+
const CONSTRAINT_REGEX = /Duplicate entry '(.+)' for key '(.+)'/;
|
|
5
|
+
|
|
6
|
+
export const uniqueViolationErrorParser = {
|
|
7
|
+
error: UniqueViolationError,
|
|
8
|
+
|
|
9
|
+
parse: (err) => {
|
|
10
|
+
if (CODES.indexOf(err.code) === -1) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const constraintMatch = CONSTRAINT_REGEX.exec(err.sqlMessage);
|
|
15
|
+
|
|
16
|
+
if (!constraintMatch) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// No way to reliably get `table` and `columns` from mysql error.
|
|
21
|
+
return {
|
|
22
|
+
constraint: constraintMatch[2],
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
subclassParsers: [],
|
|
27
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ConstraintViolationError } from '../../../../errors/ConstraintViolationError.js';
|
|
2
|
+
import { getSqlStateClass } from '../../../../utils/sqlState.js';
|
|
3
|
+
import { uniqueViolationErrorParser } from './UniqueViolationError/parser.js';
|
|
4
|
+
import { notNullViolationErrorParser } from './NotNullViolationError/parser.js';
|
|
5
|
+
import { foreignKeyViolationErrorParser } from './ForeignKeyViolationError/parser.js';
|
|
6
|
+
|
|
7
|
+
export const constraintViolationErrorParser = {
|
|
8
|
+
error: ConstraintViolationError,
|
|
9
|
+
|
|
10
|
+
parse: (err) => {
|
|
11
|
+
if (getSqlStateClass(err.sqlState) === '23' || err.code === 'ER_NO_DEFAULT_FOR_FIELD') {
|
|
12
|
+
return {};
|
|
13
|
+
} else {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
subclassParsers: [
|
|
19
|
+
uniqueViolationErrorParser,
|
|
20
|
+
notNullViolationErrorParser,
|
|
21
|
+
foreignKeyViolationErrorParser,
|
|
22
|
+
],
|
|
23
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DataError } from '../../../../errors/DataError.js';
|
|
2
|
+
import { getSqlStateClass } from '../../../../utils/sqlState.js';
|
|
3
|
+
|
|
4
|
+
const ERROR_CODES = [
|
|
5
|
+
'ER_DATA_TOO_LONG',
|
|
6
|
+
'ER_TRUNCATED_WRONG_VALUE',
|
|
7
|
+
'ER_TRUNCATED_WRONG_VALUE_FOR_FIELD',
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
export const dataErrorParser = {
|
|
11
|
+
error: DataError,
|
|
12
|
+
|
|
13
|
+
parse: (err) => {
|
|
14
|
+
// MySQL mainly uses the SQLSTATE codes, but some errors don't have
|
|
15
|
+
// an SQLSTATE equivalent.
|
|
16
|
+
if (getSqlStateClass(err.sqlState) === '22' || ERROR_CODES.includes(err.code)) {
|
|
17
|
+
return {};
|
|
18
|
+
} else {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
subclassParsers: [],
|
|
24
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { DBError } from '../../../errors/DBError.js';
|
|
2
|
+
import { mysqlErrorCodes } from '../../../errorCodes/mysql.js';
|
|
3
|
+
import { constraintViolationErrorParser } from './ConstraintViolationError/parser.js';
|
|
4
|
+
import { dataErrorParser } from './DataError/parser.js';
|
|
5
|
+
|
|
6
|
+
export const dbErrorParser = {
|
|
7
|
+
error: DBError,
|
|
8
|
+
|
|
9
|
+
parse: (err) => {
|
|
10
|
+
if (
|
|
11
|
+
typeof err.code === 'string' &&
|
|
12
|
+
'sqlMessage' in err &&
|
|
13
|
+
'sqlState' in err &&
|
|
14
|
+
err.sqlState.length === 5 &&
|
|
15
|
+
mysqlErrorCodes.has(err.code) &&
|
|
16
|
+
typeof err.errno === 'number'
|
|
17
|
+
) {
|
|
18
|
+
return {
|
|
19
|
+
nativeError: err,
|
|
20
|
+
client: 'mysql',
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return null;
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
subclassParsers: [constraintViolationErrorParser, dataErrorParser],
|
|
28
|
+
};
|
package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/CheckViolationError/parser.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CheckViolationError } from '../../../../../errors/CheckViolationError.js';
|
|
2
|
+
|
|
3
|
+
export const checkViolationErrorParser = {
|
|
4
|
+
error: CheckViolationError,
|
|
5
|
+
|
|
6
|
+
parse: (err) => {
|
|
7
|
+
if (err.code === '23514') {
|
|
8
|
+
return {
|
|
9
|
+
table: err.table,
|
|
10
|
+
constraint: err.constraint,
|
|
11
|
+
};
|
|
12
|
+
} else {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
subclassParsers: [],
|
|
18
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ForeignKeyViolationError } from '../../../../../errors/ForeignKeyViolationError.js';
|
|
2
|
+
|
|
3
|
+
export const foreignKeyViolationErrorParser = {
|
|
4
|
+
error: ForeignKeyViolationError,
|
|
5
|
+
|
|
6
|
+
parse: (err) => {
|
|
7
|
+
if (err.code === '23503') {
|
|
8
|
+
return {
|
|
9
|
+
table: err.table,
|
|
10
|
+
constraint: err.constraint,
|
|
11
|
+
};
|
|
12
|
+
} else {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
subclassParsers: [],
|
|
18
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { NotNullViolationError } from '../../../../../errors/NotNullViolationError.js';
|
|
2
|
+
|
|
3
|
+
export const notNullViolationErrorParser = {
|
|
4
|
+
error: NotNullViolationError,
|
|
5
|
+
|
|
6
|
+
parse: (err) => {
|
|
7
|
+
if (err.code === '23502') {
|
|
8
|
+
return {
|
|
9
|
+
table: err.table,
|
|
10
|
+
column: err.column,
|
|
11
|
+
};
|
|
12
|
+
} else {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
subclassParsers: [],
|
|
18
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { UniqueViolationError } from '../../../../../errors/UniqueViolationError.js';
|
|
2
|
+
|
|
3
|
+
const UNIQUE_COLUMNS_REGEX = /Key \((.+)\)=\(.+\) already exists/;
|
|
4
|
+
|
|
5
|
+
export const uniqueViolationErrorParser = {
|
|
6
|
+
error: UniqueViolationError,
|
|
7
|
+
|
|
8
|
+
parse: (err) => {
|
|
9
|
+
if (err.code !== '23505') {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const colsMatch = UNIQUE_COLUMNS_REGEX.exec(err.detail);
|
|
14
|
+
|
|
15
|
+
if (!colsMatch) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const cols = colsMatch[1]
|
|
20
|
+
.split(',')
|
|
21
|
+
.map((it) => it.trim())
|
|
22
|
+
.map((it) => stripQuotes(it));
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
table: err.table,
|
|
26
|
+
columns: cols,
|
|
27
|
+
constraint: err.constraint,
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
subclassParsers: [],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function stripQuotes(it) {
|
|
35
|
+
if (it[0] === '"' && it[it.length - 1] === '"') {
|
|
36
|
+
return it.substr(1, it.length - 2);
|
|
37
|
+
} else {
|
|
38
|
+
return it;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ConstraintViolationError } from '../../../../errors/ConstraintViolationError.js';
|
|
2
|
+
import { getSqlStateClass } from '../../../../utils/sqlState.js';
|
|
3
|
+
import { uniqueViolationErrorParser } from './UniqueViolationError/parser.js';
|
|
4
|
+
import { notNullViolationErrorParser } from './NotNullViolationError/parser.js';
|
|
5
|
+
import { foreignKeyViolationErrorParser } from './ForeignKeyViolationError/parser.js';
|
|
6
|
+
import { checkViolationErrorParser } from './CheckViolationError/parser.js';
|
|
7
|
+
|
|
8
|
+
export const constraintViolationErrorParser = {
|
|
9
|
+
error: ConstraintViolationError,
|
|
10
|
+
|
|
11
|
+
parse: (err) => {
|
|
12
|
+
if (getSqlStateClass(err.code) === '23') {
|
|
13
|
+
return {};
|
|
14
|
+
} else {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
subclassParsers: [
|
|
20
|
+
uniqueViolationErrorParser,
|
|
21
|
+
notNullViolationErrorParser,
|
|
22
|
+
foreignKeyViolationErrorParser,
|
|
23
|
+
checkViolationErrorParser,
|
|
24
|
+
],
|
|
25
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DataError } from '../../../../errors/DataError.js';
|
|
2
|
+
import { getSqlStateClass } from '../../../../utils/sqlState.js';
|
|
3
|
+
|
|
4
|
+
export const dataErrorParser = {
|
|
5
|
+
error: DataError,
|
|
6
|
+
|
|
7
|
+
parse: (err) => {
|
|
8
|
+
if (getSqlStateClass(err.code) === '22') {
|
|
9
|
+
return {};
|
|
10
|
+
} else {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
subclassParsers: [],
|
|
16
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { DBError } from '../../../errors/DBError.js';
|
|
2
|
+
import { postgresErrorCodes } from '../../../errorCodes/postgres.js';
|
|
3
|
+
import { constraintViolationErrorParser } from './ConstraintViolationError/parser.js';
|
|
4
|
+
import { dataErrorParser } from './DataError/parser.js';
|
|
5
|
+
|
|
6
|
+
export const dbErrorParser = {
|
|
7
|
+
error: DBError,
|
|
8
|
+
|
|
9
|
+
parse: (err) => {
|
|
10
|
+
if (
|
|
11
|
+
typeof err.code === 'string' &&
|
|
12
|
+
err.code.length === 5 &&
|
|
13
|
+
postgresErrorCodes.has(err.code) &&
|
|
14
|
+
'internalQuery' in err &&
|
|
15
|
+
'table' in err
|
|
16
|
+
) {
|
|
17
|
+
return {
|
|
18
|
+
nativeError: err,
|
|
19
|
+
client: 'postgres',
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null;
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
subclassParsers: [constraintViolationErrorParser, dataErrorParser],
|
|
27
|
+
};
|
package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/CheckViolationError/parser.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CheckViolationError } from '../../../../../errors/CheckViolationError.js';
|
|
2
|
+
|
|
3
|
+
const REGEX = /SQLITE_CONSTRAINT: CHECK constraint failed/;
|
|
4
|
+
|
|
5
|
+
export const checkViolationErrorParser = {
|
|
6
|
+
error: CheckViolationError,
|
|
7
|
+
|
|
8
|
+
parse: (err) => {
|
|
9
|
+
const match = REGEX.exec(err.message);
|
|
10
|
+
|
|
11
|
+
if (!match) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// No way to extract anything reliably.
|
|
16
|
+
return {};
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
subclassParsers: [],
|
|
20
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ForeignKeyViolationError } from '../../../../../errors/ForeignKeyViolationError.js';
|
|
2
|
+
|
|
3
|
+
const REGEX = /SQLITE_CONSTRAINT: FOREIGN KEY constraint failed/;
|
|
4
|
+
|
|
5
|
+
export const foreignKeyViolationErrorParser = {
|
|
6
|
+
error: ForeignKeyViolationError,
|
|
7
|
+
|
|
8
|
+
parse: (err) => {
|
|
9
|
+
const match = REGEX.exec(err.message);
|
|
10
|
+
|
|
11
|
+
if (!match) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// No way to extract anything reliably.
|
|
16
|
+
return {};
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
subclassParsers: [],
|
|
20
|
+
};
|
package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/NotNullViolationError/parser.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { NotNullViolationError } from '../../../../../errors/NotNullViolationError.js';
|
|
2
|
+
|
|
3
|
+
const NOT_COLUMN_REGEX = /SQLITE_CONSTRAINT: NOT NULL constraint failed: (.+)$/;
|
|
4
|
+
|
|
5
|
+
export const notNullViolationErrorParser = {
|
|
6
|
+
error: NotNullViolationError,
|
|
7
|
+
|
|
8
|
+
parse: (err) => {
|
|
9
|
+
const colMatch = NOT_COLUMN_REGEX.exec(err.message);
|
|
10
|
+
|
|
11
|
+
if (!colMatch) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const parts = colMatch[1]
|
|
16
|
+
.trim()
|
|
17
|
+
.split('.')
|
|
18
|
+
.map((it) => it.trim());
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
table: parts[0],
|
|
22
|
+
column: parts[1],
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
subclassParsers: [],
|
|
27
|
+
};
|
package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/UniqueViolationError/parser.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { UniqueViolationError } from '../../../../../errors/UniqueViolationError.js';
|
|
2
|
+
|
|
3
|
+
const UNIQUE_COLUMNS_REGEX = /SQLITE_CONSTRAINT: UNIQUE constraint failed: (.+)$/;
|
|
4
|
+
|
|
5
|
+
export const uniqueViolationErrorParser = {
|
|
6
|
+
error: UniqueViolationError,
|
|
7
|
+
|
|
8
|
+
parse: (err) => {
|
|
9
|
+
const colsMatch = UNIQUE_COLUMNS_REGEX.exec(err.message);
|
|
10
|
+
|
|
11
|
+
if (!colsMatch) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const cols = colsMatch[1]
|
|
16
|
+
.split(',')
|
|
17
|
+
.map((it) => it.trim())
|
|
18
|
+
.map((it) => it.split('.'))
|
|
19
|
+
.map((it) => it.map((it) => it.trim()));
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
table: cols[0][0],
|
|
23
|
+
columns: cols.map((it) => it[1]),
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
subclassParsers: [],
|
|
28
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ConstraintViolationError } from '../../../../errors/ConstraintViolationError.js';
|
|
2
|
+
import { uniqueViolationErrorParser } from './UniqueViolationError/parser.js';
|
|
3
|
+
import { notNullViolationErrorParser } from './NotNullViolationError/parser.js';
|
|
4
|
+
import { foreignKeyViolationErrorParser } from './ForeignKeyViolationError/parser.js';
|
|
5
|
+
import { checkViolationErrorParser } from './CheckViolationError/parser.js';
|
|
6
|
+
|
|
7
|
+
export const constraintViolationErrorParser = {
|
|
8
|
+
error: ConstraintViolationError,
|
|
9
|
+
|
|
10
|
+
parse: (err) => {
|
|
11
|
+
if (err.code === 'SQLITE_CONSTRAINT') {
|
|
12
|
+
return {};
|
|
13
|
+
} else {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
subclassParsers: [
|
|
19
|
+
uniqueViolationErrorParser,
|
|
20
|
+
notNullViolationErrorParser,
|
|
21
|
+
foreignKeyViolationErrorParser,
|
|
22
|
+
checkViolationErrorParser,
|
|
23
|
+
],
|
|
24
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DBError } from '../../../errors/DBError.js';
|
|
2
|
+
import { constraintViolationErrorParser } from './ConstraintViolationError/parser.js';
|
|
3
|
+
|
|
4
|
+
export const dbErrorParser = {
|
|
5
|
+
error: DBError,
|
|
6
|
+
|
|
7
|
+
parse: (err) => {
|
|
8
|
+
if (
|
|
9
|
+
typeof err.code === 'string' &&
|
|
10
|
+
err.code.startsWith('SQLITE_') &&
|
|
11
|
+
typeof err.errno === 'number'
|
|
12
|
+
) {
|
|
13
|
+
return {
|
|
14
|
+
nativeError: err,
|
|
15
|
+
client: 'sqlite',
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return null;
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
subclassParsers: [constraintViolationErrorParser],
|
|
23
|
+
};
|
package/lib/initialize.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
async function initialize(knex, modelClasses) {
|
|
1
|
+
export async function initialize(knex, modelClasses) {
|
|
4
2
|
if (!modelClasses) {
|
|
5
3
|
modelClasses = knex;
|
|
6
4
|
knex = modelClasses[0].knex();
|
|
@@ -8,7 +6,3 @@ async function initialize(knex, modelClasses) {
|
|
|
8
6
|
|
|
9
7
|
await Promise.all(modelClasses.map((modelClass) => modelClass.fetchTableMetadata({ knex })));
|
|
10
8
|
}
|
|
11
|
-
|
|
12
|
-
module.exports = {
|
|
13
|
-
initialize,
|
|
14
|
-
};
|