Coding

NodeJS / Express / MongoDB – Build a Shopping Cart – #4 Seeding Data

  • 00:00:00 now with Mongoose setup it's time to
  • 00:00:04 actually create data which we can see to
  • 00:00:06 seed some data and to do this I will
  • 00:00:09 first define the model of the data so I
  • 00:00:11 will create a new directory here in my
  • 00:00:13 project file and I'll call this folder
  • 00:00:16 models now in this models folder I will
  • 00:00:19 store all my Mongoose models because I
  • 00:00:22 said Mongoose uses schemas to define the
  • 00:00:25 data with which we work and later on I
  • 00:00:28 will use Z defined models here to then
  • 00:00:31 interact with the data and easily work
  • 00:00:33 with it while having this validation in
  • 00:00:36 place since we defined how the data
  • 00:00:38 should look like so in the models folder
  • 00:00:40 I will create a new file and I will just
  • 00:00:43 name this product yes because I will
  • 00:00:45 define the model of a product in this
  • 00:00:47 file the first thing is I will import
  • 00:00:51 menus here to with requirement goes and
  • 00:00:53 then I will create another variable here
  • 00:00:56 called schema with a capital S of course
  • 00:00:58 name is up to you but that is a good
  • 00:01:00 convention here and as well just use
  • 00:01:02 mongooses schema object here with that
  • 00:01:07 schema I can create a new schema notice
  • 00:01:10 the lowercase s at the beginning by
  • 00:01:12 simply running new schema and then as an
  • 00:01:16 argument I pass a JavaScript object
  • 00:01:18 defining that schema or describing that
  • 00:01:21 schema so here I define how the data I
  • 00:01:24 work with should look like now what
  • 00:01:27 should a product have or what does it
  • 00:01:29 currently have in our application if we
  • 00:01:33 go back to the application we have this
  • 00:01:35 product image we will have a title we'll
  • 00:01:38 have a description and of course we'll
  • 00:01:40 all will have a price now to keep this
  • 00:01:43 app simple
  • 00:01:45 I'll leave the price and dollars by
  • 00:01:47 default and I want to find currencies so
  • 00:01:50 I will not have a multi currency app
  • 00:01:52 here so we got these four fields here
  • 00:01:55 image title description and price let's
  • 00:01:58 set them up here the image will more be
  • 00:02:03 like an image path because will not
  • 00:02:06 store the actual image database but a
  • 00:02:08 path to the damage therefore and as you
  • 00:02:11 find the type or the
  • 00:02:13 and you find this key is fuel this field
  • 00:02:16 in the in a collection or in the
  • 00:02:18 document by using a JavaScript object
  • 00:02:21 here as a value this will have a type of
  • 00:02:23 string since it will be up half and I
  • 00:02:26 will make this required so I will always
  • 00:02:30 need this image path there are in
  • 00:02:32 products without images of course you
  • 00:02:34 could set this up in a different way but
  • 00:02:35 that's the way I will deal with in this
  • 00:02:37 application
  • 00:02:39 next I will duplicate this because title
  • 00:02:42 is exactly the same definition as is
  • 00:02:45 description and now regarding the price
  • 00:02:49 I will make this a number so this
  • 00:02:57 defines the schema and the schema is the
  • 00:02:59 blueprint we will use for each new entry
  • 00:03:03 we enter into the database however this
  • 00:03:06 plume print is well as as a just a
  • 00:03:09 blueprint it will not be with which we
  • 00:03:11 work in our application we will work
  • 00:03:13 with models however those models will be
  • 00:03:16 based on this blueprint now to make this
  • 00:03:20 happen and you to do this I will export
  • 00:03:24 something from that file here with model
  • 00:03:25 exports because of course I want to be
  • 00:03:28 able to import the functionality of this
  • 00:03:30 file and other files of my application
  • 00:03:32 and what I will export here is long use
  • 00:03:35 and then the model function and this
  • 00:03:38 model method here allows me to specify
  • 00:03:41 the name of the model I will name it
  • 00:03:43 product here and then the schema on
  • 00:03:46 which this model should be based in this
  • 00:03:48 is of course schema I defined up here
  • 00:03:50 and with that I'm using my blueprint to
  • 00:03:54 actually create a model with which I can
  • 00:03:56 work so this is set up the next step is
  • 00:04:00 to actually seed some data into this
  • 00:04:02 database so then we have something to
  • 00:04:04 work with now there are different ways
  • 00:04:07 you could do this you can store some
  • 00:04:09 external packages which make seeding
  • 00:04:12 symbol or like a loo-loo here you write
  • 00:04:15 your own seed or because it's no rocket
  • 00:04:17 science and if it would be you would
  • 00:04:19 learn a lot right so great thing so I
  • 00:04:21 will create a new directory which I'll
  • 00:04:23 call seed and then the
  • 00:04:26 directory I will create a new file and I
  • 00:04:28 will call this file product cedar now
  • 00:04:32 there are different setups you could
  • 00:04:33 choose here and should have kept up case
  • 00:04:39 but I will go with that it's totally up
  • 00:04:43 to you though so I will create a cedar
  • 00:04:46 per model you could have one cedar for
  • 00:04:50 all the data but for an hour legal with
  • 00:04:53 that approach here so I will import my
  • 00:04:57 product model by simply requiring the D
  • 00:05:03 file in the models folder where I export
  • 00:05:05 this model so I will navigate their
  • 00:05:08 models product notice that I don't have
  • 00:05:12 the dot J s at the end you never have
  • 00:05:13 that when requiring other files energy
  • 00:05:16 has and next step is I will create a new
  • 00:05:20 product so I will create product new
  • 00:05:26 variable which will be a new product the
  • 00:05:29 model I import here and then I pass
  • 00:05:31 JavaScript object with the bait above
  • 00:05:33 the actual data I want to write into the
  • 00:05:35 database so I will give this an image
  • 00:05:41 path and I will just pick the path of my
  • 00:05:45 view so the Harry Potter image or gothic
  • 00:05:51 Harry Potter was in the level app so the
  • 00:05:54 Gothic game just add this path here next
  • 00:06:01 step I will add a title give to the name
  • 00:06:05 gothic since this is the title of the
  • 00:06:07 game or in Gothic video game and next
  • 00:06:12 the description also gave of course and
  • 00:06:17 more exclamation marks always make a
  • 00:06:20 message more meaningful so always put as
  • 00:06:24 many exclamation marks where you as you
  • 00:06:26 can
  • 00:06:27 sarcasm of so with that next field is
  • 00:06:31 the price and I'll just give the
  • 00:06:34 surprise of ten dollars now this will
  • 00:06:37 create one product however
  • 00:06:38 I want multiple products how do I do
  • 00:06:41 this there are a lot of ways you could
  • 00:06:45 do that I will go with the following I
  • 00:06:47 will make this products and array like
  • 00:06:53 this and next step is I will simply
  • 00:07:00 create multiple products like this new
  • 00:07:02 product another new product now I don't
  • 00:07:04 want to have gothic all over a place but
  • 00:07:07 I also don't want to bore you with me
  • 00:07:09 typing or searching for fitting products
  • 00:07:12 here for the next 10 minutes so I'm
  • 00:07:13 going to quickly pull this out and then
  • 00:07:15 be right back
  • 00:07:16 with that array of products I want to
  • 00:07:19 store all the products in the database
  • 00:07:21 now how do I do this well I will simply
  • 00:07:25 loop through all these products so loop
  • 00:07:30 through and as long as we have products
  • 00:07:34 in this area – well to use and then I
  • 00:07:38 want to well store it in here now I can
  • 00:07:41 simply call products save because the
  • 00:07:46 save method with Mongoose allows me to
  • 00:07:48 save a model to the database so Mongoose
  • 00:07:51 will then create a new collection for
  • 00:07:54 that model so for let's say the product
  • 00:07:57 model model it will create a products
  • 00:07:59 collection and into this collection it
  • 00:08:02 will then insert or save this new
  • 00:08:05 document so it's like with my sequel
  • 00:08:08 where you have database tables and
  • 00:08:11 entries here you have collections and
  • 00:08:14 documents so it will create a new
  • 00:08:16 document with the save command based
  • 00:08:18 upon the data I work with here however
  • 00:08:21 this doesn't work here because Mongoose
  • 00:08:24 or MongoDB will not be connected to this
  • 00:08:27 application now you could say well we're
  • 00:08:30 connecting it at the app.js file right
  • 00:08:31 yes we are but the seeder will not be
  • 00:08:35 run in the normal application it will
  • 00:08:37 not be running all the time during run
  • 00:08:39 time because that's something I only
  • 00:08:41 want to do here during development or at
  • 00:08:44 the beginning but not it's not a normal
  • 00:08:47 part of the application so I will
  • 00:08:50 manually run this file with
  • 00:08:52 no chess and in order to do this I need
  • 00:08:56 to connect to Mongoose inside of this
  • 00:08:58 file here so I will quickly do this
  • 00:09:01 require Mongoose here and then of course
  • 00:09:04 copy the connect logic from the app.js
  • 00:09:08 file and at the end of the file I will
  • 00:09:14 disconnect also I spotted a little error
  • 00:09:19 I had in when I created this model this
  • 00:09:24 should be module exports
  • 00:09:25 I had model exports which is not correct
  • 00:09:28 to many models in my head so modules if
  • 00:09:31 I do it like this what will happen
  • 00:09:34 okay this products air is created we
  • 00:09:38 started with this loop we save the
  • 00:09:40 products then we disconnect now that's
  • 00:09:43 not going to happen
  • 00:09:44 saving to the database is asynchronous
  • 00:09:46 what will happen is we will start this
  • 00:09:49 loop we will loop through all the items
  • 00:09:52 and initiate is saving for each item but
  • 00:09:56 then we will continue to disconnecting
  • 00:09:58 and chances are high that we will
  • 00:10:00 disconnect before all the items have
  • 00:10:02 been saved therefore this is not the
  • 00:10:05 right place for the disconnect
  • 00:10:06 disconnect method here
  • 00:10:08 I should disconnect in the call back off
  • 00:10:11 this save function here however that
  • 00:10:13 would also not be the right place
  • 00:10:14 because then I would disconnect arm
  • 00:10:18 while on each object here so for for the
  • 00:10:24 first one for the second one and so on
  • 00:10:25 so that also doesn't work how can we fix
  • 00:10:28 this I will implement a little helper
  • 00:10:30 variable which we'll call done which is
  • 00:10:32 0 at the beginning and then in the whole
  • 00:10:35 back of each save arm operation here
  • 00:10:40 where you have an error or a result in
  • 00:10:43 this callback here I will increment done
  • 00:10:46 by 1 and then I will check if done is
  • 00:10:48 equal to products length so if we just
  • 00:10:54 finished with the last item in the array
  • 00:10:55 and therefore I have to increment first
  • 00:10:57 because the products length will be not
  • 00:11:01 starting at 0 like the index but instead
  • 00:11:03 of an array has 5 items the length
  • 00:11:05 five but the last item has to index four
  • 00:11:08 keep that in mind therefore I'm
  • 00:11:09 incrementing it first so if this is
  • 00:11:12 equal I know that I'm done and then I
  • 00:11:14 can call the exit function which I will
  • 00:11:17 did find out your function exit and end
  • 00:11:22 this function I will disconnect now this
  • 00:11:25 should work so if I never get into the
  • 00:11:27 seed folder and run my product cedar
  • 00:11:29 chest file and then I will go to my
  • 00:11:32 server now we'll open you terminal and
  • 00:11:34 also navigate into that MongoDB binary
  • 00:11:37 folder and I will run Mongo so not Mongo
  • 00:11:41 DeWitt was the server but just Mongo
  • 00:11:43 which is declined a shell client here I
  • 00:11:45 do this to have a look into the database
  • 00:11:48 so I'm going to use my shopping database
  • 00:11:50 then IX action by typing DB dot products
  • 00:11:56 since I said Mongoose will create
  • 00:11:59 collections which you have well the
  • 00:12:02 pluralized name of the models of the
  • 00:12:05 models product this will be products
  • 00:12:07 lowercase and then the find method to
  • 00:12:11 see all documents in this collection and
  • 00:12:13 this looks pretty good so with that the
  • 00:12:15 data is in the database I got my cedar
  • 00:12:17 working and the next thing I want to do
  • 00:12:20 is of course output those items in my or
  • 00:12:24 on my index view