Skip to content

Instantly share code, notes, and snippets.

@soltrinox
Last active February 21, 2024 11:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save soltrinox/660979c359547aaefcd9 to your computer and use it in GitHub Desktop.
Save soltrinox/660979c359547aaefcd9 to your computer and use it in GitHub Desktop.
Loopback : Model access non-related model via App object
'use strict';
var app = require('../../server/server');
module.exports = function(Restaurant, app) {
// Restaurant.validatesUniquenessOf('UUID', {message: 'UUID EXISTS'});
Restaurant.observe('before save', function setDefaultUsername(ctx, next) {
if (ctx.instance) {
if(!ctx.instance.created){
ctx.instance.created = Date.now();
}
ctx.instance.updated = Date.now();
}
next();
});
Restaurant.observe('loaded', function logQuery(ctx, next) {
console.log('RETURNING %j', ctx.instance);
next();
});
Restaurant.getChildren = function(id,cb) {
var filter = { include : [ 'DETAILS'] };
// get the restaurant
Restaurant.findById(id, filter, function(err, restaurant) {
if(err) {
console.log(err);
} else {
// get the Generics
var Rapp = Restaurant.app;
var GenericModel = Rapp.models.Generic;
GenericModel.find({ where: { UUID : id } }, function(err, generics) {
if(err) {
console.log(err);
} else {
restaurant.children = generics;
cb(null, restaurant);
}
});
}
});
};
Restaurant.remoteMethod('getChildren', {
accepts: [{arg: 'id', type: 'string'}],
returns: {arg: 'FULLOBJECT', type: 'object'},
http: {path:'/withChildren/:id', verb: 'get'}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment