1- import { MySQLPromisePool } from " @fastify/mysql" ;
2- import { FastifyInstance } from " fastify" ;
3- import fp from " fastify-plugin" ;
4- import { RowDataPacket , ResultSetHeader } from " mysql2" ;
1+ import { MySQLPromisePool } from ' @fastify/mysql'
2+ import { FastifyInstance } from ' fastify'
3+ import fp from ' fastify-plugin'
4+ import { RowDataPacket , ResultSetHeader } from ' mysql2'
55
6- declare module " fastify" {
6+ declare module ' fastify' {
77 export interface FastifyInstance {
88 repository : Repository ;
99 }
1010}
1111
12- export type Repository = MySQLPromisePool & ReturnType < typeof createRepository > ;
12+ export type Repository = MySQLPromisePool & ReturnType < typeof createRepository >
1313
14- type QuerySeparator = 'AND' | ',' ;
14+ type QuerySeparator = 'AND' | ','
1515
1616type QueryOptions = {
1717 select ?: string ;
1818 where ?: Record < string , any > ;
19- } ;
19+ }
2020
2121type WriteOptions = {
2222 data : Record < string , any > ;
2323 where ?: Record < string , any > ;
24- } ;
24+ }
2525
26- function createRepository ( fastify : FastifyInstance ) {
26+ function createRepository ( fastify : FastifyInstance ) {
2727 const processAssignmentRecord = ( record : Record < string , any > , separator : QuerySeparator ) => {
28- const keys = Object . keys ( record ) ;
29- const values = Object . values ( record ) ;
30- const clause = keys . map ( ( key ) => `${ key } = ?` ) . join ( ` ${ separator } ` ) ;
28+ const keys = Object . keys ( record )
29+ const values = Object . values ( record )
30+ const clause = keys . map ( ( key ) => `${ key } = ?` ) . join ( ` ${ separator } ` )
3131
32- return [ clause , values ] as const ;
33- } ;
32+ return [ clause , values ] as const
33+ }
3434
3535 const repository = {
3636 ...fastify . mysql ,
3737 find : async < T > ( table : string , opts : QueryOptions ) : Promise < T | null > => {
38- const { select = '*' , where = { 1 : 1 } } = opts ;
39- const [ clause , values ] = processAssignmentRecord ( where , 'AND' ) ;
38+ const { select = '*' , where = { 1 : 1 } } = opts
39+ const [ clause , values ] = processAssignmentRecord ( where , 'AND' )
4040
41- const query = `SELECT ${ select } FROM ${ table } WHERE ${ clause } LIMIT 1` ;
42- const [ rows ] = await fastify . mysql . query < RowDataPacket [ ] > ( query , values ) ;
41+ const query = `SELECT ${ select } FROM ${ table } WHERE ${ clause } LIMIT 1`
42+ const [ rows ] = await fastify . mysql . query < RowDataPacket [ ] > ( query , values )
4343 if ( rows . length < 1 ) {
44- return null ;
44+ return null
4545 }
4646
47- return rows [ 0 ] as T ;
47+ return rows [ 0 ] as T
4848 } ,
4949
5050 findMany : async < T > ( table : string , opts : QueryOptions = { } ) : Promise < T [ ] > => {
51- const { select = '*' , where = { 1 : 1 } } = opts ;
52- const [ clause , values ] = processAssignmentRecord ( where , 'AND' ) ;
51+ const { select = '*' , where = { 1 : 1 } } = opts
52+ const [ clause , values ] = processAssignmentRecord ( where , 'AND' )
5353
54- const query = `SELECT ${ select } FROM ${ table } WHERE ${ clause } ` ;
55- const [ rows ] = await fastify . mysql . query < RowDataPacket [ ] > ( query , values ) ;
54+ const query = `SELECT ${ select } FROM ${ table } WHERE ${ clause } `
55+ const [ rows ] = await fastify . mysql . query < RowDataPacket [ ] > ( query , values )
5656
57- return rows as T [ ] ;
57+ return rows as T [ ]
5858 } ,
5959
6060 create : async ( table : string , opts : WriteOptions ) : Promise < number > => {
61- const { data } = opts ;
62- const columns = Object . keys ( data ) . join ( ', ' ) ;
63- const placeholders = Object . keys ( data ) . map ( ( ) => '?' ) . join ( ', ' ) ;
64- const values = Object . values ( data ) ;
61+ const { data } = opts
62+ const columns = Object . keys ( data ) . join ( ', ' )
63+ const placeholders = Object . keys ( data ) . map ( ( ) => '?' ) . join ( ', ' )
64+ const values = Object . values ( data )
6565
66- const query = `INSERT INTO ${ table } (${ columns } ) VALUES (${ placeholders } )` ;
67- const [ result ] = await fastify . mysql . query < ResultSetHeader > ( query , values ) ;
66+ const query = `INSERT INTO ${ table } (${ columns } ) VALUES (${ placeholders } )`
67+ const [ result ] = await fastify . mysql . query < ResultSetHeader > ( query , values )
6868
69- return result . insertId ;
69+ return result . insertId
7070 } ,
7171
7272 update : async ( table : string , opts : WriteOptions ) : Promise < number > => {
73- const { data, where = { } } = opts ;
74- const [ dataClause , dataValues ] = processAssignmentRecord ( data , ',' ) ;
75- const [ whereClause , whereValues ] = processAssignmentRecord ( where , 'AND' ) ;
73+ const { data, where = { } } = opts
74+ const [ dataClause , dataValues ] = processAssignmentRecord ( data , ',' )
75+ const [ whereClause , whereValues ] = processAssignmentRecord ( where , 'AND' )
7676
77- const query = `UPDATE ${ table } SET ${ dataClause } WHERE ${ whereClause } ` ;
78- const [ result ] = await fastify . mysql . query < ResultSetHeader > ( query , [ ...dataValues , ...whereValues ] ) ;
77+ const query = `UPDATE ${ table } SET ${ dataClause } WHERE ${ whereClause } `
78+ const [ result ] = await fastify . mysql . query < ResultSetHeader > ( query , [ ...dataValues , ...whereValues ] )
7979
80- return result . affectedRows ;
80+ return result . affectedRows
8181 } ,
8282
8383 delete : async ( table : string , where : Record < string , any > ) : Promise < number > => {
84- const [ clause , values ] = processAssignmentRecord ( where , 'AND' ) ;
84+ const [ clause , values ] = processAssignmentRecord ( where , 'AND' )
8585
86- const query = `DELETE FROM ${ table } WHERE ${ clause } ` ;
87- const [ result ] = await fastify . mysql . query < ResultSetHeader > ( query , values ) ;
86+ const query = `DELETE FROM ${ table } WHERE ${ clause } `
87+ const [ result ] = await fastify . mysql . query < ResultSetHeader > ( query , values )
8888
89- return result . affectedRows ;
89+ return result . affectedRows
9090 }
91- } ;
91+ }
9292
93- return repository ;
93+ return repository
9494}
9595
9696/**
@@ -101,9 +101,9 @@ function createRepository(fastify: FastifyInstance) {
101101 */
102102export default fp (
103103 async function ( fastify ) {
104- fastify . decorate ( " repository" , createRepository ( fastify ) ) ;
104+ fastify . decorate ( ' repository' , createRepository ( fastify ) )
105105 // You should name your plugins if you want to avoid name collisions
106106 // and/or to perform dependency checks.
107107 } ,
108- { name : " repository" , dependencies : [ 'mysql' ] }
109- ) ;
108+ { name : ' repository' , dependencies : [ 'mysql' ] }
109+ )
0 commit comments