Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

運用 Exposed 管理及操作資料庫

218 views

Published on

於 GDG DevFest Kaohsiung 分享如何用 Exposed SQL 函式庫操作資料庫,以及如何與 Ktor 框架整合。

Published in: Internet
  • Be the first to comment

運用 Exposed 管理及操作資料庫

  1. 1. Exposed — (Shengyou Fan) GDG DevFest Kaohsiung 2019/12/07
  2. 2. — • Developer Advocate • Backend Developer • Open Source Community Organizer
  3. 3. Kotlin https://youtu.be/hg8oYJ8Ez8s • General-purpose • Static typing • OOP + FP • Developed by JetBrains • Open Source (Apache 2.0) https://kotlinlang.org/ Kotlin —
  4. 4. Kotlin — Browser Kotlin/JS Server Kotlin/JVM iOS Kotlin/Native Android Kotlin/JVM
  5. 5. Exposed SQL — https://github.com/JetBrains/Exposed • SQL • 100% Kotlin • • Developed by JetBrains • Open Source (Apache 2.0)
  6. 6. — PostgreSQL SQLite Oracle SQL Server
  7. 7. — DSL DAO typesafe SQL wrapping DSL lightweight data access objects
  8. 8. Exposed — // gradle.properties exposed_version=0.17.7 mysql_connector_version=5.1.46 // build.gradle dependencies { //... implementation "org.jetbrains.exposed:exposed:$ver" implementation "mysql:mysql-connector-java:$ver" }
  9. 9. MySQL — version: "2" services: database: image: mysql:5.7 ports: - "...:3306" environment: MYSQL_ROOT_PASSWORD: ... MYSQL_DATABASE: ... command: ...
  10. 10. — object Books : Table() { val id: Column<Int> = integer("id") .autoIncrement() .primaryKey() val title: Column<String> = varchar("title", 255) val isbn: Column<String> = varchar("isbn", 13) val publishedAt: Column<DateTime> = datetime("...") } object Books : IntIdTable() { val title = varchar("title", 255) val isbn = varchar("isbn", 13) val publishedAt = datetime("...") }
  11. 11. DAO — class Book(id: EntityID<Int>) : IntEntity(id) { companion object : IntEntityClass<Book>(Books) var title by Books.title var isbn by Books.isbn var publishedAt by Books.publishedAt }
  12. 12. — Database.connect( url = "...", driver = "com.mysql.jdbc.Driver", user = "...", password = "..." )
  13. 13. — transaction { // ... SchemaUtils.drop(…) // ... SchemaUtils.create(...) }
  14. 14. JavaFaker — // gradle.properties javafaker_version=1.0.1 // build.gradle dependencies { //... implementation "com.github.javafaker:javafaker:$ver" }
  15. 15. // create Books.insert { it[...] = ... } // read Books.selectAll() Books.select { ... } .orderBy(... to SortOrder.ASC) // update Books.update({ ... }) { it[...] = ... } // delete Books.deleteWhere { ... } CRUD (DSL) —
  16. 16. // create Book.new { title = ... } // read Book.all() Book.find { ... } .sortedBy { it.id } // update val book = Book.findById(...) if (book != null) { book.title = ... } // delete val book = Book.findById(1) book?.delete() CRUD (DAO) —
  17. 17. object Books : IntIdTable() { val title = varchar("title", 255) val isbn = varchar("isbn", 13) val publishedAt = datetime(“published_at") val author = reference("author", Authors) } object Authors: IntIdTable() { val name = varchar("name", 255) } (Table) —
  18. 18. class Book(id: EntityID<Int>) : IntEntity(id) { companion object : IntEntityClass<Book>(Books) var title by Books.title var isbn by Books.isbn var publishedAt by Books.publishedAt var author by Author referencedOn Books.author } class Author(id: EntityID<Int>): IntEntity(id) { companion object: IntEntityClass<Author>(Authors) var name by Authors.name val books by Book referrersOn Books.author } (Entity) —
  19. 19. Book.all().map { println("(${it.title} by ${it.author.name}") } Author.all().map { author -> author.books.forEach { println("${author.name} wrote ${it.title}") } } —
  20. 20. Ktor — https://ktor.io/ • Web Framework • Asynchronous • Servers + Clients • Developed by JetBrains • Open Source (Apache 2.0)
  21. 21. JSON API — // define respond class data class BookRespond( val title:String, val author: String, val isbn: String, val publishedAt: String ) // define api get("/api/v1/books") { val books = transaction { Book.all().sortedByDescending { it.id }.map { BookRespond( title = it.title, isbn = it.isbn, publishedAt = ..., author = it.author.name ) } } call.respond(mapOf("books" to books)) }
  22. 22. — • Exposed • DSL • DAO • Migration & Seeding • • Ktor JSON API
  23. 23. — https://github.com/shengyou/exposed-sample
  24. 24. ktor.guide Ktor —
  25. 25. (Shengyou) shengyou.fan@jetbrains.com Q&A — Exposed

×