You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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: 'kanziwoong@gmail.com'}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