Skip to content

Commit 0da9ce2

Browse files
update entity finding doc to use extension properties
1 parent 78a34fa commit 0da9ce2

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

docs/source/en/entity-finding.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ Ktorm provides a set of APIs named *Entity Sequence*, which can be used to obtai
1010

1111
## Get Entities by Sequences
1212

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:
1414

1515
```kotlin
16-
val sequence = database.sequenceOf(Employees)
16+
val Database.departments get() = this.sequenceOf(Departments)
17+
val Database.employees get() = this.sequenceOf(Employees)
1718
```
1819

19-
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:
2021

2122
```kotlin
22-
val employee = sequence.find { it.id eq 1 }
23+
val employee = database.employees.find { it.name eq "vince" }
2324
```
2425

2526
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
4445
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:
4546

4647
```kotlin
47-
val employee = sequence.find {
48+
val employee = database.employees.find {
4849
val dept = it.departmentId.referenceTable as Departments
4950
dept.location eq "Guangzhou"
5051
}
@@ -58,7 +59,7 @@ open class Employees(alias: String?) : Table<Employee>("t_employee", alias) {
5859
val department get() = departmentId.referenceTable as Departments
5960
}
6061

61-
val employee = sequence.find { it.department.location eq "Guangzhou" }
62+
val employee = database.employees.find { it.department.location eq "Guangzhou" }
6263
```
6364

6465
Generated SQL:

docs/source/zh-cn/entity-finding.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ Ktorm 提供了一套名为”实体序列”的 API,用来从数据库中获
1010

1111
## 使用序列 API 获取实体
1212

13-
要使用序列 API,我们首先要通过 `sequenceOf` 函数得到一个实体序列
13+
要使用序列 API,首先要创建实体序列的对象。一般来说,我们会给 `Database` 定义一些扩展属性,它们使用 `sequenceOf` 函数创建序列对象并返回。这些属性可以帮助我们提高代码的可读性
1414

1515
```kotlin
16-
val sequence = database.sequenceOf(Employees)
16+
val Database.departments get() = this.sequenceOf(Departments)
17+
val Database.employees get() = this.sequenceOf(Employees)
1718
```
1819

19-
返回的 `sequence` 对象可以视为保存了 `Employees` 表中所有记录的一个序列。要从这个序列中获取实体对象,可以使用 `find` 函数
20+
下面的代码使用 `find` 函数从序列中根据名字获取一个 Employee 对象
2021

2122
```kotlin
22-
val employee = sequence.find { it.id eq 1 }
23+
val employee = database.employees.find { it.name eq "vince" }
2324
```
2425

2526
这种写法十分自然,就像使用 Kotlin 标准库中的函数从一个集合中筛选符合条件的元素一样,不同的仅仅是在 lambda 表达式中的等号 `==` 被这里的 `eq` 函数代替了而已。
@@ -44,7 +45,7 @@ where t_employee.id = ?
4445
既然 Ktorm 会自动 left join 关联表,我们当然也能在筛选条件中使用关联表中的列。下面的代码可以获取一个在广州工作的员工对象,这里我们通过列的 `referenceTable` 属性获取 `departmentId` 所引用的表对象:
4546

4647
```kotlin
47-
val employee = sequence.find {
48+
val employee = database.employees.find {
4849
val dept = it.departmentId.referenceTable as Departments
4950
dept.location eq "Guangzhou"
5051
}
@@ -58,7 +59,7 @@ open class Employees(alias: String?) : Table<Employee>("t_employee", alias) {
5859
val department get() = departmentId.referenceTable as Departments
5960
}
6061

61-
val employee = sequence.find { it.department.location eq "Guangzhou" }
62+
val employee = database.employees.find { it.department.location eq "Guangzhou" }
6263
```
6364

6465
生成的 SQL 如下:

docs/source/zh-cn/quick-start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ object Employees : Table<Employee>("t_employee") {
233233

234234
```kotlin
235235
val Database.departments get() = this.sequenceOf(Departments)
236-
val Database.employees get() = this.sequenceOf(Employees)s
236+
val Database.employees get() = this.sequenceOf(Employees)
237237
```
238238

239239
下面的代码使用 `find` 函数从序列中根据名字获取一个 Employee 对象:

0 commit comments

Comments
 (0)