-
Notifications
You must be signed in to change notification settings - Fork 15.8k
chore: abstract models and daos into superset-core #35259
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""Protocol interfaces for Data Access Objects.""" | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Any, Generic, Optional, TypeVar, Union | ||
|
||
from flask_appbuilder.models.filters import BaseFilter | ||
from flask_sqlalchemy import BaseQuery | ||
|
||
from superset_core.models.base import CoreModel | ||
|
||
# Type variable bound to our CoreModel | ||
T_Model = TypeVar("T_Model", bound=CoreModel) | ||
|
||
|
||
class BaseDAO(Generic[T_Model], ABC): | ||
""" | ||
Interface for Data Access Objects. | ||
This interface defines the base that all DAOs should implement, | ||
providing consistent CRUD operations across Superset and extensions. | ||
Extension developers should implement this protocol: | ||
```python | ||
from superset_core.dao import BaseDAO | ||
from superset_core.models import CoreModel | ||
class MyDAO(BaseDAO[MyCustomModel]): | ||
model_cls = MyCustomModel | ||
@classmethod | ||
def find_by_id(cls, model_id: str | int) -> MyCustomModel | None: | ||
# Implementation here | ||
pass | ||
``` | ||
""" | ||
|
||
# Class attributes that implementations should define | ||
model_cls: Optional[type[T_Model]] | ||
base_filter: Optional[BaseFilter] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use something more generic here instead of dao = DatasetDAO()
datasets = dao.find_by_ids([1, 2]) # equivalent to `skip_base_filter=True`
dao_for_user = dao.filtered(User.id == current_user.id)
datasets = dao_for_user.find_by_ids([1, 2]) # equivalent to `skip_base_filter=False` In general I think the less dependencies There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fluent Interfaces are cool! but like that makes me think why not just use |
||
id_column_name: str | ||
uuid_column_name: str | ||
|
||
@abstractmethod | ||
def find_by_id( | ||
self, model_id: Union[str, int], skip_base_filter: bool = False | ||
) -> Optional[T_Model]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: In the future we could have models with composite keys There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding When debugging missing entities, would we rather see |
||
"""Find a model by ID.""" | ||
... | ||
|
||
@abstractmethod | ||
def find_by_id_or_uuid( | ||
self, | ||
model_id_or_uuid: str, | ||
skip_base_filter: bool = False, | ||
) -> Optional[T_Model]: | ||
"""Find a model by ID or UUID.""" | ||
... | ||
|
||
@abstractmethod | ||
def find_by_ids( | ||
self, | ||
model_ids: Union[list[str], list[int]], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use Also, can we use this opportunity to just use UUIDs everywhere, instead of having int IDs, string IDs, and UUIDs? |
||
skip_base_filter: bool = False, | ||
) -> list[T_Model]: | ||
michael-s-molina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Find models by list of IDs.""" | ||
... | ||
|
||
@abstractmethod | ||
def find_all(self) -> list[T_Model]: | ||
"""Get all entities that fit the base_filter.""" | ||
... | ||
|
||
@abstractmethod | ||
def find_one_or_none(self, **filter_by: Any) -> Optional[T_Model]: | ||
"""Get the first entity that fits the base_filter.""" | ||
... | ||
|
||
@abstractmethod | ||
def create( | ||
self, | ||
item: Optional[T_Model] = None, | ||
attributes: Optional[dict[str, Any]] = None, | ||
) -> T_Model: | ||
"""Create an object from the specified item and/or attributes.""" | ||
... | ||
|
||
@abstractmethod | ||
def update( | ||
self, | ||
item: Optional[T_Model] = None, | ||
attributes: Optional[dict[str, Any]] = None, | ||
) -> T_Model: | ||
"""Update an object from the specified item and/or attributes.""" | ||
... | ||
|
||
@abstractmethod | ||
def delete(self, items: list[T_Model]) -> None: | ||
"""Delete the specified items.""" | ||
... | ||
|
||
@abstractmethod | ||
def query(self, query: BaseQuery) -> list[T_Model]: | ||
"""Execute query with base_filter applied.""" | ||
... | ||
|
||
@abstractmethod | ||
def filter_by(self, **filter_by: Any) -> list[T_Model]: | ||
"""Get all entries that fit the base_filter.""" | ||
... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
michael-s-molina marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""Core model base classes.""" | ||
|
||
from typing import Any | ||
|
||
from flask_appbuilder import Model | ||
from sqlalchemy.orm import Mapped | ||
|
||
|
||
class CoreModel(Model): | ||
""" | ||
Abstract base class that extends Flask-AppBuilder's Model. | ||
This class provides the interface contract for all Superset models. | ||
The host package provides concrete implementations. | ||
""" | ||
|
||
__abstract__ = True | ||
|
||
|
||
class Database(CoreModel): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we call this |
||
""" | ||
Interface for Database models. | ||
This interface defines the contract that database models should implement, | ||
providing consistent database connectivity and metadata operations. | ||
""" | ||
|
||
__abstract__ = True | ||
|
||
id = Mapped[int] | ||
verbose_name = Mapped[str] | ||
database_name = Mapped[str | None] | ||
|
||
@property | ||
def name(self) -> str: | ||
raise NotImplementedError | ||
|
||
@property | ||
def backend(self) -> str: | ||
raise NotImplementedError | ||
|
||
@property | ||
def data(self) -> dict[str, Any]: | ||
raise NotImplementedError | ||
|
||
|
||
class Dataset(CoreModel): | ||
""" | ||
Interface for Dataset models. | ||
This Interface defines the contract that dataset models should implement, | ||
providing consistent data source operations and metadata. | ||
It provides the public API for Datasets implemented by the host application. | ||
""" | ||
|
||
__abstract__ = True | ||
michael-s-molina marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We can use
Query
from SQLAlchemy