Skip to content
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

autoload models into the container #145

Closed
josephmancuso opened this issue May 25, 2018 · 1 comment
Closed

autoload models into the container #145

josephmancuso opened this issue May 25, 2018 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@josephmancuso
Copy link
Member

josephmancuso commented May 25, 2018

we can set a location to look for models in the configuration setting:

MODELS_LOCATION = [
    '/app',
    '/some/other/location/models'
]

and we can look in that directory, get all the modules and classes, check they are an instance on model and load them into the container with their .__name__

@josephmancuso josephmancuso self-assigned this May 25, 2018
@josephmancuso josephmancuso added this to the Masonite 2.0 milestone May 26, 2018
@josephmancuso
Copy link
Member Author

josephmancuso commented May 26, 2018

so what I actually built here was a whole autoloader.

We can do something like this in our application/config.py:

AUTOLOAD = [
    'app',
    'app/models'
]

and when we first start the server, it will look through both directories, find all the classes in the directory and autoload them into the container and bind the name of the class and the class into the container. So with a structure like:

app/
    http/
    providers/
    models/
        Blog.py # contains 2 classes: Blog and Time
        Author.py
    Post.py
    User.py

Running this autoloader:

AUTOLOAD = [
    'app',
    'app/models'
]

will be the equivalent of this:

from app.models import Blog, Author
from app import Post, User

self.app.bind('Blog', Blog.Blog)
self.app.bind('Time', Blog.Time)
self.app.bind('Author', Author)
self.app.bind('Post', Post)
self.app.bind('User', User)

but it will be done automatically. We'll also be able to load all classes in a single module:

AUTOLOAD = [
    'app/User'
]

which will load all the classes in app/User.py. There should only be 1 class in there by convention but some developers may wish to do multiple classes just because of how Python developers are.

@josephmancuso josephmancuso added the enhancement New feature or request label May 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant