@@ -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`);
3435console . 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
3938for ( 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));
0 commit comments