{"id":20741087,"url":"https://github.com/com-lihaoyi/scalasql","last_synced_at":"2025-04-12T20:45:11.701Z","repository":{"id":214912111,"uuid":"698693054","full_name":"com-lihaoyi/scalasql","owner":"com-lihaoyi","description":"Scala ORM to query SQL databases from Scala via concise, type-safe, and familiar case classes and collection operations. Connects to Postgres, MySql, H2, and Sqlite out of the box","archived":false,"fork":false,"pushed_at":"2025-04-02T17:22:07.000Z","size":2125,"stargazers_count":216,"open_issues_count":12,"forks_count":30,"subscribers_count":15,"default_branch":"main","last_synced_at":"2025-04-04T01:45:08.555Z","etag":null,"topics":["database","h2","mysql","postgresql","query-builder","scala","sql","sqlite"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/com-lihaoyi.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-30T17:10:58.000Z","updated_at":"2025-04-03T04:57:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"6a707955-35ad-4dee-971e-fdc63cdf959e","html_url":"https://github.com/com-lihaoyi/scalasql","commit_stats":null,"previous_names":["com-lihaoyi/scalasql"],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/com-lihaoyi%2Fscalasql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/com-lihaoyi%2Fscalasql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/com-lihaoyi%2Fscalasql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/com-lihaoyi%2Fscalasql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/com-lihaoyi","download_url":"https://codeload.github.com/com-lihaoyi/scalasql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631688,"owners_count":21136556,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","h2","mysql","postgresql","query-builder","scala","sql","sqlite"],"created_at":"2024-11-17T06:33:35.372Z","updated_at":"2025-04-12T20:45:11.695Z","avatar_url":"https://github.com/com-lihaoyi.png","language":"Scala","funding_links":[],"categories":["数据库开发"],"sub_categories":[],"readme":"# ScalaSql\n\nScalaSql is a Scala ORM library that allows type-safe low-boilerplate querying of\nSQL databases, using \"standard\" Scala collections operations running against\ntyped `Table` descriptions.\n\n```scala\nimport scalasql._, SqliteDialect._\n\n// Define your table model classes\ncase class City[T[_]](\n    id: T[Int],\n    name: T[String],\n    countryCode: T[String],\n    district: T[String],\n    population: T[Long]\n)\nobject City extends Table[City]\n\n// Connect to your database (example uses in-memory sqlite, org.xerial:sqlite-jdbc:3.43.0.0)\nval dataSource = new org.sqlite.SQLiteDataSource()\ndataSource.setUrl(s\"jdbc:sqlite:file.db\")\nlazy val dbClient = new scalasql.DbClient.DataSource(\n  dataSource,\n  config = new scalasql.Config {\n    override def nameMapper(v: String) = v.toLowerCase() // Override default snake_case mapper\n    override def logSql(sql: String, file: String, line: Int) = println(s\"$file:$line $sql\")\n  }\n)\n\ndbClient.transaction{ db =\u003e\n\n  // Initialize database table schema and data\n  db.updateRaw(os.read(os.Path(\"scalasql/test/resources/world-schema.sql\", os.pwd)))\n  db.updateRaw(os.read(os.Path(\"scalasql/test/resources/world-data.sql\", os.pwd)))\n\n  // Adding up population of all cities in China\n  val citiesPop = db.run(City.select.filter(_.countryCode === \"CHN\").map(_.population).sum)\n  // SELECT SUM(city0.population) AS res FROM city city0 WHERE city0.countrycode = ?\n\n  println(citiesPop)\n  // 175953614\n\n  // Finding the 5-8th largest cities by population\n  val fewLargestCities = db.run(\n    City.select\n        .sortBy(_.population).desc\n        .drop(5).take(3)\n        .map(c =\u003e (c.name, c.population))\n  )\n  // SELECT city0.name AS res__0, city0.population AS res__1\n  // FROM city city0 ORDER BY res__1 DESC LIMIT ? OFFSET ?\n\n  println(fewLargestCities)\n  // Seq((Karachi, 9269265), (Istanbul, 8787958), (Ciudad de México, 8591309))\n}\n```\n\nScalaSql supports database connections to PostgreSQL, MySQL, Sqlite, and H2 databases. \nSupport for additional databases can be easily added.\n\nScalaSql is a relatively new library, so please try it out, but be aware you may hit bugs\nor missing features! Please open [Discussions](https://github.com/com-lihaoyi/scalasql/discussions)\nfor any questions, file [Issues](https://github.com/com-lihaoyi/scalasql/issues) for any \nbugs you hit, or send [Pull Requests](https://github.com/com-lihaoyi/scalasql/pulls) if\nyou are able to investigate and fix them!\n\n\n## Getting Started\n\nTo get started with ScalaSql, add it to your `build.sc` file as follows:\n\n```scala\nivy\"com.lihaoyi::scalasql:0.1.19\"\n```\n\nScalaSql supports Scala 2.13.x and \u003e=3.6.2\n\n## Documentation\n\n* ScalaSql Quickstart Examples: self-contained files showing how to set up ScalaSql to\n  connect your Scala code to a variety of supported databases and perform simple DDL and \n  `SELECT`/`INSERT`/`UPDATE`/`DELETE` operations:\n    * [Postgres](scalasql/test/src/example/PostgresExample.scala)\n    * [MySql](scalasql/test/src/example/MySqlExample.scala)\n    * [Sqlite](scalasql/test/src/example/SqliteExample.scala)\n    * [H2](scalasql/test/src/example/H2Example.scala)\n    * [HikariCP](scalasql/test/src/example/HikariCpExample.scala) (and other connection pools)\n\n* [ScalaSql Tutorial](docs/tutorial.md): a structured walkthrough of how to use ScalaSql,\n  connecting to a database and writing queries to `SELECT`/`INSERT`/`UPDATE`/`DELETE`\n  against it to perform useful work. Ideal for newcomers to work through from top\n  to bottom when getting started with the library.\n\n* [ScalaSql Cheat Sheet](docs/cheatsheet.md): a compact summary of the main features\n  of ScalaSql and the syntax to make use of them.\n\n* [ScalaSql Reference](docs/reference.md): a detailed listing of ScalaSql functionality,\n  comprehensively covering everything that ScalaSql supports, in a single easily searchable\n  place. Ideal for looking up exactly methods/operators ScalaSql supports, looking up\n  how ScalaSql code translates to SQL, or looking up SQL syntax to find out how to\n  express it using ScalaSql. Useful subsections include:\n  * [DbApi](docs/reference.md#dbapi), covering the main methods you can all\n    to execute queries\n  * [Transaction](docs/reference.md#transaction), covering usage of transactions\n    and savepoints\n  * [Select](docs/reference.md#select), [Insert](docs/reference.md#insert), \n    [Update](docs/reference.md#update), [Delete](docs/reference.md#delete):\n    covering operations on the primary queries you are likely to use\n  * [Join](docs/reference.md#join), covering different kinds of joins\n  * [Returning](docs/reference.md#returning), [On Conflict](docs/reference.md#onconflict):\n    covering these modifiers on `INSERT` and `UPDATE` for the databases that support them\n  * [Expression Operations](docs/reference.md#exprops), covering the different\n    types of `Expr[T]` values and the different operations you can do on each one\n  * [Option Operations](docs/reference.md#optional), operations on `Expr[Option[T]`\n  * [Window Functions](docs/reference.md#windowfunctions), \n    [With-Clauses/Common-Table-Expressions](docs/reference.md#withcte)\n  * [Postgres](docs/reference.md#postgresdialect), [MySql](docs/reference.md#mysqldialect),\n    [Sqlite](docs/reference.md#sqlitedialect), [H2](docs/reference.md#h2dialect) Dialects:\n    operations that are specific to each database that may not be generally applicable\n\n* [ScalaSql Design](docs/design.md): discusses the design of the ScalaSql library, why it\n  is built the way it is, what tradeoffs it makes, and how it compares to other \n  common Scala database query libraries. Ideal for contributors who want to understand\n  the structure of the ScalaSql codebase, or for advanced users who may need to\n  understand enough to extend ScalaSql with custom functionality.\n\n* [Developer Docs](docs/developer.md): things you should read if you want to make changes\n  to the `com-lihaoyi/scalasql` codebase\n\n## Changelog\n\n### 0.1.19\n\n*  Escape table in returning clause [#80](https://github.com/com-lihaoyi/scalasql/pull/80)\n\n### 0.1.18\n\n*  Fix for escaped update with where clause [#79](https://github.com/com-lihaoyi/scalasql/pull/79)\n\n### 0.1.17\n\n* Add `filterIf` and `filterOpt` utility methds to Select [#77](https://github.com/com-lihaoyi/scalasql/pull/77)\n* Escape table names [#73](https://github.com/com-lihaoyi/scalasql/pull/73)\n\n### 0.1.16\n\n* Add TransactionListener interface  [#71](https://github.com/com-lihaoyi/scalasql/pull/71)\n* JoinNullable deserialisation fix [#70](https://github.com/com-lihaoyi/scalasql/pull/70)\n* Insert.select on conflict [#68](https://github.com/com-lihaoyi/scalasql/pull/68)\n* Add OnConflict to InsertValues [#61](https://github.com/com-lihaoyi/scalasql/pull/61)\n\n### 0.1.15\n\n* Support `schemaName` in non-SELECT queries [#57](https://github.com/com-lihaoyi/scalasql/pull/57)\n\n### 0.1.14\n\n* Remove uneeded Numeric constraint from ordering-related functions [#47](https://github.com/com-lihaoyi/scalasql/pull/47)\n* Add `SELECT FOR UPDATE` support for Postgres and MySQL [#45](https://github.com/com-lihaoyi/scalasql/pull/45)\n\n### 0.1.11\n\n* Support null value passthrough for all `TypeMappers` [#39](https://github.com/com-lihaoyi/scalasql/pull/39)\n* Fix optional null values failing to put [#42](https://github.com/com-lihaoyi/scalasql/pull/42)\n\n### 0.1.10\n\n* Fix a typo in isNormalCharacter [#37](https://github.com/com-lihaoyi/scalasql/pull/37)\n* Instant type mapper now allows NULL values [#38](https://github.com/com-lihaoyi/scalasql/pull/38)\n\n### 0.1.9\n\n* Add support for Float data type [#32](https://github.com/com-lihaoyi/scalasql/pull/32)\n\n### 0.1.8\n\n* Introduce `TypeMapper#bimap` to make creating related `TypeMapper`s easier [#27](https://github.com/com-lihaoyi/scalasql/pull/27)\n\n### 0.1.7\n\n* Add support for columns of type `java.util.Date` [#24](https://github.com/com-lihaoyi/scalasql/pull/24)\n\n### 0.1.6\n\n* Add support for non-default database schemas in Postgres [#23](https://github.com/com-lihaoyi/scalasql/pull/23)\n\n### 0.1.5\n\n* Properly pass ON CONFLICT column names through `columnNameMapper` [#19](https://github.com/com-lihaoyi/scalasql/pull/19)\n\n### 0.1.4\n\n* Second attempt at fixing invalid version of scala-reflect dependency\n\n### 0.1.3\n\n* Support for Scala 3.4.2 and [#11](https://github.com/com-lihaoyi/scalasql/pull/11)\n\n### 0.1.2\n\n* Support `.getGeneratedKeys[R]` [#9](https://github.com/com-lihaoyi/scalasql/pull/9)\n\n### 0.1.1\n\n* Fix invalid version of scala-reflect dependency\n\n### 0.1.0\n\n* First release!\n\n# TODO\n\n* JSON columns\n* Add datetime functions\n* Make `implicit ctx =\u003e` for defining `sql\"...\"` snippets optional\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcom-lihaoyi%2Fscalasql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcom-lihaoyi%2Fscalasql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcom-lihaoyi%2Fscalasql/lists"}