@abedmurrar/objection 0.0.0-stage → 4.0.0-alpha.0

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 (190) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +69 -2
  3. package/lib/.eslintrc.json +8 -0
  4. package/lib/initialize.js +14 -0
  5. package/lib/model/AjvValidator.js +325 -0
  6. package/lib/model/Model.js +942 -0
  7. package/lib/model/ModifierNotFoundError.js +12 -0
  8. package/lib/model/NotFoundError.js +23 -0
  9. package/lib/model/RelationDoesNotExistError.js +14 -0
  10. package/lib/model/ValidationError.js +54 -0
  11. package/lib/model/Validator.js +26 -0
  12. package/lib/model/getModel.js +9 -0
  13. package/lib/model/graph/ModelGraph.js +80 -0
  14. package/lib/model/graph/ModelGraphBuilder.js +241 -0
  15. package/lib/model/graph/ModelGraphEdge.js +52 -0
  16. package/lib/model/graph/ModelGraphNode.js +240 -0
  17. package/lib/model/inheritModel.js +28 -0
  18. package/lib/model/modelBindKnex.js +83 -0
  19. package/lib/model/modelClone.js +121 -0
  20. package/lib/model/modelColPropMap.js +34 -0
  21. package/lib/model/modelId.js +62 -0
  22. package/lib/model/modelJsonAttributes.js +92 -0
  23. package/lib/model/modelParseRelations.js +82 -0
  24. package/lib/model/modelQueryProps.js +147 -0
  25. package/lib/model/modelSet.js +136 -0
  26. package/lib/model/modelTableMetadata.js +83 -0
  27. package/lib/model/modelToJson.js +187 -0
  28. package/lib/model/modelUtils.js +47 -0
  29. package/lib/model/modelValidate.js +48 -0
  30. package/lib/model/modelValues.js +126 -0
  31. package/lib/model/modelVisitor.js +41 -0
  32. package/lib/objection.js +114 -0
  33. package/lib/queryBuilder/FunctionBuilder.js +339 -0
  34. package/lib/queryBuilder/InternalOptions.js +29 -0
  35. package/lib/queryBuilder/JoinBuilder.js +157 -0
  36. package/lib/queryBuilder/QueryBuilder.js +1398 -0
  37. package/lib/queryBuilder/QueryBuilderBase.js +884 -0
  38. package/lib/queryBuilder/QueryBuilderContext.js +27 -0
  39. package/lib/queryBuilder/QueryBuilderContextBase.js +35 -0
  40. package/lib/queryBuilder/QueryBuilderOperationSupport.js +558 -0
  41. package/lib/queryBuilder/QueryBuilderUserContext.js +31 -0
  42. package/lib/queryBuilder/RawBuilder.js +93 -0
  43. package/lib/queryBuilder/ReferenceBuilder.js +284 -0
  44. package/lib/queryBuilder/RelationExpression.js +431 -0
  45. package/lib/queryBuilder/StaticHookArguments.js +134 -0
  46. package/lib/queryBuilder/ValueBuilder.js +127 -0
  47. package/lib/queryBuilder/expressions.js +151 -0
  48. package/lib/queryBuilder/functions/mysql.js +817 -0
  49. package/lib/queryBuilder/graph/GraphAction.js +69 -0
  50. package/lib/queryBuilder/graph/GraphData.js +14 -0
  51. package/lib/queryBuilder/graph/GraphFetcher.js +191 -0
  52. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +184 -0
  53. package/lib/queryBuilder/graph/GraphOperation.js +27 -0
  54. package/lib/queryBuilder/graph/GraphOptions.js +207 -0
  55. package/lib/queryBuilder/graph/GraphUpsert.js +305 -0
  56. package/lib/queryBuilder/graph/delete/GraphDelete.js +20 -0
  57. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +74 -0
  58. package/lib/queryBuilder/graph/insert/GraphInsert.js +145 -0
  59. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +225 -0
  60. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +92 -0
  61. package/lib/queryBuilder/graph/patch/GraphPatch.js +20 -0
  62. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +258 -0
  63. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +31 -0
  64. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +126 -0
  65. package/lib/queryBuilder/join/JoinResultColumn.js +23 -0
  66. package/lib/queryBuilder/join/JoinResultParser.js +119 -0
  67. package/lib/queryBuilder/join/RelationJoiner.js +341 -0
  68. package/lib/queryBuilder/join/TableNode.js +163 -0
  69. package/lib/queryBuilder/join/TableTree.js +85 -0
  70. package/lib/queryBuilder/join/utils.js +25 -0
  71. package/lib/queryBuilder/operations/DelegateOperation.js +141 -0
  72. package/lib/queryBuilder/operations/DeleteOperation.js +51 -0
  73. package/lib/queryBuilder/operations/FindByIdOperation.js +37 -0
  74. package/lib/queryBuilder/operations/FindByIdsOperation.js +29 -0
  75. package/lib/queryBuilder/operations/FindOperation.js +164 -0
  76. package/lib/queryBuilder/operations/FirstOperation.js +27 -0
  77. package/lib/queryBuilder/operations/FromOperation.js +110 -0
  78. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +49 -0
  79. package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +45 -0
  80. package/lib/queryBuilder/operations/InsertGraphOperation.js +108 -0
  81. package/lib/queryBuilder/operations/InsertOperation.js +206 -0
  82. package/lib/queryBuilder/operations/InstanceDeleteOperation.js +46 -0
  83. package/lib/queryBuilder/operations/InstanceFindOperation.js +21 -0
  84. package/lib/queryBuilder/operations/InstanceInsertOperation.js +24 -0
  85. package/lib/queryBuilder/operations/InstanceUpdateOperation.js +59 -0
  86. package/lib/queryBuilder/operations/JoinRelatedOperation.js +103 -0
  87. package/lib/queryBuilder/operations/KnexOperation.js +14 -0
  88. package/lib/queryBuilder/operations/MergeOperation.js +56 -0
  89. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +192 -0
  90. package/lib/queryBuilder/operations/OnBuildKnexOperation.js +29 -0
  91. package/lib/queryBuilder/operations/OnBuildOperation.js +29 -0
  92. package/lib/queryBuilder/operations/OnErrorOperation.js +29 -0
  93. package/lib/queryBuilder/operations/QueryBuilderOperation.js +297 -0
  94. package/lib/queryBuilder/operations/RangeOperation.js +47 -0
  95. package/lib/queryBuilder/operations/RelateOperation.js +27 -0
  96. package/lib/queryBuilder/operations/ReturningOperation.js +30 -0
  97. package/lib/queryBuilder/operations/RunAfterOperation.js +29 -0
  98. package/lib/queryBuilder/operations/RunBeforeOperation.js +29 -0
  99. package/lib/queryBuilder/operations/UnrelateOperation.js +23 -0
  100. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +80 -0
  101. package/lib/queryBuilder/operations/UpdateOperation.js +155 -0
  102. package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +45 -0
  103. package/lib/queryBuilder/operations/UpsertGraphOperation.js +66 -0
  104. package/lib/queryBuilder/operations/WhereCompositeOperation.js +57 -0
  105. package/lib/queryBuilder/operations/WhereInOperation.js +48 -0
  106. package/lib/queryBuilder/operations/eager/EagerOperation.js +61 -0
  107. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +51 -0
  108. package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +13 -0
  109. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +285 -0
  110. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +36 -0
  111. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +53 -0
  112. package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +31 -0
  113. package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +31 -0
  114. package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +167 -0
  115. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +164 -0
  116. package/lib/queryBuilder/operations/select/SelectOperation.js +66 -0
  117. package/lib/queryBuilder/operations/select/Selection.js +123 -0
  118. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +117 -0
  119. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +100 -0
  120. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +104 -0
  121. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +886 -0
  122. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +97 -0
  123. package/lib/queryBuilder/parsers/relationExpressionParser.js +1042 -0
  124. package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +141 -0
  125. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +22 -0
  126. package/lib/queryBuilder/transformations/QueryTransformation.js +11 -0
  127. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +71 -0
  128. package/lib/queryBuilder/transformations/index.js +15 -0
  129. package/lib/queryBuilder/whereScopes.js +227 -0
  130. package/lib/relations/Relation.js +401 -0
  131. package/lib/relations/RelationDeleteOperation.js +29 -0
  132. package/lib/relations/RelationFindOperation.js +149 -0
  133. package/lib/relations/RelationInsertOperation.js +33 -0
  134. package/lib/relations/RelationOwner.js +236 -0
  135. package/lib/relations/RelationProperty.js +247 -0
  136. package/lib/relations/RelationUpdateOperation.js +30 -0
  137. package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +23 -0
  138. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +44 -0
  139. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +50 -0
  140. package/lib/relations/belongsToOne/BelongsToOneRelation.js +53 -0
  141. package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +61 -0
  142. package/lib/relations/hasMany/HasManyInsertOperation.js +61 -0
  143. package/lib/relations/hasMany/HasManyRelateOperation.js +64 -0
  144. package/lib/relations/hasMany/HasManyRelation.js +49 -0
  145. package/lib/relations/hasMany/HasManyUnrelateOperation.js +33 -0
  146. package/lib/relations/hasOne/HasOneRelation.js +13 -0
  147. package/lib/relations/hasOneThrough/HasOneThroughRelation.js +13 -0
  148. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +105 -0
  149. package/lib/relations/manyToMany/ManyToManyRelation.js +507 -0
  150. package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +36 -0
  151. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +10 -0
  152. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +26 -0
  153. package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +12 -0
  154. package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +105 -0
  155. package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +52 -0
  156. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +81 -0
  157. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +14 -0
  158. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +34 -0
  159. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +16 -0
  160. package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +10 -0
  161. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +82 -0
  162. package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +12 -0
  163. package/lib/transaction.js +96 -0
  164. package/lib/utils/assert.js +25 -0
  165. package/lib/utils/buildUtils.js +21 -0
  166. package/lib/utils/classUtils.js +13 -0
  167. package/lib/utils/clone.js +2329 -0
  168. package/lib/utils/createModifier.js +42 -0
  169. package/lib/utils/deprecate.js +15 -0
  170. package/lib/utils/identifierMapping.js +227 -0
  171. package/lib/utils/internalPropUtils.js +11 -0
  172. package/lib/utils/knexUtils.js +76 -0
  173. package/lib/utils/mixin.js +25 -0
  174. package/lib/utils/mysqlJsonPath.js +55 -0
  175. package/lib/utils/normalizeIds.js +144 -0
  176. package/lib/utils/objectUtils.js +416 -0
  177. package/lib/utils/parseFieldExpression.js +51 -0
  178. package/lib/utils/postgresJsonPath.js +110 -0
  179. package/lib/utils/promiseUtils/after.js +17 -0
  180. package/lib/utils/promiseUtils/afterReturn.js +17 -0
  181. package/lib/utils/promiseUtils/index.js +17 -0
  182. package/lib/utils/promiseUtils/isPromise.js +11 -0
  183. package/lib/utils/promiseUtils/map.js +110 -0
  184. package/lib/utils/promiseUtils/mapAfterAllReturn.js +29 -0
  185. package/lib/utils/promiseUtils/try.js +22 -0
  186. package/lib/utils/resolveModel.js +107 -0
  187. package/lib/utils/tmpColumnUtils.js +16 -0
  188. package/package.json +86 -4
  189. package/typings/objection/index.d.ts +4104 -0
  190. package/typings/objection/relation-expression.d.ts +237 -0
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ const { isPlainObject } = require('../utils/objectUtils');
4
+ const { buildArg } = require('../utils/buildUtils');
5
+ const { addExpressionMethods } = require('./expressions');
6
+
7
+ class RawBuilder {
8
+ constructor(sql, args) {
9
+ this._sql = `${sql}`;
10
+ this._args = args;
11
+ this._as = null;
12
+ }
13
+
14
+ get alias() {
15
+ return this._as;
16
+ }
17
+
18
+ as(as) {
19
+ this._as = as;
20
+ return this;
21
+ }
22
+
23
+ toKnexRaw(builder) {
24
+ let args = null;
25
+ let sql = this._sql;
26
+
27
+ if (this._args.length === 1 && isPlainObject(this._args[0])) {
28
+ args = buildObject(this._args[0], builder);
29
+
30
+ if (this._as) {
31
+ args.__alias__ = this._as;
32
+ sql += ' as :__alias__:';
33
+ }
34
+ } else {
35
+ args = buildArray(this._args, builder);
36
+
37
+ if (this._as) {
38
+ args.push(this._as);
39
+ sql += ' as ??';
40
+ }
41
+ }
42
+
43
+ return builder.knex().raw(sql, args);
44
+ }
45
+ }
46
+
47
+ Object.defineProperties(RawBuilder.prototype, {
48
+ isObjectionRawBuilder: {
49
+ enumerable: false,
50
+ writable: false,
51
+ value: true,
52
+ },
53
+ });
54
+
55
+ addExpressionMethods(RawBuilder.prototype);
56
+
57
+ function buildArray(arr, builder) {
58
+ return arr.map((it) => buildArg(it, builder));
59
+ }
60
+
61
+ function buildObject(obj, builder) {
62
+ return Object.keys(obj).reduce((args, key) => {
63
+ args[key] = buildArg(obj[key], builder);
64
+ return args;
65
+ }, {});
66
+ }
67
+
68
+ function normalizeRawArgs(argsIn) {
69
+ const [sql, ...restArgs] = argsIn;
70
+
71
+ if (restArgs.length === 1 && Array.isArray(restArgs[0])) {
72
+ return {
73
+ sql,
74
+ args: restArgs[0],
75
+ };
76
+ } else {
77
+ return {
78
+ sql,
79
+ args: restArgs,
80
+ };
81
+ }
82
+ }
83
+
84
+ function raw(...argsIn) {
85
+ const { sql, args } = normalizeRawArgs(argsIn);
86
+ return new RawBuilder(sql, args);
87
+ }
88
+
89
+ module.exports = {
90
+ RawBuilder,
91
+ normalizeRawArgs,
92
+ raw,
93
+ };
@@ -0,0 +1,284 @@
1
+ 'use strict';
2
+
3
+ const { parseFieldExpression } = require('../utils/parseFieldExpression');
4
+ const { isObject } = require('../utils/objectUtils');
5
+ const { isMySql } = require('../utils/knexUtils');
6
+ const { toPgJsonPathLiteral, getJsonDialect } = require('../utils/postgresJsonPath');
7
+ const { toMySqlJsonPath, toMySqlCastType, isBooleanType } = require('../utils/mysqlJsonPath');
8
+ const { addExpressionMethods } = require('./expressions');
9
+
10
+ class ReferenceBuilder {
11
+ constructor(expr) {
12
+ this._expr = expr;
13
+ this._parsedExpr = null;
14
+ this._column = null;
15
+ this._table = null;
16
+ this._cast = null;
17
+ this._toJson = false;
18
+ this._table = null;
19
+ this._alias = null;
20
+ this._modelClass = null;
21
+
22
+ // This `if` makes it possible for `clone` to skip
23
+ // parsing the expression again.
24
+ if (expr !== null) {
25
+ this._parseExpression(expr);
26
+ }
27
+ }
28
+
29
+ get parsedExpr() {
30
+ return this._parsedExpr;
31
+ }
32
+
33
+ get column() {
34
+ return this._column;
35
+ }
36
+
37
+ set column(column) {
38
+ this._column = column;
39
+ }
40
+
41
+ get alias() {
42
+ return this._alias;
43
+ }
44
+
45
+ set alias(alias) {
46
+ this._alias = alias;
47
+ }
48
+
49
+ get tableName() {
50
+ return this._table;
51
+ }
52
+
53
+ set tableName(table) {
54
+ this._table = table;
55
+ }
56
+
57
+ get modelClass() {
58
+ return this._modelClass;
59
+ }
60
+
61
+ set modelClass(modelClass) {
62
+ this._modelClass = modelClass;
63
+ }
64
+
65
+ get isPlainColumnRef() {
66
+ return (
67
+ (!this._parsedExpr || this._parsedExpr.access.length === 0) && !this._cast && !this._toJson
68
+ );
69
+ }
70
+
71
+ get expression() {
72
+ return this._expr;
73
+ }
74
+
75
+ get cast() {
76
+ return this._cast;
77
+ }
78
+
79
+ fullColumn(builder) {
80
+ const table = this.tableName
81
+ ? this.tableName
82
+ : this.modelClass
83
+ ? builder.tableRefFor(this.modelClass)
84
+ : null;
85
+
86
+ if (table) {
87
+ return `${table}.${this.column}`;
88
+ } else {
89
+ return this.column;
90
+ }
91
+ }
92
+
93
+ castText() {
94
+ return this.castTo('text');
95
+ }
96
+
97
+ castInt() {
98
+ return this.castTo('integer');
99
+ }
100
+
101
+ castBigInt() {
102
+ return this.castTo('bigint');
103
+ }
104
+
105
+ castFloat() {
106
+ return this.castTo('float');
107
+ }
108
+
109
+ castDecimal() {
110
+ return this.castTo('decimal');
111
+ }
112
+
113
+ castReal() {
114
+ return this.castTo('real');
115
+ }
116
+
117
+ castBool() {
118
+ return this.castTo('boolean');
119
+ }
120
+
121
+ castJson() {
122
+ this._toJson = true;
123
+ return this;
124
+ }
125
+
126
+ castTo(sqlType) {
127
+ this._cast = sqlType;
128
+ return this;
129
+ }
130
+
131
+ from(table) {
132
+ this._table = table;
133
+ return this;
134
+ }
135
+
136
+ table(table) {
137
+ this._table = table;
138
+ return this;
139
+ }
140
+
141
+ model(modelClass) {
142
+ this._modelClass = modelClass;
143
+ return this;
144
+ }
145
+
146
+ as(alias) {
147
+ this._alias = alias;
148
+ return this;
149
+ }
150
+
151
+ clone() {
152
+ const clone = new this.constructor(null);
153
+
154
+ clone._expr = this._expr;
155
+ clone._parsedExpr = this._parsedExpr;
156
+ clone._column = this._column;
157
+ clone._table = this._table;
158
+ clone._cast = this._cast;
159
+ clone._toJson = this._toJson;
160
+ clone._alias = this._alias;
161
+ clone._modelClass = this._modelClass;
162
+
163
+ return clone;
164
+ }
165
+
166
+ toKnexRaw(builder) {
167
+ return builder.knex().raw(...this._createRawArgs(builder));
168
+ }
169
+
170
+ _parseExpression(expr) {
171
+ this._parsedExpr = parseFieldExpression(expr);
172
+ this._column = this._parsedExpr.column;
173
+ this._table = this._parsedExpr.table;
174
+ }
175
+
176
+ _createRawArgs(builder) {
177
+ const mysql = isMySql(builder.unsafeKnex());
178
+ let bindings = [];
179
+ let sql = this._createReferenceSql(builder, bindings);
180
+
181
+ sql = this._maybeCast(sql, mysql);
182
+ sql = this._maybeToJsonb(sql, mysql);
183
+ sql = this._maybeAlias(sql, bindings);
184
+
185
+ return [sql, bindings];
186
+ }
187
+
188
+ _createReferenceSql(builder, bindings) {
189
+ const access = this._parsedExpr.access;
190
+ bindings.push(this.fullColumn(builder));
191
+
192
+ if (access.length === 0) {
193
+ return '??';
194
+ }
195
+
196
+ if (getJsonDialect(builder.unsafeKnex(), this._expr) === 'mysql') {
197
+ bindings.push(toMySqlJsonPath(access));
198
+ // Casts need the unquoted value: `abc` instead of the JSON string `"abc"`.
199
+ return this._cast ? 'JSON_UNQUOTE(JSON_EXTRACT(??, ?))' : 'JSON_EXTRACT(??, ?)';
200
+ }
201
+
202
+ const extractor = this._cast ? '#>>' : '#>';
203
+ return `??${extractor}${toPgJsonPathLiteral(access)}`;
204
+ }
205
+
206
+ _maybeCast(sql, mysql) {
207
+ if (!this._cast) {
208
+ return sql;
209
+ } else if (!mysql) {
210
+ return `CAST(${sql} AS ${this._cast})`;
211
+ }
212
+
213
+ const isJsonField = this._parsedExpr.access.length > 0;
214
+
215
+ // MySQL can't cast to a boolean.
216
+ if (isBooleanType(this._cast)) {
217
+ return isJsonField ? `(${sql} IN ('true', '1'))` : `(${sql} <> 0)`;
218
+ }
219
+
220
+ const type = toMySqlCastType(this._cast);
221
+
222
+ // `JSON_UNQUOTE()` already returns text. Casting it would change its collation
223
+ // from the case sensitive one `->>` also uses.
224
+ if (isJsonField && type === 'CHAR') {
225
+ return sql;
226
+ }
227
+
228
+ return `CAST(${sql} AS ${type})`;
229
+ }
230
+
231
+ _maybeToJsonb(sql, mysql) {
232
+ if (!this._toJson) {
233
+ return sql;
234
+ } else if (mysql) {
235
+ return `CAST(${sql} AS JSON)`;
236
+ } else {
237
+ return `to_jsonb(${sql})`;
238
+ }
239
+ }
240
+
241
+ _maybeAlias(sql, bindings) {
242
+ if (this._shouldAlias()) {
243
+ bindings.push(this._alias);
244
+ return `${sql} as ??`;
245
+ } else {
246
+ return sql;
247
+ }
248
+ }
249
+
250
+ _shouldAlias() {
251
+ if (!this._alias) {
252
+ return false;
253
+ } else if (!this.isPlainColumnRef) {
254
+ return true;
255
+ } else {
256
+ // No need to alias if we are dealing with a simple column reference
257
+ // and the alias is the same as the column name.
258
+ return this._alias !== this._column;
259
+ }
260
+ }
261
+ }
262
+
263
+ Object.defineProperties(ReferenceBuilder.prototype, {
264
+ isObjectionReferenceBuilder: {
265
+ enumerable: false,
266
+ writable: false,
267
+ value: true,
268
+ },
269
+ });
270
+
271
+ addExpressionMethods(ReferenceBuilder.prototype);
272
+
273
+ const ref = (reference) => {
274
+ if (isObject(reference) && reference.isObjectionReferenceBuilder) {
275
+ return reference;
276
+ } else {
277
+ return new ReferenceBuilder(reference);
278
+ }
279
+ };
280
+
281
+ module.exports = {
282
+ ReferenceBuilder,
283
+ ref,
284
+ };