@@ -69,24 +69,18 @@ export class HasOne extends Association {
69
69
// add instance methods for create, get, update, delete
70
70
71
71
private attachAssociationMethodsToModel ( ) {
72
- console . log ( "target" , this . target . name )
73
- console . log ( "source" , this . source . name )
74
- console . log ( "getAccesorName of HasOne: " , this . getAccesorName )
75
- console . log ( "this.mappingDetails" , this . mappingDetails )
76
72
addMethodToModel ( this , this . target , this . getAccesorName )
77
73
addAddMethodToModel ( this , this . target , this . addAccesorName )
78
74
}
79
75
80
76
async getAssociatedData ( instance :any , options ?:any ) {
81
- console . log ( "instance? " , instance )
82
- //console.log("THIS: ", this)
83
77
let query = ''
84
78
let queryResult :any
85
79
if ( this . targetModel_MappindColumnName ) { // type checking
86
80
query = `SELECT * FROM ${ this . source . table }
87
81
WHERE ${ this . foreignKey_ColumnName } ='${ instance [ this . targetModel_MappindColumnName ] } '`
88
- }
89
- console . log ( query )
82
+ }
83
+ // console.log(query)
90
84
const db = await ConnectDb ( )
91
85
try {
92
86
queryResult = await db . queryObject ( query ) ;
@@ -95,35 +89,30 @@ export class HasOne extends Association {
95
89
} finally {
96
90
DisconnectDb ( db )
97
91
}
98
- //console.log(queryResult.rows)
99
92
return queryResult . rows
100
93
}
101
94
async addAssociatedData ( instance :any , values :any ) {
102
95
let query = ''
103
96
let queryResult :any
104
- console . log ( "HasOne's addAssociatedData" )
105
- console . log ( "values" , values )
106
- console . log ( "source table" , this . source . table ) // profiles
107
- console . log ( "target table" , this . target . table ) // users
108
97
// case1. value is an object (e.g. {id:1})
109
98
// case2. value is an instance of source table
110
99
if ( values . id ) {
111
- // <<<<<< instance id hard coded!!!!
100
+ // <<<<<< instance id hard coded for now
112
101
query = `UPDATE ${ this . source . table } SET ${ this . foreignKey_ColumnName } ='${ instance . id || instance . _id } ' WHERE ${ this . targetModel_MappindColumnName } =${ values . id } `
113
102
}
114
103
else if ( values instanceof this . source ) {
115
- console . log ( 'instance' , values )
104
+
116
105
// if this instance has no id, assuem it's not yet created in the database and create the record
117
106
// if this instance has id, assume it's in the database and just update the foreign key column
118
107
const toObj = Object . assign ( { } , values )
119
108
const objKeys = Object . keys ( toObj ) . join ( ',' )
120
109
const objVals = Object . values ( toObj ) . map ( el => `'${ el } '` ) . join ( ',' )
121
- const instanceId = instance . id || instance . _id // <<<<<<<<< HARD CODED!!!
110
+ const instanceId = instance . id || instance . _id // <<<<<<<<< HARD CODED for now
122
111
123
112
query = `INSERT INTO ${ this . source . table } (${ objKeys } , ${ this . foreignKey_ColumnName } ) VALUES (${ objVals } , '${ instanceId } ');`
124
- // postres auto converting string to numbers?
113
+ // postres auto converting string to numbers
125
114
}
126
- console . log ( query )
115
+
127
116
const db = await ConnectDb ( )
128
117
try {
129
118
await db . queryObject ( query ) ;
@@ -149,22 +138,20 @@ export class BelongsTo extends Association {
149
138
150
139
// add instance methods for create, get, update, delete
151
140
private attachAssociationMethodsToModel ( ) {
152
- console . log ( "getAccesorName: " , this . getAccesorName )
141
+ // console.log("getAccesorName: ", this.getAccesorName)
153
142
addMethodToModel ( this , this . source , this . getAccesorName )
154
143
addAddMethodToModel ( this , this . target , this . addAccesorName )
155
144
}
156
145
157
146
// this is instance method e.g. profile1.getUser(), person.getSpecies()
158
147
async getAssociatedData ( instance :any , options ?:any ) { //
159
- console . log ( "instance? " , instance )
160
- //console.log("THIS: ", this)
161
148
let query = ''
162
149
let queryResult :any
163
150
if ( this . foreignKey_ColumnName ) { // type checking
164
151
query = `SELECT * FROM ${ this . target . table }
165
152
WHERE ${ this . targetModel_MappindColumnName } ='${ instance [ this . foreignKey_ColumnName ] } '`
166
153
}
167
- console . log ( query )
154
+ // console.log(query)
168
155
const db = await ConnectDb ( )
169
156
try {
170
157
queryResult = await db . queryObject ( query ) ;
@@ -173,7 +160,6 @@ export class BelongsTo extends Association {
173
160
} finally {
174
161
DisconnectDb ( db )
175
162
}
176
- //console.log(queryResult.rows)
177
163
return queryResult . rows
178
164
}
179
165
async addAssociatedData ( ) {
@@ -200,22 +186,21 @@ export class HasMany extends Association {
200
186
// but currently only has get
201
187
private attachAssociationMethodsToModel ( model :typeof Model ) {
202
188
const methodName = this . getAccesorName
203
- console . log ( "methodName? " , methodName )
189
+ // console.log("methodName? ",methodName)
204
190
205
191
addMethodToModel ( this , model , methodName )
206
192
addAddMethodToModel ( this , this . target , this . addAccesorName )
207
193
}
208
194
209
195
// this is instance method e.g. species1.getPeople()
210
196
async getAssociatedData ( instance :any , options ?:any ) {
211
- console . log ( "instance? " , instance )
212
197
let query = ''
213
198
if ( this . mapping_ColumnName ) {
214
199
query = `
215
200
SELECT * FROM ${ this . target . table }
216
201
WHERE ${ this . foreignKey_ColumnName } ='${ instance [ this . mapping_ColumnName ] } '`
217
202
}
218
- console . log ( "association query:" , query )
203
+ // console.log("association query:", query)
219
204
220
205
const db = await ConnectDb ( )
221
206
let queryResult :any
@@ -226,7 +211,6 @@ export class HasMany extends Association {
226
211
} finally {
227
212
DisconnectDb ( db )
228
213
}
229
- //console.log(queryResult.rows)
230
214
return queryResult . rows
231
215
}
232
216
async addAssociatedData ( ) {
@@ -257,7 +241,7 @@ export class ManyToMany extends Association {
257
241
// console.log("getAccesorName_A & B: ", getAccesorName_A, getAccesorName_B)
258
242
// add instance methods for create, get, update, delete
259
243
private attachAssociationMethodsToModels ( ) {
260
- console . log ( "getAccesorName_A & B: " , this . getAccesorName_A , this . getAccesorName_B )
244
+ // console.log("getAccesorName_A & B: ", this.getAccesorName_A, this.getAccesorName_B)
261
245
262
246
addMethodToModel ( this , this . modelA , this . getAccesorName_A )
263
247
addMethodToModel ( this , this . modelB , this . getAccesorName_B )
@@ -288,7 +272,6 @@ export class ManyToMany extends Association {
288
272
WHERE ${ this . modelB . table } .${ this . modelB_mappingKey } = '${ instance [ this . modelB_mappingKey ] } ' ORDER BY ${ this . modelA . table } .${ this . modelA_mappingKey } `
289
273
}
290
274
}
291
- console . log ( query )
292
275
const db = await ConnectDb ( )
293
276
try {
294
277
queryResult = await db . queryObject ( query ) ;
@@ -307,13 +290,11 @@ export class ManyToMany extends Association {
307
290
308
291
309
292
310
-
311
-
312
293
// options are not decided yet, thus type 'any' for placeholder
313
294
314
295
function addMethodToModel < T extends Association > ( association :T , targetModel :typeof Model , ModelMethod :string ) {
315
- console . log ( "association.name: " , association . association_name )
316
- console . log ( "targetModel, ModelMethod: " , targetModel . name , ModelMethod )
296
+ // console.log("association.name: ", association.association_name)
297
+ // console.log("targetModel, ModelMethod: ", targetModel.name, ModelMethod)
317
298
Object . defineProperty ( targetModel . prototype , ModelMethod , {
318
299
enumerable : false ,
319
300
value ( options :any ) {
@@ -324,8 +305,8 @@ function addMethodToModel<T extends Association>(association:T, targetModel:type
324
305
325
306
//called by addAddMethodToModel(this, this.target, this.getAccesorName)
326
307
function addAddMethodToModel < T extends Association > ( association :T , targetModel :typeof Model , ModelMethod :string ) {
327
- console . log ( "association.name: " , association . association_name )
328
- console . log ( "targetModel, ModelMethod: " , targetModel . name , ModelMethod )
308
+ // console.log("association.name: ", association.association_name)
309
+ // console.log("targetModel, ModelMethod: ", targetModel.name, ModelMethod)
329
310
Object . defineProperty ( targetModel . prototype , ModelMethod , {
330
311
enumerable : false ,
331
312
value ( val :any , options :any ) {
0 commit comments