strongloop-community / loopback-sdk-ios

iOS Client SDK for the LoopBack framework.
Other
76 stars 39 forks source link

Modified the LBModelRepository to allow filter responses with the inc… #116

Open jpswensen opened 8 years ago

jpswensen commented 8 years ago

…lude filter to interpret arrays correctly.

I was trying to find a way to have the loopback-sdk-ios automatically include an NSArray of the appropriate model type when there was a filter request that had the "include" filter for a set of models with a hasMany relation. The attached pull request modifies the LBModelRepository to keep a global dictionary of the various repository objects and a modification of the modelWithDictionary to handle requests that have arrays of other model types.

Example usage: (1) Create a model that not only has its own fields, but also an optional array that allows for the "include" filter:

(Swift code)
class Customer : LBPersistedModel {
    var id: NSNumber!
    var customer_name: String!
    var customer_location_id: NSNumber!
    var user_closet_items: [Order]? = nil
}

class Order : LBPersistedModel {
  var id: NSNumber!
  var customer_id: NSNumber!
  var order_name: String!
}

(2) Create a CustomerRepository object and a OrderRepository object so that they are both known to the new global repositoryDictionary

(3) Execute a findWithFilter that includes a "include" filter

// Retrieve a limited list with includes
        AppDelegate.customerRepository.findWithFilter(["where":["id":3],"include":"orders"], success: { (objectList:[AnyObject]!) in
            print(objectList)

        }) { (err:NSError!) in
            print(err)
        }

(4) The objectList in Step (3) is a Customer model object that has the "orders" array populated with the associated orders. If the user had just done a query without the "include" filter then the "order" variable in the Customer model would have remained nil.

slnode commented 8 years ago

Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test."

slnode commented 8 years ago

Can one of the admins verify this patch?

slnode commented 7 years ago

Can one of the admins verify this patch?

jpswensen commented 7 years ago

Any update on whether this is acceptable? I can keep working off my own branch. I also just realized that another commit (related to filtering, but not nested JSON lists from include filters) also showed up in this pull request. I haven't used github pull requests much. I guess I should have made another branch to work off of for the second patch.

bajtos commented 7 years ago

@gunjpan @jannyHou could you PTAL?

gunjpan commented 7 years ago

@jpswensen : Hi, could you please rebase your PR against the master and push so that I can get CI running. Thank you.

slnode commented 7 years ago

Can one of the admins verify this patch?

bajtos commented 6 years ago

@jpswensen I am afraid we are no longer actively maintaining the project. Would you perhaps like to step up to become a maintainer yourself? If yes, then please leave a comment in https://github.com/strongloop-community/loopback-sdk-ios/issues/136