Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Please use Cockroach website for this assignment:Open the shell and install knex.Referring to the directions from the link above, create a knexfile.Change the client property

Please use Cockroach website for this assignment:Open the shell and install knex.Referring to the directions from the link above, create a knexfile.Change the client property in the development object to have the appropriate value.Try to create a new migration (refer to the docs link above to do this). You'll get an error.Based on the error you see, install the necessary node module and try creating a new migration file again after doing so.In the up function, using the knex Schema Builder, first await the result of creating a new schema molloyEats if it doesn't already exist. There is no knex function to do this directly so you will use a raw query , which allows you to pass the exact SQL command to use. REMEMBER THAT ALL QUERIES MUST END IN A SEMICOLON IN PostgreSQL. After this await statement, create a table menu in this new database named molloyEats (this is the schema, or the namespace you'll use). The database will have columns with an autoincremented primary key id, a name that's a string, a description that's a string (could be null), and a price that's numeric (specificType('price', 'money') won't work for some reason even though PostGres has a money type.) This will evaluate to a Promise so you can use catch to log any errors.In the down function. drop the table (to provide the instructions of how to undo or rollback this migration). You can also catch any errors.If you haven't already done so, create an environment variable (secrets on replit.com) for the connection string you got from CockroachDB. Then replace the connection property's value with this string in the development property in the knex file.Run this migration (or all of the ones that haven't been run) using the appropriate command in the shell. Again, refer to the docs in the link above.Referring to the docs again, enter the command in the shell that's used to create a new seed file.Next, copy/paste the JSON data from $WIKI_REFERENCE$/pages/menu-dot-json and paste its contents in a new file menu.json, which can be placed in this seeds directory for simplicity now. Note: This is the data file, not the script to seed the menu table with this data.In the newly created file with exports.seed, load the data from this file and insert it into the table with this schema. You can define a variable to hold the data and then replace what's currently being inserted with the value of the variable instead. Note: When referring to this table, you would prefix it with the database name and a dot before the table name. Note: Also that you will use JSON.parse since the contents of the file will be a string. Finally, after you do, you will get an Array of rows. Note PostGres can insert all the rows with a single insert command (a so-called batch insert) although you don't have to worry about this when you use an ORM such as Knex as it generates the raw queries for you:Again, refer to the documentation, this time to run the seed file(s).Create a new file menu.js in your routes directory. (READ): Add a route for GET requests to /menu that takes a query string with a name argument (again using module.exports as you did with the other routes files). Add a JOI validation to make sure that it's a string if it's provided, but it's optional, not required. Look up how to do this as well.Add a menu.js file to the models directory. In this file, first follow the example from to initialize knex and give the newly created object to Objection. However, use the development object from knexfile.js for the configuration object you give as an input to Knex (How do you do this? Do NOT copy/paste it!)Create a Menu class inheriting from the Model class. For tableName, remember that you'll need to refer to the menu with the schema as you did in the seed file. As noted in :A Model subclass represents a database table and instances of that class represent table rows. Models are created by inheriting from the Model class.Create a JSON Schema for this table with the appropriate types for id, name, price, and description. This can be used to ensure that when you try to insert, update (replace), or patch (change column values of) rows in the database, Objection will check to make sure you're giving the database an acceptable input before the database query is made. This avoids unnecessary queries to the database that would result in errors.Add a menu.js file in the services directory and create a new MenuServices class. Make a new instance of it an export as you did in the other services files. Import Menu from the menu model file.Add an async method getMenu that takes a default parameter name as an input and returns a Promise resolving to the Array of all rows with menu item names or descriptions that contain name as a substring, ignoring case. What should its default value be given you will want all of the menu items to be returned when no input is provided? The return statement will be an await with a query to Menu where you select the name, price, and description columns where the name contains name or where the description contains name as a substring. HINT: Refer to the select AND the various types of where clauses in the Knex documentation that you can use. Note that if it works on a Knex QueryBuilder object, it will also work on an Objection QueryBuilder object, which you can create from the Menu model.Add a menu.js file in the controllers directory and create a new class MenuController. Make a new instance of it an export as you did in the other controller files. OR just use static methods and export a reference to the class. When a method is static, it belongs to the class and not objects of that class. Therefore, you would not refer to instance variables such as totalMiles of a particular Car (i.e., you wouldn't be using this._______) and it's called on the class itself (e.g., MenuController.getMenu).In the MenuController class, import menuService and add a getMenu function that takes a request object as an input and calls getMenu from the menuService, passing the name parameter of the query string. await the result from this function and return a JSON string from the result. Check that this API endpoint works by adding /menu at the end of your repl.it link! Then go to /menu?name=fruit. You should get the following:(CREATE) Go back to the menu routes file and add a route that accepts POST requests to /menu/add. This time add a validation of payloads: name should be a required string, price should be a required number, and description should be an optional string. The handler should be the Menu Controller's addItem function, which you'll be adding after the next step to define the menu service function it will depend on.Define an async addItem function in MenuServices that takes name, price, and description as inputs. First make a query to find a row where the name is equal to the inputted one, again ignoring case. If there is no such row, insert the row into the database and return the awaited result. (Note the id is not required as this is an autoincremented column). Otherwise, return false.Next, in the menu controller file, extract the name, price, and description from the request input and call the menu service addItem function. Return the resolved value if it isn't false or an error string indicating that there's already a menu item with that name, otherwise. For debugging purposes, you might want to wrap the code starting with the call to the menu service addItem function in a try block. In the catch block, you can return the error. Test this with fetch or you can use curl to make a few requests in a Command Prompt or Terminal on the Mac:You can also do this with fetch in the Developer console. For example:; (DELETE) Go back to the menu routes file and add a route that accepts DELETE requests to /menu/remove. This time add a validation of payloads: name should be a required string. The handler should be the Menu Controller's removeItem function, which you'll be adding after the next step to define the menu service function it will depend on.Define an async removeItem function in MenuServices that takes name as an input. As before, make a query to find a row where the name is equal to the inputted one, again ignoring case, but this time delete (all) such rows from the database (there should be at most one if you did the previous step correctly) and return the awaited result. This should be the number of deleted rows, if you used the function I'm expecting here.Next, in the menu controller file, extract the name from the request input and call the menu service removeItem function. Return a string with the number of rows removed or an error that an item with that name doesn't exist if 0 was returned. For debugging purposes, you might want to wrap the code starting with the call to the menu service removeItem function in a try block. In the catch block, you can return the error. Test this with fetch or you can use curl to make a few requests in a Command Prompt or Terminal on the Mac. For example:(UPDATE) Go back to the menu routes file and add a route that accepts PUT requests to /menu/update. A PUT request is differentiated from a POST request by its idempotency: From : "calling it once or several times successively has the same effect (that is no side effect), whereas successive identical POST requests may have additional effects, akin to placing an order several times."As before, add a validation of payloads: name should be a required string, price should be a required number, and description should be an optional string. The handler should be the Menu Controller's updateItem function, which you'll be adding after the next step to define the menu service function it will depend on.Define an async updateItem function in MenuServices that takes name, price, and description as inputs. As before, make a query to find a row where the name is equal to the inputted one, again ignoring case, but this time update (all) such rows from the database (there should be at most one if you did the previous step correctly) with the new name, price, and description and return the awaited result. This should be the number of updated rows, if you used the function I'm expecting here.Next, in the menu controller file, extract the name, price and description from the request input and call the menu service updateItem function. Return a string with the number of rows updated or an error that an item with that name doesn't exist if 0 was returned. For debugging purposes, you might want to wrap the code starting with the call to the menu service updateItem function in a try block. In the catch block, you can return the error. Test this with fetch or you can use curl to make a few requests in a Command Prompt or Terminal on the Mac.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions