My question might be an obvious one, however i've been struggling with this one for a couple of nights. Here's a basic overview of my problem.
I have the following three models:
Model 1
/* api/models/Parent.js */
module.exports = {
tableName: "table1",
attributes: {
name: {
type: "string",
},
children: {
collection: "child",
via: "parentId"
},
},
};
Model 2
/* api/models/Child.js */
module.exports = {
tableName: "table2",
attributes: {
name: {
type: "string",
},
parentId: {
model: "parent"
},
vaccinations: {
collection: "vaccination",
via: "childId"
},
},
};
Model 3
/* api/models/Result.js */
module.exports = {
tableName: "table3",
attributes: {
name: {
type: "string",
},
childId: {
model: "child"
},
vaccinationId: {
model: "vaccination"
},
},
};
I wish to be able to query the following
GET /vaccination?parentId=1 which will retrieve all vaccinations for children that have parentId=1. I am not sure what's the best practice to follow here, i would really appreciate any help. I have read the Through Associations but for some reason, i can not get it to work.
Thanks in advance.
Following query might help you here:
let children = await Child.find({
parentId: 1
})
.populate('parentId')
.populate('vaccinations');
Related
I basically want to filter an object w/ a nested array in reference with another array of objects. The filtering needs to find all matching objects of the primary array with an AND logical operator (think Intersection). Below is a basic set up that should give you a clear idea of what I want to do. My current solution is not an 'intersection" solution I am trying to achieve. The some operator is giving me a UNION solution, instead of the INTERSECTION (OR vs AND).
interface Class1{
tags: Tag[];
}
interface Tag {
name: string;
}
sampleClassArray: Class1[] = [{
tags: [
{
name: 'Chris'
},
{
name: 'Eric'
},
{
name: 'Terry'
}
]
},
{
tags: [
{
name: 'Paul'
},
{
name: 'Brittney'
},
{
name: 'Brian'
}
]
},
{
tags: [
{
name: 'Eric'
},
{
name: 'Lee'
},
{
name: 'Polio'
}
]
}
}
sampleTagArray: Tag[] = [
{
name: 'Eric'
},
{
name: 'Bogey'
},
{
name: 'Samantha'
}
]
ngOnInit() {
let returnFilterResults = [];
returnFilterResults = this.sampleObject1.filter((sampleObject: sampleClassArray) =>
this.sampleTagArray.every((sampleTag: Tag) => sampleObject.tags.some((sampleObjectTag) => sampleTag.name === sampleObjectTag.name))
);
}
I currently have a typeDefs file that has a custom Books type. This is how it looks at the moment:
type: Books {
bookId: String
authors: [String]
description: String
title: String
}
I am using MongoDB in order to store my data. My model looks like this:
const bookSchema = new Schema({
authors: [
{
type: String,
},
],
description: {
type: String,
required: true,
},
// saved book id from GoogleBooks
bookId: {
type: String,
required: true,
},
title: {
type: String,
required: true,
}
});
And my resolvers look like this:
saveBook: async (parent, args, context) => {
if (context.user) {
const book = await Book.create({ ...args })
await User.findByIdAndUpdate(
{ _id: context.user._id },
{ $addToSet: { savedBooks: { bookId: args.bookId } } },
{ new: true }
);
return book;
}
throw new AuthenticationError('You need to be logged in!');
},
When I use graphql playground and send data in the query variable I am getting an error that String cannot represent a non string value: [\"james\", \"jameson\"]", when I send
{
"input": {
"bookId": "1",
"authors": ["james", "jameson"],
"description": "thdfkdaslkfdklsaf",
"title": "fdjsalkfj;a",
}
}
I know that it is because I am using an array of strings and entering an array of strings to gql will result in this error. I thought that if I put brackets around the String in my typeDefs it would work just find. I can't seem to find a way to send an array of strings to gql. I looked through the documentation and can't find a way to complete this..
Make a typedef out of author and then give the author variable within books the type "Author".
I think you also have to define the array in the bookschema if I'm not incorrect.
And don't forget to make sure the model naming in your database has to be the same as in your code.
Like this:
type: Author {
name: String
}
type: Books {
bookId: String
authors: [Author]
description: String
title: String
}
const bookSchema = new Schema({
authors: [
{
type: Author,
},
],
description: {
type: String,
required: true,
},
// saved book id from GoogleBooks
bookId: {
type: String,
required: true,
},
title: {
type: String,
required: true,
}
});
Hope this works :)
Given is following data structure
const list = [
{
title: 'Section One',
data: [
{
title: 'Ay',
},
{
title: 'Bx',
},
{
title: 'By',
},
{
title: 'Cx',
},
],
},
{
title: 'Section Two',
data: [
{
title: 'Ay',
},
{
title: 'Bx',
},
{
title: 'By',
},
{
title: 'Cx',
},
],
},
];
What i want to do ist to filter this list based on title property in the data array of each object.
An example would be to have the list where the title property of the childs starts with "B", so the list will look like that:
const filteredList = [
{
title: 'Section One',
data: [
{
title: 'Bx',
},
{
title: 'By',
}
],
},
{
title: 'Section Two',
data: [
{
title: 'Bx',
},
{
title: 'By',
}
],
},
];
What i tried so far was something like that:
const items = list.filter(item =>
item.data.find(x => x.title.startsWith('A')),
);
or
const filtered = list.filter(childList => {
childList.data.filter(item => {
if (item.title.startsWith('B')) {
return item;
}
return childList;
});
});
But i think i am missing a major point here, maybe some of you could give me a tip or hint what i am doing wrong
Best regards
Your issue is that you're doing .filter() on list. This will either keep or remove your objects in list. However, in your case, you want to keep all objects in list and instead map them to a new object. To do this you can use .map(). This way you can map your objects in your list array to new objects which contain filtered data arrays. Here's an example of how you might do it:
const list=[{title:"Section One",data:[{title:"Ay"},{title:"Bx"},{title:"By"},{title:"Cx"}]},{title:"Section Two",data:[{title:"Ay"},{title:"Bx"},{title:"By"},{title:"Cx"}]}];
const filterByTitle = (search, arr) =>
arr.map(
({data, ...rest}) => ({
...rest,
data: data.filter(({title}) => title.startsWith(search))
})
);
console.log(filterByTitle('B', list));
I have a Kendo Scheduler on my page.
<div kendo-scheduler k-options="schedulerOptions" k-data-source="items"></div>
My angular controller will make a call to the server to get data, it looks like this, but I do not know what my URL parameter will be until it loads up ($scope.$watch).
$scope.$watch(function () { return MyService.leadID; }, function (newValue) {
if (newValue) {
getAppointmentsTabData(newValue);
}
});
var getAppointmentsTabData = function (leadID) {
MyService.getAppointmentsTabData(leadID)
.then(function (data) {
$scope.items = data;
}
}
);
};
How can I bind this data to my Kendo Scheduler?
I can get this Scheduler to work with static data, but not the JSON list of objects that get returned when the server sends them. I would like to be able to bind my $scope.items to the dataSource, but that does not appear to work.
Here is the schedulerOptions code.
$scope.schedulerOptions = {
date: new Date("2014/10/13"),
startTime: new Date("2014/10/13 07:00 AM"),
height: 310,
views: [
"agenda",
{ type: "week", selected: true, allDaySlot: false },
{ selectedDateFormat: "{0:dd-MM-yyyy}" }
],
eventTemplate: "<span class='custom-event'>{{dataItem.title}}</span>",
allDayEventTemplate: "<div class='custom-all-day-event'>{{dataItem.title}}</div>",
timezone: "Etc/UTC",
dataSource: {
data: $scope.items,
schema: {
model: {
id: "id",
fields: {
id: { from: "ID", type: "number" },
appointmentId: { from: "AppointmentId", type: "number" },
resource: { from: "Resource", type: "number" },
description: { from: "Description" },
isAllDay: { type: "boolean", from: "IsAllDay" },
end: { from: "End", type: "date" },
start: { from: "Start", type: "date" },
title: { from: "Title", defaultValue: "No title" },
startTimezone: { from: "StartTimezone" },
endTimezone: { from: "EndTimezone" },
recurrenceRule: { from: "RecurrenceRule" },
recurrenceException: { from: "RecurrenceException" },
}
}
},
}
};
I can get the static approach to work. I cannot really use the remote data approach that looks like this (below) because I do not know what my URL is until my $scope.$watch is triggered. I need to append query string params.
dataSource: {
batch: true,
transport: {
read: {
url: "/MyController/GetMyData",
dataType: "json",
},
Does anyone have any suggestions on how I can populate my Scheduler dataSource dynamically?
I have seen this question, Kendo update scheduler options dynamically, but I am not having any luck getting the setOptions(). If only I could call $scope.myScheduler.setOptions("dataSource", myJsonObjectArry), that would be awesome, but nothing.
I am able to manipulate $scope.myScheduler._data (as an array), but I need some form of a refresh method to redraw my UI. This approach doesn't seem right though.
Thanks for any help.
I am answering my own question. In case you run into this situation, here is how I solved it.
Here is my schedulerOptions now. Notice there is no dataSource set and no schema. This is because I will populate that with my own dataSource dynamically.
$scope.schedulerOptions = {
date: new Date("2014/10/13"),
startTime: new Date("2014/10/13 07:00 AM"),
showWorkHours: true,
height: 310,
views: [
"agenda",
{ type: "week", selected: true, allDaySlot: false },
{ selectedDateFormat: "{0:dd-MM-yyyy}" }
],
edit: $scope.edit,
editable: {
template: $("#editor").html()
},
timezone: "Etc/UTC",
dataSource: {
data: [], // will be set dynamically
}
};
When my data is returned to this js controller, I will call this.
$scope.myScheduler.dataSource.data(getSchedulerEvents($scope.data.items));
Which in turn will call this, which creates the dataSource for me.
var getSchedulerEvents = function (items) {
var result = [];
var event;
for (var i = 0, length = items.length; i < length; i++) {
event = items[i];
result.push(new kendo.data.SchedulerEvent({
id: event.ID,
title: event.Title,
description: event.Description,
start: kendo.parseDate(event.Start),
end: kendo.parseDate(event.End),
isAllDay: event.IsAllDay,
recurrenceException: event.RecurrenceException,
recurrenceId: event.RecurrenceId,
recurrenceRule: event.RecurrenceRule,
resource: event.Resource,
}));
}
return result;
}
If you run into this problem, hopefully this helps.
I'm trying to render a table view with four columns, 'name', 'birthday', 'gender', 'married', but
a) they columns aren't showing up at all
b) I'm not even sure if I am passing them correctly, because when I console.log table.options the columns property is rendered as "empty":
Object {columns: Array[0], emptyContent: "no entries", onItemClick: function, sortable: false, onSort: null}
I've tried this:
var table = new Backbone.UI.TableView({
model: people,
columns: [
{ title: "Name", content: 'name' },
{ title: "Gender", content: "gender" } },
{ title: "Birthday", content: "birthday" } },
{ title: "Married", content: "married" } }
]
});
And this:
var table = new Backbone.UI.TableView({
model: people,
options: {
columns: [
{ title: "Name", content: 'name' },
{ title: "Gender", content: "gender" },
{ title: "Birthday", content: "birthday" },
{ title: "Married", content: "married" }
]
}
});
The source code change is adding the options as mu is too short said. Change the initialize method of the Backbone.UI.TableView object to be the following within the source code:
initialize : function(options) { //add parameter
Backbone.UI.CollectionView.prototype.initialize.call(this, arguments);
$(this.el).addClass('table_view');
this._sortState = {reverse : true};
this.options = _.extend({}, this.options, options); //Add this line
}
I'm sure there might be a better place to put this but I'm just going through tutorials to learn some advanced backbone stuff considering the documentation does not match the current version I would shy away from using the library in production as of right now. Hopefully it is fixed in the future.