ibm-strongloop-tech-sales-enablement / mysqldemo-lab-enablement-sl

A quick demonstration of using StrongLoop to Auto Discover MySQL.
0 stars 0 forks source link

Quick Demo Tutorial

This tutorial will teach you how to quick demonstrate StrongLoop focusing on Arc. By the end of this tutorial you should be able to:

  1. Create a StrongLoop Product
  2. Use Composer to discover models from a MySQL database
  3. Preview APIs using Explorer

All demonstrations are presented using a Macintosh.

Please direct any questions to: Ken Nelson e: ken.nelson@us.ibm.com p: (949) 444-6308 t: @KenAtIBM

Last updated: 2/1/2016

Pre-requisites:

  1. nodejs installed. For more information on installing node, please visit [www.nodejs.org]
  2. StrongLoop installed. For more information on installing StrongLoop, please visit [www.strongloop.com]
  3. A directory for creating projects. Recommended that you create a directory to store projects.

Download and Run Option

If you want to just run this application, you can clone it and perform an npm install. Otherwise scroll down to the Script section to manually create.


Script

Create a StrongLoop Project

Create a project using the slc loopback command. This will create the initial project and install the necessary libraries from npm. Steps

Create Project

Add the MySQL Connector

Add and save the Loopback MySQL Connector by installing from npm.

Steps

Install Loopback Connector for MySQL

The —-save will add the requirement for having the MySQL connector. Typing npm install in the project directory will ensure that the necessary files are downloaded and installed as well as any updates.

Create a MySQL Connection

There are a few ways to add a connection to your project. The first option is to use StrongLoop Arc. Second option is to use the StrongLoop command line. The third option is to edit the server/datasources.json file directly. For the purposes of this tutorial, use the StrongLoop Arc option to create a connection to the StrongLoop MySQL sample database. Steps

Discover Models

StrongLoop Composer has the ability discover models from relational databases with the click of a button. Steps

Review the Models with StrongLoop API Explorer

StrongLoop provides a [Swagger] interface to the APIs that have been created.

Review APIs

Play around with the available APIs. You will notice that there is a User Model that you did not create. This is a special model that is a descendent of PersistedModel and used for access management. This model will be used in future labs, you can ignore it for now.

Standard Project Structure

When creating a StrongLoop projects, a standard directory structure is created. The directories that are most important for creating APIs are: ~/server ~/server/boot ~/common/models

The sever directory contains all of your configuration scripts as well as a boot directory. The standard configuration files are;

The boot directory contains scripts that are run as the server is started. There are two standard files in the boot directory as well as any custom scripts;

(Optional) Review the Datasources.json File

The datasources.json file shows all the connections that have been created either thru StrongLoop Arc or the StrongLoop command line. You can also manually add connections by editing this file.

Open the datasources.json file. It should contain the following:

"db": {
    "name": "db",
    "connector": "memory"
  
},
"SL-Mysql": {
    "host": "demo.strongloop.com",
    "port": 3306,
    "database": "demo",
    "password": "L00pBack",
    "name": "SL-Mysql",
    "user": "demo",
    "connector": "mysql"
}

(Optional) Review the model files

When discovering the models, StrongLoop automatically created two files for each model. In the common/models directory you should see the following:

The second file is a JSON file that describes the components of the model as well as any mappings. The JSON file also maintains information about model relationships, security, any validations, as well as any methods created. Those topics will be covered in subsequent labs.


(Optional) Remove User Model

To remove the user model from the list of available APIs, open the model-config.json file. The model-config.json file configures LoopBack models, for example it binds models to data sources and specifies whether a model is exposed over REST.  The models referenced in this file must be either a built-in models or custom models defined by a JSON file in the common/models/ folder.

Steps

"User": {
    "dataSource": "db",
    "public": false
},

++Adding the public is false clause will remove the User model from the available APIs.++

(Optional) Remove Methods

To limit the available methods for a particular model, use the disableRemoteMethod method. Your existing Customer model in Explorer should look like: Existing Customer Model

Steps

Your new Customer model in Explorer should look like: New Customer API


More information

StrongLoop Documentation

StrongLoop Blog

Signup for StrongLoop Newsletter

StrongLoop: Standard project structure

StrongLoop: Getting Started Tutorial - Part 1

StrongLoop: Getting Started Tutorial - Part 2

StrongLoop: Access Control Tutorial

StrongLoop: Creating Model Relations Tutorial

StrongLoop: YouTube Videos