import{toInsertableSchema,toFullSelectableSchema,WithSchema,connect,queryBuilder}from'mysql-with-kysely'importtype{User}from'./model'// for query buildertypeDatabase={user: WithSchema<User>}// for your codetypeSelectableSchema=toFullSelectableSchema<Database>typeInsertableSchema=toInsertableSchema<Database>const{ db, close }=connect<Database>({uri: 'mysql://root:root@localhost:3306/test'});// collect your metricsdb.subscribe(({ sql, normalizedSql, durationMs, occurredAt, parameters })=>{})// your query builderconstqb=queryBuilder<Database>()// write your codes type safely// type of users is `Array<SelectableSchema['user']>`// if you don't need created_at & updated_at, use toSelectableSchema instead of toFullSelectableSchemaconstusers=awaitdb.query(qb.selectFrom('user').selectAll().orderBy('id','desc').limit(1),)// type of value is `InsertableSchema['user']`constvalue={name: 'kanziw',email: '[email protected]'}const{ insertId }=awaitdb.execute(qb.insertInto('user').values(value),)// close MySQL connectionawaitclose()
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