{"id":18351286,"url":"https://github.com/rojer95/midway-knex","last_synced_at":"2025-04-10T00:30:13.500Z","repository":{"id":62917152,"uuid":"563180688","full_name":"rojer95/midway-knex","owner":"rojer95","description":"midwayjs knex","archived":false,"fork":false,"pushed_at":"2022-11-08T09:19:09.000Z","size":96,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-16T09:49:04.339Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/rojer95.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}},"created_at":"2022-11-08T04:15:15.000Z","updated_at":"2022-11-08T09:04:36.000Z","dependencies_parsed_at":"2022-11-09T04:15:18.479Z","dependency_job_id":null,"html_url":"https://github.com/rojer95/midway-knex","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rojer95%2Fmidway-knex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rojer95%2Fmidway-knex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rojer95%2Fmidway-knex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rojer95%2Fmidway-knex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rojer95","download_url":"https://codeload.github.com/rojer95/midway-knex/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248134648,"owners_count":21053502,"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":[],"created_at":"2024-11-05T21:30:10.023Z","updated_at":"2025-04-10T00:30:13.461Z","avatar_url":"https://github.com/rojer95.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Knex\n\n本文档介绍如何在 Midway 中使用 Knex。\n\n## 安装依赖\n\n```bash\n$ npm install midway-knex knex\n```\n\n## 安装数据库 Driver\n\n常用数据库驱动如下，选择你对应连接的数据库类型安装：\n\n```bash\n$ npm install pg\n$ npm install pg-native\n$ npm install sqlite3\n$ npm install better-sqlite3\n$ npm install mysql\n$ npm install mysql2\n$ npm install oracledb\n$ npm install tedious\n```\n\n下面的文档，我们将以 `mysql2` 作为示例。\n\n## 启用组件\n\n在 `src/configuration.ts` 文件中启用组件。\n\n```typescript\nimport { Configuration } from \"@midwayjs/decorator\";\nimport { ILifeCycle } from \"@midwayjs/core\";\nimport { join } from \"path\";\nimport * as knex from \"midway-knex\";\n\n@Configuration({\n  imports: [\n    // ...\n    knex,\n  ],\n  importConfigs: [join(__dirname, \"./config\")],\n})\nexport class MainConfiguration implements ILifeCycle {\n  // ...\n}\n```\n\n## 模型定义\n\n### 创建 Model（可选）\n\n```typescript\n// src/model/person.ts\nimport { Table } from \"midway-knex\";\n\n// 由于knex不是ORM，所以这里只需要关心表名称即可\n\n@Table(\"Person\")\nclass Person {\n  name?: number;\n  name?: string;\n}\n```\n\n## 数据源配置\n\n新版本 Midway 启用了 数据源机制 ，在 `src/config.default.ts` 中配置：\n\n```typescript\n// src/config/config.default.ts\n\nexport default {\n  // ...\n  sequelize: {\n    dataSource: {\n      // 第一个数据源，数据源的名字可以完全自定义\n      default: {\n        client: \"mysql2\",\n        connection: {\n          host: \"127.0.0.1\",\n          port: 3306,\n          user: \"root\",\n          password: \"root\",\n          database: \"database\",\n          timezone: \"+08:00\",\n          dateStrings: true,\n          // ...其他配置\n        },\n      },\n\n      // 第二个数据源\n      default2: {\n        // ...\n      },\n    },\n  },\n};\n```\n\n## 使用方法\n\n```typescript\nimport { Provide } from \"@midwayjs/decorator\";\nimport { Person } from \"../model/person\";\nimport { InjectKnex, InjectKnexModel } from \"midway-knex\";\n\n@Provide()\nexport class PersonService {\n  @InjectKnexModel(Person)\n  person: Knex\u003cPerson\u003e;\n\n  @InjectKnex()\n  knex: Knex;\n\n  async createPerson() {\n    return await this.person.insert(\n      {\n        name: \"name\",\n      },\n      \"*\"\n    );\n  }\n\n  /**\n   * 事务操作\n   */\n  async transaction() {\n    return await this.knex.transaction(async (t) =\u003e {\n      const person = await this.person\n        .select()\n        .transacting(t)\n        .forUpdate()\n        .first();\n\n      if (person) {\n        await this.person\n          .update({ name: \"name2\" })\n          .where(\"id\", person.id)\n          .transacting(t);\n      }\n\n      return person;\n    });\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frojer95%2Fmidway-knex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frojer95%2Fmidway-knex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frojer95%2Fmidway-knex/lists"}