Skip to content

Commit e1a2aa7

Browse files
committed
Issue #3: CRUD User Infrastructure
- WIP (unfinished and tests are failing) - Create CRUD functions for database - Create unit tests for data layer
1 parent c7b2fc2 commit e1a2aa7

File tree

7 files changed

+135
-1
lines changed

7 files changed

+135
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
.env

Components/Example/index.unit.test.js

Whitespace-only changes.

Components/User/data.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* INCLUDES ******************************* */
2+
const dbConnect = require('../../Utils/Database/connection');
3+
/* **************************************** */
4+
5+
/* DATABASE CONNECTION ******************** */
6+
const db = dbConnect.connect();
7+
/* **************************************** */
8+
9+
/* FUNCTIONS ****************************** */
10+
async function create(passwordHash, email, bio, imageLink) {
11+
const query = {
12+
name: 'addUser',
13+
text: 'INSERT INTO users(passwordhash, email, bio, imagelink, admin) VALUES ($1, $2, $3, $4, FALSE);',
14+
values: [passwordHash, email, bio, imageLink],
15+
};
16+
try {
17+
await db.none(query);
18+
return true;
19+
} catch (err) {
20+
throw new Error(err);
21+
}
22+
}
23+
24+
async function read(email) {
25+
const query = {
26+
name: 'readUser',
27+
text: 'SELECT * FROM users WHERE email = $1',
28+
values: [email],
29+
};
30+
try {
31+
return await db.one(query);
32+
} catch (err) {
33+
throw new Error(err);
34+
}
35+
}
36+
37+
async function update(email, field, value) {
38+
const query = {
39+
name: 'updateUser',
40+
text: 'UPDATE users SET "$1" = "$2" WHERE email = "$3";',
41+
values: [field, value, email],
42+
};
43+
try {
44+
await db.none(query);
45+
return true;
46+
} catch (err) {
47+
throw new Error(err);
48+
}
49+
}
50+
51+
async function remove() {
52+
const query = {
53+
name: 'updateUser',
54+
text: 'DELETE FROM users WHERE email = $1;',
55+
values: [email],
56+
};
57+
try {
58+
await db.none(query);
59+
return true;
60+
} catch (err) {
61+
throw new Error(err);
62+
}
63+
}
64+
65+
/* **************************************** */
66+
67+
/* EXPORTS ******************************** */
68+
module.exports = {
69+
create, read, update, remove,
70+
};
71+
/* **************************************** */

Components/User/data.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const data = require('./data');
2+
3+
data.remove('[email protected]');
4+
5+
it('creates a user', async () => {
6+
const response = await data.create('1234', '[email protected]', 'This is my bio', '');
7+
expect(response).toEqual(true);
8+
});
9+
10+
it('reads a user', async () => {
11+
const response = await data.read('[email protected]');
12+
expect(response.bio).toEqual('This is my bio');
13+
});
14+
15+
it('updates a user', async () => {
16+
await data.create('bio', 'This is my new bio');
17+
const response = await data.read('[email protected]');
18+
expect(response.bio).toEqual('This is my new bio');
19+
});
20+
21+
it('deletes a user', async () => {
22+
let errors = 0;
23+
await data.create('1234', '[email protected]', 'This is my bio', '');
24+
try {
25+
await data.read('[email protected]');
26+
} catch (err) {
27+
errors += 1;
28+
}
29+
expect(errors).toEqual(1);
30+
});

Utils/Database/connection.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* INCLUDES ******************************* */
2+
const pgPromise = require('pg-promise');
3+
const dotenv = require('dotenv');
4+
5+
const pg = pgPromise();
6+
dotenv.config();
7+
/* **************************************** */
8+
9+
/* CONSTANTS ****************************** */
10+
const {
11+
DB_USER, DB_PASS, DB_HOST, DB_PORT, DB_NAME,
12+
} = process.env;
13+
14+
const connectionInfo = {
15+
host: DB_HOST,
16+
port: DB_PORT,
17+
database: DB_NAME,
18+
user: DB_USER,
19+
password: DB_PASS,
20+
};
21+
/* **************************************** */
22+
23+
/* FUNCTIONS ****************************** */
24+
function connect() {
25+
return pg(connectionInfo);
26+
}
27+
/* **************************************** */
28+
29+
/* EXPORTS ******************************** */
30+
module.exports = { connect };
31+
/* **************************************** */

index.unit.test.js

Whitespace-only changes.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"helmet": "^3.19.0",
4343
"node-fetch": "^2.6.0",
4444
"passport": "^0.4.0",
45-
"passport-local": "^1.0.0"
45+
"passport-local": "^1.0.0",
46+
"pg-promise": "^8.7.5"
4647
}
4748
}

0 commit comments

Comments
 (0)