Skip to content
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

Refactor Skeleton interface #630

Open
phorward opened this issue Jan 24, 2023 · 0 comments
Open

Refactor Skeleton interface #630

phorward opened this issue Jan 24, 2023 · 0 comments
Assignees
Labels
bootcamp Mausbrand Bootcamp Topic refactoring Pull requests that refactor code but do not change its behavior.

Comments

@phorward
Copy link
Member

phorward commented Jan 24, 2023

Ok, this will become a huger and recently updated issue relating to several things in VIURs current Skeleton interface. The reason for this issue is, that the current interface is uneven, messy and misleading in several ways.

CRUD-functions

It's called CRUD: Create, Read, Update, Delete.

Current state

For Skeletons, this is:

  • Create: toDB() with skel["key"] set to None
  • Read: fromDB()
  • Update: toDB() with a skel["key"] set
  • Delete: delete()

Okay - why is it fromDB(), toDB() but not... deleteDB()? This is uneven and messy.

Proposed state

  • Create: write() with skel["key"] set to None
  • Read: read()
  • Update: write() with a skel["key"] set
  • Delete: delete()

Hooks

In many situations, it is necessary to do some pre- or post-processing of data before it is written, read or deleted.

Current state

  • toDB():
    • preProcessSerializedData() - Do something on the already serialized db.Entity without using the bones anymore (mostly useless!)
    • preProcessBlobLocks() - Use is only relevant to blobs?
    • postSavedHandler() oh nice, I can do something after the skeleton has been saved. Why isn't it called postToDBHandler()?
    • Ok how to use the bones preliminary before writing data? Right, you override toDB(), do your preparation and afterwards make a super call. This is the current way to go.
  • fromDB(): No chance to do something here, except overriding fromDB(). Wow.
  • delete():
    • postDeletedHandler() is a hook on a SkeletonInstance which has been previously deleted.

Proposed state

It should be equivalent to module's hooks functionalities and naming:

  • write():
    • on_write() - do something before saving with the skeleton
    • serialize() - do something before saving with the db.Entity
      default/super should be a Skeleton.serialize(), which doesn't exist as well!
  • read():
    • unserialize() - do something after reading with the db.Entity
      Skeleton.unserialize() doesn't exist anymore, as bones are unserialized on-demand. Anyway, the hook should exist for override.
    • on_read() - do something after reading with the skeleton
  • delete():
    • on_delete() - do something after deleting the dataset. This is mostly equal to postDeletedHandler(), except the signature.

Shortcut functions

The following shortcut functions should be made available for fast skeleton CRD (create-read-delete).

Skeleton.get()

It's should be possible to just load a skeleton by a given key immediately.

Something like

article = skeleton.Skeleton.get(" ahBteS1mdW5ueS1wcm9qZWN0ciMLEgdhcnRpY2xlIhZteS1zZWNyZXQtYXJ0aWNsZS1oYWhhDA")

This could be achieved by a staticmethod

class Skeleton:
    @staticmethod
    def get(key: db.Key | str | int, kind: Option[str] = None) -> SkeletonInstance
        # ...
  • key: can be either a db.Key or an encoded key, or a name or an int. Latter one will only be used when kind is set. On kind mismatch, an exception is raised.
  • kind: Optionally defines the kind of the db.Entity, to determine the Skeleton. In case the kind is not provided, it is extracted from either the db.Key or the encoded-str, otherwise the kind is mandatory.

The function shall return None in case the given key wasn't found.

Skeleton.new()

tbd

Skeleton.del()

tbd

@phorward phorward added the refactoring Pull requests that refactor code but do not change its behavior. label Jan 24, 2023
@phorward phorward self-assigned this Jan 24, 2023
@phorward phorward added the bootcamp Mausbrand Bootcamp Topic label Jan 17, 2024
phorward added a commit to phorward/viur-core that referenced this issue Sep 24, 2024
Drafting new `Skeleton.update()` function based on `viur.toolkit.db.set_status()`.
This function replaces the existing Skeleton.update() which should provide a dict-like behavior. For this case, the `|`- and `|=`-operators should be used. Because Skelton/SkeletonInstance is not a dict, it should provide correct CRUD functinality.

This pull request is part of viur-framework#630 and a follow-up on viur-framework#1264, which should be merged first.
phorward added a commit that referenced this issue Oct 1, 2024
- [x] Rename `Skeleton.toDB()` into `Skeleton.write()`
- [x] Rename `Skeleton.fromDB()` into `Skeleton.read()`
- [x] Rename any `skelValues` variable into `skel`
- [x] Clean-up `Skeleton.delete()`
- [x] Improve `Skeleton.read()`, `Skeleton.write()` and
`Skeleton.delete()` to accept key-parameter
- [x] Deprecation handling
- [x] Added support for legacy fromDB/toDB definitions in
project-specific Skeletons

This pull request is a first, backward-compatible milestone on #630.
phorward added a commit that referenced this issue Oct 15, 2024
Provides a `Skeleton.patch()` function based on `viur.toolkit.db.set_status()`.

This improves the Skeleton API to a better and more integrated CRUD-API
(Create-Read-Update-Delete). It also introduces a
ReadFromClientException that can be raised internally for client parsing
error reporting.

This pull request is part of #630 and a follow-up on #1264, which
~should be~ was merged first.

This is also an alternative replacement on #991.

---------

Co-authored-by: agudermann <[email protected]>
Co-authored-by: Sven Eberth <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bootcamp Mausbrand Bootcamp Topic refactoring Pull requests that refactor code but do not change its behavior.
Projects
None yet
Development

No branches or pull requests

1 participant