hp


$ jsdoc -r ../code/NodeAPI-master/ -d ./NodeAPI-master

drop schema public cascade;
create schema public;
CREATE EXTENSION postgis;


By default the CLI will try to use the file config/config.json. You can modify that path either via the --config flag or via the option mentioned earlier. Here is how a configuration file might look like (that's is the one that sequelize init generates:




# http://dba.stackexchange.com/questions/45246/convert-units-of-measurement     http://dba.stackexchange.com/questions/18539/best-way-to-store-units-in-database
#paranoid instead of deleting setit to invisible
 npm install -g sequelize-cli

 git clone nodeAPI
 npm install mysql --save
 mysql
mysql> 

# use mysql workbench to generate the ER diagram you have drawn

 npm install --save sequelize         # sequelize-cli
 sequelize init
 sequelize model:create --name Department  --attributes name:string:allowNull:false
 sequelize model:create --name Aisle  --attributes name:string:allowNull:false
 sequelize model:create --name MeasurementUnit  --attributes name:string:allowNull:false,isMetric:boolean
#updated config/config.js
#updated bin/www


sequelize model:create --name Product --attributes name:string:allowNull:false,company:string,size:float:allowNull:false,description:text,image:string
sequelize model:create --name Store --attributes name:string:allowNull:false,address:string:allowNull:false,latitude:float:allowNull:false,longitude:float:allowNull:false,description:string
sequelize model:create --name Deal --attributes description:string,price:float:allowNull:false,fromDate:date,toDate:date
sequelize model:create --name Follow --attributes description:string   #couldnt make one without attributes.
#nodeAPI
sequelize model:create --name Person --attributes "username:string:allowNull:falsehashedPassword:string:allowNull:falsesalt:string:allowNull:false"
sequelize model:create --name RefreshToken --attributes "token:string:allowNull:false:unique:true"
sequelize model:create --name Client --attributes "name:string:allowNull:falseclientSecret:string:allowNull:false"
sequelize model:create --name AccessToken --attributes "token:string:allowNull:false"
sequelize model:create --name FailedAuthentication --attributes "description:string"   #couldnt make one without attributes.
sequelize model:create --name Role --attributes "title:string:allowNull:false" 
sequelize model:create --name ShoppingList --attributes "bought:boolean:allowNull:false, quantity:allowNull:false"
sequelize model:create --name unavailableDeal --attributes "description:string"   #couldnt make one without attributes.

# use command line to call api
http POST http://localhost:3000/api/departments name="my name 1"

$ npm install -g mocha
$ npm install --save-dev mocha   
Mocha is a unit testing framework, that I'm fond of. It allows you to find a suite of tests you want to run, then provides a done function you can call with each individual test is done. 
$ npm install --save-dev should
The module Should extends the native object prototype with functions for asserting values. Normally, extending the native object prototype is a dangerous thing to do, since it effects the entire JavaScript run time environment. But since the change is only going to last during the lifetime of the unit test, it's not as much of a concern to me
$ npm install --save-dev supertest

 mocha "test/**/*.js"
npm install -g nodemon  restart application on file change
nodemon ./bin/www





JSON.stringify(res.body, null, 4)

if(typeof body.aisles == "undefined" )
if( Object.prototype.toString.call( someVar ) === '[object Array]' ) {
    // alert( 'Array!' );
}







Creating and refreshing access tokens:

http POST http://localhost:3000/api/oauth/token grant_type=password client_id=android client_secret=SomeRandomCharsAndNumbers username=abcdefg@yahoo.com password=abcdefg1234            #user authenticates and gets a set of tokens
http POST http://localhost:3000/api/oauth/token grant_type=refresh_token client_id=android client_secret=SomeRandomCharsAndNumbers refresh_token=[TOKEN]                   # use refresh token to access the same resource
Creating your article data:

http POST http://localhost:3000/api/articles title=NewArticle author='John Doe' description='Lorem ipsum dolar sit amet' images:='[{"kind":"thumbnail", "url":"http://habrahabr.ru/images/write-topic.png"}, {"kind":"detail", "url":"http://habrahabr.ru/images/write-topic.png"}]' Authorization:'Bearer PUT_YOUR_TOKEN_HERE'     #access token
Updating your article data:

http PUT http://localhost:3000/api/articles/YOUR_ARTICLE_ID_HERE title=NewArticleUpdated author='John Doe' description='Lorem ipsum dolar sit amet' images:='[{"kind":"thumbnail", "url":"http://habrahabr.ru/images/write-topic.png"}, {"kind":"detail", "url":"http://habrahabr.ru/images/write-topic.png"}]' Authorization:'Bearer PUT_YOUR_TOKEN_HERE'      #bearer means access token
Getting your data

http http://localhost:3000/api/users/info Authorization:'Bearer PUT_YOUR_TOKEN_HERE'
http http://localhost:3000/api/articles Authorization:'Bearer PUT_YOUR_TOKEN_HERE'




# NEW USER
#create

http POST http://localhost:3000/api/people grant_type=password client_id=android client_secret=SomeRandomCharsAndNumbers username=abcdefg@yahoo.com password=abcdefg1234 name="Allen George"  # CREATE
http POST http://localhost:3000/api/oauth/token grant_type=password client_id=android client_secret=SomeRandomCharsAndNumbers username=abcdefg@yahoo.com password=abcdefg1234  # AUTHENTICATE TO GET TOKENs

http POST http://localhost:3000/api/oauth/token grant_type=refresh_token client_id=android client_secret=SomeRandomCharsAndNumbers refresh_token=3098bd61933fce77659b84f3a4329a881a8a273c8013881f4d8531316bedb27a

# create/access routes
http GET http://localhost:3000/api/people/1 Authorization:'Bearer 377db86877b4f7253166a9d192d30febafc1478ee7aba59da374916d27c47934'

http GET http://localhost:3000/api/people/1 Authorization:'Bearer 1836d5aac330aa497c99a26e01d89c854772a1a0032be53eea54c89c3aced468'

#ADMIN
http POST http://localhost:3000/api/people grant_type=password client_id=android client_secret=SomeRandomCharsAndNumbers username=m.shahriarinia@gmail.com password=test name="Morteza Shahriari Nia"  # CREATE
http POST http://localhost:3000/api/oauth/token grant_type=password client_id=android client_secret=SomeRandomCharsAndNumbers username=m.shahriarinia@gmail.com password=test  # AUTHENTICATE TO GET TOKENs
http GET http://localhost:3000/api/people/ Authorization:"Bearer 0b9d030b57cbf035e7cc8bb5362c55792a3628a400eb17ef3ee40bf852fd872b"



$ brew install postgresql
$ npm install pg-native



router.post('/', function(req, res, next) {
  passport.authenticate('bearer', { session: false}, function(err, user) {
    try{
   if (user){ // person
     var person = user;

   }else{
     httpResHandler.sendUnauthorized(res);
   }
  } catch(error){
  res.json(500,{message:error});
  }
  })(req, res, next);
});








on you DB make sure you run CREATE EXTENSION postgis;


CREATE INDEX  "Stores_location_gix" on "Stores" using gist(location);

-------------Insert 1M geoms

insert into "Stores" (location,address,"createdAt","updatedAt") 
select  ('SRID=4326;POINT('||random() * 100 - random() * 100  ||' '|| random() * 100  - random() * 100 ||')')::geometry ,'a',now(),now()
from generate_series(1,1000000); 

-------------SELECT
-- from http://postgis.refractions.net/documentation/manual-2.0/geometry_distance_centroid.html to avoid misordering of the basic <->
WITH index_query AS (
  SELECT ST_distance_sphere(location, 'SRID=4326;POINT(-82.373978 29.633657)'::geometry) as distance, ST_X("Stores"."location"), ST_Y("Stores"."location")
  FROM "Stores"
  ORDER BY location <-> 'SRID=4326;POINT(-82.373978 29.633657)'::geometry LIMIT 100) 
  SELECT * 
  FROM index_query 
  ORDER BY distance limit 10;   -- runtime 2.5ms

-- check if this way of calculating distance is eventually correct in real life 
select ST_distance_sphere('SRID=4326;POINT(-82.373978 29.633657)'::geometry, 'SRID=4326;POINT(-82.384234 29.621197)'::geometry)  -- correct result is 1704 meters

--- Deals of nearby stores
select * from "Deals" inner join (

WITH index_query AS (
 SELECT  *
   FROM "Stores"
 ORDER BY location <-> 'SRID=4326;POINT(-82.373978 29.633657)'::geometry LIMIT 100
 ) 
 SELECT *
   FROM index_query 
 where ST_distance_sphere(location, 'SRID=4326;POINT(-82.373978 29.633657)'::geometry) < 1900
 ORDER BY ST_distance_sphere(location, 'SRID=4326;POINT(-82.373978 29.633657)'::geometry) LIMIT 10 -- I might not need this
) AS "NearbyStores"

ON "Deals"."StoreId" = "NearbyStores".id

--- Deals by friends


sequelize migration:create --name geo
./node_modules/.bin/sequelize db:migrate
./node_modules/.bin/sequelize db:migrate:undo


$ cat cat.csv | sed 's/[^[:print:]]//g' > cat.o  # to get rid of non-printable characters












Subpages (1): Useful Snippets
Comments