Registry / database / mysql-with-kysely

mysql-with-kysely

JSON →
library0.2.2jsnpmunverified

Integration library connecting MySQL (via mysql2) with the Kysely query builder. v0.2.2 released 2023-10, handles MySQL BIGINT as string, provides type-safe query building with TypeScript, and includes a plugin to fix the 'Incorrect arguments to mysqld_stmt_execute' error from MySQL 8.0.22+. Ships TypeScript types, depends on kysely ^0.18.1 and mysql2 ^2.3.3. Compared to raw mysql2, it offers full Kysely integration with subscription to query metrics and mock helpers for testing.

npm install mysql-with-kysely
INSTALL
IMPORT
SIG · MYSQL-WITH-KYSELY
M
mysql-with-kysely
databasejavascriptv0.2.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

connect
import { connect } from 'mysql-with-kysely'
const connect = require('mysql-with-kysely').connect
ESM-only, no default export
queryBuilder
import { queryBuilder } from 'mysql-with-kysely'
import QueryBuilder from 'mysql-with-kysely'
Named function, not a class
WithSchema
import { WithSchema } from 'mysql-with-kysely'
import { withSchema } from 'mysql-with-kysely'
PascalCase type, not camelCase
toInsertableSchema
import { toInsertableSchema } from 'mysql-with-kysely'
Type helper, use with 'type' keyword in ts
createMockMySqlHelper
import { createMockMySqlHelper } from 'mysql-with-kysely/test'
import { createMockMySqlHelper } from 'mysql-with-kysely'
exported from subpath 'mysql-with-kysely/test'

Connects to MySQL, defines typed schema, runs select and insert queries via Kysely, then closes connection.

import { connect, queryBuilder, WithSchema, toFullSelectableSchema, toInsertableSchema } from 'mysql-with-kysely'; type Database = { user: WithSchema<{ id: number; name: string; email: string }> }; type SelectableUser = toFullSelectableSchema<Database>['user']; type InsertableUser = toInsertableSchema<Database>['user']; const { db, close } = connect<Database>({ uri: 'mysql://root:root@localhost:3306/test' }); const qb = queryBuilder<Database>(); async function main() { const users = await db.query(qb.selectFrom('user').selectAll().orderBy('id', 'desc').limit(1)); console.log(users); const value: InsertableUser = { name: 'kanziw', email: 'kanziwoong@gmail.com' }; const { insertId } = await db.execute(qb.insertInto('user').values(value)); console.log(insertId); await close(); } main().catch(console.error);
Debug
Known issues
gotchaRequires peer dependencies kysely@^0.18.1 and mysql2@^2.3.3 to be installed separately
fix
npm install kysely@^0.18.1 mysql2@^2.3.3
affects: >=0.2.0
deprecatedconnect() returns an object with db and close; db is the query executor, not the standard Kysely database instance
fix
Use db.query() and db.execute() instead of db.selectFrom() directly
affects: >=0.1.0
gotchaBIGINT values are cast to string to avoid precision loss
fix
Handle BIGINT columns as strings in TypeScript types
affects: >=0.1.0
breakingtoFullSelectableSchema includes created_at and updated_at fields; use toSelectableSchema if those are not needed
fix
Use toSelectableSchema<Database>['user'] to omit lifecycle fields
affects: >=0.2.0
Errors
Common errors & fixes
Cannot find module 'mysql-with-kysely'
Package not installed or missing
fix
npm install mysql-with-kysely
Incorrect arguments to mysqld_stmt_execute
MySQL 8.0.22+ bug with prepared statements and big integers
fix
Use mysql-with-kysely which includes the OffsetLimitTypeCastingPlugin to fix this
Property 'query' does not exist on type 'Kysely<Database>'
Confusing Kysely's db instance with mysql-with-kysely's db wrapper
fix
Use the db returned from connect(), which has query() and execute() methods, not the standard Kysely instance
Type 'string' is not assignable to type 'number'
BIGINT is returned as string; TypeScript expects number
fix
Use WithSchema which handles BIGINT as string, or cast with Number()
Upgrade
Version history
0.2.2latest on npm
Audit
Dependencies
kyselyrequiredpeer dependency for query building
mysql2requiredpeer dependency for MySQL connection
Agent activity
7 hits · last 30 days
node
6
Resources