Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static Relation.isOneToOne(), Relation.isChildToParent() & Relation.isParentToChild() #2359

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/model/graph/ModelGraphBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ModelGraphBuilder {

const relation = node.modelClass.getRelation(relationName);

if (relation.isOneToOne()) {
if (relation.constructor.isOneToOne()) {
this._buildNode(relation.relatedModelClass, relatedObjects, node, relation);
} else {
this._buildNodes(relation.relatedModelClass, relatedObjects, node, relation);
Expand Down
2 changes: 1 addition & 1 deletion lib/model/graph/ModelGraphNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class ModelGraphNode {
_createDataPath() {
if (this.parentEdge === null) {
return [];
} else if (this.parentEdge.relation.isOneToOne()) {
} else if (this.parentEdge.relation.constructor.isOneToOne()) {
return [...this.parentNode.dataPath, this.relationName];
} else {
return [...this.parentNode.dataPath, this.relationName, this.indexInRelation];
Expand Down
4 changes: 2 additions & 2 deletions lib/model/modelSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function setFast(model, obj) {
function setRelated(model, relation, models) {
relation = ensureRelation(model, relation);

if (relation.isOneToOne()) {
if (relation.constructor.isOneToOne()) {
if (Array.isArray(models)) {
if (models.length === 0) {
model[relation.name] = null;
Expand All @@ -101,7 +101,7 @@ function setRelated(model, relation, models) {
function appendRelated(model, relation, models) {
relation = ensureRelation(model, relation);

if (!model[relation.name] || relation.isOneToOne()) {
if (!model[relation.name] || relation.constructor.isOneToOne()) {
return model.$setRelated(relation, models);
} else {
if (Array.isArray(models)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/queryBuilder/join/JoinResultParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class JoinResultParser {
const model = tableNode.modelClass.fromDatabaseJson(row);

for (const childNode of tableNode.childNodes) {
if (childNode.relation.isOneToOne()) {
if (childNode.relation.constructor.isOneToOne()) {
model[childNode.relationProperty] = null;
} else {
model[childNode.relationProperty] = [];
Expand All @@ -82,7 +82,7 @@ class JoinResultParser {

_addToParent(tableNode, model, parentModel) {
if (tableNode.parentNode) {
if (tableNode.relation.isOneToOne()) {
if (tableNode.relation.constructor.isOneToOne()) {
parentModel[tableNode.relationProperty] = model;
} else {
parentModel[tableNode.relationProperty].push(model);
Expand Down
10 changes: 9 additions & 1 deletion lib/relations/Relation.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ class Relation {
: this.joinTableModelClass;
}

isOneToOne() {
static isOneToOne() {
return false;
}

static isChildToParent() {
return false;
}

static isParentToChild() {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/relations/RelationFindOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RelationFindOperation extends FindOperation {
}

onAfter2(_, related) {
const isOneToOne = this.relation.isOneToOne();
const isOneToOne = this.relation.constructor.isOneToOne();

if (this.assignResultToOwner && this.owner.isModels) {
const owners = this.owner.modelArray;
Expand Down Expand Up @@ -61,7 +61,7 @@ class RelationFindOperation extends FindOperation {
}

onAfter3(builder, related) {
const isOneToOne = this.relation.isOneToOne();
const isOneToOne = this.relation.constructor.isOneToOne();
const intOpt = builder.internalOptions();

if (!intOpt.keepImplicitJoinProps) {
Expand Down
6 changes: 5 additions & 1 deletion lib/relations/belongsToOne/BelongsToOneRelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const { BelongsToOneRelateOperation } = require('./BelongsToOneRelateOperation')
const { BelongsToOneUnrelateOperation } = require('./BelongsToOneUnrelateOperation');

class BelongsToOneRelation extends Relation {
isOneToOne() {
static isOneToOne() {
return true;
}

static isChildToParent() {
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/relations/hasMany/HasManyRelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const { HasManyRelateOperation } = require('./HasManyRelateOperation');
const { HasManyUnrelateOperation } = require('./HasManyUnrelateOperation');

class HasManyRelation extends Relation {
static isParentToChild() {
return true;
}

insert(_, owner) {
return new HasManyInsertOperation('insert', {
relation: this,
Expand Down
2 changes: 1 addition & 1 deletion lib/relations/hasOne/HasOneRelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { HasManyRelation } = require('../hasMany/HasManyRelation');

class HasOneRelation extends HasManyRelation {
isOneToOne() {
static isOneToOne() {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/relations/hasOneThrough/HasOneThroughRelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { ManyToManyRelation } = require('../manyToMany/ManyToManyRelation');

class HasOneThroughRelation extends ManyToManyRelation {
isOneToOne() {
static isOneToOne() {
return true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/relations/manyToMany/ManyToManyRelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const { ManyToManyDeleteOperation } = require('./delete/ManyToManyDeleteOperatio
const { ManyToManyDeleteSqliteOperation } = require('./delete/ManyToManyDeleteSqliteOperation');

class ManyToManyRelation extends Relation {
static isParentToChild() {
return true;
}

setMapping(mapping) {
const retVal = super.setMapping(mapping);

Expand Down
2 changes: 1 addition & 1 deletion lib/relations/manyToMany/find/ManyToManyFindOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ManyToManyFindOperation extends RelationFindOperation {
}

onAfter2(_, related) {
const isOneToOne = this.relation.isOneToOne();
const isOneToOne = this.relation.constructor.isOneToOne();

if (this.assignResultToOwner && this.owner.isModels) {
const owners = this.owner.modelArray;
Expand Down