You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/en/entity-finding.md
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,16 +10,17 @@ Ktorm provides a set of APIs named *Entity Sequence*, which can be used to obtai
10
10
11
11
## Get Entities by Sequences
12
12
13
-
To use entity sequence, we need to create a sequence object via `sequenceOf`firstly:
13
+
To use sequence APIs, we need to create sequence objects first. In general, we'd like to define some extension properties for `Database`. These properties return new created sequence objects via `sequenceOf`and they can help us improve the readability of the code:
The returned object can be thought of as a sequence holding all records in the `Employees` table. To get an entity object from this sequence, you can use the `find` function:
20
+
The following code uses the `find` function to obtain an employee by its name:
20
21
21
22
```kotlin
22
-
val employee =sequence.find { it.id eq 1 }
23
+
val employee =database.employees.find { it.name eq "vince" }
23
24
```
24
25
25
26
This is natural, just like finding from a collection via Kotlin’s built-in extension functions, the only difference is the `==` in the lambda is replace by the `eq` function.
@@ -44,7 +45,7 @@ Reading the generated SQL, we can find that Ktorm auto left joins `t_employee`'s
44
45
Now that referenced tables are auto left joined, we can also use their columns in our filter conditions. The code below uses `Column.referenceTable` to access `departmentId`'s referenced table and obtains an employee who works at Guangzhou:
45
46
46
47
```kotlin
47
-
val employee =sequence.find {
48
+
val employee =database.employees.find {
48
49
val dept = it.departmentId.referenceTable asDepartments
49
50
dept.location eq "Guangzhou"
50
51
}
@@ -58,7 +59,7 @@ open class Employees(alias: String?) : Table<Employee>("t_employee", alias) {
58
59
val department get() = departmentId.referenceTable asDepartments
59
60
}
60
61
61
-
val employee =sequence.find { it.department.location eq "Guangzhou" }
62
+
val employee =database.employees.find { it.department.location eq "Guangzhou" }
0 commit comments