Skip to content
This repository was archived by the owner on Apr 18, 2020. It is now read-only.

Make the project structure more generic - #7

Merged
bajtos merged 7 commits into
masterfrom
feature/generic-setup
May 28, 2014
Merged

Make the project structure more generic#7
bajtos merged 7 commits into
masterfrom
feature/generic-setup

Conversation

@bajtos

@bajtos bajtos commented May 27, 2014

Copy link
Copy Markdown
Member

Refactor project structure to make the boiler plate code more generic. When done, most of the scaffolding code should be moved to loopback-boot and/or loopback-workspace templates.

Task for the next iteration (another pull request):

  • Move setup of bi-direction replication to loopback core, come up with way how to enable it via models.json. Same for attaching Todo.getChangeModel() to the app.
  • Decide whether app.api.js should load the configuration from app.json or keep using the current approach (see TODO in app.api.js).
  • Find out how to specify "default": Date.now via models.json

/to @ritch please review

Comment thread html5/app.html5.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ./build?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixing build artifacts and source hurts my brain...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to keep the generated sources out of source control.

I could use node_modules, but that's is a bad idea as explained in the e-mail by @sam-github.

What options are remaining:

  • Create a new directory where to put the generated source files, e.g. html5/generated.
  • Add more structure to build, e.g. build/bundle for browserify output, build/config for generated sources.
  • Keep the generated files in html5/ and use a naming convention that allows a simple .gitignore rule, e.g. html5/browser.datasources.js and html5/browser.models.js can be ignored by rule html5/browser.*.js.

Thoughts?

Miroslav Bajtoš added 6 commits May 28, 2014 16:08
Move the configuration of datasources to datasources.json,
add environment-specific configuration files.

Modify api/app.ap.js to setup datasources via loopback-boot.
Modify configure.js script to merge all datasource config files
and create a single browserify-enabled file.

Modify html5/app.html5.js to dynamically register datasources
provided in the config file.

Modify configuration of the `watch` task to include json files in
the change detection list.
Move the definition-releated code from models/*.js to models.json.

Modify api/app.api.js to load modes via `app.boot`.

Modify html5/configure.js to generate a script that will register
all models and statically require() all models/*.js files.
Define client-only models in `html5/client.models.json`.

Rename client datasources:
    remote -> db (match shared models)
    memory -> local
Move the loop defining all data-sources from hand-written
html5/app.html5.js to the code generated by html5/configure.js.

This new way is consistent with how models are loaded.
@bajtos

bajtos commented May 28, 2014

Copy link
Copy Markdown
Member Author

Rebased on top of the current master and finished the implementation of model loading.

The pull request is mostly done. The code in html5/configure is a mess, I am expecting the cleanup should take place when we move it to gulp-loopback.

The only outstanding issue I am aware of is the place for generated models.js and datasources.js.

@bajtos bajtos changed the title Make the project structure more generic [WIP] Make the project structure more generic May 28, 2014
@bajtos

bajtos commented May 28, 2014

Copy link
Copy Markdown
Member Author

The other remaining item: get rid of ::ref:: magic and use strongloop/loopback#287 instead.

Upgrade loopback to 2.0.0-beta3.

Simplify html5/configure.js and remove the hack to convert `"memory"`
to `loopback.Memory`, since the built-in connectors are registered
in `app.connectors` now.
@bajtos

bajtos commented May 28, 2014

Copy link
Copy Markdown
Member Author

The only outstanding issue I am aware of is the place for generated models.js and datasources.js.

We have discussed this with @ritch and agreed to land this pull request without that change.

The plan is to investigate whether we can make dynamic requires work with browserify by explicitly adding files like ../models/todo.js to the browserify bunde.

The other remaining item: get rid of ::ref:: magic and use strongloop/loopback#287 instead.

Done.

@ritch

ritch commented May 28, 2014

Copy link
Copy Markdown
Member

LGTM

bajtos added a commit that referenced this pull request May 28, 2014
Make the project structure more generic
@bajtos
bajtos merged commit 71e65db into master May 28, 2014
@bajtos
bajtos deleted the feature/generic-setup branch May 28, 2014 18:37
Comment thread models/todo.js

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this before... We've gone to quite some lengths to make this pattern not required.

This makes models useful only if an app is available... I'm not entirely against this, but would like to justify the added constraint.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this before... We've gone to quite some lengths to make this pattern not required.

I know. You don't have to use this pattern as long as you know how to get hold of your app object in different means.

This makes models useful only if an app is available... I'm not entirely against this, but would like to justify the added constraint.

We want to get rid of global static model/datasource registries at some point in the future and provide containers (scopes) in which models are registered. An example of such container is a loopback app object. We have the container semantics partially implemented via app.models and app.datasources.

That means you can't define a new model by calling the global static Model.extend anymore, you need a reference to the container.

So far we recommended require('../app.js'). That works in the old server-side-only project layout, but does not work for isomorphic code - the app object is created in multiple places depending on whether you are running in a browser or on the server.

The pattern module.exports = function(dependencies...) seems like the cleanest solution to me.


On the second thought, since loopback is still maintaining a static global registry, the code can get the model from that registry without requiring the app object:

var Todo = loopback.getModel('Todo');

Such approach won't work for boot/* scripts though. We could implement an app singleton accessible via loopback.getApp() to "fix that".

Anyways. IMO the dependency injection is the direction we want to go in the future, thus I'd rather teach people this new approach.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes models useful only if an app is available... I'm not entirely against this, but would like to justify the added constraint.

There is another angle to this: how to group a set of loopback Models into a package that can be shared between projects.

In the current solution, models/ is not a standalone package, it does not export anything. It expects that app.boot() will pick up model.json and models/*.js and do whatever is necessary to setup the models in the app.

To create a package of Modules, one has to use different APIs, i.e. manually call Model.extend or DataModel.extend as was done in the previous revision.

We could come up with a project layout that will keep all models-related files in a single place (a single npm package) and then implement a mechanism for declaratively adding all/selected models from such package to a loopback app. However, I don't think we have enough time for that now, IMO this is closer to "nice to have" than to "must have".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ritch I am suggesting to move further discussions to a new GH issue to make sure it does not get lost.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... this is really important to solve for 2.0.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#13

bajtos pushed a commit that referenced this pull request May 29, 2014
Remove the dependency on packages, provide sensible static configuration
that works for all packages and non-packages alike.

Ignore `**/test/**` files.

Remove no longer used method `findSrc()`.

Note: as of #7, there is no `modules/package.json`, thus the previous
nodemon configuration was not picking changes made in `modules/*.js`.
bajtos pushed a commit that referenced this pull request May 29, 2014
Modify Todo definition and set `properties.created.default` to
`Date.now`.

This commit is a follow-up for #7, which moved Todo definition
to models.json, but did not configured the default value for `created`.
bajtos pushed a commit that referenced this pull request May 30, 2014
Remove the dependency on packages, provide sensible static configuration
that works for all packages and non-packages alike.

Ignore `**/test/**` files.

Remove no longer used method `findSrc()`.

Note: as of #7, there is no `modules/package.json`, thus the previous
nodemon configuration was not picking changes made in `modules/*.js`.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants