{"id":13405808,"url":"https://github.com/diesel-rs/diesel","last_synced_at":"2025-05-12T18:13:05.620Z","repository":{"id":38431153,"uuid":"41609775","full_name":"diesel-rs/diesel","owner":"diesel-rs","description":"A safe, extensible ORM and Query Builder for Rust","archived":false,"fork":false,"pushed_at":"2025-04-22T08:50:49.000Z","size":80469,"stargazers_count":13293,"open_issues_count":152,"forks_count":1123,"subscribers_count":110,"default_branch":"master","last_synced_at":"2025-04-23T15:50:08.019Z","etag":null,"topics":["mysql","orm","postgresql","query-builder","rust","sqlite"],"latest_commit_sha":null,"homepage":"https://diesel.rs","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diesel-rs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE-APACHE","code_of_conduct":"code_of_conduct.md","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,"zenodo":null},"funding":{"github":["sgrif","weiznich"]}},"created_at":"2015-08-29T22:51:00.000Z","updated_at":"2025-04-23T06:53:59.000Z","dependencies_parsed_at":"2023-09-28T17:35:12.057Z","dependency_job_id":"f4d186ba-1984-47fd-af3d-e6d45a2316a7","html_url":"https://github.com/diesel-rs/diesel","commit_stats":{"total_commits":4725,"total_committers":412,"mean_commits":"11.468446601941748","dds":0.7037037037037037,"last_synced_commit":"97898ec69c4fe642844511fbfbdf7d12835a3c4d"},"previous_names":[],"tags_count":90,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diesel-rs%2Fdiesel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diesel-rs%2Fdiesel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diesel-rs%2Fdiesel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diesel-rs%2Fdiesel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diesel-rs","download_url":"https://codeload.github.com/diesel-rs/diesel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252522572,"owners_count":21761753,"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":["mysql","orm","postgresql","query-builder","rust","sqlite"],"created_at":"2024-07-30T19:02:12.401Z","updated_at":"2025-05-05T15:23:44.612Z","avatar_url":"https://github.com/diesel-rs.png","language":"Rust","funding_links":["https://github.com/sponsors/sgrif","https://github.com/sponsors/weiznich"],"categories":["Rust","Libraries","wrapper/ORM","库 Libraries","ORMs \u0026 Query Builders","Databases","库","其他__大数据","sqlite","Database libraries","mysql","Uncategorized","🌐 Web Development - Frontend"],"sub_categories":["Database","数据库 Database","Rust","ORM","数据库","网络服务_其他","Uncategorized"],"readme":"[![diesel logo](https://diesel.rs/assets/images/diesel_logo_stacked_black.png)](https://diesel.rs)\n\n# A safe, extensible ORM and Query Builder for Rust\n\n[![Build Status](https://github.com/diesel-rs/diesel/workflows/CI%20Tests/badge.svg)](https://github.com/diesel-rs/diesel/actions?query=workflow%3A%22CI+Tests%22+branch%3Amaster)\n[![Crates.io](https://img.shields.io/crates/v/diesel.svg)](https://crates.io/crates/diesel)\n\nAPI Documentation: [latest release](https://docs.rs/diesel) – [master branch](https://docs.diesel.rs/master/diesel/index.html)\n\n[Homepage](https://diesel.rs)\n\nDiesel gets rid of the boilerplate for database interaction and eliminates\nruntime errors without sacrificing performance. It takes full advantage of\nRust's type system to create a low overhead query builder that \"feels like\nRust.\"\n\nSupported databases:\n1. [PostgreSQL](https://docs.diesel.rs/master/diesel/pg/index.html)\n2. [MySQL](https://docs.diesel.rs/master/diesel/mysql/index.html)\n3. [SQLite](https://docs.diesel.rs/master/diesel/sqlite/index.html)\n\nYou can configure the database backend in `Cargo.toml`:\n\n```toml\n[dependencies]\ndiesel = { version = \"\u003cversion\u003e\", features = [\"\u003cpostgres|mysql|sqlite\u003e\"] }\n```\n\n## Getting Started\n\nFind our extensive Getting Started tutorial at\n[https://diesel.rs/guides/getting-started](https://diesel.rs/guides/getting-started).\nGuides on more specific features are coming soon.\n\n## Getting help\n\nIf you run into problems, you can come ask for help at in our [GitHub Discussions](https://github.com/diesel-rs/diesel/discussions) forum. \nThis is also the right place to propose new features or show your applications.\n\n## Usage\n\n### Simple queries\n\nSimple queries are a complete breeze. Loading all users from a database:\n\n```rust\nusers::table.load(\u0026mut connection)\n```\n\nExecuted SQL:\n\n```sql\nSELECT * FROM users;\n```\n\nLoading all the posts for a user:\n\n``` rust\nPost::belonging_to(user).load(\u0026mut connection)\n```\n\nExecuted SQL:\n\n```sql\nSELECT * FROM posts WHERE user_id = 1;\n```\n\n### Complex queries\n\nDiesel's powerful query builder helps you construct queries as simple or complex as\nyou need, at zero cost.\n\n```rust\nlet versions = Version::belonging_to(krate)\n  .select(id)\n  .order(num.desc())\n  .limit(5);\nlet downloads = version_downloads\n  .filter(date.gt(now - 90.days()))\n  .filter(version_id.eq_any(versions))\n  .order(date)\n  .load::\u003cDownload\u003e(\u0026mut conn)?;\n```\n\nExecuted SQL:\n\n```sql\nSELECT version_downloads.*\n  WHERE date \u003e (NOW() - '90 days')\n    AND version_id = ANY(\n      SELECT id FROM versions\n        WHERE crate_id = 1\n        ORDER BY num DESC\n        LIMIT 5\n    )\n  ORDER BY date\n```\n\n### Less boilerplate\n\nDiesel codegen generates boilerplate for you. It lets you focus on your business logic, not mapping to and from SQL rows.\n\nThat means you can write this:\n\n```rust\n#[derive(Queryable, Selectable)]\n#[diesel(table_name = downloads)]\npub struct Download {\n    id: i32,\n    version_id: i32,\n    downloads: i32,\n    counted: i32,\n    date: SystemTime,\n}\n```\n\nInstead of this without Diesel:\n\n```rust\npub struct Download {\n    id: i32,\n    version_id: i32,\n    downloads: i32,\n    counted: i32,\n    date: SystemTime,\n}\n\nimpl Download {\n    fn from_row(row: \u0026Row) -\u003e Download {\n        Download {\n            id: row.get(\"id\"),\n            version_id: row.get(\"version_id\"),\n            downloads: row.get(\"downloads\"),\n            counted: row.get(\"counted\"),\n            date: row.get(\"date\"),\n        }\n    }\n}\n```\n\n### Inserting data\n\nIt's not just about reading data. Diesel makes it easy to use structs for new records.\n\n```rust\n#[derive(Insertable)]\n#[diesel(table_name = users)]\nstruct NewUser\u003c'a\u003e {\n    name: \u0026'a str,\n    hair_color: Option\u003c\u0026'a str\u003e,\n}\n\nlet new_users = vec![\n    NewUser { name: \"Sean\", hair_color: Some(\"Black\") },\n    NewUser { name: \"Gordon\", hair_color: None },\n];\n\ninsert_into(users)\n    .values(\u0026new_users)\n    .execute(\u0026mut connection);\n```\n\nExecuted SQL:\n\n```sql\nINSERT INTO users (name, hair_color) VALUES\n  ('Sean', 'Black'),\n  ('Gordon', DEFAULT)\n```\n\nIf you need data from the rows you inserted, just change `execute` to `get_result` or `get_results`. Diesel will take care of the rest.\n\n```rust\nlet new_users = vec![\n    NewUser { name: \"Sean\", hair_color: Some(\"Black\") },\n    NewUser { name: \"Gordon\", hair_color: None },\n];\n\nlet inserted_users = insert_into(users)\n    .values(\u0026new_users)\n    .get_results::\u003cUser\u003e(\u0026mut connection);\n```\n\nExecuted SQL:\n\n```sql\nINSERT INTO users (name, hair_color) VALUES\n  ('Sean', 'Black'),\n  ('Gordon', DEFAULT)\n  RETURNING *\n```\n\n### Updating data\n\nDiesel's codegen can generate several ways to update a row, letting you encapsulate your logic in the way that makes sense for your app.\n\nModifying a struct:\n\n```rust\npost.published = true;\npost.save_changes(\u0026mut connection);\n```\n\nOne-off batch changes:\n\n```rust\nupdate(users.filter(email.like(\"%@spammer.com\")))\n    .set(banned.eq(true))\n    .execute(\u0026mut connection)\n```\n\nUsing a struct for encapsulation:\n\n```rust\nupdate(Settings::belonging_to(current_user))\n    .set(\u0026settings_form)\n    .execute(\u0026mut connection)\n```\n\n### Raw SQL\n\nThere will always be certain queries that are just easier to write as raw SQL, or can't be expressed with the query builder. Even in these cases, Diesel provides an easy to use API for writing raw SQL.\n\n```rust\n#[derive(QueryableByName)]\n#[diesel(table_name = users)]\nstruct User {\n    id: i32,\n    name: String,\n    organization_id: i32,\n}\n\n// Using `include_str!` allows us to keep the SQL in a\n// separate file, where our editor can give us SQL specific\n// syntax highlighting.\nsql_query(include_str!(\"complex_users_by_organization.sql\"))\n    .bind::\u003cInteger, _\u003e(organization_id)\n    .bind::\u003cBigInt, _\u003e(offset)\n    .bind::\u003cBigInt, _\u003e(limit)\n    .load::\u003cUser\u003e(\u0026mut conn)?;\n```\n\n## Code of conduct\n\nAnyone who interacts with Diesel in any space, including but not limited to\nthis GitHub repository, must follow our [code of conduct](https://github.com/diesel-rs/diesel/blob/master/code_of_conduct.md).\n\n## License\n\nLicensed under either of these:\n\n * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or\n   https://www.apache.org/licenses/LICENSE-2.0)\n * MIT license ([LICENSE-MIT](LICENSE-MIT) or\n   https://opensource.org/licenses/MIT)\n\n### Contributing\n\nBefore contributing, please read the [contributors guide](https://github.com/diesel-rs/diesel/blob/master/CONTRIBUTING.md)\nfor useful information about setting up Diesel locally, coding style and common abbreviations.\n\nUnless you explicitly state otherwise, any contribution you intentionally submit\nfor inclusion in the work, as defined in the Apache-2.0 license, shall be\ndual-licensed as above, without any additional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiesel-rs%2Fdiesel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiesel-rs%2Fdiesel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiesel-rs%2Fdiesel/lists"}