Is there a way to paginate related data in sqlmodel on the frontend - fastapi

The sqlmodel docs gives an example of two classes
class Team(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(index=True)
headquarters: str
heroes: List["Hero"] = Relationship(back_populates="team")
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(index=True)
secret_name: str
age: Optional[int] = Field(default=None, index=True)
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
team: Optional[Team] = Relationship(back_populates="heroes")
I could get a Team object using the following code example
def get_team():
with Session(engine) as session:
statement = select(Team).where(Team.name == "avengers")
result = session.exec(statement)
avengers = result.one()
return avengers
and doing avengers.heroes should return a list of all heroes related to that object but What if the list contains thousands of items? is there a way to paginate this without having to make a separate query to the heroes table by myself?

To do this, you have to work with the underlying SQLAlchemy library, which you can do using the sa_relationship_kwargs argument to Relationship.
As mentioned in this answer, if you specify the relationship as dynamic, you can then paginate it. Using SQLModel, it looks something like this:
class Team(SQLModel, table=True):
...
heroes: List["Hero"] = Relationship(
back_populates="team",
sa_relationship_kwargs={"order_by": "Hero.name", "lazy": "dynamic"},
)
# When calling it
first_ten_heroes = team.heroes[:10] # or team.heroes.limit(10)

Related

Is it possible to use response_model data in read_hero function before returning it

I am reading hero with it's foreign key data from database and then return it using response_model. On read_hero function bellow, in this line hero = session.get(Hero, hero_id) hero is different from what this function return when I print it.
hero and team schema:
class HeroBase(SQLModel):
name: str = Field(index=True)
secret_name: str
age: Optional[int] = Field(default=None, index=True)
sponsor: Sponsor = Field(sa_column=Column(JSON(), nullable=False))
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
class Hero(HeroBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
team: "Team" = Relationship(back_populates="heroes")
class TeamBase(SQLModel):
name: str = Field(index=True)
headquarters: str
class Team(TeamBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
heroes: List["Hero"] = Relationship(back_populates="team")
class HeroReadWithTeam(HeroRead):
team: "TeamRead" = None
#app.get("/heroes/{hero_id}", response_model=HeroReadWithTeam)
def read_hero(*,hero_id: int, session: Session = Depends(get_session)):
hero = session.get(Hero, hero_id)
print(hero)
if not hero:
raise HTTPException(status_code=404, detail="Hero not found")
return hero
Line: print(hero) print out secret_name='clement' name='John' team_id=1 age=14 id=2
But the function it self return something like this because of response_model:
{
"name": "John",
"secret_name": "clement",
"age": 14,
"team_id": 1,
"id": 2,,
"team": {
"name": "Orlando Pirates",
"headquarters": "Orlando",
"id": 1
}
}
Is it possible to have this data inside read_hero function and use it before returning it.
You can refer from this for full code: https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/
I think you can do something like:
hero_dict = HeroReadWithTeam.from_orm(hero).dict
print(hero_dict)

Access the contents of the row inserted into Dynamodb using Pynamodb save method

I have the below model for a my dynamodb table using pynamodb :
from pynamodb.models import Model
from pynamodb.attributes import (
UnicodeAttribute, UTCDateTimeAttribute, UnicodeSetAttribute, BooleanAttribute
)
class Reminders(Model):
"""Model class for the Reminders table."""
# Information on global secondary index for the table
# user_id (hash key) + reminder_id+reminder_title(sort key)
class Meta:
table_name = 'Reminders'
region = 'eu-central-1'
reminder_id = UnicodeAttribute(hash_key=True)
user_id = UnicodeAttribute(range_key=True)
reminder_title = UnicodeAttribute()
reminder_tags = UnicodeSetAttribute()
reminder_description = UnicodeAttribute()
reminder_frequency = UnicodeAttribute(default='Only once')
reminder_tasks = UnicodeSetAttribute(default=set())
reminder_expiration_date_time = UTCDateTimeAttribute(null=True)
reminder_title_reminder_id = UnicodeAttribute()
next_reminder_date_time = UTCDateTimeAttribute()
should_expire = BooleanAttribute()
When i want to create a new reminder i do it through the below code :
class DynamoBackend:
#staticmethod
def create_a_new_reminder(new_reminder: NewReminder) -> Dict[str, Any]:
"""Create a new reminder using pynamodb."""
new_reminder = models.Reminders(**new_reminder.dict())
return new_reminder.save()
In this case the NewReminder is an instance of pydantic base model like so :
class NewReminder(pydantic.BaseModel):
reminder_id: str
user_id: str
reminder_title: str
reminder_description: str
reminder_tags: Sequence[str]
reminder_frequency: str
should_expire: bool
reminder_expiration_date_time: Optional[datetime.datetime]
next_reminder_date_time: datetime.datetime
reminder_title_reminder_id: str
when i call the save method on the model object i receive the below response:
{
"ConsumedCapacity": {
"CapacityUnits": 2.0,
"TableName": "Reminders"
}
}
Now my question is the save method is directly being called by a lambda function which is in turn called by an API Gateway POST endpoint so ideally the response should be a 201 created and instead of returning the consumed capacity and table name , would be great if it returns the item inserted in the database. Below is my route code :
def create_a_new_reminder():
"""Creates a new reminder in the database."""
request_context = app.current_request.context
request_body = json.loads(app.current_request.raw_body.decode())
request_body["reminder_frequency"] = data_structures.ReminderFrequency[request_body["reminder_frequency"]]
reminder_details = data_structures.ReminderDetailsFromRequest.parse_obj(request_body)
user_details = data_structures.UserDetails(
user_name=request_context["authorizer"]["claims"]["cognito:username"],
user_email=request_context["authorizer"]["claims"]["email"]
)
reminder_id = str(uuid.uuid1())
new_reminder = data_structures.NewReminder(
reminder_id=reminder_id,
user_id=user_details.user_name,
reminder_title=reminder_details.reminder_title,
reminder_description=reminder_details.reminder_description,
reminder_tags=reminder_details.reminder_tags,
reminder_frequency=reminder_details.reminder_frequency.value[0],
should_expire=reminder_details.should_expire,
reminder_expiration_date_time=reminder_details.reminder_expiration_date_time,
next_reminder_date_time=reminder_details.next_reminder_date_time,
reminder_title_reminder_id=f"{reminder_details.reminder_title}-{reminder_id}"
)
return DynamoBackend.create_a_new_reminder(new_reminder=new_reminder)
I am very new to REST API creation and best practices so would be great if someone would guide me here . Thanks in advance !

Writing Corda custom query by concating two schema column values and compare?

We have Name schema contains,
FirstName : Rock
LastName : John
Prefix : Mr
MiddleName : ""
Suffix: "Jr"
We are creating some states, schema with the definition.
But Now Want to field the states with values. We need to filter the values like
(FirstName+LastName).equals("RockJohn").
We are trying to write the custom vault query.
Is there any way to achieve this?
In Java, you'd write something like:
FieldInfo firstNameField = getField("firstName", NameSchemaV1.PersistentName.class);
FieldInfo lastNameField = getField("lastName", NameSchemaV1.PersistentName.class);
CriteriaExpression firstNameIndex = Builder.equal(firstNameField, "Rock");
CriteriaExpression lastNameIndex = Builder.equal(lastNameField, "John");
QueryCriteria firstNameCriteria = new QueryCriteria.VaultCustomQueryCriteria(firstNameIndex);
QueryCriteria lastNameCriteria = new QueryCriteria.VaultCustomQueryCriteria(lastNameIndex);
QueryCriteria criteria = firstNameCriteria.and(lastNameCriteria);
Vault.Page<ContractState> results = getServiceHub().getVaultService().queryBy(NameState.class, criteria);
In Kotlin, you'd write something like:
val results = builder {
val firstNameIndex = NameSchemaV1.PersistentName::firstName.equal("Rock")
val lastNameIndex = NameSchemaV1.PersistentName::lastName.equal("John")
val firstNameCriteria = QueryCriteria.VaultCustomQueryCriteria(firstNameIndex)
val lastNameCriteria = QueryCriteria.VaultCustomQueryCriteria(lastNameIndex)
val criteria = firstNameCriteria.and(lastNameCriteria)
serviceHub.vaultService.queryBy(NameState::class.java, criteria)
}
You can use a Hibernate formula to create a dynamic/calculated property:
#Formula(value = " concat(first_name, last_name) ")
String fullName
Then treat it as a regular property/field in your queries

How to get the class reference from KParameter in kotlin?

The code below is about reflection.
It tries to do 2 things:
case1() creates an instance from SimpleStudent class, it works.
case2() creates an instance from Student class, not work.
The reason that case2() not work as well as the question, is that inside that generateValue():
I don't know how to check it is kotlin type or my own type(I have a dirty way to check param.type.toString() not contain "kotlin" but I wonder if there is a better solution
I don't know how to get its class reference when it's a custom class. The problem is that even though param.type.toString() == "Lesson", when I tried to get param.type::class, it's class kotlin.reflect.jvm.internal.KTypeImpl
So, how to solve it? Thanks
==============
import kotlin.reflect.KParameter
import kotlin.reflect.full.primaryConstructor
import kotlin.test.assertEquals
data class Lesson(val title:String, val length:Int)
data class Student(val name:String, val major:Lesson )
data class SimpleStudent(val name:String, val age:Int )
fun generateValue(param:KParameter, originalValue:Map<*,*>):Any? {
var value = originalValue[param.name]
// if (param.type is not Kotlin type){
// // Get its ::class so that we could create the instance of it, here, I mean Lesson class?
// }
return value
}
fun case1(){
val classDesc = SimpleStudent::class
val constructor = classDesc.primaryConstructor!!
val value = mapOf<Any,Any>(
"name" to "Tom",
"age" to 16
)
val params = constructor.parameters.associateBy (
{it},
{generateValue(it, value)}
)
val result:SimpleStudent = constructor.callBy(params)
assertEquals("Tom", result.name)
assertEquals(16, result.age)
}
fun case2(){
val classDesc = Student::class
val constructor = classDesc.primaryConstructor!!
val value = mapOf<Any,Any>(
"name" to "Tom",
"major" to mapOf<Any,Any>(
"title" to "CS",
"length" to 16
)
)
val params = constructor.parameters.associateBy (
{it},
{generateValue(it, value)}
)
val result:Student = constructor.callBy(params)
assertEquals("Tom", result.name)
assertEquals(Lesson::class, result.major::class)
assertEquals("CS", result.major.title)
}
fun main(args : Array<String>) {
case1()
case2()
}
Problem solved:
You could get that ::class by using param.type.classifier as KClass<T> where param is KParameter

form.SchemaEditForm with ignoreContext

everybody
I'm trying to make an edit form for an object other than the context. It's a dictionary stored in the session. I'm following Martin Aspeli Schema Drive Forms. It should be easy, but for some reason, the edit form doesn't load any data. Maybe I'm losing some simple detail, but I can't find it. I made ignoreContext=True and tried returning a dictionary and after that an instance that implements the schema, but it didn't work.
from plone.directives import form, dexterity
from zope.interface import invariant, Invalid, implements
class IUser(form.Schema):
'''Represents an user'''
uid = schema.TextLine(
title = _(u'Login'),
)
gn = schema.TextLine(
title = _(u'Name'),
)
sn = schema.TextLine(
title = _(u'Surname'),
)
uniqueIdentifier = schema.TextLine(
title = _(u'Identifier'),
description = _(u'Ej. V11222333'),
)
accClass = schema.TextLine(
title = _(u'User class'),
)
dateExpiration = schema.TextLine(
title = _(u'Date of expiration'),
)
class User:
implements(IUser)
def __init__(self, **kargs):
self.uid = kargs['uid']
self.gn = kargs['givenName']
self.sn = kargs['sn']
self.uniqueIdentifier = kargs['uniqueIdentifier']
self.accClass = kargs['accClass']
self.dateExpiration = kargs.get('dateExpiration', '2015/06')
class Edit(form.SchemaEditForm):
'''Modify User'''
grok.context(IUserManager) # It's ok. This is a view of an IUserManager object
grok.require('zope2.View') # <- just for testing
grok.name('modify')
schema = IUser
ignoreContext = True
label = 'Modify an User'
def getContent(self):
# I've tried returning a dictionary too, but it's useless
user = User(**SessionUsers(self.context).current())
return user

Resources