objection 3.9.0 → 4.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (248) hide show
  1. package/lib/dbErrors/LICENSE +21 -0
  2. package/lib/dbErrors/errorCodes/mssql.js +19 -0
  3. package/lib/dbErrors/errorCodes/mysql.js +805 -0
  4. package/lib/dbErrors/errorCodes/postgres.js +241 -0
  5. package/lib/dbErrors/errors/CheckViolationError.js +10 -0
  6. package/lib/dbErrors/errors/ConstraintViolationError.js +3 -0
  7. package/lib/dbErrors/errors/DBError.js +9 -0
  8. package/lib/dbErrors/errors/DataError.js +3 -0
  9. package/lib/dbErrors/errors/ForeignKeyViolationError.js +11 -0
  10. package/lib/dbErrors/errors/NotNullViolationError.js +12 -0
  11. package/lib/dbErrors/errors/UniqueViolationError.js +12 -0
  12. package/lib/dbErrors/index.js +59 -0
  13. package/lib/dbErrors/parsers/mssql/DBError/CheckViolationError/parser.js +28 -0
  14. package/lib/dbErrors/parsers/mssql/DBError/DataError/parser.js +25 -0
  15. package/lib/dbErrors/parsers/mssql/DBError/ForeignKeyViolationError/parser.js +39 -0
  16. package/lib/dbErrors/parsers/mssql/DBError/NotNullViolationError/parser.js +30 -0
  17. package/lib/dbErrors/parsers/mssql/DBError/UniqueViolationError/parser.js +53 -0
  18. package/lib/dbErrors/parsers/mssql/DBError/parser.js +51 -0
  19. package/lib/dbErrors/parsers/mssql/DBError/util.js +16 -0
  20. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +43 -0
  21. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/NotNullViolationError/parser.js +29 -0
  22. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/UniqueViolationError/parser.js +27 -0
  23. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/parser.js +23 -0
  24. package/lib/dbErrors/parsers/mysql/DBError/DataError/parser.js +24 -0
  25. package/lib/dbErrors/parsers/mysql/DBError/parser.js +28 -0
  26. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/CheckViolationError/parser.js +18 -0
  27. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +18 -0
  28. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/NotNullViolationError/parser.js +18 -0
  29. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/UniqueViolationError/parser.js +40 -0
  30. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/parser.js +25 -0
  31. package/lib/dbErrors/parsers/postgres/DBError/DataError/parser.js +16 -0
  32. package/lib/dbErrors/parsers/postgres/DBError/parser.js +27 -0
  33. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/CheckViolationError/parser.js +20 -0
  34. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +20 -0
  35. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/NotNullViolationError/parser.js +27 -0
  36. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/UniqueViolationError/parser.js +28 -0
  37. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/parser.js +24 -0
  38. package/lib/dbErrors/parsers/sqlite/DBError/parser.js +23 -0
  39. package/lib/dbErrors/utils/sqlState.js +3 -0
  40. package/lib/initialize.js +1 -7
  41. package/lib/model/AjvValidator.js +51 -117
  42. package/lib/model/Model.js +84 -59
  43. package/lib/model/ModifierNotFoundError.js +1 -7
  44. package/lib/model/NotFoundError.js +1 -7
  45. package/lib/model/RelationDoesNotExistError.js +1 -7
  46. package/lib/model/ValidationError.js +3 -10
  47. package/lib/model/Validator.js +1 -7
  48. package/lib/model/getModel.js +7 -6
  49. package/lib/model/inheritModel.js +1 -7
  50. package/lib/model/modelBindKnex.js +4 -10
  51. package/lib/model/modelClone.js +5 -11
  52. package/lib/model/modelColPropMap.js +3 -10
  53. package/lib/model/modelId.js +3 -10
  54. package/lib/model/modelJsonAttributes.js +35 -16
  55. package/lib/model/modelParseRelations.js +2 -8
  56. package/lib/model/modelQueryProps.js +43 -27
  57. package/lib/model/modelSet.js +89 -41
  58. package/lib/model/modelTableMetadata.js +4 -11
  59. package/lib/model/modelToJson.js +8 -23
  60. package/lib/model/modelUtils.js +14 -13
  61. package/lib/model/modelValidate.js +3 -9
  62. package/lib/model/modelValues.js +5 -14
  63. package/lib/model/modelVisitor.js +1 -7
  64. package/lib/objection.js +33 -71
  65. package/lib/queryBuilder/FunctionBuilder.js +4 -11
  66. package/lib/queryBuilder/InternalOptions.js +4 -9
  67. package/lib/queryBuilder/JoinBuilder.js +5 -8
  68. package/lib/queryBuilder/QueryBuilder.js +126 -95
  69. package/lib/queryBuilder/QueryBuilderBase.js +41 -123
  70. package/lib/queryBuilder/QueryBuilderContext.js +2 -8
  71. package/lib/queryBuilder/QueryBuilderContextBase.js +2 -8
  72. package/lib/queryBuilder/QueryBuilderOperationSupport.js +7 -21
  73. package/lib/queryBuilder/QueryBuilderUserContext.js +1 -7
  74. package/lib/queryBuilder/RawBuilder.js +5 -13
  75. package/lib/queryBuilder/ReferenceBuilder.js +20 -48
  76. package/lib/queryBuilder/RelationExpression.js +18 -16
  77. package/lib/queryBuilder/StaticHookArguments.js +74 -16
  78. package/lib/queryBuilder/ValueBuilder.js +11 -13
  79. package/lib/queryBuilder/getJoinBuilder.js +10 -0
  80. package/lib/queryBuilder/graph/GraphLoader.js +304 -0
  81. package/lib/queryBuilder/graph/GraphNode.js +235 -0
  82. package/lib/queryBuilder/graph/GraphOptions.js +55 -248
  83. package/lib/queryBuilder/graph/GraphPlan.js +395 -0
  84. package/lib/queryBuilder/graph/GraphUpsert.js +466 -410
  85. package/lib/queryBuilder/graph/GraphWriter.js +1263 -0
  86. package/lib/queryBuilder/join/JoinResultColumn.js +1 -7
  87. package/lib/queryBuilder/join/JoinResultParser.js +6 -11
  88. package/lib/queryBuilder/join/RelationJoiner.js +66 -84
  89. package/lib/queryBuilder/join/TableNode.js +2 -8
  90. package/lib/queryBuilder/join/TableTree.js +3 -9
  91. package/lib/queryBuilder/join/utils.js +5 -15
  92. package/lib/queryBuilder/jsonDialects/JsonDialect.js +51 -0
  93. package/lib/queryBuilder/jsonDialects/MySqlJsonDialect.js +81 -0
  94. package/lib/queryBuilder/jsonDialects/PostgresJsonDialect.js +45 -0
  95. package/lib/queryBuilder/jsonDialects/SqliteJsonDialect.js +67 -0
  96. package/lib/queryBuilder/jsonDialects/index.js +44 -0
  97. package/lib/queryBuilder/operations/DelegateOperation.js +2 -8
  98. package/lib/queryBuilder/operations/DeleteOperation.js +13 -35
  99. package/lib/queryBuilder/operations/FindByIdOperation.js +7 -12
  100. package/lib/queryBuilder/operations/FindByIdsOperation.js +4 -8
  101. package/lib/queryBuilder/operations/FindOperation.js +14 -34
  102. package/lib/queryBuilder/operations/FirstOperation.js +6 -10
  103. package/lib/queryBuilder/operations/FromOperation.js +3 -9
  104. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +13 -12
  105. package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +4 -10
  106. package/lib/queryBuilder/operations/InsertGraphOperation.js +7 -18
  107. package/lib/queryBuilder/operations/InsertOperation.js +118 -43
  108. package/lib/queryBuilder/operations/InstanceDeleteOperation.js +5 -12
  109. package/lib/queryBuilder/operations/InstanceFindOperation.js +4 -11
  110. package/lib/queryBuilder/operations/InstanceInsertOperation.js +2 -8
  111. package/lib/queryBuilder/operations/InstanceUpdateOperation.js +7 -14
  112. package/lib/queryBuilder/operations/JoinRelatedOperation.js +5 -11
  113. package/lib/queryBuilder/operations/KnexOperation.js +2 -8
  114. package/lib/queryBuilder/operations/LimitPerParentOperation.js +7 -69
  115. package/lib/queryBuilder/operations/MergeOperation.js +5 -11
  116. package/lib/queryBuilder/operations/NoneOperation.js +2 -8
  117. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +20 -49
  118. package/lib/queryBuilder/operations/OnBuildKnexOperation.js +2 -8
  119. package/lib/queryBuilder/operations/OnBuildOperation.js +2 -8
  120. package/lib/queryBuilder/operations/OnErrorOperation.js +2 -8
  121. package/lib/queryBuilder/operations/QueryBuilderOperation.js +1 -7
  122. package/lib/queryBuilder/operations/RangeOperation.js +11 -26
  123. package/lib/queryBuilder/operations/RelateOperation.js +2 -8
  124. package/lib/queryBuilder/operations/ReturningOperation.js +3 -9
  125. package/lib/queryBuilder/operations/RunAfterOperation.js +2 -12
  126. package/lib/queryBuilder/operations/RunBeforeOperation.js +2 -8
  127. package/lib/queryBuilder/operations/UnrelateOperation.js +2 -8
  128. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +14 -17
  129. package/lib/queryBuilder/operations/UpdateOperation.js +39 -125
  130. package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +8 -11
  131. package/lib/queryBuilder/operations/UpsertGraphOperation.js +6 -12
  132. package/lib/queryBuilder/operations/WhereCompositeOperation.js +3 -9
  133. package/lib/queryBuilder/operations/WhereGroupOperation.js +2 -8
  134. package/lib/queryBuilder/operations/eager/EagerOperation.js +3 -9
  135. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +72 -10
  136. package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +2 -8
  137. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +23 -44
  138. package/lib/queryBuilder/operations/fieldExpressions.js +122 -0
  139. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +20 -0
  140. package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +44 -0
  141. package/lib/queryBuilder/operations/jsonApi/WhereJsonTypeOperation.js +14 -0
  142. package/lib/queryBuilder/operations/select/SelectOperation.js +11 -21
  143. package/lib/queryBuilder/operations/select/Selection.js +19 -17
  144. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +4 -10
  145. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +4 -10
  146. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +4 -10
  147. package/lib/queryBuilder/operations/windowOrder.js +56 -0
  148. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +3 -7
  149. package/lib/queryBuilder/parsers/relationExpressionParser.js +42 -43
  150. package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +39 -36
  151. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +2 -8
  152. package/lib/queryBuilder/transformations/QueryTransformation.js +1 -7
  153. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +3 -9
  154. package/lib/queryBuilder/transformations/index.js +3 -11
  155. package/lib/queryBuilder/whereGroups.js +9 -24
  156. package/lib/queryBuilder/withoutOrder.js +4 -11
  157. package/lib/relations/Relation.js +47 -28
  158. package/lib/relations/RelationDeleteOperation.js +4 -14
  159. package/lib/relations/RelationFindOperation.js +119 -90
  160. package/lib/relations/RelationInsertOperation.js +2 -8
  161. package/lib/relations/RelationOwner.js +5 -11
  162. package/lib/relations/RelationProperty.js +4 -10
  163. package/lib/relations/RelationUpdateOperation.js +4 -14
  164. package/lib/relations/RelationWriteResultOperation.js +15 -0
  165. package/lib/relations/belongsToMany/BelongsToManyDeleteOperation.js +23 -0
  166. package/lib/relations/belongsToMany/BelongsToManyFindOperation.js +35 -0
  167. package/lib/relations/belongsToMany/BelongsToManyInsertOperation.js +28 -0
  168. package/lib/relations/belongsToMany/BelongsToManyRelateOperation.js +16 -0
  169. package/lib/relations/belongsToMany/BelongsToManyRelation.js +276 -0
  170. package/lib/relations/belongsToMany/BelongsToManyUnrelateOperation.js +11 -0
  171. package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +2 -8
  172. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +6 -11
  173. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +3 -9
  174. package/lib/relations/belongsToOne/BelongsToOneRelation.js +8 -19
  175. package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +53 -27
  176. package/lib/relations/hasMany/HasManyInsertOperation.js +8 -9
  177. package/lib/relations/hasMany/HasManyRelateOperation.js +5 -11
  178. package/lib/relations/hasMany/HasManyRelation.js +9 -11
  179. package/lib/relations/hasMany/HasManyUnrelateOperation.js +3 -9
  180. package/lib/relations/hasOne/HasOneRelation.js +2 -8
  181. package/lib/relations/hasOneThrough/HasOneThroughRelation.js +2 -8
  182. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +10 -15
  183. package/lib/relations/manyToMany/ManyToManyMySqlModifyMixin.js +2 -8
  184. package/lib/relations/manyToMany/ManyToManyRelation.js +82 -50
  185. package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +2 -8
  186. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +5 -9
  187. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +2 -8
  188. package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -9
  189. package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +20 -59
  190. package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +6 -11
  191. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +4 -10
  192. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateMySqlOperation.js +3 -9
  193. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +5 -9
  194. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +3 -9
  195. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +3 -9
  196. package/lib/relations/manyToMany/update/ManyToManyUpdateMySqlOperation.js +3 -9
  197. package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +5 -9
  198. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +79 -64
  199. package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -9
  200. package/lib/transaction.js +3 -9
  201. package/lib/utils/assert.js +3 -11
  202. package/lib/utils/buildUtils.js +2 -8
  203. package/lib/utils/classUtils.js +1 -7
  204. package/lib/utils/createModifier.js +2 -8
  205. package/lib/utils/identifierMapping.js +23 -27
  206. package/lib/utils/internalPropUtils.js +1 -7
  207. package/lib/utils/knexUtils.js +84 -40
  208. package/lib/utils/mixin.js +3 -10
  209. package/lib/utils/normalizeIds.js +2 -8
  210. package/lib/utils/objectUtils.js +41 -60
  211. package/lib/utils/parseFieldExpression.js +29 -14
  212. package/lib/utils/promiseUtils/after.js +2 -8
  213. package/lib/utils/promiseUtils/afterReturn.js +2 -8
  214. package/lib/utils/promiseUtils/index.js +7 -16
  215. package/lib/utils/promiseUtils/isPromise.js +2 -8
  216. package/lib/utils/promiseUtils/map.js +25 -14
  217. package/lib/utils/promiseUtils/mapAfterAllReturn.js +2 -8
  218. package/lib/utils/promiseUtils/try.js +2 -8
  219. package/lib/utils/resolveModel.js +10 -17
  220. package/lib/utils/tmpColumnUtils.js +2 -9
  221. package/lib/utils/warnings.js +2 -14
  222. package/package.json +25 -17
  223. package/typings/objection/index.d.ts +3217 -2179
  224. package/lib/.eslintrc.json +0 -8
  225. package/lib/model/graph/ModelGraph.js +0 -80
  226. package/lib/model/graph/ModelGraphBuilder.js +0 -241
  227. package/lib/model/graph/ModelGraphEdge.js +0 -52
  228. package/lib/model/graph/ModelGraphNode.js +0 -253
  229. package/lib/queryBuilder/graph/GraphAction.js +0 -69
  230. package/lib/queryBuilder/graph/GraphData.js +0 -53
  231. package/lib/queryBuilder/graph/GraphFetcher.js +0 -199
  232. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +0 -184
  233. package/lib/queryBuilder/graph/GraphOperation.js +0 -27
  234. package/lib/queryBuilder/graph/delete/GraphDelete.js +0 -73
  235. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +0 -88
  236. package/lib/queryBuilder/graph/insert/DeferredRelationGraphInsertAction.js +0 -57
  237. package/lib/queryBuilder/graph/insert/GraphInsert.js +0 -257
  238. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +0 -238
  239. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +0 -92
  240. package/lib/queryBuilder/graph/patch/GraphPatch.js +0 -20
  241. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +0 -277
  242. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +0 -31
  243. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +0 -126
  244. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasPostgresOperation.js +0 -30
  245. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectPostgresOperation.js +0 -37
  246. package/lib/queryBuilder/operations/jsonApi/WhereJsonPostgresOperation.js +0 -62
  247. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +0 -192
  248. package/lib/utils/wrapDbError.js +0 -50
@@ -0,0 +1,241 @@
1
+ export const postgresErrorCodes = new Set([
2
+ '000',
3
+ '00000',
4
+ '01000',
5
+ '0100C',
6
+ '01008',
7
+ '01003',
8
+ '01007',
9
+ '01006',
10
+ '01004',
11
+ '01P01',
12
+ '02000',
13
+ '02001',
14
+ '03000',
15
+ '08000',
16
+ '08003',
17
+ '08006',
18
+ '08001',
19
+ '08004',
20
+ '08007',
21
+ '08P01',
22
+ '09000',
23
+ '0A000',
24
+ '0B000',
25
+ '0F000',
26
+ '0F001',
27
+ '0L000',
28
+ '0LP01',
29
+ '0P000',
30
+ '0Z000',
31
+ '0Z002',
32
+ '20000',
33
+ '21000',
34
+ '22000',
35
+ '2202E',
36
+ '22021',
37
+ '22008',
38
+ '22012',
39
+ '22005',
40
+ '2200B',
41
+ '22022',
42
+ '22015',
43
+ '2201E',
44
+ '22014',
45
+ '22016',
46
+ '2201F',
47
+ '2201G',
48
+ '22018',
49
+ '22007',
50
+ '22019',
51
+ '2200D',
52
+ '22025',
53
+ '22P06',
54
+ '22010',
55
+ '22023',
56
+ '2201B',
57
+ '2201W',
58
+ '2201X',
59
+ '2202H',
60
+ '2202G',
61
+ '22009',
62
+ '2200C',
63
+ '2200G',
64
+ '22004',
65
+ '22002',
66
+ '22003',
67
+ '22026',
68
+ '22001',
69
+ '22011',
70
+ '22027',
71
+ '22024',
72
+ '2200F',
73
+ '22P01',
74
+ '22P02',
75
+ '22P03',
76
+ '22P04',
77
+ '22P05',
78
+ '2200L',
79
+ '2200M',
80
+ '2200N',
81
+ '2200S',
82
+ '2200T',
83
+ '23000',
84
+ '23001',
85
+ '23502',
86
+ '23503',
87
+ '23505',
88
+ '23514',
89
+ '23P01',
90
+ '24000',
91
+ '25000',
92
+ '25001',
93
+ '25002',
94
+ '25008',
95
+ '25003',
96
+ '25004',
97
+ '25005',
98
+ '25006',
99
+ '25007',
100
+ '25P01',
101
+ '25P02',
102
+ '25P03',
103
+ '26000',
104
+ '27000',
105
+ '28000',
106
+ '28P01',
107
+ '2B000',
108
+ '2BP01',
109
+ '2D000',
110
+ '2F000',
111
+ '2F005',
112
+ '2F002',
113
+ '2F003',
114
+ '2F004',
115
+ '34000',
116
+ '38000',
117
+ '38001',
118
+ '38002',
119
+ '38003',
120
+ '38004',
121
+ '39000',
122
+ '39001',
123
+ '39004',
124
+ '39P01',
125
+ '39P02',
126
+ '39P03',
127
+ '3B000',
128
+ '3B001',
129
+ '3D000',
130
+ '3F000',
131
+ '40000',
132
+ '40002',
133
+ '40001',
134
+ '40003',
135
+ '40P01',
136
+ '42000',
137
+ '42601',
138
+ '42501',
139
+ '42846',
140
+ '42803',
141
+ '42P20',
142
+ '42P19',
143
+ '42830',
144
+ '42602',
145
+ '42622',
146
+ '42939',
147
+ '42804',
148
+ '42P18',
149
+ '42P21',
150
+ '42P22',
151
+ '42809',
152
+ '42703',
153
+ '42883',
154
+ '42P01',
155
+ '42P02',
156
+ '42704',
157
+ '42701',
158
+ '42P03',
159
+ '42P04',
160
+ '42723',
161
+ '42P05',
162
+ '42P06',
163
+ '42P07',
164
+ '42712',
165
+ '42710',
166
+ '42702',
167
+ '42725',
168
+ '42P08',
169
+ '42P09',
170
+ '42P10',
171
+ '42611',
172
+ '42P11',
173
+ '42P12',
174
+ '42P13',
175
+ '42P14',
176
+ '42P15',
177
+ '42P16',
178
+ '42P17',
179
+ '44000',
180
+ '53000',
181
+ '53100',
182
+ '53200',
183
+ '53300',
184
+ '53400',
185
+ '54000',
186
+ '54001',
187
+ '54011',
188
+ '54023',
189
+ '55000',
190
+ '55006',
191
+ '55P02',
192
+ '55P03',
193
+ '57000',
194
+ '57014',
195
+ '57P01',
196
+ '57P02',
197
+ '57P03',
198
+ '57P04',
199
+ '58000',
200
+ '58030',
201
+ '58P01',
202
+ '58P02',
203
+ '72000',
204
+ 'F0000',
205
+ 'F0001',
206
+ 'HV000',
207
+ 'HV005',
208
+ 'HV002',
209
+ 'HV010',
210
+ 'HV021',
211
+ 'HV024',
212
+ 'HV007',
213
+ 'HV008',
214
+ 'HV004',
215
+ 'HV006',
216
+ 'HV091',
217
+ 'HV00B',
218
+ 'HV00C',
219
+ 'HV00D',
220
+ 'HV090',
221
+ 'HV00A',
222
+ 'HV009',
223
+ 'HV014',
224
+ 'HV001',
225
+ 'HV00P',
226
+ 'HV00J',
227
+ 'HV00K',
228
+ 'HV00Q',
229
+ 'HV00R',
230
+ 'HV00L',
231
+ 'HV00M',
232
+ 'HV00N',
233
+ 'P0000',
234
+ 'P0001',
235
+ 'P0002',
236
+ 'P0003',
237
+ 'P0004',
238
+ 'XX000',
239
+ 'XX001',
240
+ 'XX002',
241
+ ]);
@@ -0,0 +1,10 @@
1
+ import { ConstraintViolationError } from './ConstraintViolationError.js';
2
+
3
+ export class CheckViolationError extends ConstraintViolationError {
4
+ constructor(args) {
5
+ super(args);
6
+
7
+ this.table = args.table;
8
+ this.constraint = args.constraint;
9
+ }
10
+ }
@@ -0,0 +1,3 @@
1
+ import { DBError } from './DBError.js';
2
+
3
+ export class ConstraintViolationError extends DBError {}
@@ -0,0 +1,9 @@
1
+ export class DBError extends Error {
2
+ constructor(args) {
3
+ super(args.message ?? args.nativeError.message);
4
+
5
+ this.name = this.constructor.name;
6
+ this.nativeError = args.nativeError;
7
+ this.client = args.client;
8
+ }
9
+ }
@@ -0,0 +1,3 @@
1
+ import { DBError } from './DBError.js';
2
+
3
+ export class DataError extends DBError {}
@@ -0,0 +1,11 @@
1
+ import { ConstraintViolationError } from './ConstraintViolationError.js';
2
+
3
+ export class ForeignKeyViolationError extends ConstraintViolationError {
4
+ constructor(args) {
5
+ super(args);
6
+
7
+ this.table = args.table;
8
+ this.constraint = args.constraint;
9
+ this.schema = args.schema;
10
+ }
11
+ }
@@ -0,0 +1,12 @@
1
+ import { ConstraintViolationError } from './ConstraintViolationError.js';
2
+
3
+ export class NotNullViolationError extends ConstraintViolationError {
4
+ constructor(args) {
5
+ super(args);
6
+
7
+ this.table = args.table;
8
+ this.column = args.column;
9
+ this.schema = args.schema;
10
+ this.database = args.database;
11
+ }
12
+ }
@@ -0,0 +1,12 @@
1
+ import { ConstraintViolationError } from './ConstraintViolationError.js';
2
+
3
+ export class UniqueViolationError extends ConstraintViolationError {
4
+ constructor(args) {
5
+ super(args);
6
+
7
+ this.table = args.table;
8
+ this.columns = args.columns;
9
+ this.constraint = args.constraint;
10
+ this.schema = args.schema;
11
+ }
12
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Vendored from db-errors 0.2.3 (https://github.com/Vincit/db-errors),
3
+ * converted to ES modules, with support for the mssql errors of tedious and
4
+ * for mssql primary key violations.
5
+ *
6
+ * Copyright (c) 2017 Vincit
7
+ * Released under the MIT license, see ./LICENSE
8
+ */
9
+
10
+ import { dbErrorParser as postgresParser } from './parsers/postgres/DBError/parser.js';
11
+ import { dbErrorParser as sqliteParser } from './parsers/sqlite/DBError/parser.js';
12
+ import { dbErrorParser as mysqlParser } from './parsers/mysql/DBError/parser.js';
13
+ import { dbErrorParser as mssqlParser } from './parsers/mssql/DBError/parser.js';
14
+
15
+ export { DBError } from './errors/DBError.js';
16
+ export { ConstraintViolationError } from './errors/ConstraintViolationError.js';
17
+ export { ForeignKeyViolationError } from './errors/ForeignKeyViolationError.js';
18
+ export { NotNullViolationError } from './errors/NotNullViolationError.js';
19
+ export { UniqueViolationError } from './errors/UniqueViolationError.js';
20
+ export { CheckViolationError } from './errors/CheckViolationError.js';
21
+ export { DataError } from './errors/DataError.js';
22
+
23
+ // The parsers are tried in this order.
24
+ const parsers = [postgresParser, sqliteParser, mysqlParser, mssqlParser];
25
+
26
+ export function wrapError(err) {
27
+ for (let i = 0, l = parsers.length; i < l; ++i) {
28
+ const result = parse(parsers[i], err, null);
29
+
30
+ if (result !== null) {
31
+ return new result.node.error(result.args);
32
+ }
33
+ }
34
+
35
+ return err;
36
+ }
37
+
38
+ function parse(node, err, parentResult) {
39
+ const args = node.parse(err);
40
+
41
+ if (args === null) {
42
+ return null;
43
+ }
44
+
45
+ const result = {
46
+ node,
47
+ args: Object.assign({}, parentResult && parentResult.args, args),
48
+ };
49
+
50
+ for (let i = 0; i < node.subclassParsers.length; ++i) {
51
+ const subResult = parse(node.subclassParsers[i], err, result);
52
+
53
+ if (subResult !== null) {
54
+ return subResult;
55
+ }
56
+ }
57
+
58
+ return result;
59
+ }
@@ -0,0 +1,28 @@
1
+ import { CheckViolationError } from '../../../../errors/CheckViolationError.js';
2
+ import { isCode } from '../util.js';
3
+
4
+ const REGEX =
5
+ /The (INSERT|UPDATE) statement conflicted with the CHECK constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)", column '(.+)'./;
6
+
7
+ export const checkViolationErrorParser = {
8
+ error: CheckViolationError,
9
+
10
+ parse: (err) => {
11
+ if (isCode(err, 16, 547)) {
12
+ const match = REGEX.exec(err.message);
13
+
14
+ if (!match) {
15
+ return null;
16
+ }
17
+
18
+ return {
19
+ table: match[3],
20
+ constraint: match[1],
21
+ };
22
+ } else {
23
+ return null;
24
+ }
25
+ },
26
+
27
+ subclassParsers: [],
28
+ };
@@ -0,0 +1,25 @@
1
+ import { DataError } from '../../../../errors/DataError.js';
2
+ import { isCode } from '../util.js';
3
+
4
+ export const dataErrorParser = {
5
+ error: DataError,
6
+
7
+ parse: (err) => {
8
+ // 241 - Conversion failed when converting date and/or time from character string.
9
+ // 242 - The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
10
+ // 245 - Conversion failed when converting the nvarchar value 'lol' to data type int.
11
+ // 8152 - String or binary data would be truncated.
12
+ if (
13
+ isCode(err, 16, 241) ||
14
+ isCode(err, 16, 242) ||
15
+ isCode(err, 16, 245) ||
16
+ isCode(err, 16, 8152)
17
+ ) {
18
+ return {};
19
+ } else {
20
+ return null;
21
+ }
22
+ },
23
+
24
+ subclassParsers: [],
25
+ };
@@ -0,0 +1,39 @@
1
+ import { ForeignKeyViolationError } from '../../../../errors/ForeignKeyViolationError.js';
2
+ import { isCode } from '../util.js';
3
+
4
+ const INSERT_UPDATE_REGEX =
5
+ /The (?:INSERT|UPDATE) statement conflicted with the FOREIGN KEY constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)\.(.+)", column '(.+)'./;
6
+ const DELETE_REGEX =
7
+ /The DELETE statement conflicted with the REFERENCE constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)\.(.+)", column '(.+)'./;
8
+
9
+ export const foreignKeyViolationErrorParser = {
10
+ error: ForeignKeyViolationError,
11
+
12
+ parse: (err) => {
13
+ if (isCode(err, 16, 547)) {
14
+ const insertUpdateMatch = INSERT_UPDATE_REGEX.exec(err.message);
15
+
16
+ if (insertUpdateMatch) {
17
+ return {
18
+ table: insertUpdateMatch[4],
19
+ schema: insertUpdateMatch[3],
20
+ constraint: insertUpdateMatch[1],
21
+ };
22
+ }
23
+
24
+ const deleteMatch = DELETE_REGEX.exec(err.message);
25
+
26
+ if (deleteMatch) {
27
+ return {
28
+ table: deleteMatch[4],
29
+ schema: deleteMatch[3],
30
+ constraint: deleteMatch[1],
31
+ };
32
+ }
33
+ }
34
+
35
+ return null;
36
+ },
37
+
38
+ subclassParsers: [],
39
+ };
@@ -0,0 +1,30 @@
1
+ import { NotNullViolationError } from '../../../../errors/NotNullViolationError.js';
2
+ import { isCode } from '../util.js';
3
+
4
+ const REGEX =
5
+ /Cannot insert the value NULL into column '(.+)', table '(.+)\.(.+)\.(.+)'; column does not allow nulls. (?:INSERT|UPDATE) fails./;
6
+
7
+ export const notNullViolationErrorParser = {
8
+ error: NotNullViolationError,
9
+
10
+ parse: (err) => {
11
+ if (isCode(err, 16, 515) || isCode(err, 16, 50000)) {
12
+ const match = REGEX.exec(err.message);
13
+
14
+ if (!match) {
15
+ return null;
16
+ }
17
+
18
+ return {
19
+ column: match[1],
20
+ database: match[2],
21
+ schema: match[3],
22
+ table: match[4],
23
+ };
24
+ } else {
25
+ return null;
26
+ }
27
+ },
28
+
29
+ subclassParsers: [],
30
+ };
@@ -0,0 +1,53 @@
1
+ import { UniqueViolationError } from '../../../../errors/UniqueViolationError.js';
2
+ import { isCode } from '../util.js';
3
+
4
+ const UNIQUE_INDEX_REGEX =
5
+ /Cannot insert duplicate key row in object '(.+)\.(.+)' with unique index '(.+)'. The duplicate key value is (.+)./;
6
+ const UNIQUE_CONSTRAINT_REGEX =
7
+ /Violation of UNIQUE KEY constraint '(.+)'. Cannot insert duplicate key in object '(.+)\.(.+)'. The duplicate key value is \((.+)\)/;
8
+
9
+ // The same error number (2627) is also used for primary key violations (#2688).
10
+ const PRIMARY_KEY_REGEX =
11
+ /Violation of PRIMARY KEY constraint '(.+)'. Cannot insert duplicate key in object '(.+)\.(.+)'. The duplicate key value is \((.+)\)/;
12
+
13
+ // 2601 - Violation in unique index
14
+ // 2627 - Violation in unique or primary key constraint (although it is implemented using unique index)
15
+
16
+ export const uniqueViolationErrorParser = {
17
+ error: UniqueViolationError,
18
+
19
+ parse: (err) => {
20
+ if (isCode(err, 14, 2627)) {
21
+ const constraintMatch =
22
+ UNIQUE_CONSTRAINT_REGEX.exec(err.message) || PRIMARY_KEY_REGEX.exec(err.message);
23
+
24
+ if (!constraintMatch) {
25
+ return null;
26
+ }
27
+
28
+ return {
29
+ table: constraintMatch[3],
30
+ schema: constraintMatch[2],
31
+ constraint: constraintMatch[1],
32
+ };
33
+ }
34
+
35
+ if (isCode(err, 14, 2601)) {
36
+ const indexMatch = UNIQUE_INDEX_REGEX.exec(err.message);
37
+
38
+ if (!indexMatch) {
39
+ return null;
40
+ }
41
+
42
+ return {
43
+ table: indexMatch[2],
44
+ constraint: indexMatch[3],
45
+ schema: indexMatch[1],
46
+ };
47
+ }
48
+
49
+ return null;
50
+ },
51
+
52
+ subclassParsers: [],
53
+ };
@@ -0,0 +1,51 @@
1
+ import { DBError } from '../../../errors/DBError.js';
2
+ import { mssqlErrorCodes } from '../../../errorCodes/mssql.js';
3
+ import { getCode } from './util.js';
4
+ import { checkViolationErrorParser } from './CheckViolationError/parser.js';
5
+ import { dataErrorParser } from './DataError/parser.js';
6
+ import { foreignKeyViolationErrorParser } from './ForeignKeyViolationError/parser.js';
7
+ import { notNullViolationErrorParser } from './NotNullViolationError/parser.js';
8
+ import { uniqueViolationErrorParser } from './UniqueViolationError/parser.js';
9
+
10
+ export const dbErrorParser = {
11
+ error: DBError,
12
+
13
+ parse: (err) => {
14
+ // db-errors was written for the `mssql` package, which wraps the errors of
15
+ // tedious in `originalError`. knex uses tedious directly, so we parse those.
16
+ if (err.code === 'EREQUEST' && mssqlErrorCodes.has(getCode(err))) {
17
+ return {
18
+ nativeError: err,
19
+ client: 'mssql',
20
+ };
21
+ }
22
+
23
+ // tedious throws an AggregateError with an empty message when SQL Server
24
+ // reports several errors for one statement, and knex only prepends the SQL
25
+ // to it. Add the messages of its errors, to give the reasons.
26
+ if (isTediousAggregateError(err)) {
27
+ return {
28
+ nativeError: err,
29
+ client: 'mssql',
30
+ message: err.message + err.errors.map((error) => error.message).join(' '),
31
+ };
32
+ }
33
+
34
+ return null;
35
+ },
36
+
37
+ subclassParsers: [
38
+ checkViolationErrorParser,
39
+ dataErrorParser,
40
+ foreignKeyViolationErrorParser,
41
+ notNullViolationErrorParser,
42
+ uniqueViolationErrorParser,
43
+ ],
44
+ };
45
+
46
+ function isTediousAggregateError(err) {
47
+ return (
48
+ err instanceof AggregateError &&
49
+ err.errors.every((error) => error?.code === 'EREQUEST' && Number.isInteger(error.number))
50
+ );
51
+ }
@@ -0,0 +1,16 @@
1
+ export function getCode(error) {
2
+ return {
3
+ errorCode: error.number,
4
+ severity: error.class,
5
+ };
6
+ }
7
+
8
+ export function isCode(error, severity, errorCode) {
9
+ const code = getCode(error);
10
+
11
+ if (code) {
12
+ return code.errorCode === errorCode && code.severity === severity;
13
+ }
14
+
15
+ return false;
16
+ }
@@ -0,0 +1,43 @@
1
+ import { ForeignKeyViolationError } from '../../../../../errors/ForeignKeyViolationError.js';
2
+
3
+ const NO_REFERENCED_REGEX =
4
+ /a foreign key constraint fails \(`(.+)`\.`(.+)`, CONSTRAINT `(.+)` FOREIGN KEY \(`(.+)`\) REFERENCES `(.+)` \(`(.+)`\)/;
5
+ const ROW_IS_REFERENCED_REGEX =
6
+ /Cannot delete or update a parent row: a foreign key constraint fails \(`(.+)`\.`(.+)`, CONSTRAINT `(.+)` FOREIGN KEY \(`(.+)`\) REFERENCES `(.+)` \(`(.+)`\)/;
7
+
8
+ export const foreignKeyViolationErrorParser = {
9
+ error: ForeignKeyViolationError,
10
+
11
+ parse: (err) => {
12
+ let match = null;
13
+
14
+ if (err.code === 'ER_NO_REFERENCED_ROW' || err.code === 'ER_ROW_IS_REFERENCED') {
15
+ // For these variants, there is no table or constraint information available.
16
+ // These seem to be mostly thrown on mysql 8 when the db user doesn't have
17
+ // priviledges to the parent table. There seems to be a bug however that
18
+ // causes these generic errors to be thrown even in some cases where the
19
+ // user DOES have priviledges.
20
+ return {
21
+ table: null,
22
+ constraint: null,
23
+ };
24
+ }
25
+
26
+ if (err.code === 'ER_NO_REFERENCED_ROW_2') {
27
+ match = NO_REFERENCED_REGEX.exec(err.sqlMessage);
28
+ } else if (err.code === 'ER_ROW_IS_REFERENCED_2') {
29
+ match = ROW_IS_REFERENCED_REGEX.exec(err.sqlMessage);
30
+ }
31
+
32
+ if (!match) {
33
+ return null;
34
+ }
35
+
36
+ return {
37
+ table: match[2],
38
+ constraint: match[3],
39
+ };
40
+ },
41
+
42
+ subclassParsers: [],
43
+ };
@@ -0,0 +1,29 @@
1
+ import { NotNullViolationError } from '../../../../../errors/NotNullViolationError.js';
2
+
3
+ const COLUMN_REGEX_BAD_NULL_ERROR = /Column '(.+)' cannot be null/;
4
+ const COLUMN_REGEX_NO_DEFAULT_FOR_FIELD = /Field '(.+)' doesn't have a default value/;
5
+
6
+ export const notNullViolationErrorParser = {
7
+ error: NotNullViolationError,
8
+
9
+ parse: (err) => {
10
+ let columnMatch = null;
11
+
12
+ if (err.code === 'ER_BAD_NULL_ERROR') {
13
+ columnMatch = COLUMN_REGEX_BAD_NULL_ERROR.exec(err.sqlMessage);
14
+ } else if (err.code === 'ER_NO_DEFAULT_FOR_FIELD') {
15
+ columnMatch = COLUMN_REGEX_NO_DEFAULT_FOR_FIELD.exec(err.sqlMessage);
16
+ }
17
+
18
+ if (!columnMatch) {
19
+ return null;
20
+ }
21
+
22
+ // No way to reliably get `table` from mysql error.
23
+ return {
24
+ column: columnMatch[1],
25
+ };
26
+ },
27
+
28
+ subclassParsers: [],
29
+ };