I would really love to see this become part of the Anvil standard library. I think it would really help beginners like me, if only we can first understand what it is. Unfortunately, I had no previous familiarity with the ORM concept so I had difficulty wrapping my head around this initially. I found the tutorial really helpful, though. Maybe I’ll submit some pull requests to your documentation as I develop a better grasp of the details.
Unfortunately, I haven’t had time to polish the docs and the library actually does far more than is covered in the tutorial - the titles of the howtos are there to remind me what I need to write up!
Working on a simple one-off project, and am trying to integrate the ORM library to get some experience with it. I ran into one issue, and had some questions:
These imports in persistence didn’t work for me based on how the tutorial said to create the folder structure:
from app.server_lib.crud import security
from app.client_lib.crud.particles import
I had to change them to this:
from . import security
from .lib.crud.particles import ModelSearchResults
It doesn’t seem possible to use get to get a single item using something other than the uid. In my case I have a unique field in the table I want to fetch an item by. I could use search, check the length, and get the first element, but it’d be really nice if there were an equivalent to the Anvil data tables get. Maybe there’s something I’m just not seeing to do that?
Related to #2 above, how would I go about getting just the first item out of a set of search results? The ModelSearchIterator doesn’t respond well to next(results), claiming it’s not an iterator. What I ended up doing is very hacky:
rooms = Room.search(roomid=self.url_dict['id'])
for room in rooms:
self.roomname.text = room.name
break
Is there an order by equivalent for search?
How do you go about searching by linked table rows? I’d assumed it’d be by passing in the full object, but that gives me an invalid query value issue. It was for this model:
@model_type
class Roll:
room = Relationship(class_name="Room")
name = Attribute()
when = Attribute()
rolls = Attribute()
For the first problem, the docs are a little behind the code and it looks like a copy/paste has also crept in. I’ll try to get those fixed shortly.
2 and 3 sound like good ideas which could be added.
4 and 5 aren’t supported and aren’t likely to be.
UPDATE
After some conversation via DM, I utterly failed to read and understand number 5 before answering! That is supported at the moment by a wholly undocumented ‘cross reference’ feature.
I’ve pushed a new release (v0.1.18) with (hopefully) the package structure, imports and docs aligned. I’ll try to carve out some time to work on this library but, unless one of my paying jobs has a need, it’s tricky to find that time at the moment.
@owen.campbell
Thanks for this great contribution!
Your ORM lib is a simple yet very effective way to quickly create and manage an app data model.
However, I stuck with issue #5 mentioned by @jshaffstall
I am trying to understand how to use cross-reference for search but would appreciate it if you could give me a hint.