Skip to content

Commit 2ac471c

Browse files
authored
docs: EXPOSED-747 Add a get started tutorial for Exposed DAO (#2556)
* docs: add a new snippets project and tutorial for getting started with Exposed DAO * fix text, code snippets and create a new toc-element * include get-started-with-exposed to the snippets project and update refs * update downloadable gradle project
1 parent e8c1770 commit 2ac471c

File tree

23 files changed

+487
-433
lines changed

23 files changed

+487
-433
lines changed

documentation-website/Writerside/hi.tree

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
<toc-element topic="Home.topic" toc-title="Home"/>
1010
<toc-element topic="About.topic" toc-title="About"/>
11-
<toc-element topic="Getting-Started-with-Exposed.topic" toc-title="Get started with Exposed"/>
11+
<toc-element toc-title="Get started">
12+
<toc-element topic="Get-Started-with-Exposed.topic" toc-title="Get started with Exposed" accepts-web-file-names="getting-started-with-exposed.html"/>
13+
<toc-element topic="Get-started-with-Exposed-DAO.md"/>
14+
</toc-element>
1215
<toc-element topic="Exposed-Modules.md"/>
1316
<toc-element toc-title="Databases">
1417
<toc-element topic="Working-with-Database.md"/>
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE USERS ADD PRIMARY KEY (ID);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Get started with Exposed DAO tutorial project
2+
3+
A sample Gradle/Kotlin project built by following the steps explained in
4+
the [Get started with Exposed DAO](https://www.jetbrains.com/help/exposed/get-started-with-exposed-dao.html)
5+
tutorial.
6+
7+
## Build
8+
9+
To build the application, navigate to the `snippets` folder and run the following command:
10+
11+
```shell
12+
./gradlew :get-started-with-exposed-dao:build
13+
```
14+
15+
## Run
16+
17+
To run the application, execute the following command:
18+
19+
```bash
20+
./gradlew :get-started-with-exposed-dao:run
21+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Kotlin application project to get you started.
5+
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.8/userguide/building_java_projects.html in the Gradle documentation.
6+
*/
7+
8+
plugins {
9+
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
10+
alias(libs.plugins.jvm)
11+
12+
// Apply the application plugin to add support for building a CLI application in Java.
13+
application
14+
}
15+
16+
repositories {
17+
// Use Maven Central for resolving dependencies.
18+
mavenCentral()
19+
}
20+
21+
dependencies {
22+
// Use the Kotlin JUnit 5 integration.
23+
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
24+
25+
// Use the JUnit 5 integration.
26+
testImplementation(libs.junit.jupiter.engine)
27+
28+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
29+
30+
// This dependency is used by the application.
31+
implementation(libs.guava)
32+
implementation(libs.exposed.core)
33+
implementation(libs.exposed.dao)
34+
implementation(libs.exposed.jdbc)
35+
implementation(libs.h2)
36+
}
37+
38+
// Apply a specific Java toolchain to ease working on different environments.
39+
java {
40+
toolchain {
41+
languageVersion = JavaLanguageVersion.of(21)
42+
}
43+
}
44+
45+
application {
46+
// Define the main class for the application.
47+
mainClass = "org.example.AppKt"
48+
}
49+
50+
tasks.named<Test>("test") {
51+
// Use JUnit Platform for unit tests.
52+
useJUnitPlatform()
53+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.example
2+
3+
import org.jetbrains.exposed.v1.core.StdOutSqlLogger
4+
import org.jetbrains.exposed.v1.jdbc.Database
5+
import org.jetbrains.exposed.v1.jdbc.SchemaUtils
6+
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
7+
8+
fun main() {
9+
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
10+
11+
transaction {
12+
addLogger(StdOutSqlLogger)
13+
14+
// ...
15+
16+
SchemaUtils.create(Tasks)
17+
18+
val task1 = Task.new {
19+
title = "Learn Exposed DAO"
20+
description = "Follow the DAO tutorial"
21+
}
22+
23+
val task2 = Task.new {
24+
title = "Read The Hobbit"
25+
description = "Read chapter one"
26+
isCompleted = true
27+
}
28+
29+
println("Created new tasks with ids ${task1.id} and ${task2.id}")
30+
31+
val completed = Task.find { Tasks.isCompleted eq true }.toList()
32+
println("Completed tasks: ${completed.count()}")
33+
34+
// Update
35+
task1.title = "Try Exposed DAO"
36+
task1.isCompleted = true
37+
println("Updated task1: $task1")
38+
39+
// Delete
40+
task2.delete()
41+
println("Remaining tasks: ${Task.all().toList()}")
42+
}
43+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.example
2+
3+
// ...
4+
import org.jetbrains.exposed.v1.core.dao.id.EntityID
5+
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
6+
import org.jetbrains.exposed.v1.dao.IntEntity
7+
import org.jetbrains.exposed.v1.dao.IntEntityClass
8+
9+
@Suppress("MagicNumber")
10+
object Tasks : IntIdTable("tasks") {
11+
val title = varchar("name", 128)
12+
val description = varchar("description", 128)
13+
val isCompleted = bool("completed").default(false)
14+
}
15+
16+
// ...
17+
18+
class Task(id: EntityID<Int>) : IntEntity(id) {
19+
companion object : IntEntityClass<Task>(Tasks)
20+
21+
var title by Tasks.title
22+
var description by Tasks.description
23+
var isCompleted by Tasks.isCompleted
24+
25+
override fun toString(): String {
26+
return "Task(id=$id, title=$title, completed=$isCompleted)"
27+
}
28+
}

documentation-website/Writerside/snippets/get-started-with-exposed/.gitattributes

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# Get started with Exposed tutorial project
22

33
A sample Gradle/Kotlin project built by following the steps explained in
4-
the [Get started with Exposed](https://jetbrains.github.io/Exposed/getting-started-with-exposed.html) tutorial.
4+
the [Get started with Exposed](https://www.jetbrains.com/help/exposed/get-started-with-exposed.html) tutorial.
5+
6+
## Build
7+
8+
To build the application, navigate to the `snippets` folder and run the following command:
9+
10+
```shell
11+
./gradlew :get-started-with-exposed:build
12+
```
513

614
## Run
715

816
To run the application, execute the following command in the repository's root directory:
917

1018
```bash
11-
./gradlew run
19+
./gradlew :get-started-with-exposed:run
1220
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131
implementation(libs.guava)
3232
implementation(libs.exposed.core)
3333
implementation(libs.exposed.jdbc)
34-
implementation("com.h2database:h2:2.2.224")
34+
implementation(libs.h2)
3535
}
3636

3737
// Apply a specific Java toolchain to ease working on different environments.

0 commit comments

Comments
 (0)