-
-
Save anonymous/5dbdcdbb96cd12ad1a96a28b8fa079c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as mysql from 'mysql'; | |
import { RepositoryClass } from '../constructs/repository_class' | |
import { UserModel } from '../models/user_model'; | |
export class UserRepository extends RepositoryClass | |
{ | |
constructor() | |
{ | |
super(); | |
} | |
getUsers():UserModel[] | |
{ | |
let userModels:UserModel[] = []; | |
let queryPromise:Promise<any> = new Promise<any>((resolve,reject)=>{ | |
this._db.connect((err) => { | |
if (err){ | |
this._db.end(); | |
console.log("Failed connection to database at UsersRepository.getUsers()."); | |
reject(err); | |
} else { | |
this._db.query("select * from users", (error,rows,fields) => { | |
if (error) { | |
this._db.end(); | |
console.log("Failed to query users table in database at UsersRepository.getUsers()."); | |
reject(error); | |
} else { | |
this._db.end(); | |
resolve(rows); | |
} | |
}); | |
} | |
}); | |
}); | |
queryPromise.then( | |
(rows) => { | |
rows.forEach( (row:any) => { | |
let user:UserModel = new UserModel(); | |
user.emailAddres = row["email_address"]; | |
user.failedAttemptCount = row["failed"]; | |
user.isActive = row["is_active"]; | |
user.isLocked = row["is_locked"]; | |
user.lastFailedAttemptAt = row["last_failed_attempt_at"]; | |
user.lastLoginAt = row["last_login_at"]; | |
user.loginPendingFrom = row["login_pending_from"]; | |
user.passwordHash = row["password_hash"]; | |
user.passwordResetKey = row["password_reset_key"]; | |
user.passwordSetAt = row["password_set_at"]; | |
user.userId = row["user_id"]; | |
userModels.push(user); | |
}); | |
}, | |
(error) => { | |
console.log( "Query promise was rejected on error: " + JSON.stringify(error) ); | |
} | |
); | |
return userModels; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment