how to get Deep data but filter a sub collection - backand

I have an object which has many sub collections, one of the sub collections usually has more than 100 items and each of those have multiple nested objects. So I want to get deep data of the object but filter out just one sub collection so the response time and data gets minimised.
I want deep data of an object but I want to prevent Backand to get deep inside one of sub collection.
{
sub_A:[1,2,3],
sub_B:[1,2,3],
sub_C:[1,2,3],
sub_D:[1,2,3],
}
let say in the above object is it possible to get all except sub_D

You cannot use filter with deep, however you can create an on-demand action for that. Here is an example for user with many items:
function backandCallback(userInput, dbRow, parameters, userProfile) {
// get the user main level information
var user = $http({
method: "GET",
url: CONSTS.apiUrl + "/1/objects/users/" + parameters.userId
});
// get the user's related items
var userItems = $http({
method: "GET",
url: CONSTS.apiUrl + "/1/objects/items",
params: {
filter: [{
fieldName: "user",
operator:"in",
value:user.id
},
{
fieldName: "name",
operator:"contains",
value:parameters.namePart
}]
}
});
// get the user's related items
user.items = userItems.data;
return user;
}

Related

one to many - sequelize update - not removing/inserting children

So I've been struggling for a few hours now with a one-to-many mapping update.
I've got a project which has certain tasks (for example).
I add and remove tasks through the frontend and send the revised object to by backend running with sequelize.
Then I tried to update the records as follows:
return models.Project
.findOne({
where: { id: projectToUpdate.id },
include: [models.Task]
})
.then(function (ProjectFromDb) {
return models.sequelize
.transaction({
isolationLevel: models.sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED
},
function (t) {
return ProjectFromDb
.update(projectToUpdate,
{
include: [{ model: models.Task }]
})
});
})
.then(function (result) {
return output.getSuccessResult(....
})
.catch(function (error) {
return output.getErrorResult(....
});
But this would only update the Project
Next I tried to update them with an additional then call:
.then(function (updateResult) {
return updateResult.setTasks(projectToUpdate.Tasks, {transaction: t})
})
But this would give me the result that he is trying to update the Task and set the ProjectId to NULL which is not possible because it is non-nullable.
I am currently "manually" adding the tasks and removing them but this seems to be a silly way of using the framework.
Can anyone tell me how to properly make this work with a one-to-many relationship without me calling Tasks.createBulk and Tasks.destroy?
EDIT TO INCLUDE MODEL
JSON object looks like this:
{
id: 1,
projectName: 'nameOfTheProject',
Tasks: [
projectId: 1,
name: 'taskName'
]
}
Please try changing the property name projectId to ProjectId on your Tasks objects that are nested to the projectToUpdate object.
Update
Looking at sequelize's source, it seems that the Instance.$save() function (which is called by Instance.$update() that you're using) does not support nested models creation when you're updating it - it checks if the flag wasNewRecord is true before doing it.

Backbone fetch not working with single model

Here is my backbone code
var UserModel=Backbone.Model.extend({
url: 'http://api.myapi.com/user'
});
var user=new UserModel();
user.set({id:1});
user.fetch();
console.log(user.get('screenname'));
This returns the whole collection of users instead of user with the id of 1. When I change the url to
url: 'http://api.myapi.com/user/1'
I get back the user that I want. Why don't I get the record for 'user 1' when I user.set({id:1});
Note - My api is at a different domain, that is why I have the entire url in my 'url' property. Please help, I am ready to give up on backbone.
You have to set the urlRoot not url and Backbone will add the id to the end of your url :
var UserModel=Backbone.Model.extend({
urlRoot: 'http://api.myapi.com/user'
});
You will need to add the id to your model url like so.
url: function() {
if(this.id) {
return 'http://api.myapi.com/user/' + this.id;
}
return 'http://api.myapi.com/user';
}
And then when you instantiate the user model you can pass it an id like this.
var user = new UserModel({id: 1});
Then when you do user.fetch() it will get 'http://api.myapi.com/user/1'
Also by not passing an id to UserModel Backbone will send a POST request to 'http://api.myapi.com/user'
There are two points you need to validate:
Model use urlRoot as rest API url, but Collection use url.
Output result in the fetch success callback
var UserModel=Backbone.Model.extend({
urlRoot: 'http://api.myapi.com/user'
});
var user=new UserModel();
user.set({id:1});
//It is safe to output result in success callback which guarantee the api is called
user.fetch({success: function(){
console.log(user.get('screenname'));
}});

Get model with both id and query parameters

I have a route that should load a model (BatchDetail) and a number of related items (BatchItems). Since there are a great number of items I should be able to do pagination with the help of two request parameters, limit and offset.
Here is the route I set up:
App.BatchDetailRoute = Ember.Route.extend({
model: function(params) {
var store = this.get('store');
var adapter = store.get('adapter');
var id = params.batch_detail_id;
var rejectionHandler = function(reason) {
Ember.Logger.error(reason, reason.message);
throw reason
}
return adapter.ajax("/batch_details/" + id, "GET", {
data: { limit: 50, offset: 100 }
}).then(function(json) {
adapter.didFindRecord(store, App.BatchDetail, json, id);
}).then(null, rejectionHandler);
},
setupController: function(controller, model) {
return this.controllerFor('batchItems').set('model', model.get('items'));
}
})
This way, when I go to /batch_details/1 my REST adapter will fetch the correct data which I receive in json in the above code.
Now, the model hook should return a model object or a promise that can be resolved to a model object, and that's where the problem lies. In setupController (which runs after the model hook) model is set to undefined and so my code explodes.
That means that whatever adapter.ajax returns does not resolve correctly but instead returns undefined. I'm baffled, since the above mechanism is exactly how the different find methods in ember-data (findById, findByQuery, etc.) work and that's where I got my idea from.
Can you shed some light on what I'm not getting?
Thank you.

Nesting Backbone models or collections so that they are linked

I have a User entity that has a subscriptions property. This is an array of IDs.
When I perform a fetch, the API will populate those subscriptions, and return something like this:
{
subscriptions: [1, 2, 3],
__subscriptions: [
{
id: 1,
name: 'Example'
},
{
id: 2,
name: 'Example'
},
{
id: 3,
name: 'Example'
}
]
}
I have done this so that I can still perform actions on the original subscriptions and then save them back to the API. Any changes I make to __subscriptions will not be persisted as the API doesn't recognise this field – it is simply the populated data.
In the parse function of my User, I create the nested collection:
parse: function (response) {
this.subscriptions = new Subscriptions(response.__subscriptions)
}
However, if I want to remove a subscription, I have to splice it from the subscriptions field of the User entity, and then I also have to remove it from the subscriptions collected that is nested as a property on the User:
// Clone the subscriptions property, delete the model with a matching ID, and then set it again.
var value = _.clone(this.get('subscriptions'))
// Use splice instead of delete so that we don't leave an undefined value
// in the array
value.splice(value.indexOf(model.id), 1)
// Also remove the same model from the nested collection
var removedSubscription = this.subscriptions.get(model)
this.subscriptions.remove(removedSubscription)
this.set('subscriptions', value)
this.save()
This is sort of annoying. Ideally, removing an ID from the subscriptions property should automatically update the collection.
Does this seem like a good way to deal with nested models and collections? I've heard bad things about Backbone.Relational so I was interested in a simpler solution.
I would listen to events of Subscriptions collection and update subscriptions argument accordingly.
var User = Backbone.Model.extend({
initialize: function () {
this.subscriptions = new Subscriptions;
this.subscriptions.on('add remove', this.updateSubscriptions, this)
},
updateSubscriptions: function() {
this.set('subscriptions', this.subscriptions.pluck('id'))
},
parse: function (response) {
this.subscriptions.reset(response.__subscriptions);
return Backbone.Model.parse.call(this, response);
}
});
So then removing subscription will update subscriptions attribute of user model:
user.subscriptions.remove(subscription)

413 Error FULL HEAD Query URL too large Rally

I am using app SDK 2.0.I have the following code to retrieve the tasks for first 100 Team Members of the project for a particular Iteration
//getting the first 100 elements of the owners Array
owners = Ext.Array.splice(owners,0,100);
//Initial configuration of the filter object
var ownerFilter = Ext.create('Rally.data.QueryFilter', {
property: 'Owner.DisplayName',
operator:'=',
value: owners[0]
});
/*Creating the filter object to get all the tasks for 100 members in that project*/
Ext.Array.each(owners,function(member){
ownerFilter = ownerFilter.or(
Ext.create('Rally.data.QueryFilter',{
property: 'Owner.DisplayName',
operator:'=',
value: member
}));
});
//Iteration Filter for the Object
var iterationFilter = Ext.create('Rally.data.QueryFilter', {
property: 'Iteration.Name',
operator:'=',
value: 'Iteration 4.2'
});
var filter = ownerFilter.and(iterationFilter);
Rally.data.ModelFactory.getModel({
type: 'Task',
success: function(model) {
var taskStore = Ext.create('Rally.data.WsapiDataStore', {
model: model,
fetch:true,
limit:Infinity,
pageSize:200,
filters:[filter],
autoLoad:true,
projectScopeDown:true,
listeners:{
load:function(store,data,success) {
Ext.Array.each(data, function(record){
console.log(record.data);
});
}
}
});
}
});
This code is giving me a 413 error since the URL for the query is too large.The request URL has names of all 100 members of the project.How can I solve this problem ? Are there any efficient filtering options available?
Team Membership is a tough attribute to filter Artifacts on, since it resides as an attribute on Users and is a collection that can't be used in Queries.
In this case you might be better off doing more of the filtering client-side. Since Team Membership "approximates" or associates to Projects, you may wish to start by filtering your Tasks on Project, i.e. (Project.Name = "My Project"). Then narrow things down client side by looping through and ensuring each Task is owned by a member of the Team in your owners collection.
Alternatively, you might look to do something like use Tags on your Tasks that mirror the Team Names. You could then filter your Tasks server side by doing a query such as:
(Tags.Name contains "My Team")
I know these aren't ideal options and probably not the answer you're looking for, but, they are some ideas to avoid having to build a massive username-based query.