objection 3.0.0-rc.2 → 3.0.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.
@@ -858,8 +858,8 @@ function getReadOnlyAttributes(modelClass) {
858
858
  }
859
859
 
860
860
  function getReadOnlyAttributesRecursively(modelClass) {
861
- if (modelClass === Model) {
862
- // Stop recursion to the model class.
861
+ if (modelClass === Model || modelClass.prototype == undefined) {
862
+ // Stop recursion to the model class or its prototype is null or undefined.
863
863
  return [];
864
864
  }
865
865
 
@@ -56,7 +56,13 @@ function cloneWithOpt(model, shallow, stripInternal) {
56
56
  const key = keys[i];
57
57
  const value = model[key];
58
58
 
59
- if ((shallow && relationNames.includes(key)) || (stripInternal && isInternalProp(key))) {
59
+ if (shallow && relationNames.includes(key)) {
60
+ // The constructor may have given default values for relations.
61
+ delete clone[key];
62
+ continue;
63
+ }
64
+
65
+ if (stripInternal && isInternalProp(key)) {
60
66
  continue;
61
67
  }
62
68
 
@@ -3,6 +3,7 @@
3
3
  const { isString, isFunction, isRegExp, last } = require('../utils/objectUtils');
4
4
  const { QueryBuilderContextBase } = require('./QueryBuilderContextBase');
5
5
  const { QueryBuilderUserContext } = require('./QueryBuilderUserContext');
6
+ const { deprecate } = require('../utils/deprecate');
6
7
 
7
8
  const AllSelector = () => true;
8
9
  const SelectSelector =
@@ -494,6 +495,7 @@ class QueryBuilderOperationSupport {
494
495
  }
495
496
 
496
497
  skipUndefined() {
498
+ deprecate('skipUndefined() is deprecated and will be removed in objection 4.0');
497
499
  this.internalOptions().skipUndefined = true;
498
500
  return this;
499
501
  }
@@ -291,7 +291,7 @@ function modelToNode(model, node) {
291
291
  for (let r = 0, lr = relationNames.length; r < lr; ++r) {
292
292
  const relName = relationNames[r];
293
293
 
294
- if (model.hasOwnProperty(relName)) {
294
+ if (model[relName] !== undefined) {
295
295
  let childNode = node[relName];
296
296
 
297
297
  if (!childNode) {
@@ -18,7 +18,7 @@ class GraphRecursiveUpsert extends GraphOperation {
18
18
 
19
19
  function hasRelations(obj) {
20
20
  for (const relationName of obj.constructor.getRelationNames()) {
21
- if (obj.hasOwnProperty(relationName)) {
21
+ if (obj[relationName] !== undefined) {
22
22
  return true;
23
23
  }
24
24
  }
@@ -71,12 +71,10 @@ class RelationOwner {
71
71
  if (values) {
72
72
  builder.whereInComposite(relatedRefs, values);
73
73
  } else {
74
- builder.where(false);
75
- builder.resolve([]);
74
+ builder.where(false).resolve([]);
76
75
  }
77
76
  } else {
78
- builder.where(false);
79
- builder.resolve([]);
77
+ builder.where(false).resolve([]);
80
78
  }
81
79
 
82
80
  return builder;
@@ -37,25 +37,29 @@ function isMsSql(knex) {
37
37
  }
38
38
 
39
39
  function isKnexQueryBuilder(value) {
40
- return hasConstructor(value, 'Builder') && 'client' in value;
40
+ return (
41
+ hasConstructor(value) &&
42
+ isFunction(value.select) &&
43
+ isFunction(value.column) &&
44
+ value.select === value.column &&
45
+ 'client' in value
46
+ );
41
47
  }
42
48
 
43
49
  function isKnexJoinBuilder(value) {
44
- return hasConstructor(value, 'JoinClause') && 'joinType' in value;
50
+ return hasConstructor(value) && value.grouping === 'join' && 'joinType' in value;
45
51
  }
46
52
 
47
53
  function isKnexRaw(value) {
48
- return hasConstructor(value, 'Raw') && 'client' in value;
54
+ return hasConstructor(value) && value.isRawInstance && 'client' in value;
49
55
  }
50
56
 
51
57
  function isKnexTransaction(knex) {
52
58
  return !!getDialect(knex) && isFunction(knex.commit) && isFunction(knex.rollback);
53
59
  }
54
60
 
55
- function hasConstructor(value, constructorName) {
56
- return (
57
- isObject(value) && isFunction(value.constructor) && value.constructor.name === constructorName
58
- );
61
+ function hasConstructor(value) {
62
+ return isObject(value) && isFunction(value.constructor);
59
63
  }
60
64
 
61
65
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "objection",
3
- "version": "3.0.0-rc.2",
3
+ "version": "3.0.1",
4
4
  "description": "An SQL-friendly ORM for Node.js",
5
5
  "main": "lib/objection.js",
6
6
  "license": "MIT",
@@ -73,7 +73,7 @@
73
73
  "pg": "^8.7.1",
74
74
  "prettier": "2.4.1",
75
75
  "sqlite3": "^5.0.2",
76
- "typescript": "^4.4.4",
76
+ "typescript": "^4.5.4",
77
77
  "vuepress": "1.8.2"
78
78
  }
79
79
  }
@@ -179,6 +179,11 @@ declare namespace Objection {
179
179
  */
180
180
  type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
181
181
 
182
+ /**
183
+ * Type that attempts to only select the user-defined model properties.
184
+ */
185
+ type DataPropertyNames<T> = Exclude<NonFunctionPropertyNames<T>, 'QueryBuilderType'>;
186
+
182
187
  /**
183
188
  * Removes `undefined` from a type.
184
189
  */
@@ -188,14 +193,14 @@ declare namespace Objection {
188
193
  * A Pojo version of model.
189
194
  */
190
195
  type ModelObject<T extends Model> = {
191
- [K in Exclude<NonFunctionPropertyNames<T>, 'QueryBuilderType'>]: T[K];
196
+ [K in DataPropertyNames<T>]: T[K];
192
197
  };
193
198
 
194
199
  /**
195
200
  * Any object that has some of the properties of model class T match this type.
196
201
  */
197
202
  type PartialModelObject<T extends Model> = {
198
- [K in NonFunctionPropertyNames<T>]?: Defined<T[K]> extends Model
203
+ [K in DataPropertyNames<T>]?: Defined<T[K]> extends Model
199
204
  ? T[K]
200
205
  : Defined<T[K]> extends Array<infer I>
201
206
  ? I extends Model
@@ -217,7 +222,7 @@ declare namespace Objection {
217
222
  * Just like PartialModelObject but this is applied recursively to relations.
218
223
  */
219
224
  type PartialModelGraph<M, T = M & GraphParameters> = {
220
- [K in NonFunctionPropertyNames<T>]?: Defined<T[K]> extends Model
225
+ [K in DataPropertyNames<T>]?: Defined<T[K]> extends Model
221
226
  ? PartialModelGraph<Defined<T[K]>>
222
227
  : Defined<T[K]> extends Array<infer I>
223
228
  ? I extends Model
@@ -1,103 +0,0 @@
1
- 'use strict';
2
-
3
- const { isObject } = require('../utils/objectUtils');
4
- const { isInternalProp } = require('../utils/internalPropUtils');
5
-
6
- function omit(model, args) {
7
- if (args.length === 1 && isObject(args[0])) {
8
- const keys = args[0];
9
-
10
- if (Array.isArray(keys)) {
11
- omitArray(model, keys);
12
- } else {
13
- omitObject(model, keys);
14
- }
15
- } else {
16
- omitArray(model, args);
17
- }
18
-
19
- return model;
20
- }
21
-
22
- function pick(model, args) {
23
- if (args.length === 1 && isObject(args[0])) {
24
- const keys = args[0];
25
-
26
- if (Array.isArray(keys)) {
27
- pickArray(model, keys);
28
- } else {
29
- pickObject(model, keys);
30
- }
31
- } else {
32
- pickArray(model, args);
33
- }
34
-
35
- return model;
36
- }
37
-
38
- function omitObject(model, keyObj) {
39
- const modelClass = model.constructor;
40
- const keys = Object.keys(keyObj);
41
-
42
- for (let i = 0, l = keys.length; i < l; ++i) {
43
- const key = keys[i];
44
- const value = keyObj[key];
45
-
46
- if (value && !isInternalProp(key) && model.hasOwnProperty(key)) {
47
- modelClass.omitImpl(model, key);
48
- }
49
- }
50
- }
51
-
52
- function omitArray(model, keys) {
53
- const modelClass = model.constructor;
54
-
55
- for (let i = 0, l = keys.length; i < l; ++i) {
56
- const key = keys[i];
57
-
58
- if (!isInternalProp(key) && model.hasOwnProperty(key)) {
59
- modelClass.omitImpl(model, key);
60
- }
61
- }
62
- }
63
-
64
- function pickObject(model, keyObj) {
65
- const modelClass = model.constructor;
66
- const keys = Object.keys(model);
67
-
68
- for (let i = 0, l = keys.length; i < l; ++i) {
69
- const key = keys[i];
70
-
71
- if (!isInternalProp(key) && !keyObj[key]) {
72
- modelClass.omitImpl(model, key);
73
- }
74
- }
75
- }
76
-
77
- function pickArray(model, pick) {
78
- const modelClass = model.constructor;
79
- const keys = Object.keys(model);
80
-
81
- for (let i = 0, l = keys.length; i < l; ++i) {
82
- const key = keys[i];
83
-
84
- if (!isInternalProp(key) && !contains(pick, key)) {
85
- modelClass.omitImpl(model, key);
86
- }
87
- }
88
- }
89
-
90
- function contains(arr, value) {
91
- for (let i = 0, l = arr.length; i < l; ++i) {
92
- if (arr[i] === value) {
93
- return true;
94
- }
95
- }
96
-
97
- return false;
98
- }
99
-
100
- module.exports = {
101
- omit,
102
- pick,
103
- };