Creating users
Now that we have our Nameko dependency in place, we can start to add functionality to our UserWrapper
. We will start by creating users. Add the following to the UserWrapper
class:
def create(self, **kwargs): user = User(**kwargs) self.session.add(user) self.session.commit()
This create
method will create a new User
object, add it to our database session, commit that change to the database, and return the user. Nothing fancy here! But let's talk about the process of self.session.add
and self.session.commit
. When we first add
the user to the session, this adds the user to our local database session in memory, rather than adding them to our actual database. The new user has been staged but no changes have actually taken place in our database. This is rather useful. Say we wanted to do multiple updates to the database, making a number of calls to the database can be expensive, so we first make all the changes we want in memory, then commit
them all with a single...