Servercode: how to define a function and use it in multiple Services

We have two types of module in Servercode Javascript:
the service modules, which are added using this line:

Backendless.ServerCode.addService(className);

and the Model:

module.exports = Backendless.ServerCode.addType(className);

I’m willing to write a function and use it accross ServerCode services, this function will interact with the database such as

Backendless.Data.of(“table_name”).findById(…)

is this possible? Because I couldn’t figure it out.

Thanks in advance,
Hassan

Hello @Hassan_Kanso

You can create a separate js file and require it in other js files, for ex:

//foo.js

exports.foobar = function (){ 
 return Backendless.Data.of(“table_name”).findById(…)
 }
//service.js
const { foobar } = require('./foo')

class ClassName {
  async myMethod(){
     const r = await foobar() 

     ... 
   }
}

Regards, Vlad

just to add some info, the solution provided by @vladimir-upirov will not work if you define the function inside of persistenceItem’s file, in models folder. (atleast in my experience)

Could you please attach the code of these two files?
Do not share the entire content just show how you export the function and import