Files
OpenNoodl/packages/noodl-runtime/src/nodes/std-library/data/newdbmodelpropertiesnode.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

80 lines
2.4 KiB
JavaScript

'use strict';
var Model = require('../../../model');
var DbModelCRUDBase = require('./dbmodelcrudbase');
const CloudStore = require('../../../api/cloudstore');
var NewDbModelPropertiedNodeDefinition = {
node: {
name: 'NewDbModelProperties',
docs: 'https://docs.noodl.net/nodes/data/cloud-data/create-new-record',
displayName: 'Create New Record',
usePortAsLabel: 'collectionName',
inputs: {
store: {
displayName: 'Do',
group: 'Actions',
valueChangedToTrue: function () {
this.storageInsert();
}
},
sourceObjectId: {
type: { name: 'string', allowConnectionsOnly: true },
displayName: 'Source Object Id',
group: 'General',
set: function (value) {
if (value instanceof Model) value = value.getId(); // Can be passed as model as well
this._internal.sourceObjectId = value; // Wait to fetch data
}
}
},
outputs: {
created: {
type: 'signal',
displayName: 'Success',
group: 'Events'
}
},
methods: {
storageInsert: function () {
const internal = this._internal;
if (!this.checkWarningsBeforeCloudOp()) return;
this.scheduleOnce('StorageInsert', () => {
const initValues = Object.assign(
{},
internal.sourceObjectId ? (this.nodeScope.modelScope || Model).get(internal.sourceObjectId).data : {},
internal.inputValues
);
const cloudstore = CloudStore.forScope(this.nodeScope.modelScope);
cloudstore.create({
collection: internal.collectionId,
data: initValues,
acl: this._getACL(),
success: (data) => {
// Successfully created
const m = cloudstore._fromJSON(data, internal.collectionId);
this.setModel(m);
this.sendSignalOnOutput('created');
},
error: (err) => {
this.setError(err || 'Failed to insert.');
}
});
});
}
}
}
};
DbModelCRUDBase.addBaseInfo(NewDbModelPropertiedNodeDefinition);
DbModelCRUDBase.addModelId(NewDbModelPropertiedNodeDefinition, {
includeOutputs: true
});
DbModelCRUDBase.addInputProperties(NewDbModelPropertiedNodeDefinition);
DbModelCRUDBase.addAccessControl(NewDbModelPropertiedNodeDefinition);
module.exports = NewDbModelPropertiedNodeDefinition;