Comments (7)
if (typeof customerId === 'undefined') {
customerId = getNextCustomerIdOrWhateverIDK();
}
Small detail is that getNextCustomerIdOrWhateverIDK()
is likely going to return a promise, so we'll need a way to bridge that gap unfortunately 😒 . But I like where this is going 😀
from eriksen.
That's a flippin' great idea. Love it. No one ruin how much i love this idea by bringing any contrary "facts" into this.
from eriksen.
lol sounds good to me, my nitpick concern is attaching those hooks directly to the secondaryModel... maybe since you can only override the secondary model methods, we can just have something like
marshal.override('createAccount', ({ args, result, secondaryModel }) => {
// instead of calling secondaryModel.createAccount, we can call insertAccount
// which simply inserts the row. or we can call createAccount and check if the second
// argument is defined.
return secondaryModel.insertAccount(args[0], result);
})
and inside the proxify method we can call the hook method with
methodHook({ args, result, secondaryModel: options.secondaryModel })
or something?
from eriksen.
I know this is not super clear, just getting the idea down somewhere so we can talk about it later :)
from eriksen.
Yeah exactly, that was the "or whatever idk" part of that function lol
from eriksen.
What if we allow you to use a hook to override the second model's call to createAccount
, where you can do something like this in the supermodel:
const marshal = new Eriksen('accounts');
marshal.addModel('cassandra', require('lib/models/cassandra/accounts'));
marshal.addModel('aws', require('lib/models/aws/accounts'));
marshal.configure({ primary: 'cassandra', secondary: 'aws' });
const marshallSettings = {
primary: 'cassandra',
secondary: 'aws'
};
marshal.configure(marshallSettings);
// adding the hooks.
let secondaryModel = marshal.models.get(marshallSettings.secondary);
secondaryModel._hooks = {
createAccount: function(callData) {
var args = callData.args
, result = callData.result;
// instead of calling secondaryModel.createAccount, we can call insertAccount
// which simply inserts the row. or we can call createAccount and check if the second
// argument is defined.
return secondaryModel.insertAccount(args[0], result);
}
};
module.exports = marshal.proxy;
Then in the proxy, we can check to see if there are any overrides defined for the secondaryModel's function and call that instead of the usual function.
function proxify(method, options) {
return function() {
const args = (arguments.length === 1) ? [arguments[0]] : Array.apply(null, arguments);
return callMethod(options.primaryModel, method, args).then((result) => {
let methodHook
, methodCall;
if (!options.secondary || !_.isFunction(options.secondaryModel[method])) {
return result;
}
// check to see if there's a hook defined
methodHook = _.get(options.secondaryModel, `_hooks.${method}`);
if (methodHook) {
// we can pass in the args and the result of the first model's call to the hook.
methodCall = methodHook({ args: args, result: result });
} else {
methodCall = callMethod(options.secondaryModel, method, args);
}
// don't return here and swallow errors so that secondary call is non-blocking
methodCall.catch((err) => {
options.logger.error(`[Eriksen] Captured error on secondary model: ${options.secondary}#${method}`, err);
});
return result;
});
};
}
What do you think?
from eriksen.
@jasonrhodes yesss, i like attaching the overrides to the marshaller and letting it figure it out. good stuff.
from eriksen.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eriksen.