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

Support database URLs #307

Closed
josephmancuso opened this issue Jan 11, 2021 · 6 comments
Closed

Support database URLs #307

josephmancuso opened this issue Jan 11, 2021 · 6 comments
Labels
feature request A feature that does not yet exist but will be a good addition to the library help wanted Extra attention is needed medium These issues are geared for people who have contributed to the project before

Comments

@josephmancuso
Copy link
Member

We should be able to somehow support database URL's

postgres://user:secret@localhost:5432/mydatabasename

We should be able to discuss the best way to approach this. Maybe we either have a function that returns the information in a correct format like:

# config/database.py
from masoniteorm.helpers import database_url

DATABASES = {
    'mysql': database_url(env("DATABASE_URL"))
}

Maybe this returns a dictionary.

Another option is maybe just adding a URL attribute, and if it exists, use that instead of all the other config options:

# config/database.py
from masoniteorm.helpers import database_url

DATABASES = {
    'mysql': {
        "user": "..",
        "password": "..",
        "url": "mysql://user:secret@localhost:5432/mydatabasename"
    }
}

If url is set, use that, if not then don't use it?

Open for discussion. Can see how other libraries do this

@josephmancuso josephmancuso added help wanted Extra attention is needed medium These issues are geared for people who have contributed to the project before feature request A feature that does not yet exist but will be a good addition to the library labels Jan 11, 2021
@girardinsamuel
Copy link
Contributor

girardinsamuel commented Jan 14, 2021

For Django there is django-environ package which provide an improved env helper:

DATABASES = {
    "default": env.db(),  # here DATABASE_URL variable is looked up in environment variables
}

# or
DATABASES = {
    "default": {
		"NAME": env("DB_NAME"),
		"USER:" env("DB_USER"),
		# ...
	}
}

@girardinsamuel
Copy link
Contributor

I like the first option but it has the downside to make it difficult to add other parameters such as :

DATABASES = {
  "mysql": {
    "prefix": "",
    "grammar": "mysql",
    "options": {
        "charset": "utf8mb4",
    },
  }
}

@Marlysson
Copy link
Contributor

Marlysson commented Jan 20, 2021

About the ideas:

In django we set the "default" key to define all data in dictionary from connection.
We use the dj-database-url to parse the url... then it return the dictionary and put all it in "default" key back. So The option of define just the url and make this lib that we will build return all dictionary necessary to connection seems make more sense to me.

DATABASES = {
    "default": database_url(env("DATABASE_URL")) # mysql/sqlite/postgres
}

@girardinsamuel
Copy link
Contributor

Yes django-environ is using dj-database-url internally ;)

@josephmancuso
Copy link
Member Author

Hm maybe we can have a. mix then. I see the benefits of having both

@girardinsamuel
Copy link
Contributor

I have give it a try, let me know what you think !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A feature that does not yet exist but will be a good addition to the library help wanted Extra attention is needed medium These issues are geared for people who have contributed to the project before
Projects
None yet
Development

No branches or pull requests

3 participants