From 879a9048176dcb274099a2fd976fe33881e14841 Mon Sep 17 00:00:00 2001 From: Matthew Altberg Date: Tue, 5 Dec 2023 12:24:36 -0500 Subject: [PATCH 1/2] Add optional config path to ConnectionResolver and ConnectionFactory --- src/masoniteorm/connections/ConnectionFactory.py | 9 +++++---- src/masoniteorm/connections/ConnectionResolver.py | 5 ++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/masoniteorm/connections/ConnectionFactory.py b/src/masoniteorm/connections/ConnectionFactory.py index 9b924a3d..b36292dd 100644 --- a/src/masoniteorm/connections/ConnectionFactory.py +++ b/src/masoniteorm/connections/ConnectionFactory.py @@ -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): @@ -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() diff --git a/src/masoniteorm/connections/ConnectionResolver.py b/src/masoniteorm/connections/ConnectionResolver.py index f62eac30..0266f751 100644 --- a/src/masoniteorm/connections/ConnectionResolver.py +++ b/src/masoniteorm/connections/ConnectionResolver.py @@ -6,7 +6,7 @@ class ConnectionResolver: _connections = {} _morph_map = {} - def __init__(self): + def __init__(self, config_path=None): from ..connections import ( SQLiteConnection, PostgresConnection, @@ -16,8 +16,7 @@ def __init__(self): 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) From 7a09edd28b4646b4c01a848d3121c49d5a3eef46 Mon Sep 17 00:00:00 2001 From: Matthew Altberg Date: Tue, 5 Dec 2023 12:42:37 -0500 Subject: [PATCH 2/2] Add config_path to Schema and QueryBuilder classes --- src/masoniteorm/connections/ConnectionResolver.py | 1 + src/masoniteorm/query/QueryBuilder.py | 6 ++++-- src/masoniteorm/schema/Schema.py | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/masoniteorm/connections/ConnectionResolver.py b/src/masoniteorm/connections/ConnectionResolver.py index 0266f751..f408c09c 100644 --- a/src/masoniteorm/connections/ConnectionResolver.py +++ b/src/masoniteorm/connections/ConnectionResolver.py @@ -14,6 +14,7 @@ def __init__(self, config_path=None): MSSQLConnection, ) + self.config_path = config_path from ..connections import ConnectionFactory self.connection_factory = ConnectionFactory(config_path=config_path) diff --git a/src/masoniteorm/query/QueryBuilder.py b/src/masoniteorm/query/QueryBuilder.py index fea2c674..5bd886ec 100644 --- a/src/masoniteorm/query/QueryBuilder.py +++ b/src/masoniteorm/query/QueryBuilder.py @@ -47,6 +47,7 @@ def __init__( scopes=None, schema=None, dry=False, + config_path=None ): """QueryBuilder initializer @@ -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 @@ -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) @@ -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") diff --git a/src/masoniteorm/schema/Schema.py b/src/masoniteorm/schema/Schema.py index 817872e9..6d49f886 100644 --- a/src/masoniteorm/schema/Schema.py +++ b/src/masoniteorm/schema/Schema.py @@ -57,6 +57,7 @@ def __init__( grammar=None, connection_details=None, schema=None, + config_path=None ): self._dry = dry self.connection = connection @@ -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) @@ -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")