mysql-with-kysely

mysql-with-kysely
use mysql2 and kysely
- Handle MySQL BIGINT type as string
Incorrect arguments to mysqld_stmt_execute
error safe!
- Since MySQL 8.0.22, mysql2 driver has
Incorrect arguments to mysqld_stmt_execute
issue
- LimitCasingPlugin resolves it
Installation
$ yarn add mysql-with-kysely
Usages
import { queryBuilder, WithSchema, SelectableSchema } from 'mysql-with-kysely'
import type { User } from './model'
type Database = {
user: WithSchema<User>
}
type Schema = SelectableSchema<Database>
type InsertValueSchema = InsertableSchema<Database>
const { db, close } = connect<Database>({ uri: 'mysql://root:root@localhost:3306/test' });
db.subscribe(({ sql, normalizedSql, durationMs, occurredAt, parameters }) => {
})
const qb = queryBuilder<Database>()
const users = await db.query(qb
.selectFrom('user')
.selectAll()
.orderBy('id', 'desc')
.limit(1),
)
const value = { name: 'kanziw', email: 'kanziwoong@gmail.com' }
const { insertId } = await db.execute(qb
.insertInto('user')
.values(value),
)
await close()
Database type
WithPkId
: for auto increment id
column
WithDataLifecycleTracker
created_at
: for DATETIME DEFAULT CURRENT_TIMESTAMP
updated_at
: for DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
WithSchema
: WithPkId
& WithDataLifecycleTracker
Recommended Usages Personally
- Write DDL
- Prepare types
- Set up your own query builder
- Write your own code!
- Write your test code using createMockMySqlHelper
- Subscribe MySQL Metrics