@@ -13,15 +13,27 @@ import {
13
13
} from "../types" ;
14
14
import { buildWhereClause } from "../utils" ;
15
15
16
+ interface RepositoryOptions {
17
+ logging ?: boolean ;
18
+ }
19
+
16
20
export class Repository < T > {
17
21
constructor (
18
22
protected readonly tableName : string ,
19
- protected readonly executor : SqlExecutor
23
+ protected readonly executor : SqlExecutor ,
24
+ protected options ?: RepositoryOptions
20
25
) { }
21
26
22
27
async findRow ( where : WhereCondition < T > ) : Promise < T | null > {
23
28
const builder = new SelectQueryBuilder < T > ( this . tableName , this . executor ) ;
24
29
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
+ }
25
37
return result . rows [ 0 ] ?? null ;
26
38
}
27
39
@@ -34,9 +46,15 @@ export class Repository<T> {
34
46
if ( payload ?. orderBy ) builder . orderBy ( payload . orderBy ) ;
35
47
if ( payload ?. limit ) builder . limit ( payload . limit ) ;
36
48
if ( payload ?. offset ) builder . offset ( payload . offset ) ;
37
- // console.log(builder.build().sql);
38
- // console.log(builder.build().values);
39
49
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
+ }
40
58
return result . rows ;
41
59
}
42
60
@@ -76,6 +94,13 @@ export class Repository<T> {
76
94
) : Promise < T > {
77
95
const builder = new InsertQueryBuilder < T > ( this . tableName , this . executor ) ;
78
96
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
+ }
79
104
return result . rows [ 0 ] ;
80
105
}
81
106
@@ -85,6 +110,13 @@ export class Repository<T> {
85
110
) : Promise < T [ ] > {
86
111
const builder = new InsertQueryBuilder < T > ( this . tableName , this . executor ) ;
87
112
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
+ }
88
120
return result . rows ;
89
121
}
90
122
@@ -101,17 +133,32 @@ export class Repository<T> {
101
133
. where ( where )
102
134
. returning ( returning )
103
135
. 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
+ }
104
143
return result . rows [ 0 ] ?? null ;
105
144
}
106
145
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 > {
113
150
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
+ }
115
162
return result . rows [ 0 ] ?? null ;
116
163
}
117
164
}
0 commit comments