'use strict';module.exports=function(MyModel){varapp=require('../../server/server');// Disable method updateMyModel.disableRemoteMethodByName("replaceById",true);MyModel.validateAsync('cementerios',cementerios_validacion,{});// { message: 'No se ha encontrado el patio' }asyncfunctioncementerios_validacion(err,done){//// Use your custom variable for explicit validation is (validation_manual) to check if is called explicit // if(this.validation_manual) ...../* YOUR CODE... */}MyModel.modificar=function(req,res,options,id,data,cb){// Use isvalidconstmymodel_data=newapp.models.MyModel(data);mymodel_data.validation_manual=true;myModel.isValid(function(valid){if(!valid){mymodel_data.errors// hash of errors {attr: [errmessage, errmessage, ...], attr: ...}}})/* YOUR CODE... */};// remoteMethodMyModel.remoteMethod('modificar',{accepts: [{arg: 'req',type: 'object','http': {source: 'req'}},{arg: 'res',type: 'object','http': {source: 'res'}},{arg: "options",type: "object",http: "optionsFromRequest"},{arg: 'id',type: 'number',required: true},{arg: 'data',type: "object",'http': {source: 'body'}}],http: {path: '/modificar/:id',verb: 'put'},returns: {arg: 'cementerio',type: 'object',root: true}});};Example of a controller file: Link for gist
'use strict';module.exports=function(Examplemodel){varapp=require('../../server/server');varseguridad=require('../seguridad/seguridad');varutilidades=require('../utilidades/utilidades');// Override the method create for model Examplemodel.once('attached',function(){var_create=Examplemodel.create;Examplemodel.create=function(filter,options,cb){_create.apply(this).then((data=>{// DO SOMETHINGcb(null,data)})).catch((err=>{// DO SOMETHINGcb(err)}))}})};// Use the method this.isNewRecord() to check thatModel.validate('end_date',fecha_fin,{message: 'The end_date can not be less than the start_date'});functionend_date(err){if(this.isNewRecord()&&this.end_date&&this.end_date<this.start_date){err();}};One option
// usage
const result = await executeAsync(dataSource, sqlStatement, params, options);
// impletation of function to wrap it
function executeAsync(dataSource, sqlStatement, params, options) {
return new Promise((resolve, reject) => {
dataSource.connector.execute(sqlStatement, params, options, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
Two option documentation promisify
const { promisify } = require('util')
await promisify(mysqlDs.connector.execute).bind(mysqlDs.connector)(sql_stmt, params)
app.models.<YOUR MODEL>().toObject()
Dont'use cb() function. Just return the value directly.
If you need to send an error don't return in, throw it.
Model.myRemoteMethod=asyncfunction(param1,param2,...){try{letresult=// ....returnresult}catch(err){throwerr}}