-
Notifications
You must be signed in to change notification settings - Fork 16
Aggregate calls using bluebird #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,44 @@ | ||
| 'use strict'; | ||
|
|
||
| const app = require('../server'); | ||
| const promise = require('bluebird'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are usually using const Promise = require('bluebird'); |
||
|
|
||
| module.exports = function(Account) { | ||
| let accountService, customerService, transactionService; | ||
| let customerService, accountService, transactionService; | ||
|
|
||
| app.on('started', function() { | ||
| Account.getAccountSummary = function(accountNumber) { | ||
| let accountSummary = {}; | ||
| return findAccountSummary({id: accountNumber, accountNumber: accountNumber}) | ||
| .then(function(data) { | ||
| accountSummary = data.obj; | ||
| console.log("summary", accountSummary); | ||
| }) | ||
| .then(function() { | ||
| return findAccount({id: accountNumber, accountNumber: accountNumber}) | ||
| }) | ||
| .then(function(data) { | ||
| accountSummary.account = data.obj; | ||
| console.log("account" , data.obj); | ||
| }) | ||
| .then(function() { | ||
| return findCustomer({id: accountSummary.account.customerNumber, customerNumber: accountSummary.account.customerNumber}) | ||
| }) | ||
| .then(function(data) { | ||
| console.log(data); | ||
| accountSummary.customer = data.obj; | ||
| return accountSummary; | ||
| }); | ||
| } | ||
|
|
||
| Account.listAllAccounts = function(customerNumber, cb) { | ||
| let findAccount = accountService.getFunction('Account', 'find'); | ||
| findAccount({}, function(err, account) { | ||
| if (err) return cb(err); | ||
| cb(null, account.obj); | ||
| }); | ||
| }; | ||
|
|
||
| app.on('started', function(){ | ||
| customerService = app.dataSources.Customer; | ||
| accountService = app.dataSources.Account; | ||
| transactionService = app.dataSources.Transaction; | ||
|
|
@@ -14,36 +47,48 @@ module.exports = function(Account) { | |
| transactionService.getFunction = getFunction; | ||
| }); | ||
|
|
||
| function getFunction(model, method) { | ||
| let functionName = model + '_' + method; | ||
| return this.createModel(model, {})[functionName]; | ||
| function findTransaction(input) { | ||
| let find = transactionService.getFunction('Transaction', 'findById'); | ||
| return new Promise(function (resolve, reject) { | ||
| find(input, function(err, data) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is sort of an anti-pattern in Bluebird, use |
||
| if (err) return reject(new Error(err)); | ||
| resolve(data); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| Account.getAccountSummary = function(accountNumber, cb) { | ||
| let findCustomer = customerService.getFunction('Customer', 'findById'); | ||
| let findAccount = accountService.getFunction('Account', 'findById'); | ||
| let findAccountSummary = accountService.getFunction('AccountSummary', 'findById'); | ||
| let findTransaction = transactionService.getFunction('Transaction', 'findById'); | ||
| findAccountSummary({id: accountNumber, accountNumber: accountNumber}, function(err, accountSummary) { | ||
| if (err) return cb(err); | ||
| accountSummary = accountSummary.obj; | ||
| findAccount({id: accountNumber, accountNumber: accountNumber}, function(err, account) { | ||
| if (err) return cb(err); | ||
| findCustomer({id: account.obj.customerNumber, customerNumber: account.obj.customerNumber}, function(err, customer) { | ||
| if (err) return cb(err); | ||
| accountSummary.customer = customer.obj; | ||
| accountSummary.account = account.obj; | ||
| cb(null, accountSummary); | ||
| }); | ||
| function findAccountSummary(input) { | ||
| let find = accountService.getFunction('AccountSummary', 'findById'); | ||
| return new Promise(function (resolve, reject) { | ||
| find(input, function(err, data) { | ||
| if (err) return reject(new Error(err)); | ||
| resolve(data); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| Account.listAllAccounts = function(customerNumber, cb) { | ||
| let findAccount = accountService.getFunction('Account', 'find'); | ||
| findAccount({}, function(err, account) { | ||
| if (err) return cb(err); | ||
| cb(null, account.obj); | ||
| function findAccount(input) { | ||
| let find = accountService.getFunction('Account', 'findById'); | ||
| return new Promise(function (resolve, reject) { | ||
| find(input, function(err, data) { | ||
| if (err) return reject(new Error(err)); | ||
| resolve(data); | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| function findCustomer(input) { | ||
| let find = customerService.getFunction('Customer', 'findById'); | ||
| return new Promise(function (resolve, reject) { | ||
| find(input, function(err, data) { | ||
| if (err) return reject(new Error(err)); | ||
| resolve(data); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function getFunction(model, method) { | ||
| let functionName = model + '_' + method; | ||
| return this.createModel(model, {})[functionName]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, this is the single place where you are looking up callback-based functions. In which case this is the place where you can promisify the function before you return it back. return Promise.promisify(this.createModel(model, {})[functionName]);However, this will not work for functions that expect function getFunction(modelName, methodName) {
const functionName = modelName + '_' + methodName;
const model = this.createModel(modelName, {});
const method = Promise.promisify(model[functionName]);
return method.bind(model);
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, this should not be needed once strongloop/loopback-connector-swagger#25 is landed and released. |
||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not depend on
latest. Always use^x.y.z,"bluebird": "^3.5.0"in this particular case.If you depend on
latestand a new major version of your dependency is released, your application is very likely to break, because a new major version typically has backwards-incompatible changes.