memory (sqla): New node_id, with_parent and with_link argument for get_pubsub_node:
- A node can now be retrieve directly by its ID. - `with_parent` and `with_link` let load `parent_node` and `linked_node`. rel 476
This commit is contained in:
parent
2c3d11e7ff
commit
4b91ea873e
1 changed files with 33 additions and 7 deletions
|
|
@ -1186,10 +1186,13 @@ class Storage:
|
|||
self,
|
||||
client: SatXMPPEntity,
|
||||
service: jid.JID,
|
||||
name: str,
|
||||
name: str | None = None,
|
||||
node_id: int | None = None,
|
||||
with_items: bool = False,
|
||||
with_subscriptions: bool = False,
|
||||
with_affiliations: bool = False,
|
||||
with_parent: bool = False,
|
||||
with_link: bool = False,
|
||||
create: bool = False,
|
||||
create_kwargs: dict | None = None,
|
||||
) -> PubsubNode | None:
|
||||
|
|
@ -1197,28 +1200,49 @@ class Storage:
|
|||
|
||||
@param service: service hosting the node
|
||||
@param name: node's name
|
||||
Can't be used with ``node_id``
|
||||
@param node_id: node's ID
|
||||
Can't be used with ``node_id` or ``create``
|
||||
@param with_items: retrieve items in the same query
|
||||
@param with_subscriptions: retrieve subscriptions in the same query
|
||||
@param with_affiliations: retrieve affiliations in the same query
|
||||
@param with_parent: retrieve parent_node in the same query
|
||||
@param with_link: retrieve linked_node in the same query
|
||||
@param create: if the node doesn't exist in DB, create it
|
||||
@param create_kwargs: keyword arguments to use with ``set_pubsub_node`` if the node
|
||||
needs to be created.
|
||||
@raise ValueError: if both name and node_id are provided, or neither.
|
||||
"""
|
||||
if node_id is not None:
|
||||
if name is not None:
|
||||
raise ValueError('"node_id" and "name" can\'t be used at the same time.')
|
||||
if create:
|
||||
raise ValueError("Cannot use create=True with node_id")
|
||||
|
||||
async with self.session() as session:
|
||||
stmt = select(PubsubNode).filter_by(
|
||||
service=service,
|
||||
name=name,
|
||||
profile_id=self.profiles[client.profile],
|
||||
)
|
||||
if node_id is not None:
|
||||
# When querying by ID, we only need it.
|
||||
stmt = select(PubsubNode).filter_by(id=node_id)
|
||||
else:
|
||||
stmt = select(PubsubNode).filter_by(
|
||||
service=service,
|
||||
profile_id=self.profiles[client.profile],
|
||||
name=name,
|
||||
)
|
||||
if with_items:
|
||||
stmt = stmt.options(joinedload(PubsubNode.items))
|
||||
if with_subscriptions:
|
||||
stmt = stmt.options(joinedload(PubsubNode.subscriptions))
|
||||
if with_affiliations:
|
||||
stmt = stmt.options(joinedload(PubsubNode.affiliations))
|
||||
if with_parent:
|
||||
stmt = stmt.options(joinedload(PubsubNode.parent_node))
|
||||
if with_link:
|
||||
stmt = stmt.options(joinedload(PubsubNode.linked_node))
|
||||
result = await session.execute(stmt)
|
||||
ret = result.unique().scalar_one_or_none()
|
||||
if ret is None and create:
|
||||
assert name is not None
|
||||
# we auto-create the node
|
||||
if create_kwargs is None:
|
||||
create_kwargs = {}
|
||||
|
|
@ -1235,10 +1259,12 @@ class Storage:
|
|||
self.get_pubsub_node(
|
||||
client,
|
||||
service,
|
||||
name,
|
||||
name=name,
|
||||
with_items=with_items,
|
||||
with_subscriptions=with_subscriptions,
|
||||
with_affiliations=with_affiliations,
|
||||
with_parent=with_parent,
|
||||
with_link=with_link,
|
||||
)
|
||||
)
|
||||
else:
|
||||
|
|
|
|||
Loading…
Reference in a new issue