@abedmurrar/objection 4.0.0-beta.6 → 4.0.0-beta.8
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/README.md +1 -1
- package/lib/initialize.js +4 -5
- package/lib/model/AjvValidator.js +24 -21
- package/lib/model/Model.js +116 -114
- package/lib/model/ModifierNotFoundError.js +4 -4
- package/lib/model/NotFoundError.js +4 -4
- package/lib/model/RelationDoesNotExistError.js +4 -4
- package/lib/model/ValidationError.js +8 -8
- package/lib/model/Validator.js +7 -6
- package/lib/model/getModel.js +4 -4
- package/lib/model/graph/ModelGraph.js +9 -7
- package/lib/model/graph/ModelGraphBuilder.js +24 -24
- package/lib/model/graph/ModelGraphEdge.js +4 -4
- package/lib/model/graph/ModelGraphNode.js +17 -11
- package/lib/model/inheritModel.js +3 -4
- package/lib/model/modelBindKnex.js +12 -13
- package/lib/model/modelClone.js +15 -16
- package/lib/model/modelColPropMap.js +7 -8
- package/lib/model/modelConcurrency.js +6 -7
- package/lib/model/modelId.js +4 -5
- package/lib/model/modelJsonAttributes.js +10 -11
- package/lib/model/modelParseRelations.js +5 -6
- package/lib/model/modelQueryProps.js +12 -13
- package/lib/model/modelSet.js +17 -18
- package/lib/model/modelTableMetadata.js +9 -10
- package/lib/model/modelToJson.js +13 -14
- package/lib/model/modelUtils.js +7 -7
- package/lib/model/modelValidate.js +5 -6
- package/lib/model/modelValues.js +10 -10
- package/lib/model/modelVisitor.js +3 -4
- package/lib/transaction.js +44 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ npm install @abedmurrar/objection@next knex
|
|
|
18
18
|
- **MySQL and MariaDB as first-class databases**, not only postgres: JSON field expressions, the `whereJson*` methods, batch inserts and an `fn.*` helper for every MySQL 8.4 function. MariaDB 10.6 and 11.4 are tested in CI. See [MySQL and MariaDB](doc/fork/mysql.md).
|
|
19
19
|
- **Expressions without raw SQL**, like `Pet.ref('age').gte(3)`, `fn.count().filter(...)`, `fn.case()` and window functions. See [Expressions](doc/fork/expressions.md).
|
|
20
20
|
- **Safer.** JSON field keys are escaped (in objection.js 3.1.5 they allowed SQL injection), an `orWhere()` can no longer match the rows of other relation owners, and `for()` is no longer silently ignored. See [Breaking changes](doc/fork/breaking-changes.md).
|
|
21
|
-
- **Modern.** Node.js 24+, ES modules and CommonJS from one package, knex 3.3, the knex methods objection didn't have,
|
|
21
|
+
- **Modern.** Node.js 24+, ES modules and CommonJS from one package, knex 3.3, the knex methods objection didn't have, no lodash and no bluebird, and `npm audit` reports nothing. The source is written in strict TypeScript and compiled with TypeScript 7.
|
|
22
22
|
|
|
23
23
|
## A quick look
|
|
24
24
|
|
package/lib/initialize.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initialize = initialize;
|
|
2
4
|
async function initialize(knex, modelClasses) {
|
|
3
5
|
if (!modelClasses) {
|
|
4
6
|
modelClasses = knex;
|
|
5
7
|
knex = modelClasses[0].knex();
|
|
6
8
|
}
|
|
7
|
-
await Promise.all(modelClasses.map((modelClass) => modelClass.fetchTableMetadata({ knex })));
|
|
9
|
+
await Promise.all(modelClasses.map((modelClass) => modelClass.fetchTableMetadata({ knex: knex })));
|
|
8
10
|
}
|
|
9
|
-
module.exports = {
|
|
10
|
-
initialize,
|
|
11
|
-
};
|
|
@@ -1,22 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AjvValidator = void 0;
|
|
7
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
8
|
+
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
9
|
+
const Validator_1 = require("./Validator");
|
|
10
|
+
const ValidationError_1 = require("../model/ValidationError");
|
|
11
|
+
const objectUtils_1 = require("../utils/objectUtils");
|
|
12
|
+
class AjvValidator extends Validator_1.Validator {
|
|
8
13
|
static init(self, conf) {
|
|
9
14
|
super.init(self, conf);
|
|
10
15
|
self.ajvOptions = Object.assign({}, conf.options, {
|
|
11
16
|
allErrors: true,
|
|
12
17
|
});
|
|
13
18
|
// Create a normal Ajv instance.
|
|
14
|
-
self.ajv = new
|
|
19
|
+
self.ajv = new ajv_1.default(Object.assign({
|
|
15
20
|
useDefaults: true,
|
|
16
21
|
}, self.ajvOptions));
|
|
17
22
|
// Create an instance that doesn't set default values. We need this one
|
|
18
23
|
// to validate `patch` objects (objects that have a subset of properties).
|
|
19
|
-
self.ajvNoDefaults = new
|
|
24
|
+
self.ajvNoDefaults = new ajv_1.default(Object.assign({}, self.ajvOptions, {
|
|
20
25
|
useDefaults: false,
|
|
21
26
|
}));
|
|
22
27
|
// Caches for the compiled validator functions: by the serialized schema,
|
|
@@ -29,7 +34,7 @@ class AjvValidator extends Validator {
|
|
|
29
34
|
}
|
|
30
35
|
// Only add Ajv formats if they weren't added in user-space already
|
|
31
36
|
if (!ajv.formats['date-time']) {
|
|
32
|
-
|
|
37
|
+
(0, ajv_formats_1.default)(ajv);
|
|
33
38
|
}
|
|
34
39
|
};
|
|
35
40
|
setupAjv(self.ajv);
|
|
@@ -74,7 +79,7 @@ class AjvValidator extends Validator {
|
|
|
74
79
|
// runs once per schema object. Classes bound to a knex instance share their
|
|
75
80
|
// validator with the class they are bound from, and a bound class can have
|
|
76
81
|
// a schema of its own, so the cache isn't keyed by the class name.
|
|
77
|
-
const isClassSchema = jsonSchema === modelClass.getJsonSchema() && isObject(jsonSchema);
|
|
82
|
+
const isClassSchema = jsonSchema === modelClass.getJsonSchema() && (0, objectUtils_1.isObject)(jsonSchema);
|
|
78
83
|
let validators = isClassSchema ? this.schemaCache.get(jsonSchema) : undefined;
|
|
79
84
|
let validator = null;
|
|
80
85
|
if (!validators) {
|
|
@@ -119,6 +124,7 @@ class AjvValidator extends Validator {
|
|
|
119
124
|
return this.ajv.compile(jsonSchema);
|
|
120
125
|
}
|
|
121
126
|
}
|
|
127
|
+
exports.AjvValidator = AjvValidator;
|
|
122
128
|
function parseValidationError(errors, modelClass, options, ajvOptions) {
|
|
123
129
|
if (!errors) {
|
|
124
130
|
return null;
|
|
@@ -165,23 +171,23 @@ function parseValidationError(errors, modelClass, options, ajvOptions) {
|
|
|
165
171
|
return null;
|
|
166
172
|
}
|
|
167
173
|
return modelClass.createValidationError({
|
|
168
|
-
type: ValidationErrorType.ModelValidation,
|
|
174
|
+
type: ValidationError_1.ValidationErrorType.ModelValidation,
|
|
169
175
|
data: errorHash,
|
|
170
176
|
});
|
|
171
177
|
}
|
|
172
178
|
function cloneDeep(obj) {
|
|
173
|
-
if (isObject(obj) && obj.$isObjectionModel) {
|
|
179
|
+
if ((0, objectUtils_1.isObject)(obj) && obj.$isObjectionModel) {
|
|
174
180
|
return obj.$clone();
|
|
175
181
|
}
|
|
176
182
|
else {
|
|
177
|
-
return
|
|
183
|
+
return (0, objectUtils_1.cloneDeep)(obj);
|
|
178
184
|
}
|
|
179
185
|
}
|
|
180
186
|
// Whether a schema has default values is checked for every validated model,
|
|
181
187
|
// so the answer is cached per schema object.
|
|
182
188
|
const setsDefaultValuesCache = new WeakMap();
|
|
183
189
|
function setsDefaultValues(jsonSchema) {
|
|
184
|
-
if (!isObject(jsonSchema)) {
|
|
190
|
+
if (!(0, objectUtils_1.isObject)(jsonSchema)) {
|
|
185
191
|
return jsonSchema && jsonSchema.properties && hasDefaults(jsonSchema.properties);
|
|
186
192
|
}
|
|
187
193
|
let result = setsDefaultValuesCache.get(jsonSchema);
|
|
@@ -202,7 +208,7 @@ function hasDefaults(obj) {
|
|
|
202
208
|
function arrayHasDefaults(arr) {
|
|
203
209
|
for (let i = 0, l = arr.length; i < l; ++i) {
|
|
204
210
|
const val = arr[i];
|
|
205
|
-
if (isObject(val) && hasDefaults(val)) {
|
|
211
|
+
if ((0, objectUtils_1.isObject)(val) && hasDefaults(val)) {
|
|
206
212
|
return true;
|
|
207
213
|
}
|
|
208
214
|
}
|
|
@@ -217,7 +223,7 @@ function objectHasDefaults(obj) {
|
|
|
217
223
|
}
|
|
218
224
|
else {
|
|
219
225
|
const val = obj[key];
|
|
220
|
-
if (isObject(val) && hasDefaults(val)) {
|
|
226
|
+
if ((0, objectUtils_1.isObject)(val) && hasDefaults(val)) {
|
|
221
227
|
return true;
|
|
222
228
|
}
|
|
223
229
|
}
|
|
@@ -226,7 +232,7 @@ function objectHasDefaults(obj) {
|
|
|
226
232
|
}
|
|
227
233
|
function jsonSchemaWithoutRequired(jsonSchema) {
|
|
228
234
|
const subSchemaProps = ['anyOf', 'oneOf', 'allOf', 'not', 'then', 'else', 'properties'];
|
|
229
|
-
return Object.assign(omit(jsonSchema, ['required', ...subSchemaProps]), ...subSchemaProps.map((prop) => subSchemaWithoutRequired(jsonSchema, prop)), jsonSchema && jsonSchema.definitions && Object.keys(jsonSchema.definitions).length > 0
|
|
235
|
+
return Object.assign((0, objectUtils_1.omit)(jsonSchema, ['required', ...subSchemaProps]), ...subSchemaProps.map((prop) => subSchemaWithoutRequired(jsonSchema, prop)), jsonSchema && jsonSchema.definitions && Object.keys(jsonSchema.definitions).length > 0
|
|
230
236
|
? {
|
|
231
237
|
definitions: Object.assign(...Object.keys(jsonSchema.definitions).map((prop) => ({
|
|
232
238
|
[prop]: jsonSchemaWithoutRequired(jsonSchema.definitions[prop]),
|
|
@@ -273,6 +279,3 @@ function jsonSchemaArrayWithoutRequired(jsonSchemaArray) {
|
|
|
273
279
|
function isNotEmptyObject(obj) {
|
|
274
280
|
return Object.keys(obj).length !== 0;
|
|
275
281
|
}
|
|
276
|
-
module.exports = {
|
|
277
|
-
AjvValidator,
|
|
278
|
-
};
|