@@ -13,15 +13,27 @@ import {
1313} from "../types" ;
1414import { buildWhereClause } from "../utils" ;
1515
16+ interface RepositoryOptions {
17+ logging ?: boolean ;
18+ }
19+
1620export class Repository < T > {
1721 constructor (
1822 protected readonly tableName : string ,
19- protected readonly executor : SqlExecutor
23+ protected readonly executor : SqlExecutor ,
24+ protected options ?: RepositoryOptions
2025 ) { }
2126
2227 async findRow ( where : WhereCondition < T > ) : Promise < T | null > {
2328 const builder = new SelectQueryBuilder < T > ( this . tableName , this . executor ) ;
2429 const result = await builder . where ( where ) . limit ( 1 ) . commit ( ) ;
30+ if ( this . options ?. logging ) {
31+ console . log ( {
32+ sql : builder . build ( ) . sql ,
33+ values : builder . build ( ) . values ,
34+ result : result . rows [ 0 ]
35+ } ) ;
36+ }
2537 return result . rows [ 0 ] ?? null ;
2638 }
2739
@@ -34,9 +46,15 @@ export class Repository<T> {
3446 if ( payload ?. orderBy ) builder . orderBy ( payload . orderBy ) ;
3547 if ( payload ?. limit ) builder . limit ( payload . limit ) ;
3648 if ( payload ?. offset ) builder . offset ( payload . offset ) ;
37- // console.log(builder.build().sql);
38- // console.log(builder.build().values);
3949 const result = await builder . commit ( ) ;
50+
51+ if ( this . options ?. logging ) {
52+ console . log ( {
53+ sql : builder . build ( ) . sql ,
54+ values : builder . build ( ) . values ,
55+ result : result . rows [ 0 ]
56+ } ) ;
57+ }
4058 return result . rows ;
4159 }
4260
@@ -76,6 +94,13 @@ export class Repository<T> {
7694 ) : Promise < T > {
7795 const builder = new InsertQueryBuilder < T > ( this . tableName , this . executor ) ;
7896 const result = await builder . values ( data ) . returning ( returning ) . commit ( ) ;
97+ if ( this . options ?. logging ) {
98+ console . log ( {
99+ sql : builder . build ( ) . sql ,
100+ values : builder . build ( ) . values ,
101+ result : result . rows [ 0 ]
102+ } ) ;
103+ }
79104 return result . rows [ 0 ] ;
80105 }
81106
@@ -85,6 +110,13 @@ export class Repository<T> {
85110 ) : Promise < T [ ] > {
86111 const builder = new InsertQueryBuilder < T > ( this . tableName , this . executor ) ;
87112 const result = await builder . values ( data ) . returning ( returning ) . commit ( ) ;
113+ if ( this . options ?. logging ) {
114+ console . log ( {
115+ sql : builder . build ( ) . sql ,
116+ values : builder . build ( ) . values ,
117+ result : result . rows [ 0 ]
118+ } ) ;
119+ }
88120 return result . rows ;
89121 }
90122
@@ -101,17 +133,32 @@ export class Repository<T> {
101133 . where ( where )
102134 . returning ( returning )
103135 . commit ( ) ;
136+ if ( this . options ?. logging ) {
137+ console . log ( {
138+ sql : builder . build ( ) . sql ,
139+ values : builder . build ( ) . values ,
140+ result : result . rows [ 0 ]
141+ } ) ;
142+ }
104143 return result . rows [ 0 ] ?? null ;
105144 }
106145
107- async delete (
108- arg : {
109- where : WhereCondition < T > ;
110- returning ?: Array < keyof T > ;
111- }
112- ) : Promise < T | null > {
146+ async delete ( arg : {
147+ where : WhereCondition < T > ;
148+ returning ?: Array < keyof T > ;
149+ } ) : Promise < T | null > {
113150 const builder = new DeleteQueryBuilder < T > ( this . tableName , this . executor ) ;
114- const result = await builder . where ( arg . where ) . returning ( arg . returning ) . commit ( ) ;
151+ const result = await builder
152+ . where ( arg . where )
153+ . returning ( arg . returning )
154+ . commit ( ) ;
155+ if ( this . options ?. logging ) {
156+ console . log ( {
157+ sql : builder . build ( ) . sql ,
158+ values : builder . build ( ) . values ,
159+ result : result . rows [ 0 ]
160+ } ) ;
161+ }
115162 return result . rows [ 0 ] ?? null ;
116163 }
117164}
0 commit comments