Skip to main content
Version: 2.5.0 (terbaru)

The Shell Scripts

Your models are ready to be used. As in the previous tutorial, you will use shell scripts to feed the database.

The create-user script#

A script called create-user already exists in the scripts directory.

Open the file and replace its content with the following:

// 3pimport { hashPassword } from '@foal/core';import { createConnection, getConnection } from 'typeorm';
// Appimport { User } from '../app/entities';
export const schema = {  additionalProperties: false,  properties: {    email: { type: 'string', format: 'email', maxLength: 255 },    password: { type: 'string' },    name: { type: 'string', maxLength: 255 },  },  required: [ 'email', 'password' ],  type: 'object',};
export async function main(args: { email: string, password: string, name?: string }) {  const user = new User(); =;  user.password = await hashPassword(args.password); = ?? 'Unknown';  user.avatar = '';
  await createConnection();
  try {    console.log(await;  } catch (error) {    console.log(error.message);  } finally {    await getConnection().close();  }}

Some parts of this code should look familiar to you.

The schema object is used to validate the arguments typed on the command line. In this case, the script expects two mandatory parameters email and password and an optional name. The format property checks that the email string is an email (presence of @ character, etc).

The main function is called after successful validation. It is divided into several parts. First, it creates a new user with the arguments specified in the command line. Then it establishes a connection to the database and saves the user.

The hashPassword function is used to hash and salt passwords before storing them in the database. For security reasons, you should use this function before saving passwords.

Build the script.

npm run build

Then create two new users.

foal run create-user email="" password="john_password" name="John"foal run create-user email="" password="mary_password" name="Mary"

If you try to re-run one of these commands, you'll get the MySQL error below as the email key is unique.

ER_DUP_ENTRY: Duplicate entry '' for key 'IDX_xxx'

The create-story script#

The create-story script is a bit more complex as Story has a many-to-one relation with User.

foal generate script create-story

Open the create-story.ts file and replace its content.

import { createConnection } from 'typeorm';import { Story, User } from '../app/entities';
export const schema = {  additionalProperties: false,  properties: {    author: { type: 'string', format: 'email', maxLength: 255 },    title: { type: 'string', maxLength: 255 },    link: { type: 'string', maxLength: 255 },  },  required: [ 'author', 'title', 'link' ],  type: 'object',};
export async function main(args: { author: string, title: string, link: string }) {  const connection = await createConnection();
  const user = await User.findOneOrFail({ email: });
  const story = new Story(); = user;  story.title = args.title; =;
  try {    console.log(await;  } catch (error) {    console.error(error);  } finally {    await connection.close();  }}

We added an author parameter to know which user posted the story. It expects the user's email.

The main function then tries to find the user who has this email. If it exists, the user is added to the story as the author. If it does not, then the script ends with a message displayed in the console.

Build the script.

npm run build

And create new stories for each user.

foal run create-story author="" title="How to build a simple to-do list" link=""foal run create-story author="" title="FoalTS architecture overview" link=""foal run create-story author="" title="Authentication with Foal" link=""