Skip to content

Commit

Permalink
Merge pull request #864 from matt2930/feature/863-config-path
Browse files Browse the repository at this point in the history
Add optional config path to ConnectionResolver and ConnectionFactory
  • Loading branch information
josephmancuso authored Dec 6, 2023
2 parents 2288c40 + 7a09edd commit 0ee98f3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/masoniteorm/connections/ConnectionFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
class ConnectionFactory:
"""Class for controlling the registration and creation of connection types."""

_connections = {
#
}
_connections = {}

def __init__(self, config_path=None):
self.config_path = config_path

@classmethod
def register(cls, key, connection):
Expand Down Expand Up @@ -35,7 +36,7 @@ def make(self, key):
masoniteorm.connection.BaseConnection -- Returns an instance of a BaseConnection class.
"""

DB = load_config().DB
DB = load_config(config_path=self.config_path).DB

connections = DB.get_connection_details()

Expand Down
6 changes: 3 additions & 3 deletions src/masoniteorm/connections/ConnectionResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ class ConnectionResolver:
_connections = {}
_morph_map = {}

def __init__(self):
def __init__(self, config_path=None):
from ..connections import (
SQLiteConnection,
PostgresConnection,
MySQLConnection,
MSSQLConnection,
)

self.config_path = config_path
from ..connections import ConnectionFactory

self.connection_factory = ConnectionFactory()

self.connection_factory = ConnectionFactory(config_path=config_path)
self.register(SQLiteConnection)
self.register(PostgresConnection)
self.register(MySQLConnection)
Expand Down
6 changes: 4 additions & 2 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
scopes=None,
schema=None,
dry=False,
config_path=None
):
"""QueryBuilder initializer
Expand All @@ -57,6 +58,7 @@ def __init__(
connection {masoniteorm.connection.Connection} -- A connection class (default: {None})
table {str} -- the name of the table (default: {""})
"""
self.config_path = config_path
self.grammar = grammar
self.table(table)
self.dry = dry
Expand Down Expand Up @@ -103,7 +105,7 @@ def __init__(
self.set_action("select")

if not self._connection_details:
DB = load_config().DB
DB = load_config(config_path=self.config_path).DB
self._connection_details = DB.get_connection_details()

self.on(connection)
Expand Down Expand Up @@ -382,7 +384,7 @@ def method(*args, **kwargs):
)

def on(self, connection):
DB = load_config().DB
DB = load_config(self.config_path).DB

if connection == "default":
self.connection = self._connection_details.get("default")
Expand Down
4 changes: 3 additions & 1 deletion src/masoniteorm/schema/Schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(
grammar=None,
connection_details=None,
schema=None,
config_path=None
):
self._dry = dry
self.connection = connection
Expand All @@ -68,6 +69,7 @@ def __init__(
self._blueprint = None
self._sql = None
self.schema = schema
self.config_path = config_path

if not self.connection_class:
self.on(self.connection)
Expand All @@ -85,7 +87,7 @@ def on(self, connection_key):
Returns:
cls
"""
DB = load_config().DB
DB = load_config(config_path=self.config_path).DB

if connection_key == "default":
self.connection = self.connection_details.get("default")
Expand Down

0 comments on commit 0ee98f3

Please sign in to comment.