@@ -19,13 +19,12 @@ Let's see how you can get started with the package.
19
19
Step #1 is to create a model that represents something like a row in a table.
20
20
21
21
``` py
22
- from sjd.entity.properties import IntProperty, StrProperty
23
- from sjd.entity import TEntity
22
+ from sjd import TEntity, properties as props
24
23
25
24
class Student (TEntity ):
26
- student_id = IntProperty(required = True )
27
- first_name = StrProperty(required = True )
28
- last_name = StrProperty(required = True )
25
+ student_id = props. IntProperty(required = True )
26
+ first_name = props. StrProperty(required = True )
27
+ last_name = props. StrProperty(required = True )
29
28
```
30
29
31
30
1 . Your model should inherit from ` TEntity ` .
@@ -36,11 +35,11 @@ class Student(TEntity):
36
35
We have an ` Engine ` and some ` Collection ` s. The first one is your database, the second is your table.
37
36
38
37
``` py
39
- from sjd.database import Engine, __Collection__
38
+ from sjd import Engine
40
39
41
40
class AppEngine (Engine ):
42
41
43
- students = __Collection__ (Student)
42
+ students = Engine.set (Student)
44
43
45
44
def __init__ (self ):
46
45
super ().__init__ (Path(" __test_db__" ))
@@ -50,7 +49,7 @@ class AppEngine(Engine):
50
49
1 . The engine will create a directory named ` __test_db__ ` .
51
50
2 . inside ` __test_db__ ` the collections are stored.
52
51
3 . You ** SHOULD** pass the type of your entity (` entity_type ` ) to the ` __Collection__ ` . Here is ` Student ` .
53
- 4 . type ` __Collection__ ` is a descriptor! The actual thing is ` Collection ` .
52
+ 4 . ` Engine.set() ` returns type ` __Collection__ ` which is a descriptor! The actual thing is ` Collection ` .
54
53
5 . The collection name will be class variable's name ( ` students ` here ).
55
54
56
55
### Create engine instance
@@ -94,9 +93,9 @@ Personally, i prefer classes with initializers. so let's add an `__init__` to th
94
93
class Student (TEntity ):
95
94
__json_init__ = True
96
95
97
- student_id = IntProperty(required = True )
98
- first_name = StrProperty(required = True )
99
- last_name = StrProperty()
96
+ student_id = props. IntProperty(required = True )
97
+ first_name = props. StrProperty(required = True )
98
+ last_name = props. StrProperty()
100
99
101
100
def __init__ (self , student_id : int , first_name : str , last_name : str ):
102
101
self .student_id = student_id
@@ -138,8 +137,8 @@ async for student in collection:
138
137
Get data with some filters.
139
138
140
139
``` py
141
- async for student in collection.as_queryable.where (
142
- lambda s : s.last_name == " Doe"
140
+ async for student in engine.students.iter_by_prop_value (
141
+ lambda s : s.last_name, " Doe"
143
142
):
144
143
print (student.first_name)
145
144
@@ -152,8 +151,8 @@ async for student in collection.as_queryable.where(
152
151
Ops they were all "Doe".
153
152
154
153
``` py
155
- async for student in collection.as_queryable.where (
156
- lambda s : s.first_name == " John"
154
+ async for student in engine.students.iter_by_prop_value (
155
+ lambda s : s.first_name, " John"
157
156
):
158
157
print (student.first_name)
159
158
@@ -164,7 +163,7 @@ If it's going to be one, we have more options.
164
163
165
164
``` py
166
165
167
- student = await collection.as_queryable.first (lambda s : s.first_name == " John" )
166
+ student = await collection.get_first (lambda s : s.first_name == " John" )
168
167
print (student.first_name)
169
168
170
169
# John
@@ -204,16 +203,16 @@ Let's begin with creating another model called `Grade` that includes some inform
204
203
Since this model is an embed entity, You should inherit from ` EmbedEntity ` .
205
204
206
205
``` py
207
- from src.entity import TEntity, EmbedEntity
206
+ from sjd import TEntity, EmbedEntity, properties as props
208
207
209
208
# ---- sniff ----
210
209
211
210
class Grade (EmbedEntity ):
212
211
__json_init__ = True
213
212
214
- course_id = IntProperty(required = True )
215
- course_name = StrProperty(required = True )
216
- score = IntProperty(required = True )
213
+ course_id = props. IntProperty(required = True )
214
+ course_name = props. StrProperty(required = True )
215
+ score = props. IntProperty(required = True )
217
216
218
217
def __init__ (self , course_id : int , course_name : str , score : int ):
219
218
self .course_id = course_id
@@ -226,17 +225,16 @@ To add this as a new property to the `Student`, we'll use `ComplexProperty`. ( O
226
225
Your ` Student ` class should looks like this:
227
226
228
227
``` py
229
- from src.entity.properties import IntProperty, StrProperty, OptionalComplexProperty
230
228
231
229
# ---- sniff ----
232
230
233
231
class Student (TEntity ):
234
232
__json_init__ = True
235
233
236
- student_id = IntProperty(required = True )
237
- first_name = StrProperty(required = True )
238
- last_name = StrProperty()
239
- grade = OptionalComplexProperty(Grade)
234
+ student_id = props. IntProperty(required = True )
235
+ first_name = props. StrProperty(required = True )
236
+ last_name = props. StrProperty()
237
+ grade = props. OptionalComplexProperty(Grade)
240
238
241
239
def __init__ (
242
240
self ,
@@ -264,7 +262,7 @@ async for student in collection:
264
262
Let's check if it's working
265
263
266
264
``` py
267
- jill = await collection.as_queryable.first (lambda s : s.first_name == " Jill" )
265
+ jill = await collection.get_first (lambda s : s.first_name == " Jill" )
268
266
if jill.grade:
269
267
print (jill.grade.course_name, jill.grade.score)
270
268
@@ -276,17 +274,16 @@ jill = await collection.as_queryable.first(lambda s: s.first_name == "Jill")
276
274
As we all know, there may be more than one course per student! Then the grade property, can be grades.
277
275
278
276
``` py
279
- from src.entity.properties import IntProperty, StrProperty, ListProperty
280
277
281
278
# ---- sniff ----
282
279
283
280
class Student (TEntity ):
284
281
__json_init__ = True
285
282
286
- student_id = IntProperty(required = True )
287
- first_name = StrProperty(required = True )
288
- last_name = StrProperty()
289
- grades = ListProperty(Grade, default_factory = list )
283
+ student_id = props. IntProperty(required = True )
284
+ first_name = props. StrProperty(required = True )
285
+ last_name = props. StrProperty()
286
+ grades = props. ListProperty(Grade, default_factory = list )
290
287
291
288
def __init__ (
292
289
self ,
@@ -315,7 +312,7 @@ async for student in collection:
315
312
Let's change Jill's english score to 50.
316
313
317
314
``` py
318
- jill = await collection.as_queryable.first (lambda s : s.first_name == " Jill" )
315
+ jill = await collection.get_first (lambda s : s.first_name == " Jill" )
319
316
if jill.grades:
320
317
jill.grades[- 1 ].score = 50
321
318
await collection.update(jill)
@@ -324,9 +321,11 @@ if jill.grades:
324
321
Oh who's score was 50 ??!
325
322
326
323
``` py
327
- with_50_score = await collection.as_queryable.where(
328
- lambda s : any (x.score == 50 for x in s.grades)).single()
329
- print (with_50_score.first_name)
324
+ async with collection as iter_ctx:
325
+
326
+ with_50_score = await iter_ctx.as_queryable.where(
327
+ lambda s : any (x.score == 50 for x in s.grades)).single()
328
+ print (with_50_score.first_name)
330
329
```
331
330
332
331
### Getting data ( Fast way )
@@ -342,6 +341,10 @@ There are two methods that are probably faster than `as_queryable` way.
342
341
Use this to do an ` async iterate ` over all entities with ` entity[__prop] == __value ` .
343
342
We use this to work with virtual objects.
344
343
344
+ 3 . ` get_first(__prop: str, __value: Any) `
345
+
346
+ Just like 2, but returns the first.
347
+
345
348
``` py
346
349
async for student in engine.students.iter_by_prop_value(" first_name" , " Jill" ):
347
350
print (student.last_name)
@@ -371,16 +374,14 @@ Since the `Grade` is going to be a separate entity, we should add it to our `App
371
374
``` py
372
375
# ---- sniff ----
373
376
374
- from sjd.entity.properties import VirtualListProperty
375
-
376
377
class Student (TEntity ):
377
378
__json_init__ = True
378
379
379
- student_id = IntProperty(required = True )
380
- first_name = StrProperty(required = True )
381
- last_name = StrProperty()
380
+ student_id = props. IntProperty(required = True )
381
+ first_name = props. StrProperty(required = True )
382
+ last_name = props. StrProperty()
382
383
383
- grades = VirtualListProperty(Grade, " student_id" )
384
+ grades = props. VirtualListProperty(Grade, " student_id" )
384
385
385
386
def __init__ (
386
387
self ,
@@ -405,16 +406,14 @@ Since the `Grade` is going to be a separate entity, we should add it to our `App
405
406
```py
406
407
# ---- sniff ----
407
408
408
- from sjd.entity.properties import ReferenceProperty
409
-
410
409
class Grade (TEntity ):
411
410
__json_init__ = True
412
411
413
- course_id = IntProperty(required = True )
414
- course_name = StrProperty(required = True )
415
- score = IntProperty(required = True )
412
+ course_id = props. IntProperty(required = True )
413
+ course_name = props. StrProperty(required = True )
414
+ score = props. IntProperty(required = True )
416
415
417
- student_id = ReferenceProperty()
416
+ student_id = props. ReferenceProperty()
418
417
419
418
def __init__ (self , course_id : int , course_name : str , score : int ):
420
419
self .course_id = course_id
@@ -431,8 +430,8 @@ Since the `Grade` is going to be a separate entity, we should add it to our `App
431
430
```py
432
431
class AppEngine (Engine ):
433
432
434
- students = __Collection__ (Student)
435
- grades = __Collection__ (Grade)
433
+ students = Engine.set (Student)
434
+ grades = Engine.set (Grade)
436
435
437
436
def __init__ (self ):
438
437
super ().__init__ (" __test_db__" )
@@ -453,7 +452,7 @@ Since the `Grade` is going to be a separate entity, we should add it to our `App
453
452
If you try getting one of your students now, you' ll see the `grades` property is an empty list.
454
453
455
454
```py
456
- arash = await engine.students.as_queryable.first (
455
+ arash = await engine.students.get_first (
457
456
lambda s : s.first_name == " Arash"
458
457
)
459
458
print (arash.grades)
0 commit comments