@@ -41,16 +41,16 @@ async def create_a_model_record() -> UUID:
4141
4242 Create a new record using the default Model.create.one method.
4343 """
44- new_record : AModel = {
45- '_id ' : uuid4 (),
44+ new_record = AModel ( ** {
45+ 'id ' : uuid4 (),
4646 'string' : 'some string' ,
4747 'integer' : 1 ,
4848 'array' : ['an' , 'array' , 'of' , 'strings' ],
49- }
49+ })
5050
5151 await a_model .create .one (new_record )
5252
53- return new_record [ '_id' ]
53+ return new_record . id
5454
5555
5656async def read_a_model (id_value : UUID ) -> AModel :
@@ -62,28 +62,31 @@ async def read_a_model(id_value: UUID) -> AModel:
6262
6363async def create_extended_models () -> None :
6464 """Show how using an extended Model can be the same as the defaults."""
65- new_records : List [ ExtendedModelData ] = [{
66- '_id ' : uuid4 (),
65+ dicts = [{
66+ 'id ' : uuid4 (),
6767 'string' : 'something' ,
6868 'integer' : 1 ,
69- 'json ' : {'a' : 1 , 'b' : 2 , 'c' : True }
69+ 'data ' : {'a' : 1 , 'b' : 2 , 'c' : True }
7070 }, {
71- '_id ' : uuid4 (),
71+ 'id ' : uuid4 (),
7272 'string' : 'something' ,
7373 'integer' : 1 ,
74- 'json ' : {'a' : 1 , 'b' : 2 , 'c' : True }
74+ 'data ' : {'a' : 1 , 'b' : 2 , 'c' : True }
7575 }, {
76- '_id ' : uuid4 (),
76+ 'id ' : uuid4 (),
7777 'string' : 'something' ,
7878 'integer' : 1 ,
79- 'json ' : {'a' : 1 , 'b' : 2 , 'c' : True }
79+ 'data ' : {'a' : 1 , 'b' : 2 , 'c' : True }
8080 }, {
81- '_id ' : uuid4 (),
81+ 'id ' : uuid4 (),
8282 'string' : 'something' ,
8383 'integer' : 1 ,
84- 'json ' : {'a' : 1 , 'b' : 2 , 'c' : True }
84+ 'data ' : {'a' : 1 , 'b' : 2 , 'c' : True }
8585 }]
8686
87+ new_records : List [ExtendedModelData ] = [
88+ ExtendedModelData (** record ) for record in dicts ]
89+
8790 # by looping over a list of records, you can use the default create.one
8891 # method to create each record as a separate transaction
8992 for record in new_records :
@@ -100,8 +103,14 @@ async def read_extended_models() -> List[ExtendedModelData]:
100103
101104async def run () -> None :
102105 """Show how to make a connection, execute queries, & disconnect."""
106+
107+ # First, have the client make a connection to the database
103108 await client .connect ()
104109
110+ # Then, execute queries using the models that were initialized
111+ # with the client above.
112+ # Doing this inside a try/finally block allows client to gracefully
113+ # disconnect even when an exception is thrown.
105114 try :
106115 new_id = await create_a_model_record ()
107116 created_a_model = await read_a_model (new_id )
@@ -110,6 +119,7 @@ async def run() -> None:
110119 finally :
111120 await client .disconnect ()
112121
122+ # Print results to stdout
113123 print (json .dumps (created_a_model , cls = UUIDJsonEncoder ))
114124 print (json .dumps (extended_models , cls = UUIDJsonEncoder ))
115125
0 commit comments