Controller
CRUD stands for
CREATE: create one or multiple new objectsREAD: read one, or several objectUPDATE: edit one or several objectsDELETE: delete one or several objects
Generally, you're going to:
create one object
update one object at a time (and maybe related objects)
delete one object at a time (and maybe related objects)
However, you will often have multiple READ methods: get one article, list all articles, list all articles by date, get all articles by author, search results, etc.
For our Contacts model, we will keep it simple. We want:
one method to create one contact. It will take a name and an email, as one object
one method to delete one contact. It will take an id
one method to update one contact. It will take an id, and either a name, an email, or both
one method to read one contact. It will receive an id
one method to read all contact, with options to order them by name, or by email
That's 5 methods total. We already know their signatures, thanks to the description above. Here are the signatures:
Note: the above is not valid Javascript. This is not actually valid anything. It looks a bit like ES6, a bit like Typescript, a bit like Haskell, and hopefully conveys 3 informations:
what the function is (
asyncorsync)what the function needs, in terms of arguments. I'm using
?to signify the argument is optional, and|to signify options (e.g.,a|bmeansaorb).what the function returns
We already have a skeleton of getContactsList. We only need to implement the order by logic.
As you see, we construct the statement differently, depending on the orderBy argument.
Let's implement them very basically:
We can try them. Open back/src/index.js, comment out everything inside ofstart`, and write instead:
Run it, observe the output in your terminal.
One confusing part might be this result.stmt stuff that is going on from time to time. This is returned from sqlite's run method, and has this shape:
lastID contains the last ID operated on, and changes contains a number representing the number of changes.
Take some time, try to understand what those methods are doing. Try thinking how you would, for example, implement a getContactByName or getContactByEmail function.
Do take a moment, because they're going to get more complex right after this. We need to do error checking, and make sure we receive the right arguments. While none of that is going to be difficult, those checks will decrease the readability of the code.
I will also add documentation, in the jsdoc format. This format is a comment, over the function, that explains what it does. This also pops up in some editors (like VSCode) when using the function.
Ready? Let's add checks:
Re-run the software. The output in the terminal should not have changed; however, if you try illegal things, it should not work.
Try to make the software crash by changing the arguments in index.js.
For example, let's try to add two users with the same email:
We should get an error. Once we've verified our controller works, we can restore index.js to what it was, and uncomment the previous code.
Last updated
Was this helpful?