Last active
February 21, 2024 11:37
-
-
Save soltrinox/660979c359547aaefcd9 to your computer and use it in GitHub Desktop.
Loopback : Model access non-related model via App object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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