4141class JSONSchemaToPostgres (JSONSchemaToSQL ):
4242 """Convert JSON Schema types to Postgres types."""
4343
44- def __init__ (self , * , content_encoding : bool = True ) -> None :
44+ def __init__ (self , * , content_encoding : bool = True , ** kwargs ) :
4545 """Initialize the JSONSchemaToPostgres instance."""
46- super ().__init__ ()
46+ super ().__init__ (** kwargs )
4747 self .content_encoding = content_encoding
4848
49+ @classmethod
50+ def from_config (
51+ cls : type [JSONSchemaToPostgres ],
52+ config : dict ,
53+ * ,
54+ max_varchar_length : int | None = None ,
55+ ) -> JSONSchemaToPostgres :
56+ """Create a JSONSchemaToPostgres instance from a configuration."""
57+ return cls (
58+ content_encoding = config .get ("interpret_content_encoding" , False ),
59+ max_varchar_length = max_varchar_length ,
60+ )
61+
4962 def handle_raw_string (self , schema ):
5063 """Handle a raw string type."""
5164 if self .content_encoding and schema .get ("contentEncoding" ) == "base16" :
@@ -63,6 +76,8 @@ class PostgresConnector(SQLConnector):
6376 allow_merge_upsert : bool = True # Whether MERGE UPSERT is supported.
6477 allow_temp_tables : bool = True # Whether temp tables are supported.
6578
79+ jsonschema_to_sql_converter = JSONSchemaToPostgres
80+
6681 def __init__ (self , config : dict ) -> None :
6782 """Initialize a connector to a Postgres database.
6883
@@ -100,18 +115,6 @@ def __init__(self, config: dict) -> None:
100115 sqlalchemy_url = url .render_as_string (hide_password = False ),
101116 )
102117
103- @cached_property
104- def interpret_content_encoding (self ) -> bool :
105- """Whether to interpret schema contentEncoding to set the column type.
106-
107- It is an opt-in feature because it might result in data loss if the
108- actual data does not match the schema's advertised encoding.
109-
110- Returns:
111- True if the feature is enabled, False otherwise.
112- """
113- return self .config .get ("interpret_content_encoding" , False )
114-
115118 def prepare_table ( # type: ignore[override] # noqa: PLR0913
116119 self ,
117120 full_table_name : str | FullyQualifiedName ,
@@ -258,7 +261,10 @@ def _handle_array_type(self, jsonschema: dict) -> ARRAY | JSONB:
258261 @cached_property
259262 def jsonschema_to_sql (self ) -> JSONSchemaToSQL :
260263 """Return a JSONSchemaToSQL instance with custom type handling."""
261- to_sql = JSONSchemaToPostgres (content_encoding = self .interpret_content_encoding )
264+ to_sql = JSONSchemaToPostgres .from_config (
265+ self .config ,
266+ max_varchar_length = self .max_varchar_length ,
267+ )
262268 to_sql .fallback_type = TEXT
263269 to_sql .register_type_handler ("integer" , BIGINT )
264270 to_sql .register_type_handler ("object" , JSONB )
0 commit comments