{"id":20633818,"url":"https://github.com/kanziw/mysql-with-kysely","last_synced_at":"2026-06-20T23:31:10.264Z","repository":{"id":38846702,"uuid":"484373773","full_name":"kanziw/mysql-with-kysely","owner":"kanziw","description":"mysql2 driver wrapper with kysely","archived":false,"fork":false,"pushed_at":"2022-06-02T06:16:00.000Z","size":40927,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-10T01:25:20.402Z","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/kanziw.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":".github/CODEOWNERS","security":null,"support":null}},"created_at":"2022-04-22T09:27:18.000Z","updated_at":"2023-07-18T08:46:08.000Z","dependencies_parsed_at":"2022-08-26T20:00:53.241Z","dependency_job_id":null,"html_url":"https://github.com/kanziw/mysql-with-kysely","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kanziw/mysql-with-kysely","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanziw%2Fmysql-with-kysely","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanziw%2Fmysql-with-kysely/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanziw%2Fmysql-with-kysely/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanziw%2Fmysql-with-kysely/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kanziw","download_url":"https://codeload.github.com/kanziw/mysql-with-kysely/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanziw%2Fmysql-with-kysely/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34589204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-16T14:22:26.421Z","updated_at":"2026-06-20T23:31:10.245Z","avatar_url":"https://github.com/kanziw.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mysql-with-kysely\n\n![CI](https://github.com/kanziw/mysql-with-kysely/actions/workflows/ci.yml/badge.svg)\n[![codecov](https://codecov.io/gh/kanziw/mysql-with-kysely/branch/main/graph/badge.svg?token=DYH0PQHQ9R)](https://codecov.io/gh/kanziw/mysql-with-kysely)\n[![npm version](https://img.shields.io/npm/v/mysql-with-kysely)](https://www.npmjs.com/package/mysql-with-kysely)\n[![license](https://img.shields.io/npm/l/mysql-with-kysely)](https://www.npmjs.com/package/mysql-with-kysely)\n[![npm downloads](https://img.shields.io/npm/dt/mysql-with-kysely)](https://www.npmjs.com/package/mysql-with-kysely)\n\n- `mysql-with-kysely` use [mysql2](https://github.com/sidorares/node-mysql2) and [kysely](https://github.com/koskimas/kysely)\n- Handle MySQL BIGINT type as string\n- `Incorrect arguments to mysqld_stmt_execute` error safe!\n  - Since MySQL 8.0.22, mysql2 driver has `Incorrect arguments to mysqld_stmt_execute` [issue](https://github.com/sidorares/node-mysql2/issues/1239)\n  - [OffsetLimitTypeCastingPlugin](./src/kyselyPlugins/offsetLimitTypeCastingPlugin.ts) resolves it\n\n\n## Installation\n\n```zsh\n$ yarn add mysql-with-kysely\n```\n\n\n## Usages\n\n```ts\nimport { toInsertableSchema, toFullSelectableSchema, WithSchema, connect, queryBuilder } from 'mysql-with-kysely'\nimport type { User } from './model'\n\n// for query builder\ntype Database = {\n  user: WithSchema\u003cUser\u003e\n}\n\n// for your code\ntype SelectableSchema = toFullSelectableSchema\u003cDatabase\u003e\ntype InsertableSchema = toInsertableSchema\u003cDatabase\u003e\n\nconst { db, close } = connect\u003cDatabase\u003e({ uri: 'mysql://root:root@localhost:3306/test' });\n\n// collect your metrics\ndb.subscribe(({ sql, normalizedSql, durationMs, occurredAt, parameters }) =\u003e {\n})\n\n// your query builder\nconst qb = queryBuilder\u003cDatabase\u003e()\n\n// write your codes type safely\n// type of users is `Array\u003cSelectableSchema['user']\u003e`\n// if you don't need created_at \u0026 updated_at, use toSelectableSchema instead of toFullSelectableSchema\nconst users = await db.query(qb\n  .selectFrom('user')\n  .selectAll()\n  .orderBy('id', 'desc')\n  .limit(1),\n)\n\n// type of value is `InsertableSchema['user']`\nconst value = { name: 'kanziw', email: 'kanziwoong@gmail.com' }     \nconst { insertId } = await db.execute(qb\n  .insertInto('user')\n  .values(value),\n)\n\n// close MySQL connection\nawait close()\n```\n\n\n### Database type\n\n1. `WithPkId`: for auto increment `id` column\n2. `WithDataLifecycleTracker`\n    1. `created_at`: for `DATETIME DEFAULT CURRENT_TIMESTAMP`\n    2. `updated_at`: for `DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`\n3. `WithSchema`: `WithPkId` \u0026 `WithDataLifecycleTracker`\n\n\n## Recommended Usages Personally\n\n1. Write DDL\n    1.  See [ddl.sql](./ddl.sql)\n2. Prepare types\n    1. Apply DDL to MySQL Server using [mysqldef](https://github.com/k0kubun/sqldef)\n    2. Generate model using [sql-ts](https://github.com/rmp135/sql-ts)\n    3. See `db:sync` script in [package.json](./package.json)\n3. Set up your own query builder\n    1. See [mysql.ts](./src/__tests__/fixtures/mysql.ts)\n4. Write your own code!\n    1. See [integration.spec.ts](./src/__tests__/integration.spec.ts)\n5. Write your test code using [createMockMySqlHelper](./src/test/mockMySql.ts)\n    1. See [mockMySql.spec.ts](./src/__tests__/mockMySql.spec.ts)\n6. Subscribe MySQL Metrics\n    1. See [subscribe.spec.ts](./src/__tests__/subscribe.spec.ts)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkanziw%2Fmysql-with-kysely","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkanziw%2Fmysql-with-kysely","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkanziw%2Fmysql-with-kysely/lists"}