-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a interface for interacting with the database. References #61.
- Loading branch information
1 parent
ee83801
commit 37dd634
Showing
4 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
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
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
18 changes: 18 additions & 0 deletions
18
server/services/user-service/src/interfaces/users-database/index.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {Pool as PostgresPool} from 'pg'; | ||
import makeOrdersDatabase from './users-database'; | ||
import config from '../../../../core/config'; | ||
|
||
const databasePool = new PostgresPool({ | ||
host: config.postgresUrl, | ||
port: config.postgresPort, | ||
user: config.postgresUser, | ||
password: config.postgresPassword, | ||
database: config.postgresDb, | ||
}); | ||
|
||
const ordersDatabase = makeOrdersDatabase({ | ||
databaseClient: databasePool, | ||
databaseTable: 'users', | ||
}); | ||
|
||
export default ordersDatabase; |
100 changes: 100 additions & 0 deletions
100
server/services/user-service/src/interfaces/users-database/users-database.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
DatabaseClient, | ||
DatabaseQueryResults, | ||
UserDatabaseController, | ||
UserDatabaseSchema, | ||
User, | ||
} from '../../../../core/@types/global'; | ||
|
||
/** | ||
* Database interactions interface. | ||
* | ||
* @export | ||
* @param {({ | ||
* databaseClient: DatabaseClient, | ||
* databaseTable: string | ||
* })} { | ||
* databaseClient, | ||
* databaseTable, | ||
* } - dependency injection | ||
* @return {UserDatabaseController} - database controller object | ||
*/ | ||
export default function makeUsersDatabase({ | ||
databaseClient, | ||
databaseTable, | ||
}: { | ||
databaseClient: DatabaseClient, | ||
databaseTable: string | ||
}): UserDatabaseController { | ||
/** | ||
* Finds an entry in the database. | ||
* | ||
* @param {string} userId | ||
* @return { | ||
* Promise<User | { error: string; }> | ||
* } - user entry from database | ||
*/ | ||
async function findById( | ||
userId: string, | ||
): Promise<User | { error: string; }> { | ||
const resultRows: void | DatabaseQueryResults<UserDatabaseSchema> = await | ||
databaseClient.query(` | ||
SELECT * FROM ${databaseTable} WHERE id = $1 | ||
`, [userId], | ||
).catch((e: Error) => console.log(e)); | ||
|
||
if (!resultRows) { | ||
return {error: 'No such user found.'}; | ||
} | ||
|
||
const result = resultRows.rows[0]; | ||
return Object.freeze({ | ||
id: result.id, | ||
token: result.token, | ||
phoneNumber: result.phone_number, | ||
}); | ||
} | ||
|
||
/** | ||
* Inserts an entry in the database. | ||
* | ||
* @param {User} { | ||
* id, | ||
* sender, | ||
* receiver, | ||
* source, | ||
* createdOn, | ||
* } - user details | ||
* @return {string} - user id | ||
*/ | ||
async function insert({ | ||
id, | ||
token, | ||
phoneNumber, | ||
}: User): Promise<{ id: string } | { error: string; }> { | ||
const resultRows: void | DatabaseQueryResults<UserDatabaseSchema> = await | ||
databaseClient.query(` | ||
INSERT INTO ${databaseTable} | ||
( | ||
id, | ||
token, | ||
phone_number, | ||
) | ||
VALUES ($1, $2, $3) | ||
RETURNING id | ||
`, [id, token, phoneNumber], | ||
).catch((e: Error) => console.log(e)); | ||
|
||
if (!resultRows) { | ||
return {error: 'Error creating user.'}; | ||
} | ||
|
||
const result = resultRows.rows[0]; | ||
return {id: result.id}; | ||
} | ||
|
||
return Object.freeze({ | ||
findById: findById, | ||
insert: insert, | ||
}); | ||
} |