Files
OpenNoodl/packages/noodl-runtime/src/nodes/std-library/data/dbmodelnode-addrelation.js
Michael Cartner b9c60b07dc Initial commit
Co-Authored-By: Eric Tuvesson <eric.tuvesson@gmail.com>
Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com>
Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com>
Co-Authored-By: Anders Larsson <64838990+anders-topp@users.noreply.github.com>
Co-Authored-By: Johan  <4934465+joolsus@users.noreply.github.com>
Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com>
Co-Authored-By: victoratndl <99176179+victoratndl@users.noreply.github.com>
2024-01-26 11:52:55 +01:00

96 lines
3.2 KiB
JavaScript

'use strict';
var Model = require('../../../model');
var DbModelCRUDBase = require('./dbmodelcrudbase');
const CloudStore = require('../../../api/cloudstore');
var AddDbModelRelationNodeDefinition = {
node: {
name: 'AddDbModelRelation',
docs: 'https://docs.noodl.net/nodes/data/cloud-data/add-record-relation',
displayNodeName: 'Add Record Relation',
usePortAsLabel: 'collectionName',
// shortDesc: "Stores any amount of properties and can be used standalone or together with Collections and For Each nodes.",
inputs: {
store: {
displayName: 'Do',
group: 'Actions',
valueChangedToTrue: function () {
this.scheduleAddRelation();
}
}
},
outputs: {
relationAdded: {
type: 'signal',
displayName: 'Success',
group: 'Events'
}
},
methods: {
validateInputs: function () {
if (!this.context.editorConnection) return;
const _warning = (message) => {
this.context.editorConnection.sendWarning(this.nodeScope.componentOwner.name, this.id, 'add-relation', {
message
});
};
if (this._internal.collectionId === undefined) {
_warning('No class specified');
} else if (this._internal.relationProperty === undefined) {
_warning('No relation property specified');
} else if (this._internal.targetModelId === undefined) {
_warning('No target record Id (the record to add a relation to) specified');
} else if (this._internal.model === undefined) {
_warning('No record Id specified (the record that should get the relation)');
} else {
this.context.editorConnection.clearWarning(this.nodeScope.componentOwner.name, this.id, 'add-relation');
}
},
scheduleAddRelation: function (key) {
const _this = this;
const internal = this._internal;
this.scheduleOnce('StorageAddRelation', function () {
_this.validateInputs();
if (!internal.model) return;
var model = internal.model;
var targetModelId = internal.targetModelId;
if (targetModelId === undefined) return;
CloudStore.forScope(_this.nodeScope.modelScope).addRelation({
collection: internal.collectionId,
objectId: model.getId(),
key: internal.relationProperty,
targetObjectId: targetModelId,
targetClass: (_this.nodeScope.modelScope || Model).get(targetModelId)._class,
success: function (response) {
for (var _key in response) {
model.set(_key, response[_key]);
}
// Successfully added relation
_this.sendSignalOnOutput('relationAdded');
},
error: function (err) {
_this.setError(err || 'Failed to add relation.');
}
});
});
}
}
}
};
DbModelCRUDBase.addBaseInfo(AddDbModelRelationNodeDefinition, {
includeRelations: true
});
DbModelCRUDBase.addModelId(AddDbModelRelationNodeDefinition);
DbModelCRUDBase.addRelationProperty(AddDbModelRelationNodeDefinition);
module.exports = AddDbModelRelationNodeDefinition;