Skip to content

Instantly share code, notes, and snippets.

Created September 28, 2013 09:28
Show Gist options
  • Save anonymous/6740316 to your computer and use it in GitHub Desktop.
Save anonymous/6740316 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Sep 28, 2013.
    329 changes: 329 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,329 @@

    #LoopBack Cheatsheet

    ## LoopBack from api-example
    Instantiate the sls-sample-app
    ```sh
    slc lb api-example
    cd sls-sample-app
    slc install strong-agent
    slc install
    slc run app.js
    ```

    ## LoopBack from base
    Create a custom LoopBack Node server
    ```sh
    slc lb workspace loopback-workspace
    cd loopback-workspace/
    slc lb project loopback-project
    cd loopback-project/
    slc install
    slc run .
    ```

    Add a custom model type : product
    ```sh
    slc lb model product
    ```

    #Running your Loopback server
    run
    ```sh
    slc run .
    ```
    debug
    ```sh
    slc debug .
    ```

    Run with verbose output
    ```sh
    DEBUG=* node app.js
    ```

    ##Exploring LoopBack
    Verify the server is running [localhost:3000/](http://localhost:3000/)
    API explorer [http://localhost:3000/explorer/](http://localhost:3000/explorer/)
    Routes Inventory [http://localhost:3000/routes](http://localhost:3000/routes)
    LoopBack Docs [http://docs.strongloop.com/loopback#modelfindfilter-callback](http://docs.strongloop.com/loopback#modelfindfilter-callback)


    Test the endpoint for the product model type: verif '[ ]' empty set
    [localhost:3000/products](http://localhost:3000/products)
    ```sh
    open http://localhost:3000/products
    ```

    Add some test data of type product
    ```sh
    //add some test data with curl to the 'product' model type
    curl -X POST -H "Content-Type:application/json" -d '{"name": "Product A", "inventory": 11, "UPC": "127890"}' http://localhost:3000/products;
    curl -X POST -H "Content-Type:application/json" -d '{"name": "Product B", "inventory": 22, "UPC": "127891"}' http://localhost:3000/products;
    curl -X POST -H "Content-Type:application/json" -d '{"name": "Product C", "inventory": 33, "UPC": "127892"}' http://localhost:3000/products;
    curl -X POST -H "Content-Type:application/json" -d '{"name": "Product D", "inventory": 44, "UPC": "123893"}' http://localhost:3000/products
    ```

    Test the endpoint for the product model type: and verify newly added product instances
    [localhost:3000/products](http://localhost:3000/products)


    LoopBack from base to sailboat app
    ===

    data Source : http://bluewaterboats.org/valiant-40/

    Start interactive mode
    ```sh
    slc model sailboat -i
    ```

    Define the schema
    ```sh
    "designer" "builder" "loa" "lwl" "beam" "draft" "ballast" "displacement" "sailarea"
    ```

    Add some default data to the repo

    ```sh

    curl -X POST -H "Content-Type:application/json" -d '{"designer": "Robert H. Perry","builder": "Valiant Yachts","loa": "39’ 11″ (12.16 m.)","lwl": " 34’ 0″ (10.36 m.)","beam": "12’ 4″ (3.76 m.)","draft": "6’ 0″ (1.83 m.)","ballast": "7,700 lbs. (3,493 kg.)","displacement": "22,500 lbs. (10,206 kg.)","sailarea": "772 sq. ft. (71.7 sq. m.)" }' http://localhost:3000/sailboats;

    curl -X POST -H "Content-Type:application/json" -d '{"designer": "Robert H. Perry","builder": "Valiant Yachts","loa": "37′ 0″","lwl": "31′ 7″","beam": "11′ 5″","draft": "5′ 9″","ballast": " 6,600 lbs","displacement": "17,000 lbs","sailarea": "667 sqft" }' http://localhost:3000/sailboats;

    curl -X POST -H "Content-Type:application/json" -d '{"designer": "Robert H. Perry","builder": "Valiant Yachts","loa": "37′ 0″","lwl": "31′ 7″","beam": "11′ 5″","draft": "5′ 9″","ballast": " 6,600 lbs","displacement": "17,000 lbs","sailarea": "667 sqft" }' http://localhost:3000/sailboats;

    curl -X POST -H "Content-Type:application/json" -d '{"designer": "Robert H. Perry","builder": "Valiant Yachts","loa": "37′ 0″","lwl": "31′ 7″","beam": "11′ 5″","draft": "5′ 9″","ballast": " 6,600 lbs","displacement": "17,000 lbs","sailarea": "667 sqft" }' http://localhost:3000/sailboats

    ```
    - template


    ```sh
    curl -X POST -H "Content-Type:application/json" -d '{"designer": "","builder": "","loa": "","lwl": "","beam": "","draft": "","ballast": "","displacement": "","sailarea": "" }' http://localhost:3000/sailboats
    ```

    Verify the data from the browser
    [localhost:/3000/sailboats](http://localhost:3000/sailboats);


    optional - from base
    ====

    add docs:
    insert app.docs({basePath: 'http://localhost:3000'});

    add the API Browser :
    http://localhost:3000/explorer/#!/sailboats/sail_find_get_4


    Mobile Application Integration from sample
    ===
    iOS App

    [ iOS Sample ](https://github.com/strongloop-community/loopback-mobile-getting-started)
    * Coming Soon* [ Android Sample ](https://github.com/strongloop-community/loopback-android-sample)
    [ Titanium Sample ](https://github.com/strongloop-community/loopback-titanium-sample)
    [ Xamarin Sample ](https://github.com/strongloop-community/loopback-xamarin-sample)
    * Coming Soon* [ Mobile Web Angular ](https://github.com/strongloop-community/loopback-angular-sample)

    Mobile Application Integration from base
    ===

    SDK install path : /usr/local/share/strongloop-node/strongloop/sdks
    ```sh
    open /usr/local/share/strongloop-node/strongloop/sdks/
    ```
    X.h #import <LoopBack/LoopBack.h>


    LoopBack from preconfigured mobile app and loopback-node-server
    ===
    ```sh
    git clone mobile-solution
    ```


    - (void) findProductLowestInventory {

    //Find the Product with the Product With the Lowest Inventory

    // Docs reference:
    // http://docs.strongloop.com/loopback/#example-request-4

    // HTTP REST Call:
    // http://localhost:3000/products?filter%5Blimit%5D=2

    void (^staticMethodErrorBlock)(NSError *) = ^(NSError *error) {
    NSLog( @"Error %@", error.description);
    [AppDelegate showGuideMessage: @"No Server Found"];
    };//end selfFailblock

    // Define the load success block for the LBModelPrototype allWithSuccess message
    void (^staticMethodSuccessBlock)() = ^(id result) {
    NSLog(@"Success on Static Method result: %@", result);

    LBModel *model = (LBModel *)[result objectAtIndex:0];
    self.labelMethod1.text = [[NSString alloc] initWithFormat:@"%@ - %@",
    [model objectForKeyedSubscript:@"name"] ,
    (int)[model objectForKeyedSubscript:@"inventory"] ];

    };//end staticMethodSuccessBlock

    LBModelPrototype *objectProto = [ [AppDelegate adapter] prototypeWithName:@"products"];

    [[ [AppDelegate adapter] contract] addItem:[SLRESTContractItem itemWithPattern:@"/products" verb:@"GET"] forMethod:@"products.filter"];

    //Product with lowest inventory
    // http://localhost:3000/products?filter[order]=inventory%20ASC&filter[limit]=1': The highest inventory products
    [objectProto invokeStaticMethod:@"filter" parameters:@{ @"filter[order]":@"inventory ASC",@"filter[limit]":@1} success:staticMethodSuccessBlock failure:staticMethodErrorBlock];

    }//end findPoductLowestInventory

    - (void) findProductLowestPrice {

    //Find the Product with the Product With the Lowest Inventory

    // Docs reference:
    // http://docs.strongloop.com/loopback/#example-request-4

    // HTTP REST Call:
    // http://localhost:3000/products?filter%5Blimit%5D=2

    void (^staticMethodErrorBlock)(NSError *) = ^(NSError *error) {
    NSLog( @"Error %@", error.description);
    [AppDelegate showGuideMessage: @"No Server Found"];
    };//end selfFailblock

    // Define the load success block for the LBModelPrototype allWithSuccess message
    void (^staticMethodSuccessBlock)() = ^(id result) {
    NSLog(@"Success on Static Method result: %@", result);

    LBModel *model = (LBModel *)[result objectAtIndex:0];
    self.labelMethod2.text = [[NSString alloc] initWithFormat:@"%@ - %@",
    [model objectForKeyedSubscript:@"name"],
    [model objectForKeyedSubscript:@"price"] ];
    };//end staticMethodSuccessBlock

    LBModelPrototype *objectProto = [ [AppDelegate adapter] prototypeWithName:@"products"];

    [[ [AppDelegate adapter] contract] addItem:[SLRESTContractItem itemWithPattern:@"/products" verb:@"GET"] forMethod:@"products.filter"];

    //Product with lowest inventory
    // http://localhost:3000/products?filter[order]=inventory%20ASC&filter[limit]=1': The highest inventory products
    [objectProto invokeStaticMethod:@"filter" parameters:@{ @"filter[order]":@"price ASC",@"filter[limit]":@1} success:staticMethodSuccessBlock failure:staticMethodErrorBlock];

    }//end findProductHighest


    //Filter Limit 2 http://localhost:3000/products?filter%5Blimit%5D=2
    //[objectProto invokeStaticMethod:@"filter" parameters:@{ @"filter[limit]":@2} success:staticMethodSuccessBlock failure:staticMethodErrorBlock];

    //Filter Pagination 2 http://localhost:3000/products?filter[limit]=2&filter[offset]=5
    // TODO [objectProto invokeStaticMethod:@"filter" parameters:@{ @"filter[limit]=2&filter[offset]=5":@2} success:staticMethodSuccessBlock failure:staticMethodErrorBlock];

    //Products with name Product A http://localhost:3000/products?filter[where][name]='Product A':
    // TODO [objectProto invokeStaticMethod:@"filter" parameters:@{ @"filter[where][name]":@"Product A"} success:staticMethodSuccessBlock failure:staticMethodErrorBlock];

    // Product with highest inventory
    // http://localhost:3000/products?filter[order]=inventory%20DESC&filter[limit]=1': The highest inventory products
    //[objectProto invokeStaticMethod:@"filter" parameters:@{ @"filter[order]=inventory&DESC&filter[limit]":@1} success:staticMethodSuccessBlock failure:staticMethodErrorBlock];


    //Product sorted Ascending ASC Descending DESC
    // http://localhost:3000/products?filter[order]=inventory%20ASC&filter[limit]=1': The highest inventory products
    //[objectProto invokeStaticMethod:@"filter" parameters:@{ @"filter[order]":@"inventory DESC"} success:staticMethodSuccessBlock failure:staticMethodErrorBlock];

    //'/weapons?filter[fields][name]=1&filter[fields][effectiveRange]=1': Only name and effective ranges
    //'/weapons?filter[where][effectiveRange][gt]=900&filter[limit]=3': The top 3 weapons with a range over 900 meters
    //'/locations': Locations
    //'/locations?filter[where][geo][near]=153.536,-28.1&filter[limit]=3': The 3 closest locations to a given geo point

    /*
    [[[AppDelegate adapter] contract] addItem:[SLRESTContractItem itemWithPattern:@"/location/nearby" verb:@"GET"] forMethod:@"locations.getNearestFew"];
    [LocationProto invokeStaticMethod:@"getNearestFew" parameters:@{
    @"here": @{
    @"lat": 37.587409,
    @"lng": -122.338225
    }
    } success:staticMethodSuccessBlock failure:staticMethodErrorBlock];
    */


    -( void) customMethod1 {

    // Define the load error functional block
    void (^staticMethodErrorBlock)(NSError *) = ^(NSError *error) {
    NSLog( @"Error %@", error.description);
    [AppDelegate showGuideMessage: @"No Server Found"];
    };//end selfFailblock

    // Define the load success block for the LBModelPrototype allWithSuccess message
    void (^staticMethodSuccessBlock)() = ^(id result) {
    NSLog(@"Success on Static Method result: %@", result);
    self.labelMethod1.text = @"Response 1";
    };//end staticMethodSuccessBlock

    //Get a local representation of the 'cars' model type
    LBModelPrototype *objectB = [ [AppDelegate adapter] prototypeWithName:@"cars"];
    [[ [AppDelegate adapter] contract] addItem:[SLRESTContractItem itemWithPattern:@"/cars/custommethod1" verb:@"GET"] forMethod:@"cars.custommethod1"];

    // Invoke the invokeStaticMethod message for the 'cars' LBModelPrototype
    // Equivalent http JSON endpoint request : http://localhost:3000/cars/custommethod1
    [objectB invokeStaticMethod:@"custommethod1" parameters:@{} success:staticMethodSuccessBlock failure:staticMethodErrorBlock ];

    }//end CustomMethod1

    -( void) customMethod2 {
    // Define the load error functional block
    void (^staticMethodErrorBlock)(NSError *) = ^(NSError *error) {
    NSLog( @"Error %@", error.description);
    [AppDelegate showGuideMessage: @"No Server Found"];
    };//end selfFailblock

    // Define the load success block for the LBModelPrototype allWithSuccess message
    void (^staticMethodSuccessBlock)() = ^(id result) {
    NSLog(@"Success on Static Method result: %@", result);

    self.labelMethod2.text = @"Response 2";

    };//end staticMethodSuccessBlock

    //Get a local representation of the 'cars' model type
    LBModelPrototype *objectB = [ [AppDelegate adapter] prototypeWithName:@"cars"];
    [[ [AppDelegate adapter] contract] addItem:[SLRESTContractItem itemWithPattern:@"/cars/custommethod2" verb:@"GET"] forMethod:@"cars.custommethod2"];

    // Invoke the invokeStaticMethod message for the 'cars' LBModelPrototype
    // Equivalent http JSON endpoint request : http://localhost:3000/cars/custommethod2?arg1=yack&arg2=123
    [objectB invokeStaticMethod:@"custommethod2" parameters:@{@"arg1":@"yack" , @"arg2":@123} success:staticMethodSuccessBlock failure:staticMethodErrorBlock ];

    }//end CustomMethod2

    - (void)customMethod3 {
    // ++++++++++++++++++++++++++++++++++++
    // Uncomment the comment block below to call a custom method on the server
    // ++++++++++++++++++++++++++++++++++++

    // Define the load error functional block
    void (^staticMethodErrorBlock)(NSError *) = ^(NSError *error) {
    NSLog( @"Error %@", error.description);
    [AppDelegate showGuideMessage: @"No Server Found"];
    };//end selfFailblock

    // Define the load success block for the LBModelPrototype allWithSuccess message
    void (^staticMethodSuccessBlock)() = ^(id result) {
    NSLog(@"Success on Static Method result: %@", result);
    };//end staticMethodSuccessBlock

    //Get a local representation of the 'cars' model type
    LBModelPrototype *objectB = [ [AppDelegate adapter] prototypeWithName:@"cars"];
    [[ [AppDelegate adapter] contract] addItem:[SLRESTContractItem itemWithPattern:@"/cars/custommethod3" verb:@"GET"] forMethod:@"cars.custommethod3"];

    // Invoke the invokeStaticMethod message for the 'cars' LBModelPrototype
    // Equivalent http JSON endpoint request : http://localhost:3000/cars/custommethod3?arg1=yack&arg2=123
    [objectB invokeStaticMethod:@"custommethod3" parameters:@{@"arg1":@"yack" , @"arg2":@123} success:staticMethodSuccessBlock failure:staticMethodErrorBlock ];

    }//end customMethod3