Skip to content

Commit

Permalink
simplify database provider common code
Browse files Browse the repository at this point in the history
  • Loading branch information
rog555 committed May 16, 2024
1 parent 7d83476 commit 2cfdc0d
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 155 deletions.
63 changes: 18 additions & 45 deletions abnosql/plugins/table/cosmos.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@

import abnosql.exceptions as ex
from abnosql.plugin import PM
from abnosql.table import add_audit
from abnosql.table import add_change_meta
from abnosql.table import audit_callback
from abnosql.table import check_exists
from abnosql.table import check_exists_enabled
from abnosql.table import delete_item_post
from abnosql.table import delete_item_pre
from abnosql.table import get_item_post
from abnosql.table import get_item_pre
from abnosql.table import get_key_attrs
from abnosql.table import get_sql_params
from abnosql.table import kms_decrypt_item
from abnosql.table import kms_encrypt_item
from abnosql.table import kms_process_query_items
from abnosql.table import parse_connstr
from abnosql.table import put_item_post
from abnosql.table import put_item_pre
from abnosql.table import TableBase
from abnosql.table import validate_item
from abnosql.table import validate_key_attrs
from abnosql.table import validate_query_attrs

hookimpl = pluggy.HookimplMarker('abnosql.table')
Expand Down Expand Up @@ -201,9 +200,8 @@ def _container(self, name):

@cosmos_ex_handler()
def get_item(self, **kwargs) -> t.Optional[t.Dict]:
key = validate_key_attrs(self.key_attrs, dict(**kwargs), False)
self.pm.hook.get_item_pre(table=self.name, key=key)
_check_exists = dict(**kwargs).pop('abnosql_check_exists', None)
audit_key, _check_exists = get_item_pre(self, dict(**kwargs))

item = None
try:
item = strip_cosmos_attrs(
Expand All @@ -217,12 +215,7 @@ def get_item(self, **kwargs) -> t.Optional[t.Dict]:
else:
raise ex.NotFoundException('item not found')

_item = self.pm.hook.get_item_post(table=self.name, item=item)
if _item:
item = _item
item = kms_decrypt_item(self.config, item)
audit_callback(self, 'get', key)
return item
return get_item_post(self, dict(**kwargs), item, audit_key)

@cosmos_ex_handler()
def put_item(
Expand All @@ -231,27 +224,11 @@ def put_item(
update: t.Optional[bool] = False,
audit_user: t.Optional[str] = None
) -> t.Dict:
operation = 'update' if update else 'create'
key = validate_key_attrs(self.key_attrs, item)

# cosmos has to do create/update on delete but don't audit this
abnosql_audit_callback = item.pop('abnosql_audit_callback', None)
validate_item(self.config, operation, item)
item = check_exists(self, operation, item)

audit_user = audit_user or self.config.get('audit_user')
if audit_user:
item = add_audit(item, update or False, audit_user)
item, key = put_item_pre(self, item, update, audit_user)

# add change metadata if enabled
if self.change_meta is True:
item = add_change_meta(
item, self.name, 'MODIFY' if update is True else 'INSERT'
)

_item = self.pm.hook.put_item_pre(table=self.name, item=item)
if _item:
item = _item[0]
item = kms_encrypt_item(self.config, item)
# do update
if update is True:
kwargs = {
Expand All @@ -267,12 +244,10 @@ def put_item(
else:
item = self._container(self.name).upsert_item(item)
item = strip_cosmos_attrs(item)
self.pm.hook.put_item_post(table=self.name, item=item)
if abnosql_audit_callback is not False:
audit_callback(
self, 'update' if update else 'create', key, audit_user
)
return item

return put_item_post(
self, item, update, audit_user, abnosql_audit_callback
)

@cosmos_ex_handler()
def put_items(
Expand All @@ -288,8 +263,7 @@ def put_items(

@cosmos_ex_handler()
def delete_item(self, **kwargs):
check_exists(self, 'delete', dict(kwargs))
key = validate_key_attrs(self.key_attrs, dict(**kwargs), False)
key = delete_item_pre(self, dict(kwargs))

# if change metadata enabled do update first then delete
if self.change_meta is True:
Expand All @@ -315,9 +289,8 @@ def delete_item(self, **kwargs):
self._container(self.name).delete_item(
**get_key_kwargs(**kwargs)
)
self.pm.hook.delete_item_post(table=self.name, key=dict(kwargs))
if item is not None:
audit_callback(self, 'delete', key)

delete_item_post(self, key)

@cosmos_ex_handler()
def query(
Expand Down
59 changes: 17 additions & 42 deletions abnosql/plugins/table/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@

import abnosql.exceptions as ex
from abnosql.plugin import PM
from abnosql.table import add_audit
from abnosql.table import audit_callback
from abnosql.table import check_exists
from abnosql.table import check_exists_enabled
from abnosql.table import delete_item_post
from abnosql.table import delete_item_pre
from abnosql.table import get_item_post
from abnosql.table import get_item_pre
from abnosql.table import get_key_attrs
from abnosql.table import get_sql_params
from abnosql.table import kms_decrypt_item
from abnosql.table import kms_encrypt_item
from abnosql.table import kms_process_query_items
from abnosql.table import put_item_post
from abnosql.table import put_item_pre
from abnosql.table import TableBase
from abnosql.table import validate_item
from abnosql.table import validate_key_attrs
from abnosql.table import validate_query_attrs

hookimpl = pluggy.HookimplMarker('abnosql.table')
Expand Down Expand Up @@ -180,23 +179,15 @@ def set_config(self, config: t.Optional[dict]):

@dynamodb_ex_handler()
def get_item(self, **kwargs) -> t.Optional[t.Dict]:
key = validate_key_attrs(self.key_attrs, dict(**kwargs), False)
self.pm.hook.get_item_pre(table=self.name, key=key)
audit_key, _ = get_item_pre(self, dict(**kwargs))

response = deserialize(self.table.get_item(
TableName=self.name,
Key=get_key(**kwargs)
), self.config.get('deserializer'))
_check_exists = dict(**kwargs).pop('abnosql_check_exists', None)
item = response.get('Item')
_item = self.pm.hook.get_item_post(table=self.name, item=item)
if _item:
item = _item
item = kms_decrypt_item(self.config, item)
if _check_exists is not False:
check_exists(self, 'get', item)
if item is not None:
audit_callback(self, 'get', key)
return item

return get_item_post(self, dict(**kwargs), item, audit_key)

@dynamodb_ex_handler()
def put_item(
Expand All @@ -205,18 +196,7 @@ def put_item(
update: t.Optional[bool] = False,
audit_user: t.Optional[str] = None
) -> t.Dict:
operation = 'update' if update else 'create'
key = validate_key_attrs(self.key_attrs, item)
validate_item(self.config, operation, item)
item = check_exists(self, operation, item)

audit_user = audit_user or self.config.get('audit_user')
if audit_user:
item = add_audit(item, update or False, audit_user)
_item = self.pm.hook.put_item_pre(table=self.name, item=item)
if _item:
item = _item[0]
item = kms_encrypt_item(self.config, item)
item, _ = put_item_pre(self, item, update, audit_user)

# do update
if update is True:
Expand All @@ -243,11 +223,7 @@ def put_item(
else:
self.table.put_item(Item=item)

self.pm.hook.put_item_post(table=self.name, item=item)
audit_callback(
self, 'update' if update else 'create', key, audit_user
)
return item
return put_item_post(self, item, update, audit_user)

@dynamodb_ex_handler()
def put_items(
Expand All @@ -263,12 +239,11 @@ def put_items(

@dynamodb_ex_handler()
def delete_item(self, **kwargs):
key = validate_key_attrs(self.key_attrs, dict(**kwargs), False)
check_exists(self, 'delete', dict(kwargs))
_key = get_key(**kwargs)
self.table.delete_item(Key=_key)
self.pm.hook.delete_item_post(table=self.name, key=key)
audit_callback(self, 'delete', key)
key = delete_item_pre(self, dict(kwargs))

self.table.delete_item(Key=get_key(**kwargs))

delete_item_post(self, key)

@dynamodb_ex_handler()
def query(
Expand Down
56 changes: 15 additions & 41 deletions abnosql/plugins/table/firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@

import abnosql.exceptions as ex
from abnosql.plugin import PM
from abnosql.table import add_audit
from abnosql.table import audit_callback
from abnosql.table import check_exists
from abnosql.table import check_exists_enabled
from abnosql.table import delete_item_post
from abnosql.table import delete_item_pre
from abnosql.table import get_item_post
from abnosql.table import get_item_pre
from abnosql.table import get_key_attrs
from abnosql.table import kms_decrypt_item
from abnosql.table import kms_encrypt_item
from abnosql.table import kms_process_query_items
from abnosql.table import parse_connstr
from abnosql.table import put_item_post
from abnosql.table import put_item_pre
from abnosql.table import TableBase
from abnosql.table import validate_item
from abnosql.table import validate_key_attrs
from abnosql.table import validate_query_attrs

import sqlglot
Expand Down Expand Up @@ -139,20 +138,12 @@ def set_config(self, config: t.Optional[dict]):

@firestore_ex_handler()
def get_item(self, **kwargs) -> t.Optional[t.Dict]:
key = validate_key_attrs(self.key_attrs, dict(**kwargs), False)
self.pm.hook.get_item_pre(table=self.name, key=key)
_check_exists = dict(**kwargs).pop('abnosql_check_exists', None)
audit_key, _ = get_item_pre(self, dict(**kwargs))

doc = self.table.document(self._docid(**kwargs)).get()
item = doc.to_dict() if doc.exists else None
_item = self.pm.hook.get_item_post(table=self.name, item=item)
if _item:
item = _item
item = kms_decrypt_item(self.config, item)
if _check_exists is not False:
check_exists(self, 'get', item)
if item is not None:
audit_callback(self, 'get', key)
return item

return get_item_post(self, dict(**kwargs), item, audit_key)

@firestore_ex_handler()
def put_item(
Expand All @@ -161,18 +152,7 @@ def put_item(
update: t.Optional[bool] = False,
audit_user: t.Optional[str] = None
) -> t.Dict:
operation = 'update' if update else 'create'
key = validate_key_attrs(self.key_attrs, item)
validate_item(self.config, operation, item)
item = check_exists(self, operation, item)

audit_user = audit_user or self.config.get('audit_user')
if audit_user:
item = add_audit(item, update or False, audit_user)
_item = self.pm.hook.put_item_pre(table=self.name, item=item)
if _item:
item = _item[0]
item = kms_encrypt_item(self.config, item)
item, _ = put_item_pre(self, item, update, audit_user)

# do update
docid = self._docid(**item)
Expand All @@ -190,17 +170,12 @@ def put_item(
else:
ref.set(item)

self.pm.hook.put_item_post(table=self.name, item=item)

# firestore doesnt return updated item, so make this optional if needed
# note encrypted attrs won't be decrypted
if self.config.get('put_get') is True:
item = self.table.document(docid).get().to_dict()

audit_callback(
self, 'update' if update else 'create', key, audit_user
)
return item
return put_item_post(self, item, update, audit_user)

@firestore_ex_handler()
def put_items(
Expand All @@ -221,12 +196,11 @@ def put_items(

@firestore_ex_handler()
def delete_item(self, **kwargs):
key = validate_key_attrs(self.key_attrs, dict(**kwargs), False)
check_exists(self, 'delete', dict(kwargs))
key = delete_item_pre(self, dict(kwargs))

docid = self._docid(**kwargs)
self.table.document(docid).delete()
self.pm.hook.delete_item_post(table=self.name, key=dict(kwargs))
audit_callback(self, 'delete', key)
delete_item_post(self, key)

@firestore_ex_handler()
def query(
Expand Down
Loading

0 comments on commit 2cfdc0d

Please sign in to comment.