Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49159b6

Browse files
lehnikoskimas
authored andcommitted
Add public Relation.joinModelClass accessor
Rename private joinModelClass(knex) function to getJoinModelClass(knex) for internal use
1 parent 224b937 commit 49159b6

File tree

11 files changed

+54
-39
lines changed

11 files changed

+54
-39
lines changed

doc/includes/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8105,6 +8105,7 @@ ownerModelClass|function|The model class that has defined the relation.
81058105
relatedModelClass|function|The model class of the related objects.
81068106
ownerProp|[`RelationProperty`](#relationproperty)|The relation property in the `ownerModelClass`.
81078107
relatedProp|[`RelationProperty`](#relationproperty)|The relation property in the `relatedModelClass`.
8108+
joinModelClass|function|The model class representing the join table. This class is automatically generated by Objection if none is provided in the `join.through.modelClass` setting of the relation mapping, see [`RelationThrough`](#relationthrough).
81088109
joinTable|string|The name of the join table (only for `ManyToMany` and `HasOneThrough` relations).
81098110
joinTableOwnerProp|[`RelationProperty`](#relationproperty)|The join table property pointing to `ownerProp` (only for `ManyToMany` and `HasOneThrough` relations).
81108111
joinTableRelatedProp|[`RelationProperty`](#relationproperty)|The join table property pointing to `relatedProp` (only for `ManyToMany` and `HasOneThrough` relations).

lib/queryBuilder/graphInserter/GraphInserter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class GraphInserter {
186186
let tableInsertion = batch[conn.relation.joinTable];
187187

188188
const ownerProp = conn.relation.ownerProp.getProps(node.model);
189-
const modelClass = conn.relation.joinModelClass(this.knex);
189+
const modelClass = conn.relation.getJoinModelClass(this.knex);
190190
let joinModel = conn.relation.createJoinModels(ownerProp, [conn.node.model])[0];
191191

192192
if (conn.refNode) {

lib/relations/Relation.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,17 @@ class Relation {
6363
}
6464

6565
get joinTable() {
66-
if (this.joinTableModelClass) {
67-
return this.joinTableModelClass.getTableName();
68-
} else {
69-
return null;
70-
}
66+
return this.joinTableModelClass ? this.joinTableModelClass.getTableName() : null;
7167
}
7268

73-
joinModelClass(knex) {
74-
if (knex && knex !== this.joinTableModelClass.knex()) {
75-
return this.joinTableModelClass.bindKnex(knex);
76-
} else {
77-
return this.joinTableModelClass;
78-
}
69+
get joinModelClass() {
70+
return this.getJoinModelClass(this.ownerModelClass.knex());
71+
}
72+
73+
getJoinModelClass(knex) {
74+
return this.joinTableModelClass && knex !== this.joinTableModelClass.knex()
75+
? this.joinTableModelClass.bindKnex(knex)
76+
: this.joinTableModelClass;
7977
}
8078

8179
relatedTableAlias(builder) {

lib/relations/manyToMany/ManyToManyRelation.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ManyToManyRelation extends Relation {
3535
ownerProp: this.ownerProp,
3636
relatedProp: this.relatedProp,
3737

38-
joinModelClass: null,
38+
joinTableModelClass: null,
3939
joinTableOwnerProp: null,
4040
joinTableRelatedProp: null,
4141
joinTableBeforeInsert: null,
@@ -52,7 +52,7 @@ class ManyToManyRelation extends Relation {
5252
ctx = finalizeJoinModelClass(ctx);
5353

5454
this.joinTableExtras = ctx.joinTableExtras;
55-
this.joinTableModelClass = ctx.joinModelClass;
55+
this.joinTableModelClass = ctx.joinTableModelClass;
5656
this.joinTableOwnerProp = ctx.joinTableOwnerProp;
5757
this.joinTableRelatedProp = ctx.joinTableRelatedProp;
5858
this.joinTableBeforeInsert = ctx.joinTableBeforeInsert;
@@ -314,11 +314,11 @@ function checkThroughObject(ctx) {
314314
}
315315

316316
function resolveJoinModelClassIfDefined(ctx) {
317-
let joinModelClass = null;
317+
let joinTableModelClass = null;
318318

319319
if (ctx.mapping.join.through.modelClass) {
320320
try {
321-
joinModelClass = resolveModel(
321+
joinTableModelClass = resolveModel(
322322
ctx.mapping.join.through.modelClass,
323323
ctx.ownerModelClass.modelPaths,
324324
'join.through.modelClass'
@@ -328,7 +328,7 @@ function resolveJoinModelClassIfDefined(ctx) {
328328
}
329329
}
330330

331-
return Object.assign(ctx, { joinModelClass });
331+
return Object.assign(ctx, { joinTableModelClass });
332332
}
333333

334334
function createJoinProperties(ctx) {
@@ -368,17 +368,17 @@ function createJoinProperties(ctx) {
368368

369369
function createRelationProperty(ctx, refString, messagePrefix) {
370370
let prop = null;
371-
let joinModelClass = ctx.joinModelClass;
371+
let joinTableModelClass = ctx.joinTableModelClass;
372372

373373
const resolveModelClass = table => {
374-
if (joinModelClass === null) {
375-
joinModelClass = inheritModel(getModel());
376-
joinModelClass.tableName = table;
377-
joinModelClass.idColumn = null;
374+
if (joinTableModelClass === null) {
375+
joinTableModelClass = inheritModel(getModel());
376+
joinTableModelClass.tableName = table;
377+
joinTableModelClass.idColumn = null;
378378
}
379379

380-
if (joinModelClass.getTableName() === table) {
381-
return joinModelClass;
380+
if (joinTableModelClass.getTableName() === table) {
381+
return joinTableModelClass;
382382
} else {
383383
return null;
384384
}
@@ -397,7 +397,7 @@ function createRelationProperty(ctx, refString, messagePrefix) {
397397
}
398398

399399
return {
400-
ctx: Object.assign(ctx, { joinModelClass }),
400+
ctx: Object.assign(ctx, { joinTableModelClass }),
401401
prop
402402
};
403403
}
@@ -421,9 +421,9 @@ function parseExtras(ctx) {
421421

422422
return {
423423
joinTableCol: val,
424-
joinTableProp: ctx.joinModelClass.columnNameToPropertyName(val),
424+
joinTableProp: ctx.joinTableModelClass.columnNameToPropertyName(val),
425425
aliasCol: key,
426-
aliasProp: ctx.joinModelClass.columnNameToPropertyName(key)
426+
aliasProp: ctx.joinTableModelClass.columnNameToPropertyName(key)
427427
};
428428
});
429429

@@ -443,10 +443,10 @@ function parseBeforeInsert(ctx) {
443443
}
444444

445445
function finalizeJoinModelClass(ctx) {
446-
if (ctx.joinModelClass.getIdColumn() === null) {
446+
if (ctx.joinTableModelClass.getIdColumn() === null) {
447447
// We cannot know if the join table has a primary key. Therefore we set some
448448
// known column as the idColumn so that inserts will work.
449-
ctx.joinModelClass.idColumn = ctx.joinTableRelatedProp.cols;
449+
ctx.joinTableModelClass.idColumn = ctx.joinTableRelatedProp.cols;
450450
}
451451

452452
return ctx;

lib/relations/manyToMany/ManyToManySqliteModifyMixin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const ManyToManySqliteModifyMixin = Operation => {
1616

1717
applyModifyFilterForJoinTable(builder) {
1818
const joinTableOwnerRefs = this.relation.joinTableOwnerProp.refs(builder);
19-
const tableRef = builder.tableRefFor(this.relation.joinModelClass(builder));
19+
const tableRef = builder.tableRefFor(this.relation.getJoinModelClass(builder));
2020
const rowIdRef = `${tableRef}.${SQLITE_BUILTIN_ROW_ID}`;
2121

2222
const ownerIds = this.relation.ownerProp.getProps(this.owner);

lib/relations/manyToMany/insert/ManyToManyInsertOperation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ManyToManyInsertOperation extends RelationInsertOperation {
1717
return after(maybePromise, inserted => {
1818
const ownerId = this.relation.ownerProp.getProps(owner);
1919
const joinModels = this.relation.createJoinModels(ownerId, inserted);
20-
const joinModelClass = this.relation.joinModelClass(builder.knex());
20+
const joinModelClass = this.relation.getJoinModelClass(builder.knex());
2121

2222
for (let i = 0, l = joinModels.length; i < l; ++i) {
2323
joinModels[i] = joinModelClass.fromJson(joinModels[i]);

lib/relations/manyToMany/relate/ManyToManyRelateOperation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ManyToManyRelateOperation extends RelateOperation {
1111
queryExecutor(builder) {
1212
const ownerId = this.relation.ownerProp.getProps(this.owner);
1313
const joinModels = this.relation.createJoinModels(ownerId, this.ids);
14-
const joinModelClass = this.relation.joinModelClass(builder.knex());
14+
const joinModelClass = this.relation.getJoinModelClass(builder.knex());
1515

1616
for (let i = 0, l = joinModels.length; i < l; ++i) {
1717
joinModels[i] = joinModelClass.fromJson(joinModels[i]);

lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const UnrelateOperation = require('../../../queryBuilder/operations/UnrelateOper
33
class ManyToManyUnrelateOperationBase extends UnrelateOperation {
44
queryExecutor(builder) {
55
const unrelateQuery = this.relation
6-
.joinModelClass(builder.knex())
6+
.getJoinModelClass(builder.knex())
77
.query()
88
.childQueryOf(builder)
99
.delete();

lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ManyToManyUpdateOperationBase extends UpdateOperation {
5353
onAfter1(builder, result) {
5454
if (this.hasExtraProps) {
5555
const joinTableUpdateQuery = this.relation
56-
.joinModelClass(builder.knex())
56+
.getJoinModelClass(builder.knex())
5757
.query()
5858
.childQueryOf(builder)
5959
.patch(this.joinTablePatch);

tests/unit/relations/ManyToManyRelation.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe('ManyToManyRelation', () => {
140140
expect(relation.joinTable).to.equal('JoinModel');
141141
expect(relation.joinTableOwnerProp.cols).to.eql(['ownerId']);
142142
expect(relation.joinTableRelatedProp.props).to.eql(['relatedId']);
143-
expect(classUtils.isSubclassOf(relation.joinModelClass(mockKnex), JoinModel)).to.equal(true);
143+
expect(classUtils.isSubclassOf(relation.joinModelClass, JoinModel)).to.equal(true);
144144
});
145145

146146
it('should accept an absolute file path to a join model in join.through object', () => {
@@ -163,9 +163,9 @@ describe('ManyToManyRelation', () => {
163163
expect(relation.joinTable).to.equal('JoinModel');
164164
expect(relation.joinTableOwnerProp.cols).to.eql(['ownerId']);
165165
expect(relation.joinTableRelatedProp.cols).to.eql(['relatedId']);
166-
expect(
167-
classUtils.isSubclassOf(relation.joinModelClass(mockKnex), require('./files/JoinModel'))
168-
).to.equal(true);
166+
expect(classUtils.isSubclassOf(relation.joinModelClass, require('./files/JoinModel'))).to.equal(
167+
true
168+
);
169169
});
170170

171171
it('should accept a composite keys in join.through object (1)', () => {

0 commit comments

Comments
 (0)