The Shell Scripts
Like in the previous tutorial, you are going to use shell scripts to populate the database.
create-user
script#
The A script called create-user
already exists in the scripts/
directory. It has a lot of commented lines that let you create users with permissions and groups.
Open the file and replace its content with the following:
Some parts of this code should look familiar.
The schema
object is used to validate the arguments typed in the command line. In that case, the script defines two required parameters: an email and a password. The format
property checks that the email
string is really an email (presence of the @
character, etc).
The main
function is divided in several parts. First it instanciates a connection to the database. Then, it creates a new user with the arguments specified in the command line. The isCommon
function compares the given password with a list of ten thousands common passwords (ex: 123456
, password
, etc). It returns true if it is found in the list. Finally the user is saved in the database and, if an error is thrown, the error message is pretty printed.
As you may have noticed, the isCommon
utility comes from the @foal/password
package. You have to install it.
Now build the script.
Create two new users.
If you try to re-run one of these commands, you'll get the error below as the email key is unique.
SQLITE_CONSTRAINT: UNIQUE constraint failed: user.email
create-todo
script#
The The create-todo
script is a bit more complex as Todo
has a many-to-one relation with User
.
Open the create-todo.ts
file and replace its content.
We added an owner
parameter to know which user the todo belongs to. It expects the email of the user.
The main
function then tries to get the user who has this email. If he or she does not exist, then the script terminates with a message displayed in the console. If not, the user is added to the todo as her/his owner.
Build the script.
Create new todos for each user.