-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- WIP (unfinished and tests are failing) - Create CRUD functions for database - Create unit tests for data layer
- Loading branch information
Showing
7 changed files
with
135 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
.env |
Empty file.
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,71 @@ | ||
/* INCLUDES ******************************* */ | ||
const dbConnect = require('../../Utils/Database/connection'); | ||
/* **************************************** */ | ||
|
||
/* DATABASE CONNECTION ******************** */ | ||
const db = dbConnect.connect(); | ||
/* **************************************** */ | ||
|
||
/* FUNCTIONS ****************************** */ | ||
async function create(passwordHash, email, bio, imageLink) { | ||
const query = { | ||
name: 'addUser', | ||
text: 'INSERT INTO users(passwordhash, email, bio, imagelink, admin) VALUES ($1, $2, $3, $4, FALSE);', | ||
values: [passwordHash, email, bio, imageLink], | ||
}; | ||
try { | ||
await db.none(query); | ||
return true; | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
} | ||
|
||
async function read(email) { | ||
const query = { | ||
name: 'readUser', | ||
text: 'SELECT * FROM users WHERE email = $1', | ||
values: [email], | ||
}; | ||
try { | ||
return await db.one(query); | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
} | ||
|
||
async function update(email, field, value) { | ||
const query = { | ||
name: 'updateUser', | ||
text: 'UPDATE users SET "$1" = "$2" WHERE email = "$3";', | ||
values: [field, value, email], | ||
}; | ||
try { | ||
await db.none(query); | ||
return true; | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
} | ||
|
||
async function remove() { | ||
const query = { | ||
name: 'updateUser', | ||
text: 'DELETE FROM users WHERE email = $1;', | ||
values: [email], | ||
}; | ||
try { | ||
await db.none(query); | ||
return true; | ||
} catch (err) { | ||
throw new Error(err); | ||
} | ||
} | ||
|
||
/* **************************************** */ | ||
|
||
/* EXPORTS ******************************** */ | ||
module.exports = { | ||
create, read, update, remove, | ||
}; | ||
/* **************************************** */ |
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,30 @@ | ||
const data = require('./data'); | ||
|
||
data.remove('test@test.com'); | ||
|
||
it('creates a user', async () => { | ||
const response = await data.create('1234', 'test@test.com', 'This is my bio', ''); | ||
expect(response).toEqual(true); | ||
}); | ||
|
||
it('reads a user', async () => { | ||
const response = await data.read('test@test.com'); | ||
expect(response.bio).toEqual('This is my bio'); | ||
}); | ||
|
||
it('updates a user', async () => { | ||
await data.create('bio', 'This is my new bio'); | ||
const response = await data.read('test@test.com'); | ||
expect(response.bio).toEqual('This is my new bio'); | ||
}); | ||
|
||
it('deletes a user', async () => { | ||
let errors = 0; | ||
await data.create('1234', 'test@test.com', 'This is my bio', ''); | ||
try { | ||
await data.read('test@test.com'); | ||
} catch (err) { | ||
errors += 1; | ||
} | ||
expect(errors).toEqual(1); | ||
}); |
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,31 @@ | ||
/* INCLUDES ******************************* */ | ||
const pgPromise = require('pg-promise'); | ||
const dotenv = require('dotenv'); | ||
|
||
const pg = pgPromise(); | ||
dotenv.config(); | ||
/* **************************************** */ | ||
|
||
/* CONSTANTS ****************************** */ | ||
const { | ||
DB_USER, DB_PASS, DB_HOST, DB_PORT, DB_NAME, | ||
} = process.env; | ||
|
||
const connectionInfo = { | ||
host: DB_HOST, | ||
port: DB_PORT, | ||
database: DB_NAME, | ||
user: DB_USER, | ||
password: DB_PASS, | ||
}; | ||
/* **************************************** */ | ||
|
||
/* FUNCTIONS ****************************** */ | ||
function connect() { | ||
return pg(connectionInfo); | ||
} | ||
/* **************************************** */ | ||
|
||
/* EXPORTS ******************************** */ | ||
module.exports = { connect }; | ||
/* **************************************** */ |
Empty file.
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