Skip to content

Commit 337bf02

Browse files
committed
commenting on database seeding scripts
1 parent c88eab4 commit 337bf02

File tree

7 files changed

+67
-126
lines changed

7 files changed

+67
-126
lines changed

database/psql/seedCustomerOrders.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,3 @@ console.log('Seeding customerOrders');
2828
for (let i = 0; i < 25; i++) {
2929
seedCustomerOrders();
3030
}
31-
32-
// this isn't logging in the right spot because of async activity...
33-
// TODO I can fix this with a promise all that's fed the seed function...
34-
// TODO ...there are more important battles right now
35-
// Note: this actually isn't too far off because seed runs asyncronously and each
36-
// query is being awaited separately
37-
// Pool.query('SELECT COUNT(*) FROM customers')
38-
// .then((result) => console.log('The total customer count is', result.rows[0].count));

database/psql/seedCustomers.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ async function seedCustomers() {
1616
faker.phone.phoneNumber(),
1717
];
1818

19-
// console.log('full input array is', values);
20-
19+
// use pool connections to reduce connection timeout errors due to a full pool
2120
await Pool.connect()
2221
.then((client) => {
2322
client.query(`
@@ -38,22 +37,9 @@ async function seedCustomers() {
3837
});
3938
}
4039

41-
// seed with a random number of inputs
42-
// const random = Math.random() * 25;
43-
// console.log(`Seeding ${Math.floor(random) + 1} values`);
4440
console.log('Seeding customers and warehouses');
4541

46-
// // seems to be fixed:
47-
// // EXPECT ERRORS HERE as js will create a lot of pool connections faster than they can be handled
4842
// create the 25 customers in the database
4943
for (let i = 0; i < 25; i++) {
5044
seedCustomers();
5145
}
52-
53-
// this isn't logging in the right spot because of async activity...
54-
// TODO I can fix this with a promise all that's fed the seed function...
55-
// TODO ...there are more important battles right now
56-
// Note: this actually isn't too far off because seed runs asyncronously and each
57-
// query is being awaited separately
58-
// Pool.query('SELECT COUNT(*) FROM customers')
59-
// .then((result) => console.log('The total customer count is', result.rows[0].count));

database/psql/seedOrderProducts.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,23 @@ async function seedCustomerOrders() {
88
Math.ceil(Math.random() * 25),
99
];
1010

11-
// console.log('full input array is', values);
12-
13-
await Pool.query(`
14-
INSERT INTO "orderProducts"("productId", "productQty", "orderId")
15-
VALUES ($1, $2, $3)
16-
RETURNING *
17-
`, values)
18-
.then((newRow) => console.log(`NEW PRODUCT FOR ORDER: ${newRow.rows[0].productId}`))
19-
.catch((err) => console.log('ERROR ADDING ORDER PRODUCT', err, 'values: ', values));
11+
// use pool connection clients to reduce connection timeout errors from a full pool
12+
await Pool.connect()
13+
.then(async (client) => {
14+
await client.query(`
15+
INSERT INTO "orderProducts"("productId", "productQty", "orderId")
16+
VALUES ($1, $2, $3)
17+
RETURNING *
18+
`, values)
19+
.then((newRow) => console.log(`NEW PRODUCT FOR ORDER: ${newRow.rows[0].orderId}`))
20+
.finally(client.release());
21+
})
22+
.catch((err) => console.log('ERROR ADDING PRODUCT TO ORDER', err, '\n\n VALUES ARE \n', values));
2023
}
2124

22-
// seed with a random number of inputs
23-
// const random = Math.random() * 25;
24-
// console.log(`Seeding ${Math.floor(random) + 1} values`);
2525
console.log('Seeding customerOrders');
2626

27-
// // seems to be fixed:
28-
// // EXPECT ERRORS HERE as js will create a lot of pool connections faster than they can be handled
29-
// create the 25 customers in the database
27+
// create the 100 products in random orders in the database
3028
for (let i = 0; i < 100; i++) {
3129
seedCustomerOrders();
3230
}
33-
34-
// this isn't logging in the right spot because of async activity...
35-
// TODO I can fix this with a promise all that's fed the seed function...
36-
// TODO ...there are more important battles right now
37-
// Note: this actually isn't too far off because seed runs asyncronously and each
38-
// query is being awaited separately
39-
// Pool.query('SELECT COUNT(*) FROM customers')
40-
// .then((result) => console.log('The total customer count is', result.rows[0].count));

database/psql/seedProducts.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const faker = require('faker');
22
const Pool = require('./dbConnection');
33

4-
// console.log(typeof Number(faker.address.zipCode()));
54
async function seedProducts() {
65
// create an array of variables to be inserted into the database
76
const values = [
@@ -11,33 +10,23 @@ async function seedProducts() {
1110
Math.ceil(Math.random() * 50),
1211
];
1312

14-
// console.log('full input array is', values);
15-
16-
await Pool.query(`
17-
INSERT INTO products("name", "description", "price", "weight")
18-
VALUES ($1, $2, $3, $4)
19-
RETURNING *
20-
`, values)
21-
.then((newRow) => console.log(`NEW PRODUCT ADDED: ${newRow.rows[0].name}`))
22-
.catch((err) => console.log('ERROR ADDING PRODUCT (THIS IS SOMEWHAT EXPECTED FOR SEEDING SCRIPT)', err));
13+
// use pool connect clients to reduce instances of connection timeout errors from a full pool
14+
await Pool.connect()
15+
.then(async (client) => {
16+
await client.query(`
17+
INSERT INTO products("name", "description", "price", "weight")
18+
VALUES ($1, $2, $3, $4)
19+
RETURNING *
20+
`, values)
21+
.then((newRow) => console.log(`NEW PRODUCT ADDED: ${newRow.rows[0].name}`))
22+
.finally(() => client.release());
23+
})
24+
.catch((err) => console.log('ERROR ADDING PRODUCT', err));
2325
}
2426

25-
// seed with a random number of inputs
26-
// const random = Math.random() * 25;
27-
// console.log(`Seeding ${Math.floor(random) + 1} values`);
2827
console.log('Seeding products');
2928

30-
// // seems to be fixed:
31-
// // EXPECT ERRORS HERE as js will create a lot of pool connections faster than they can be handled
32-
// create the 25 customers in the database
29+
// create the 250 products in the database
3330
for (let i = 0; i < 250; i++) {
3431
seedProducts();
3532
}
36-
37-
// this isn't logging in the right spot because of async activity...
38-
// TODO I can fix this with a promise all that's fed the seed function...
39-
// TODO ...there are more important battles right now
40-
// Note: this actually isn't too far off because seed runs asyncronously and each
41-
// query is being awaited separately
42-
Pool.query('SELECT COUNT(*) FROM products')
43-
.then((result) => console.log('The total product count is', result.rows[0].count));

database/psql/seedWarehouseInventory.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,30 @@ const Pool = require('./dbConnection');
33
async function seedWarehouseInventory() {
44
// create an array of variables to be inserted into the database
55
const values = [
6-
75 + Math.ceil(Math.random() * 114),
6+
Math.ceil(Math.random() * 250),
77
Math.ceil(Math.random() * 25),
88
Math.ceil(Math.random() * 2500),
99
];
1010

1111
// console.log('full input array is', values);
1212

13-
await Pool.query(`
14-
INSERT INTO "warehouseInventory"("productId", "warehouseId", quantity)
15-
VALUES ($1, $2, $3)
16-
RETURNING *
17-
`, values)
18-
.then((newRow) => console.log(`NEW INVENTORY ADDED FOR WAREHOUSE: ${newRow.rows[0].warehouseId}`))
19-
.catch((err) => console.log('ERROR ADDING CUSTOMER AND/OR ADDRESS (THIS IS SOMEWHAT EXPECTED FOR SEEDING SCRIPT)', err));
13+
// use a connection client to prevent errors from a full pool & connection timeouts
14+
await Pool.connect()
15+
.then((client) => {
16+
client.query(`
17+
INSERT INTO "warehouseInventory"("productId", "warehouseId", quantity)
18+
VALUES ($1, $2, $3)
19+
RETURNING *
20+
`, values)
21+
.then((newRow) => console.log(`NEW INVENTORY ADDED FOR WAREHOUSE: ${newRow.rows[0].warehouseId}`))
22+
.catch((err) => console.log('ERROR ADDING WAREHOUSE INVENTORY', err, values))
23+
.finally(() => client.release());
24+
});
2025
}
2126

22-
// seed with a random number of inputs
23-
// const random = Math.random() * 25;
24-
// console.log(`Seeding ${Math.floor(random) + 1} values`);
2527
console.log('Seeding warehouse inventory');
2628

27-
// // seems to be fixed:
28-
// // EXPECT ERRORS HERE as js will create a lot of pool connections faster than they can be handled
29-
// create the 25 customers in the database
29+
// create the warehouse inventories for the 25 warehouses
3030
for (let i = 0; i < 25; i++) {
3131
seedWarehouseInventory();
3232
}
33-
34-
// this isn't logging in the right spot because of async activity...
35-
// TODO I can fix this with a promise all that's fed the seed function...
36-
// TODO ...there are more important battles right now
37-
// Note: this actually isn't too far off because seed runs asyncronously and each
38-
// query is being awaited separately
39-
// Pool.query('SELECT COUNT(*) FROM customers')
40-
// .then((result) => console.log('The total customer count is', result.rows[0].count));

database/psql/seedWarehouses.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,33 @@ async function seedWarehouses() {
88
faker.address.secondaryAddress(),
99
faker.address.city(),
1010
faker.address.state(),
11-
faker.address.zipCode(), // this seems to sometimes throw an error when the column type is INT
11+
faker.address.zipCode(),
1212
faker.commerce.department(),
1313
];
1414

15-
// console.log('full input array is', values);
16-
17-
await Pool.query(`
18-
WITH newAddress AS (
19-
INSERT INTO addresses("address", "address2", "city", "state", "zipCode")
20-
VALUES ($1, $2, $3, $4, $5)
21-
RETURNING *
22-
)
23-
INSERT INTO warehouses("name", "addressId")
24-
VALUES ($6, (SELECT id FROM newAddress))
25-
RETURNING *
26-
`, values)
27-
.then((newRow) => console.log(`NEW WAREHOUSE ADDED: ${newRow.rows[0].name}`))
28-
.catch((err) => console.log('ERROR ADDING CUSTOMER AND/OR ADDRESS (THIS IS SOMEWHAT EXPECTED FOR SEEDING SCRIPT)', err));
15+
// connect to a pool client to reduce chances of a full pool & connection errors
16+
await Pool.connect()
17+
.then(async (client) => {
18+
// send query to client to insert and address and warehouse
19+
await client.query(`
20+
WITH newAddress AS (
21+
INSERT INTO addresses("address", "address2", "city", "state", "zipCode")
22+
VALUES ($1, $2, $3, $4, $5)
23+
RETURNING *
24+
)
25+
INSERT INTO warehouses("name", "addressId")
26+
VALUES ($6, (SELECT id FROM newAddress))
27+
RETURNING *
28+
`, values)
29+
.then((newRow) => console.log(`NEW WAREHOUSE ADDED: ${newRow.rows[0].name}`))
30+
.finally(() => client.release());
31+
})
32+
.catch((err) => console.log('ERROR ADDING WAREHOUSE', err));
2933
}
3034

31-
// seed with a random number of inputs
32-
// const random = Math.random() * 25;
33-
// console.log(`Seeding ${Math.floor(random) + 1} values`);
3435
console.log('Seeding warehouses');
3536

36-
// // seems to be fixed:
37-
// // EXPECT ERRORS HERE as js will create a lot of pool connections faster than they can be handled
38-
// create the 25 customers in the database
37+
// create the 25 warehouses in the database
3938
for (let i = 0; i < 25; i++) {
4039
seedWarehouses();
4140
}
42-
43-
// this isn't logging in the right spot because of async activity...
44-
// TODO I can fix this with a promise all that's fed the seed function...
45-
// TODO ...there are more important battles right now
46-
// Note: this actually isn't too far off because seed runs asyncronously and each
47-
// query is being awaited separately
48-
// Pool.query('SELECT COUNT(*) FROM customers')
49-
// .then((result) => console.log('The total customer count is', result.rows[0].count));

database/psql/setupTablesPSQL.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const Pool = require('./dbConnection');
22

3-
// note: sql queries cannot have trailing commas for the last argument (like in js)
43
// function that will create each table and then add necessary constraints
54
async function setup() {
65
// setup the customer table
@@ -39,7 +38,7 @@ async function setup() {
3938
.finally(() => client.release());
4039
});
4140

42-
41+
// setup customers to orders join table
4342
await Pool.connect()
4443
.then(async (client) => {
4544
client.query(`
@@ -51,7 +50,7 @@ async function setup() {
5150
.finally(() => client.release());
5251
});
5352

54-
53+
// create orrders to products join table (also stores quantities)
5554
await Pool.connect()
5655
.then(async (client) => {
5756
await client.query(`
@@ -65,6 +64,7 @@ async function setup() {
6564
.finally(() => client.release());
6665
});
6766

67+
// create table for all products
6868
await Pool.connect()
6969
.then(async (client) => {
7070
await client.query(`
@@ -79,6 +79,7 @@ async function setup() {
7979
.finally(() => client.release());
8080
});
8181

82+
// create table for all warehouses
8283
await Pool.connect()
8384
.then(async (client) => {
8485
await client.query(`
@@ -91,6 +92,7 @@ async function setup() {
9192
.finally(() => client.release());
9293
});
9394

95+
// create table for warehouseInventories
9496
await Pool.connect()
9597
.then(async (client) => {
9698
await client.query(`
@@ -105,9 +107,8 @@ async function setup() {
105107
});
106108

107109

110+
// ! Add foregin key constraints
108111
// add foregin key constraint, this will throw an error if the previous queries are not
109-
// awaited because the tables won't exist yet
110-
console.log('setting up addresses_customer constraint...');
111112
await Pool.connect()
112113
.then(async (client) => {
113114
await client.query(`

0 commit comments

Comments
 (0)