5

What is the best way to define custom functions in loopback api that can be used in models defined ?

For example a basic express application can have functions in helper folder on root directory, but doing same in loopback is not recommended and does not maintain loopback way.

Any help would be highly appreciated.

Dharmendra Yadav
  • 152
  • 1
  • 13

4 Answers4

2

This is very well documented.

Custom logic can be placed in

  • boot scripts
  • middlewares
  • models:
    • remote methods
    • remote hooks
    • operation hooks

application-decoupled logic can very well be put in helper folders, separate folders at root level, etc. There is nothing wrong with modularizing your program this way, it is actually a good thing.

Overdrivr
  • 6,296
  • 5
  • 44
  • 70
  • `remoteMethod`, `remoteHooks`, `operationHooks` all these methods are invoked directly with the url or on some event. – Dharmendra Yadav Oct 18 '16 at 12:42
  • 1
    but as i mentioned, I need helper functions which would be called in models as helping function, not directly with remoteHook or remoteMethod. I am very well aware of remoteMethods. – Dharmendra Yadav Oct 18 '16 at 12:44
  • 1
    Feel free to upvote if my answer helped you. Let me clarify. – Overdrivr Oct 18 '16 at 14:41
  • Seems no one understand the question. An example would be: he wants a function that given a number it adds 2, like: `function(n) { return n+2;}` to be a common function across any model for data processing. – Sebastian Aug 18 '17 at 16:04
  • Well that has nothing to do with loopback. Any folder with .js files in it will do... – Overdrivr Aug 20 '17 at 14:26
  • Don't know if it's you DharmendraYadav or you @Sebastian who downvoted, but next time please consider adding a feedback. – Overdrivr Aug 23 '17 at 05:07
  • @Overdrivr was me. Most of them don't apply to the question. And for the one that could be applied you provided just a link to a whole section and you don't explain how it can be achieved. – Sebastian Aug 23 '17 at 13:49
  • Ok well that's because the question is too general or vague. If there was a piece of code in question it would a whole lot easier to answer accurately. Thanks for the feedback anyway – Overdrivr Aug 24 '17 at 04:45
0

As mentioned by others, the Loopback documentation answers your question like this:

  • Adding logic to models - adding remote methods, remote hooks and operation hooks.
  • Defining boot scripts - writing scripts (in the /server/boot directory) that run when the application starts.
  • Defining middleware - adding custom middleware to the application .

That's a great answer if you have custom functions for a particular model, boot script, or middleware. And as Dharmendra Yadav said, mixins can be another option:

You can use mixins to perform different common actions on models such as observing changes using operation hooks and adding model attributes.

But what about code that simply doesn't fit into any of those categories?

I don't have experience with a lot of web frameworks, but one framework I have used is Grails, which is very opinionated and gives you a place for just about everything. And if your code doesn't fit into any of those categories, they give you a place for that too:

  • src/main/groovy - Supporting sources

So when I ran into this same problem in Loopback, I just created a src directory under server and that's where I put some helper classes that don't seem to fit anywhere else. And I include them as needed:

const HelperClass = require('../src/HelperClass');
HelperClass.helperFunction()...

Of course you can name the folder however you'd like: src, helpers, support, whatever. And then put it under common or server as appropriate.

bmaupin
  • 14,427
  • 5
  • 89
  • 94
-1

The best way to define functions in loopback i found is to use mixins. Here is the sample way of doing so..

https://loopback.io/doc/en/lb3/Defining-mixins.html

You can inherit these defined mixins into your models through .json of your model with {mixins:yourmixin.js}, nice and easy.

Dharmendra Yadav
  • 152
  • 1
  • 13
  • how this is the answer to the question? – Sebastian Aug 18 '17 at 16:09
  • based on my need, i needed to add some functions related to a specific model/route, and as i done a little research in loopback docs. this is what i find best match for my need and it is working nicely, it does not load with every other model, just with the one model i required it for, also it makes modular code, model itself is very clean – Dharmendra Yadav Aug 21 '17 at 08:40
  • 1
    I agree that this is the way for defining such methods, that is, when you need to tight models, routes and some specifics. But, when you want a common functionality that has nothing to do with models I think this is not the approach. Something like a function that returns true if a number is even has nothing to do with models. – Sebastian Aug 21 '17 at 20:26
  • for an independent function, normally you have to make a library and require it wherever needed which is common way among node developers, but using loopback and based on my need as i explained, this is what best suited and i don't have to require it, loopback has its way for loading the mixin – Dharmendra Yadav Aug 22 '17 at 06:17
-2

Here's some sample code to get you headed in the right direction.

mynewmodel.js

http://domain/api/mynewmodel/myfunc.js

function myfunc(data, callback) {
    // ** Put your code here **
    console.log('myfunc');
}

function remoteMethod(model) {

    model.remoteMethod(
        'myfunc',
        {
            http: { verb: 'post' },
            accepts: { arg: 'data', type: 'data' },
            returns: [
                { arg: 'returndata', type: 'data' }
            ]
        }
    )
}

UPDATE Generally your js files go in common/models

A2MetalCore
  • 1,621
  • 4
  • 25
  • 49
  • I am new.. so can you specify folder path for `mynewmodel.js` and how this `dropTable` function will call `myfunc` ? – Dharmendra Yadav Oct 24 '16 at 13:54
  • 1
    Generally your js files go in common/models in your loopback project. Also, 'dropTable' should be 'myfunc'. I made the changes in the example. – A2MetalCore Oct 24 '16 at 14:04
  • I have a whole class `myclass.js` that i want to use with this mynewmodel.js andd the functions of `myclass.js` will be used in this `myfunc` function. How can i connect it ? – Dharmendra Yadav Oct 24 '16 at 14:26
  • 1
    Create a new loopback model (i.e. myfunc.js above), add functions for each endpoint that you want to expose ('myfunc') and then call your functions from those endpoints. You'll need to create a remoteMethod function for each endpoint as well. – A2MetalCore Oct 24 '16 at 17:02
  • 1
    You didn't get it, I dont want exposed model, no extra endpoint. It's like extension to `mynewmodel.js` which is already an end point. – Dharmendra Yadav Oct 24 '16 at 18:46