Git Product home page Git Product logo

Comments (7)

awinder avatar awinder commented on May 30, 2024 1
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.

awinder avatar awinder commented on May 30, 2024 1

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.

jasonrhodes avatar jasonrhodes commented on May 30, 2024 1

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.

jasonrhodes avatar jasonrhodes commented on May 30, 2024

I know this is not super clear, just getting the idea down somewhere so we can talk about it later :)

from eriksen.

jasonrhodes avatar jasonrhodes commented on May 30, 2024

Yeah exactly, that was the "or whatever idk" part of that function lol

from eriksen.

nancyhabecker avatar nancyhabecker commented on May 30, 2024

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.

nancyhabecker avatar nancyhabecker commented on May 30, 2024

@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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.