Automating Google Workspace (GWS) & more with Apps Script
Â
MEAN Stack Workshop at Node Philly, 4/9/14
1. 1 Hour MEAN Stack Hackathon
Valeri Karpov
Software Engineer, MongoDB
www.thecodebarbarian.com
www.slideshare.net/vkarpov15
github.com/vkarpov15
@code_barbarian
Building a Food Journal Single Page App + Workflow
2. *
Who is this guy?
â˘Coined the MEAN stack in April â13
â˘Contributor to:
⢠node-mongodb-native
⢠mongoose
⢠mquery
⢠omni-di, etc.
â˘AngularJS since 0.9.4 in 2010
â˘Production MEAN apps: Ascot Project, Bookalokal
3. *
General Outline
â˘Building a single page app - LeanMEAN
â˘Food journal counts calories for you (FitDay)
â˘MEAN = MongoDB, ExpressJS, AngularJS, NodeJS
â˘Additional tools:
⢠browserify
⢠make
⢠omni-di
⢠mongoose
⢠MongoDB 2.6 text search
⢠PassportJS / Twitter oauth
5. *
Beyond the Hack
â˘Nitty-gritty deep dive into code and workflow
â˘Build tools and workflow: browserify, make
â˘Code organization: browserify, omni-di
â˘Unit testing and benchmarks: mocha
6. *
Step by Step
â˘Step 0: Understand SR-25 data set
â˘Step 1: Create Express app
â˘Step 2: Restructure Express app
â˘Step 3: Construct Models
â˘Step 4: Define API
â˘Step 5: Set up client-side routing
â˘Step 6: Client-side integration with API
7. *
Step by Step, Continued
â˘Step 7: Unit testing
â˘Step 8: Authentication
9. *
Step 0: USDA SR-25 Nutrition Data
â˘Need data: calories, carbs, lipids for common foods
â˘Thankfully available from USDAâs website
â˘mongorestore-friendly dump for MongoDB here
â˘My blog post about the data set
14. *
Text Search in MongoDB 2.6
â˘Donât want users to have to enter âKale, rawâ
â˘Example: top 3 results for âgrass-fed beefâ
15. *
Text Search in MongoDB 2.6
â˘Static data = perfect use case for text search
â˘Need to create a text index first from shell:
⢠db.nutrition.ensureIndex({ description :
âtextâ });
â˘Read more here
16. *
Step 1: Creating an Express App
â˘Can create an Express app with 2 commands:
⢠`npm install express -g` installs Express
⢠`express lean-mean-nutrition-sample` creates the
app
17. *
Step 2: Restructuring the App
â˘Single page app doesnât need Jade templating
â˘views folder obsolete
â˘Set up package.json file
â˘package.json - workflow for setting up env:
⢠`git clone`: pull down repo
⢠`npm install`: install dependencies
⢠`npm test`: run tests
⢠`npm start`: start the server
19. *
Step 3: Create Database Schema
âOntology deals with questions concerning what entities
exist or can be said to exist, and how such entities can
be grouped, related within a hierarchy, and subdivided
according to similarities and differencesâ
- Wikipedia article on ontology
20. *
Quick Overview of Mongoose
â˘ODM for MongoDB and NodeJS
â˘Schema design and validation
â˘Convenience objects
â˘MEAN Stackâs official best friend
21. *
Objects in LeanMEAN World
â˘FoodItem: from SR-25
â˘User: because any real API scopes by user
â˘Day: the FoodItems a User ate on a given date
25. *
Day Schema Subtleties
â˘Want to let user select from multiple weights
â˘Want user to enter custom amount for a weight
â˘Difference between selectedWeight / weights
â˘Nutrient amounts per 100G
26. *
Omni-di to tie this all together
â˘Avoid dependency hell: donât require in every file!
34. *
Wait, Whereâs The Work?
â˘Mongoose validates client foods data w/ schema
â˘Only modifying foods - free access control
â˘No need to check if date exists: upsert flag
â˘isNew flag in `GET /api/date/:date`
35. *
Step 5: AngularJS + Browserify
â˘Single Page App: how to manage code bloat?
36. *
Browserify = Write Node For Client
â˘AngularJS dependency in package.json
â˘Never deal with flakey CDNs again!
37. *
Build Process with Browserify
â˘Output: all.js, contains all JS in 1 file
â˘Input: all files in client directory + dependencies
â˘browserify -o ./public/javascripts/all.js
client/*
â˘Or, make build_client
42. *
Why Single Page App?
â˘No server side templating:
⢠Better server throughput
⢠Cleaner separation of concerns
⢠Less bandwidth usage
â˘More control over UX
43. *
Step 6: Letâs Build a Client!
â˘AngularJS controller for each particular view
â˘Right now only need TrackController
â˘Controller talks to server
â˘Controller provides API for UI
51. *
NodeJS SPA and Code Sharing
â˘Code sharing is trivial with browserify
â˘MEAN stack principle: The objects your server deals
with should be almost identical to the objects your client
deals with and the objects stored in your database.
â˘Same objects => easy code sharing
â˘Calculations a good candidate in this case
55. *
Get Serious About Testing
â˘Foundation: proper unit tests, automation
â˘Heuristic: code âworksâ iff npm test succeeds
â˘Grunt or Makefile, either works well
56. *
Omni-di and unit tests
â˘Beauty of DI: easy to control how much to stub
â˘For unit tests, stub everything
64. *
Authentication in SPA
â˘PassportJS makes oauth easy
â˘But⌠requires redirect
â˘Not that much of a problem
â˘Handle cases where user hits API but not logged in
72. *
And thatâs a wrap! Time to Review
â˘Single page app with MEAN stack
â˘AngularJS routing
â˘Browserify for building client code
â˘Validating complex objects with Mongoose
â˘MongoDB text search
â˘Testing and automation
â˘Twitter Oauth
73. *
Thanks for Listening!
â˘Slides on:
⢠Twitter: @code_barbarian
⢠Slideshare: slideshare.net/vkarpov15
â˘Repo on github: github.com/vkarpov15/lean-mean-
nutrition-sample
â˘Blog post on SR-25 data set