breeze: pagination on expand - pagination

Is it possible to do the following with Breeze ?
var query = breeze.EntityQuery.from("Mandates").where("Id", "==", mandatId)
.expand("OpenPositions").skip(offset).take(pageSize).inlineCount(true);
I would like the paging to occur on the OpenPositions collection but instead it's applied to the Mandates.

Not sure if this is possible without seeing your schema but why not query on "OpenPositions"? i.e. something like
var query = breeze.EntityQuery.from("OpenPositions")
.where("Mandate.Id", "==", mandatId)
.expand("Mandate").skip(offset).take(pageSize).inlineCount(true);

Related

How to query multiple documents from multiple nested subcollections in firebase firestore?

I have 3 subcollections nested under one another under one single main collection.
I want to get all the documents under 'colection3' for each document in 'collection2' for each document in 'collection1'
I want to query something like -
admin.firestore().collection('collection1').doc('FOR ALL DOCS IN COLLECTION 1').collection('collection2').doc('FOR ALL DOCS IN COLLECTION 2').collection('collection3').get()
My question is, can I make such query ? Will following query work ?
collection('collection1/*/collection2/*/collection3')
Is this a valid path? What does "*" indicates?
I tried something like this,
const baseRef = admin.firestore().collection(`collection1/*/collection2/*/collection3`);
const querySnap = baseRef.get()
It returned me a querySnapshot but when I tried to loop through this querySnapShot, it didn't print anything
querySnap.forEach(doc => console.log(doc.id))
output was nothing.
I was expecting that doc Ids should get printed in the console.
This can be achieved with collection group level queries.
The collectionGroup option returns all documents within the collection group you have created.
const allDocsQuery = admin.firestore()
.collectionGroup('collection1');
await allDocsQuery.get().then((snap) => {
// do operations with your data
});
Note that you will need to create a collection group in order to use this - it will throw an error with a link to the creation page, but you can also do it by following the documentation in the link above.

Loop through collection and find documents where name equals in firebase functions

I am trying to make as much logic in the firebase cloud functions, but not sure how to! Here is what I am trying to accomplish:
const myGroups = admin.firestore().collection('groups').find({"playerId":userId});
I know that above is not correct, but i cant seem to find a solution for this. I would like to get all my groups into an array or something and send them back to my app.
I am not sure if this is wise?!? But think that it would be easier to add or change to the script from one place, instead of all devices.
Hi you need to use firestore compound queries you can find the documentation here: https://firebase.google.com/docs/firestore/query-data/queries
so in your case the code will look something like:
const myGroups = await admin.firestore().collection('groups').where("playerId", "==", userId}).get();
You can use get() on the CollectionReference to get all documents.
const myGroups = await admin.firestore().collection('groups').get()
return myGroups.docs.map(group => ({id: group.id, ...group.data()}))
Additionally, if you are trying to find groups where playerId is userId. Then you can use where() clause to narrow search results:
const myGroups = await admin.firestore()
.collection('groups')
.where("playerId", "==", userId)
.get()
You can read more about queries in the documentation.

Sails.js: How to find records based on values in associated collection?

I'm looking for a way to make sub-query using Sails.js Waterline. The record has an association with Charge model.
I would expect something like that to work, but that doesn't seem to be supported:
var topRecords = await Record.find({'charge.paid':true});
The closest I got was this:
var topRecords = await Record.find().populate('charge',{paid:true});
but the problem is that it still returns all the Records regardless, just doesn't populate the records that do not match populate where statement.
The reason why I can't search for charges first is that I want to sort the data based on one of the values in Record.
You can fetch the Charges then use .map to get the records from there.
const topCharges = await Charge.find({ paid: true }).populate('records');
const topRecords = topCharges.map(charge => charge.record);

How do I reset query part with knex.js

I tried to find anything here in documentation with no success.
What I would like to do is to clone my query builder and reset some query parts like order by or group by. How can I do that ?
It looks like there is no many methods available for this query parts
And there are clear methods only for selects and where conditions.
How do you do it ?
Example:
const qb = knex
.select('id')
.from('table')
.where({ visibility: 'public' })
// some left joins here
.groupBy('id')
How can I do then something like
const new_qb = qb
.clone()
// remove group by here
.clearSelect()
.count()
To reset query part there is a way for example:
this.qb._clearGrouping('order'); // reset order by
this.qb._clearGrouping('group'); // reset group by
and so on.
You can clone the query and add parts to those individual queries.
Like this:
//base query
let query = knex('table_name').where({id: 1});
const countQuery = query.clone().count();
const selectQuery = query.clone().select().limit(10).offset(0);
return Promise.all([countQuery, selectQuery]);

mongoose: how to get string representation of query

I implementing module who automatically generate mongoose query by requested params, so for simplified test process I need to be able to get text representation of final query. How could I do that?
Like we have something like this:
var q = AppModel.find({id:777}).sort({date:-1})
I need to get something like this
"db.appmodels.where({id:777}).sort({date: -1})"
You can set debug for mongoose, which would by default send the queries to console, to use the following:
mongoose.set('debug', function (collectionName, method, query, doc) {
// Here query is what you are looking for.
// so whatever you want to do with the query
// would be done in here
})
Given a query object q you can rebuild the query using its fields, namely q._conditions and q._update. This is undocumented though and could easily break between versions of Mongoose (tested on Mongoose 4.0.4).

Resources