loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Spike: How to write a LB3-like LB4 application in JavaScript

Open dhmlau opened this issue 8 years ago • 28 comments

Description / Steps to reproduce / Feature proposal

In the spike,

  1. Find out the following, focus on what can be done right now without changing the code too much:
  • how to create and boot application class in javascript
  • how to register route
  • how to customize the sequence without dependency injection
  • data access should not depend on LB Repository. Options: 1/ Juggler + connectors. 2/ use db driver directly (e.g. mysql/mongodb modules)
  1. Identify pain points

Connect to #560

Out of Scope

  • Dependency injection
  • use of decorators
  • use of Repository and service proxy

See Reporting Issues for more tips on writing good issues

dhmlau avatar Nov 05 '18 14:11 dhmlau

Good questions, @dhmlau. The 6 spikes above are addressing different technical aspects (road blocks) that are preventing LB4 users from writing their applications in JavaScript. They don't necessarily lead to an MVP solution - some of them may be beyond the scope of MVP, we may discover more missing pieces when we start putting together a cohesive MVP story.

I see the following challenge this research: we need to work both at high-level design to find a solution that's easy to use and attractive to JS developers, and at the same time keep in mind low-level technical limitations imposed by JS.

At high-level, we already have several tools and multiple different ways for designing the applications. Some of them may work better in JS, some will not work at all.

Few examples of different high-level approaches we haven't looked at yet:

To define the REST API, one can use design-first approach where the API is described in openapi.json (or openapi.yaml) file, provided to the application via app.spec, and then the endpoints described in the spec are linked to individual controller methods/route handlers. LB4 defines extension fields for that: x-handler, x-controller-name + x-operation-name. The downside of this approach is that parameter descriptions are far away from the function accepting them (spec file vs. source code file).

Dependency management can be implemented via Service Locator pattern. I am personally not fan of this approach, it makes it too easy to create highly-coupled code that's difficult to test in isolation, but if it makes JavaScript code easier to write & read compared to DI alternatives, then it's probably a trade-off we should accept.

What I am trying to say here is that we haven't explorer all options already offered by the current LB4 code base, not to mention new options that will require only minimal improvements.

From MVP perspective, I am proposing to use our examples/todo application (CRUD only, no Geocoder service integration) as the target MVP application we want to build using pure JavaScript.

If it makes things simpler, I am fine with leaving out customization of the Sequence. AFAICT, the Todo example app is working well will the built-in default sequence.

Now the question I am not able to answer without further research: considering developer experience but also implementation effort required in our runtime, what is a better approach for building the Todo example app: use handler-based routes via app.route(verb, path, spec, handler) or controller classes? While this seems like an easy question, there is more to it.

Take GET /todos?filter=... for an example.

  • We need to describe the filter parameter and the return value (response body). We don't want to write the schema by hand, it should be generated from our Todo model. How are we going to wire that up?
  • The implementation needs to call todoRepository.find(filter). How do we obtain the repository instance? The repository instance needs a DataSource instance, how is that wired up?
  • Are we interested in unit-level tests in JS, similarly to what we have in TS? If yes, then how can we mock the repository instance used by the controller or the handler function?

A comment on a side: I have an idea for an API allowing route handlers to receive dependencies via DI. It's not a solution for DI in general, but may work well for this particular case.

app.route(
  'get',
  '/todos',
  ['repositories.TodoRepository'], // a list of dependencies
  spec, // describe filter arg & return values
  async function findTodos(TodoRepository, filter) {
    // ^^^ dependencies first, spec parameters second
    return TodoRepository.find(filter);
  });

Because the number of arguments is growing out of hand a bit, a new object-style may work better:

app.route(
  verb: 'get',
  path: '/todos',
  inject: ['repositories.TodoRepository'], // a list of dependencies
  spec, // describe filter arg & return values
  handler: async function findTodos(TodoRepository, filter) {
    // ^^^ dependencies first, spec parameters second
    return TodoRepository.find(filter);
  },
});

bajtos avatar Feb 15 '19 08:02 bajtos

here is the proposal for the followup tasks. Anything needs to be clarified or you'd like to be added, do let me know.

@hacksparrow please copy this proposal into a markdown file and commit it into a git. I find it difficult to discuss changes in content that's posted as a comment only.

bajtos avatar Feb 15 '19 09:02 bajtos

@bajtos created this PR https://github.com/strongloop/loopback-next/pull/2404 with the file.

hacksparrow avatar Feb 15 '19 14:02 hacksparrow

Links to the follow up tasks for this spike:

  1. https://github.com/strongloop/loopback-next/issues/2473
  2. https://github.com/strongloop/loopback-next/issues/2474
  3. https://github.com/strongloop/loopback-next/issues/2475
  4. https://github.com/strongloop/loopback-next/issues/2476
  5. https://github.com/strongloop/loopback-next/issues/2477
  6. https://github.com/strongloop/loopback-next/issues/2478

@bajtos can we close this spike now?

hacksparrow avatar Feb 26 '19 10:02 hacksparrow

Please create a spike or a story to create a JavaScript version of our Todo example application (minus GeoCoder API), as described in my earlier comment. I think we also need a follow-up task to modify our lb4 app generator to allow users to scaffold JavaScript applications.

I also posted few comments in the follow-up issues you created, please consider to include the information from those comment in issue descriptions/acceptance criteria.

  • https://github.com/strongloop/loopback-next/issues/2474#issuecomment-467456485
  • https://github.com/strongloop/loopback-next/issues/2476#issuecomment-467460777
  • https://github.com/strongloop/loopback-next/issues/2475#issuecomment-467459894
  • https://github.com/strongloop/loopback-next/issues/2477#issuecomment-467463138
  • https://github.com/strongloop/loopback-next/issues/2478#issuecomment-467464431
  • https://github.com/strongloop/loopback-next/issues/2473#issuecomment-467468041

Other than that, I am fine with closing this issue as done.

bajtos avatar Feb 26 '19 14:02 bajtos

Added spike for Todo example application- https://github.com/strongloop/loopback-next/issues/2501.

hacksparrow avatar Feb 28 '19 06:02 hacksparrow

Added issue for supporting the JS app scaffolding in lb4 app - https://github.com/strongloop/loopback-next/issues/2503.

hacksparrow avatar Feb 28 '19 08:02 hacksparrow

@bajtos good to close this now?

hacksparrow avatar Feb 28 '19 13:02 hacksparrow