Skip to content

Instantly share code, notes, and snippets.

@edwardsmarkf
Last active October 26, 2017 16:14
Show Gist options
  • Save edwardsmarkf/1cfb3a50ae4aab75958f138e0d78c127 to your computer and use it in GitHub Desktop.
Save edwardsmarkf/1cfb3a50ae4aab75958f138e0d78c127 to your computer and use it in GitHub Desktop.
trails-sequelize-express-sqlLite
Instructions For Creating a Working Trails-Sequelize-Express-SqlList Environment
These instructions are written for the complete beginner who has never used trailsJS before. I am starting out
with absolutely nothing, except a ten cent ($0.10) charge for a google-cloud virtual server.
With the exception of the "yo" command, this script could be ran as a complete script. My suggestion is to copy/paste
the top half, run "yo" manually, and run the bottom half as copy/paste.
Optionally, it may be more educational to run each command individually. YMMV.
A very special thank-you goes out to Scott B. Wyatt who was the sole inspiration behind this script, and whose extraordinary
patience with newbies made this script possible. Written from here:
https://github.com/scott-wyatt/trails-example-sequelize/blob/master/newbie-instructions.md
## install getpostman:
## postman is a great free tool for testing POST, PUT, and GET requests.
##
https://www.getpostman.com/
## create a virtual server:
##
https://console.cloud.google.com
1) name your virtual server something like trails-express-sequelize-sqlLite
2) create machine-type: micro (saves you $$)
3) choose centos7 environment (centos7 is the best environment to use)
## connect to your new virtual server and run the following initial setup steps (5-10 minutes)
##
set -o vi ; ## optional, but i love it.
sudo yum --assumeyes update ; ## optional but always a good idea
sudo yum --assumeyes gcc gcc-c++ ; ## optional
sudo yum --assumeyes install bzip2 bzip2-devel curl git tar wget yum-utils ;
sudo yum --assumeyes groupinstall "Development Tools" ;
## we need to open the port since trails uses port 3000 by default:
##
sudo firewall-cmd --zone=dmz --add-port=3000/tcp --permanent ; sudo firewall-cmd --reload ;
## install node & npm
##
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash - ;
sudo yum --assumeyes install nodejs;
node --version ; ## look for v8.8.0 (at the time of this writing)
## install trails
##
sudo npm install -g yo generator-trails ;
## create our working directory and switch into it
##
mkdir trails-test; cd trails-test;
## manually run yo trails
##
yo trails ; ## notice defaults below:
## Get ready to blaze a new Trails Application!
##
## Checking for updates...
## ? Choose a Web Server express [default WAS 'hapi']
## ? What express version do you want to use ? 5 [default WAS 4]
## ? Choose an ORM sequelize [default WAS 'waterline']
## ? Do you want to use Footprints (automatic REST API from models) ? Yes
## ? Module Name trails-test
## ? Description
## ? Project homepage url
## ? Author's Name
## ? Author's Email [email protected] [default WAS blank]
## ? Author's Homepage
## ? Package keywords (comma to split)
## ? GitHub username or organization Guilhemehss
## ? Which license do you want to use? MIT [default was Apache]
##
## required node packages
##
npm install sqlite3 --save ;
## lets get some more debug info!
##
/usr/bin/sed -i "s/level: 'info'/level: 'debug'/;" ./config/log.js ;
## uncomment out the sqlite section in ./config/database.js
##
/usr/bin/sed -i '/^\s\{4\}\/\*$/d; /^\s\{4\}\*\//d; ' ./config/database.js ;
## add a line at the end of the ./api/models/index.js file
##
/usr/bin/cat <<END >> ./api/models/index.js ; ## notice we are merely ADDING a line here
exports.Mytable = require('./myTable')
END
## create our model
##
/usr/bin/cat <<END >./api/models/myTable.js ;
'use strict'
const Model = require('trails/model')
module.exports = class Mytable extends Model {
static config(app, Sequelize) {
return {
options: {
underscored: true
}
}
}
static schema(app, Sequelize) {
return {
username: {
type: Sequelize.STRING
},
}
}
}
END
## finally, start the app
##
npm start;
## in postman: get 'external IP' from console)
## GET http://123.123.123.123:3000/api/v1/mytable/ ( result should be "[]" )
## POST http://123.123.123.123:3000/api/v1/mytable/ {"username": "New Name Test"}
## GET http://123.123.123.123:3000/api/v1/mytable/
Outstanding issues:
hapi would not work while express did. this should be investigated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment