How to filter the result whose type is array in loopback 4? - loopbackjs

import {Entity, model, property} from '#loopback/repository';
#model()
export class Misc extends Entity {
#property({
type: 'number',
id: true,
generated: true,
})
id?: number;
#property({
type: 'array',
itemType: 'number',
required: true,
})
members: number[];
constructor(data?: Partial<Misc>) {
super(data);
}
}
export interface MiscRelations {
// describe navigational properties here
}
export type MiscWithRelations = Misc & MiscRelations;
Above is the model for misc API. I am using PostgreSQL.
I have inserted data in the table. Result of GET request from this table is as following -
[
{
"id":1,
"members":[
1,
2,
3
]
},
{
"id":2,
"members":[
1,
2,
3,
4,
5
]
},
{
"id":3,
"members":[
10,
20,
30,
40,
50
]
},
{
"id":4,
"members":[
100,
200,
300,
400,
500
]
},
{
"id":5,
"members":[
1,
2,
3,
500,
1000,
5000
]
}
]
I want to get the records who have members with value 1, so I applied a filter like this -
http://localhost:3000/miscs?filter[where][inq][members]=1
But this isn't working. If there is no way to execute such a query then can I do some change in the model to adjust its type such that it can accept CSV values and also can filter those data?
Please help. Thanks in advance!

For the Postgresql connector, use contains, which accepts an array:
?filter[where][contains][members][0]=1
- SAME AS -
{
where: {
contains: [1]
}
}

Finally, I found an answer. Regex can be used to match the record here like this
filter[where][members][regexp]=1,|1]

Related

Can I pass an array of object to filterBy?

My ember version is 1.13 and I would like to ask if the line of code below is applicable to the version of my ember app above?
console.log(this.get('arrayOfObjects').filterBy('zoneName', ['zoneNameOne', 'zoneNameTwo'])); ?
Sample data of selectedZoneOrCityName is
selectedZoneOrCityName = ['zoneNameOne', 'zoneNameTwo'],
I want to use it something like these
if (selectedZoneOrCityName) {
return this.get('arrayOfObjects').filterBy('zoneName', selectedZoneOrCityName).mapBy('cityName');
} else {
console.log('reads nothing');
return [];
}
you can use simple filter like below code snippet.
var arrayOfObjects = [
{
id: 1,
name: 'one',
zoneName: 'zoneNameOne'
},
{
id: 2,
name: 'two',
zoneName: 'one zoneName'
},
{
id: 3,
name: 'three',
zoneName: 'zoneNameOne'
},
{
id: 4,
name: 'four',
zoneName: 'zoneNameTwo'
}
];
var selectedZoneOrCityName = ['zoneNameOne', 'zoneNameTwo'];
arrayOfObjects = arrayOfObjects.filter((item) => {
return selectedZoneOrCityName.includes(item.zoneName);
});
console.log('final filtered array : ', arrayOfObjects);
if you are usign filterBy then you have to chain filterBy for each array value.

Ember data, how can i use nested(two-depth) json-format for hasMany Relationship

Generally, Ember HasMany relationship json format is follow,
{ "post" : { id:1, title:"this is title", comments:[1,2], writer: ...} }
But, i want to use next json format (because, my server return like this)
{ "post" : { id:1, title:"this is title",
comments:[
{id:1, bodytext:"blarblar...."},
{id:2, bodytext:"second blarblar...."},
], writer: ...} }
How can I use this?
Isn't there any problem in ember store relationship?
This is a job for EmbeddedRecordsMixin.
If you're implementing the server yourself and it's purpose-built for your ember application, you should consider switching to side-loading instead:
{ "post" : { id: 1,
title: "this is title",
comments: [1,2],
writer: ...
},
"comments": [ { id: 1,
bodytext: "blarblar...."
},
{ id: 2,
bodytext: "second blarblar...."
}
]
}
That way, it would be still just one request, but would work for more complicated structures (other than trees) as well.

EmberJS auto loading inverse relations?

I have a model like:
var Survey = DS.Model.extend({
title: DS.attr('string'),
});
and:
var SurveySection = DS.Model.extend({
survey: DS.belongsTo('survey', {async:true}),
title: DS.attr('string'),
help_text: DS.attr('string'),
});
If i have a survey object, can I just do survey.get('survey_section') to get all the sections associated with a particular survey, because this isn't working? It doesn't even work if I add a survey_sections: DS.hasMany('survey_sections', {async:true}) to my survey model.
I should add that I'm using fixtures. When I release the app they'll still be used (ie so if the RESTAdapater would fix it, that's not a fix in my case):
Survey.reopenClass({
FIXTURES: [
{
"id": 1,
"title": "Favourite food"
},
{
"id": 2,
"title": "Sports"
}
]
});
and:
SurveySection.reopenClass({
FIXTURES: [
{
"help_text": "",
"id": 1,
"survey": 1,
"title": "Italian food"
},
{
"help_text": "",
"id": 2,
"survey": 1,
"title": "Team sports"
}, ...]});
Is the fixture adapter not able to retrieve related records in the reverse direction like this? If not, will I have to go the laborious route of manually getting the sections with survey=1, for example (laborious because I'll have to take this approach throughout my whole app with other models)?
Update
Specifically my failing code is (with survey 1):
this.get('survey').get('survey_sections').then(function(survey_sections) {
// survey_sections contains no objects, so objectAt(0) returns undefined.
survey_sections.objectAt(0).get('questions').then(function(questions) {
console.log('Set first question ID to ' + self.get('firstQuestionId'));
});
});
As the fixtures show there should be 2 SurveySection objects in survey_sections, but instead none are found.
I didn't want to do it, but I had to add the forward relation to the items in the Survey fixture, e.g.:
Survey.reopenClass({
FIXTURES: [
{
"id": 1,
"title": "Favourite food",
"survey_sections": [1, 2],
},
{
"id": 2,
"title": "Sports",
"survey_sections": [3],
}
]
});
and update the Survey model with:
survey_sections: DS.hasMany('survey_sections', {async:true})

couchdb - startkey endkey doesn't filter records with key arrays as expected

I have Couch-DB map-reduce view which outputs this;
{
rows: [
{
key: [
"2014-08-20",
2,
"registration"
],
value: 2
},
{
key: [
"2014-08-20",
2,
"search"
],
value: 3
},
{
key: [
"2014-08-21",
2,
"registration"
],
value: 3
},
{
key: [
"2014-08-21",
2,
"search"
],
value: 4
}
]
}
I need to query all the records that has between 2014-08-20 and 2014-08-21 Also the same time I need the integer value in the middle to be 2 and the last value to be "registration".
My curl request URL looks like this
BASE_URL?group=true&startkey=["2014-08-20",1,"registration"]
&endkey=["2014-08-21",1,"registration"]
This filters out the date but none of the other elements (the integer value and the string). Anyone got an idea whats happening?
Any help would be appreciated.
UPDATE
My Document structure looks something like this
[
{
platform_version: 2,
UDID: "EWHSJeNp20sBFuzqcorkKVVi",
session: {
timestamp: "2014-08-20T00:00:00.000Z",
session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
ip: "202.150.213.66",
location: "1.30324,103.5498"
},
events: [
{
event_type: "search",
timestamp: "2014-08-21T00:00:00.000Z",
location: "1.30354,103.5301",
attributes: {
}
}
]
},
{
platform_version: 2,
UDID: "EWHSJeNp20sBFuzqcorkKVVi",
session: {
timestamp: "2014-08-21T00:00:00.000Z",
session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
ip: "202.150.213.66",
location: "1.30324,103.5498"
},
events: [
{
event_type: "search",
timestamp: "2014-08-21T00:00:00.000Z",
location: "1.30354,103.5301",
attributes: {
}
}
]
},
{
platform_version: 2,
UDID: "EWHSJeNp20sBFuzqcorkKVVi",
session: {
timestamp: "2014-08-20T00:00:00.000Z",
session_id: "kOnNIhCNQ31LlkpEPQ7XnN1D",
ip: "202.150.213.66",
location: "1.30324,103.5498"
},
events: [
{
event_type: "click",
timestamp: "2014-08-21T00:00:00.000Z",
location: "1.30354,103.5301",
attributes: {
}
}
]
}
]
and the map reduce function looks like this.
function(doc) {
date = doc.session.timestamp.split("T")[0];
eventArray = doc.events;
for (i = 0; i < eventArray.length; i++) {
emit([doc.app_version,eventArray[i].event_type,date],1);
}
}
It started working after I change the order of the keys. but still, I can't use a wildcard to query all the event types.
You are getting documents with different second array item different then 1 because in the CouchDB the limits (startkey and endkey) are compared to map keys using lexicographical order. In the lexicographical order [1,1] < [1,2] < [2,1] < [2,2].
What you need is either multidimensional queries (which are not supported by CouchDB), additional client-side filtering (which may increase data transferred between CouchDB and your app) or additional server-side filtering with list function (which increase processing time of queries).
If your app needs filtering using range only on the first element of key array (just like in your example query), you would solve your problem easily by placing the item at the last position of array, eg. emitting [1, "registration", "2014-08-21"] instead of ["2014-08-21", 1, "registration"].

How do I create a polymorphic 1:1 relationships wtih Ember Data fixtures?

Page has a polymorphic 1:1 relationship with a model called PageContent. PageContent has two subtypes (TextOnly and Video). I want to be able to able to do a findAll for "page" and get all of the content back. What am I doing wrong?
JSBin
This seems to work: http://jsbin.com/names/1/edit
Only wrong thing I could see is the App.Page.FIXTURES.
It should be:
App.Page.FIXTURES = [
{
id: 1,
title: "Introduction",
pageContent: 1,
pageContentType: "textOnly"
},{
id: 2,
title: "Summary",
pageContent: 1,
pageContentType: "Video"
}
];
or
App.Page.FIXTURES = [
{
id: 1,
title: "Introduction",
pageContent: {
id: 1,
type: "textOnly"
}
},{
id: 2,
title: "Summary",
pageContent: {
id: 1,
type: "Video"
}
}
];