Skip to content

Commit dccfa22

Browse files
authored
Merge pull request #51 from oslabs-beta/mh/cleanup
Mh/cleanup
2 parents 2b729a1 + 40b63af commit dccfa22

File tree

5 files changed

+29
-65
lines changed

5 files changed

+29
-65
lines changed

Test/abstraction.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,4 @@ describe('Abstraction Test', () => {
291291
// *** Methods that cannot have stand-alone test ***
292292
// : 'where', 'having', 'query','queryInstance'
293293

294-
})
295-
296-
297-
298-
// const testInstance = new Test();
299-
// testInstance.name = 'tesia';
300-
// testInstance.hair_color = 'black' ;
301-
// await testInstance.save();
302-
// // Test {name = 'kristen', hair_color = 'black'}
303-
// // testInstance.hair_color = 'brown'
304-
// testInstance.name = 'kristen';
305-
// // Test {name = 'tesia', hair_color: 'brown'}
306-
// await testInstance.update();
294+
})

Test/association.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe('Testing Associations and Methods ', () => {
178178
await userTeamAssociation.syncAssociation();
179179
// check to see if the users table has foreign key field referencing teams table
180180
const mappingKeys = await getMappingKeys(User.table, Team.table)
181-
console.log('mappingKeys', mappingKeys)
181+
//console.log('mappingKeys', mappingKeys)
182182
assertStrictEquals(mappingKeys!.source_table, User.table)
183183
assertStrictEquals(mappingKeys!.source_keyname, `${Team.name.toLowerCase()}_id`)
184184
})
@@ -236,8 +236,8 @@ describe('Testing Associations and Methods ', () => {
236236
await manyToMany(Member, Club, { through: Member_Club });
237237
const member1Clubs = await member1.getClubs()
238238
const club1Members = await club1.getMembers();
239-
console.log("====member1Clubs=====",member1Clubs)
240-
console.log("====club1Members=====",club1Members)
239+
//console.log("====member1Clubs=====",member1Clubs)
240+
//console.log("====club1Members=====",club1Members)
241241
assertEquals(member1Clubs[0].clubname, 'Book Club')
242242
assertEquals(member1Clubs[1].clubname, 'Tennis Club')
243243
assert(club1Members[0].name==='member_one' || club1Members[0].name==='member_two')

src/class/Association.ts

+16-35
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,18 @@ export class HasOne extends Association {
6969
// add instance methods for create, get, update, delete
7070

7171
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)
7672
addMethodToModel(this, this.target, this.getAccesorName)
7773
addAddMethodToModel(this, this.target, this.addAccesorName)
7874
}
7975

8076
async getAssociatedData(instance:any, options?:any) {
81-
console.log("instance? ", instance)
82-
//console.log("THIS: ", this)
8377
let query = ''
8478
let queryResult:any
8579
if(this.targetModel_MappindColumnName) { // type checking
8680
query = `SELECT * FROM ${this.source.table}
8781
WHERE ${this.foreignKey_ColumnName} ='${instance[this.targetModel_MappindColumnName]}'`
88-
}
89-
console.log(query)
82+
}
83+
//console.log(query)
9084
const db = await ConnectDb()
9185
try {
9286
queryResult = await db.queryObject(query);
@@ -95,35 +89,30 @@ export class HasOne extends Association {
9589
} finally {
9690
DisconnectDb(db)
9791
}
98-
//console.log(queryResult.rows)
9992
return queryResult.rows
10093
}
10194
async addAssociatedData(instance:any, values:any){
10295
let query = ''
10396
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
10897
// case1. value is an object (e.g. {id:1})
10998
// case2. value is an instance of source table
11099
if(values.id) {
111-
// <<<<<< instance id hard coded!!!!
100+
// <<<<<< instance id hard coded for now
112101
query = `UPDATE ${this.source.table} SET ${this.foreignKey_ColumnName}='${instance.id || instance._id}' WHERE ${this.targetModel_MappindColumnName}=${values.id}`
113102
}
114103
else if(values instanceof this.source) {
115-
console.log('instance', values)
104+
116105
// if this instance has no id, assuem it's not yet created in the database and create the record
117106
// if this instance has id, assume it's in the database and just update the foreign key column
118107
const toObj = Object.assign({}, values)
119108
const objKeys = Object.keys(toObj).join(',')
120109
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
122111

123112
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
125114
}
126-
console.log(query)
115+
127116
const db = await ConnectDb()
128117
try {
129118
await db.queryObject(query);
@@ -149,22 +138,20 @@ export class BelongsTo extends Association {
149138

150139
// add instance methods for create, get, update, delete
151140
private attachAssociationMethodsToModel() {
152-
console.log("getAccesorName: ", this.getAccesorName)
141+
//console.log("getAccesorName: ", this.getAccesorName)
153142
addMethodToModel(this, this.source, this.getAccesorName)
154143
addAddMethodToModel(this, this.target, this.addAccesorName)
155144
}
156145

157146
// this is instance method e.g. profile1.getUser(), person.getSpecies()
158147
async getAssociatedData(instance:any, options?:any) { //
159-
console.log("instance? ", instance)
160-
//console.log("THIS: ", this)
161148
let query = ''
162149
let queryResult:any
163150
if(this.foreignKey_ColumnName) { // type checking
164151
query = `SELECT * FROM ${this.target.table}
165152
WHERE ${this.targetModel_MappindColumnName} ='${instance[this.foreignKey_ColumnName]}'`
166153
}
167-
console.log(query)
154+
//console.log(query)
168155
const db = await ConnectDb()
169156
try {
170157
queryResult = await db.queryObject(query);
@@ -173,7 +160,6 @@ export class BelongsTo extends Association {
173160
} finally {
174161
DisconnectDb(db)
175162
}
176-
//console.log(queryResult.rows)
177163
return queryResult.rows
178164
}
179165
async addAssociatedData(){
@@ -200,22 +186,21 @@ export class HasMany extends Association {
200186
// but currently only has get
201187
private attachAssociationMethodsToModel(model:typeof Model) {
202188
const methodName = this.getAccesorName
203-
console.log("methodName? ",methodName)
189+
//console.log("methodName? ",methodName)
204190

205191
addMethodToModel(this, model, methodName)
206192
addAddMethodToModel(this, this.target, this.addAccesorName)
207193
}
208194

209195
// this is instance method e.g. species1.getPeople()
210196
async getAssociatedData(instance:any, options?:any) {
211-
console.log("instance? ", instance)
212197
let query = ''
213198
if(this.mapping_ColumnName) {
214199
query = `
215200
SELECT * FROM ${this.target.table}
216201
WHERE ${this.foreignKey_ColumnName} ='${instance[this.mapping_ColumnName]}'`
217202
}
218-
console.log("association query:", query)
203+
//console.log("association query:", query)
219204

220205
const db = await ConnectDb()
221206
let queryResult:any
@@ -226,7 +211,6 @@ export class HasMany extends Association {
226211
} finally {
227212
DisconnectDb(db)
228213
}
229-
//console.log(queryResult.rows)
230214
return queryResult.rows
231215
}
232216
async addAssociatedData(){
@@ -257,7 +241,7 @@ export class ManyToMany extends Association {
257241
// console.log("getAccesorName_A & B: ", getAccesorName_A, getAccesorName_B)
258242
// add instance methods for create, get, update, delete
259243
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)
261245

262246
addMethodToModel(this, this.modelA, this.getAccesorName_A)
263247
addMethodToModel(this, this.modelB, this.getAccesorName_B)
@@ -288,7 +272,6 @@ export class ManyToMany extends Association {
288272
WHERE ${this.modelB.table}.${this.modelB_mappingKey} = '${instance[this.modelB_mappingKey]}' ORDER BY ${this.modelA.table}.${this.modelA_mappingKey}`
289273
}
290274
}
291-
console.log(query)
292275
const db = await ConnectDb()
293276
try {
294277
queryResult = await db.queryObject(query);
@@ -307,13 +290,11 @@ export class ManyToMany extends Association {
307290

308291

309292

310-
311-
312293
// options are not decided yet, thus type 'any' for placeholder
313294

314295
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)
317298
Object.defineProperty(targetModel.prototype, ModelMethod, {
318299
enumerable: false,
319300
value(options:any) {
@@ -324,8 +305,8 @@ function addMethodToModel<T extends Association>(association:T, targetModel:type
324305

325306
//called by addAddMethodToModel(this, this.target, this.getAccesorName)
326307
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)
329310
Object.defineProperty(targetModel.prototype, ModelMethod, {
330311
enumerable: false,
331312
value(val:any, options:any) {

src/class/Model.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class Model {
198198
for (let i = 1; i < condition.length; i++) {
199199
this.sql += ` ${condition[i]}`;
200200
}
201-
console.log(this.query);
201+
//console.log(this.query);
202202
return this;
203203
}
204204

@@ -308,7 +308,7 @@ export class Model {
308308

309309
// IF Existing relationships
310310
if (mappings !== undefined && mappings !== null) {
311-
console.log('========== EXISTING ASSOCIATION ===========');
311+
//console.log('========== EXISTING ASSOCIATION ===========');
312312
foreignKey_ColumnName = mappings.source_keyname;
313313
mappingTarget_ColumnName = mappings.target_keyname;
314314
// const columnAtt = {
@@ -319,7 +319,7 @@ export class Model {
319319
// Object.assign(this.columns[foreignKey_ColumnName], columnAtt)
320320
} else {
321321
// IF forming new relationships // not allowing user option for now (defaulting to target's primary key)
322-
console.log('========== FORMING NEW ASSOCIATION ===========');
322+
//console.log('========== FORMING NEW ASSOCIATION ===========');
323323
foreignKey_ColumnName = `${targetModel.name.toLocaleLowerCase()}_id`;
324324
const tempPrime = await getprimaryKey(targetModel.table);
325325
mappingTarget_ColumnName = tempPrime ? tempPrime : 'id' || '_id'; // << hard coded
@@ -334,7 +334,7 @@ export class Model {
334334
};
335335

336336
this.columns[foreignKey_ColumnName] = columnAtt;
337-
console.log('columnAtt: ', columnAtt);
337+
//console.log('columnAtt: ', columnAtt);
338338
// only if there's NO existing association or existing foreign key
339339
associationQuery = `
340340
ALTER TABLE ${this.table} ADD ${foreignKey_ColumnName} ${
@@ -346,7 +346,7 @@ export class Model {
346346
targetModel.table
347347
} ON DELETE SET NULL ON UPDATE CASCADE
348348
;`; // and this will NOT executed unless use explictly execute sync() on association instance created below
349-
console.log('associationQuery:', associationQuery);
349+
//console.log('associationQuery:', associationQuery);
350350
}
351351

352352
// ========= COMPOSITE FOREIGN KEYS ONLY ============
@@ -366,9 +366,6 @@ export class Model {
366366
foreignKey_ColumnName: foreignKey_ColumnName,
367367
mapping_ColumnName: mappingTarget_ColumnName,
368368
};
369-
// maybe making associations object in Model class?
370-
// e.g.
371-
// { Person_belongsTo_Species:mappingDetails }
372369

373370
//console.log('mappingDetails:', mappingDetails)
374371
if (options?.associationName === 'hasOne') {
@@ -389,9 +386,8 @@ export class Model {
389386
let associationQuery = '';
390387

391388
const mappings = await getMappingKeys(targetModel.table, this.table);
392-
//console.log(mappings)
393389
if (mappings !== undefined && mappings !== null) {
394-
console.log('========== EXISTING ASSOCIATION ===========');
390+
//console.log('========== EXISTING ASSOCIATION ===========');
395391
mapping_ColumnName = mappings.target_keyname;
396392
targetModel_foreignKey = mappings.source_keyname;
397393
const columnAtt = {
@@ -407,8 +403,8 @@ export class Model {
407403
//console.log(targetModel.columns[targetModel_foreignKey])
408404
} else {
409405
// IF forming new relationships // not allowing user option for now (defaulting to target's primary key)
410-
console.log('========== FORMING NEW ASSOCIATION ===========');
411-
console.log('========== need to execute belongsTo first ===========');
406+
//console.log('========== FORMING NEW ASSOCIATION ===========');
407+
//console.log('========== need to execute belongsTo first ===========');
412408
}
413409
// ========= WHEN COMPOSITE FOREIGN KEYS ...============
414410

@@ -464,7 +460,7 @@ export async function getMappingKeys<T>(
464460
} finally {
465461
DisconnectDb(db);
466462
}
467-
console.log('getMappingKeys RESULT', result);
463+
//console.log('getMappingKeys RESULT', result);
468464
//if('rows' in result)
469465
if (typeof result === 'object' && 'rows' in result) {
470466
return result.rows[0] as IgetMappingKeysResult;

src/functions/init.ts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export function init() {
44
// create .env file in root directory
55
const envFilePath = "./";
66
const envFileContent = `
7-
# See the documentation for more detail: // detail here!
87
# Set your environment either 'development' or 'test'
98
ENVIRONMENT=development
109
# Please enter your database uri below :

0 commit comments

Comments
 (0)