Why isn't my $sort Operator on dates function consistent in my query? - sorting

I am trying to write a query to sort out documents based on descending dates ...{sort: {paymentDate: -1 }} order. The first time the query runs, the query section {sort: {paymentDate: -1 }} seems get ignored!
However when I refresh the page in the browser, the query section {sort: {paymentDate: -1 }} is applied, and the query displays in the correct sort order.
I need to know how to correct this issue!
Find below the contents of my document after I run the recipientsDetails.find().fetch(); query in the browser console:
0:
payersUserId: "hbieZBFNE53GpE8LP"
paymentDate: "2019-02-11 02:37:05"
payersNumber: "+25478887633"
paymentStatus: "Failed"
recipientNumber: "+25478887633"
_id: "eFShDRzp9JM9ejG5S"
1:
payersUserId: "hbieZBFNE53GpE8LP"
paymentDate: "2019-02-08 16:02:25"
payersNumber: "+2547078887633"
paymentStatus: "Failed"
recipientNumber: "+25478887633"
_id: "SnpNwsx49mZfPNSg7"
2:
payersUserId: "hbieZBFNE53GpE8LP"
paymentDate: "2019-02-08 15:00:02"
payersNumber: "+254707888633"
paymentStatus: "Failed"
recipientNumber: "+25478087703"
_id: "ZHWSiRBYk2xoZvDzb"
The above results is also the desired sorted order.
Perhaps the below helper code might shade some light.
../client/main.js
Template.paymentB2C.helpers({
'enableButton': function () {
var enableButtonStatusArray = [];
var userIdCode = Meteor.userId();
var phoneNumber = Meteor.users.findOne({_id: userIdCode }, { fields: { "profile.telephoneNumber": 1 } } );
var usersPhoneNumber = phoneNumber.profile.telephoneNumber;
var selectedRecipientDetails = recipientsDetails.find( { $or: [ { payersNumber: usersPhoneNumber }, { recipientNumber: usersPhoneNumber } ] },
{ fields: {
"payersUserId": 1,
"paymentDate": 1,
"paymentStatus": 1,
"_id": 1
} }).fetch();
selectedRecipientDetails.forEach((user) => {
payersUserId = user.payersUserId;
paymentDate = user.paymentDate;
paymentStatus = user.paymentStatus;
_id = user._id;
if(paymentStatus === "Failed"){
enableButtonStatusArray.push({
paymentStatus: paymentStatus,
paymentDate: paymentDate,
_id: _id
});
}
else if(paymentStatus === "Passed"){
enableButtonStatusArray.push({
paymentStatus: paymentStatus,
paymentDate: paymentDate,
_id: _id});
}
Session.set('enableButtonStatusArray2', enableButtonStatusArray );
});
var enableButtonStatusArrayForPrint = Session.get('enableButtonStatusArray2');
return enableButtonStatusArrayForPrint;
}
});
Note that the query here lacks a ...{sort: {paymentDate: -1 }} function.
Find below my Router code:
../client/main.js
Router.route('/paymentB2C', {
name: 'paymentB2C',
template: 'paymentB2C',
waitOn: function(){
return Meteor.subscribe('pendingPayments')
}
});
This leads to my Meteor.subscribe('pendingPayments') publish function:
../server/main.js
Meteor.publish('pendingPayments', function pendingPayments(){
return recipientsDetails.find({}, {sort: {paymentDate: -1 }});
});
Note that here is where I have the sort function.
Can someone explain why when codes first runs, the sort is ignored and the the document is randomly sorted, however is sorted out as designed (correctly) after hitting refresh in the browser?
Looking forward to your help.

Ideally, you should sort the data on the client-side query after you subscribe, instead of sorting it in the publish method.
The reason is that if the client subscribes to more than one publish function which will publish data from the same collections, your find query in the client-side will have access to the data from both publish as well as sort won't be effective. Moreover, publish is something which will grant data access to the subscriber and if the mini-mongo on the client side already has the data, it won't sync the data unless new data arrives.
Hence, you should always do sort and filter in your find queries on the client side as well.
Also, I notice that the format of the paymentDate field is not a 'Date'. It should ideally be of the Date format and should look something like ISODate("2019-02-11T02:37:05.000Z") instead of String format "2019-02-11 02:37:05". So if the sorting on the client side is also not working, try saving the paymentDate in the database as Date instead as a String.

Related

How to modify just a property from a dexie store without deleting the rest?

I'm having the dexie stores showed in the print screen below:
Dexie stores print screen
My goal is to update a dexie field row from a store without losing the rest of the data.
For example: when I edit and save the field "com_name" from the second row (key={2}) I want to update "com_name" only and not lose the rest of the properties, see first and the third row.
I already tried with collection.modify and table.update but both deleted the rest of the properties when used the code below:
dexieDB.table('company').where('dexieKey').equals('{1}')
//USING table.update
//.update(dexieRecord.dexiekey, {
// company: {
// com_name: "TOP SERVE 2"
// }
//})
.modify(
{
company:
{
com_name: TOP SERVE 2
}
}
)
.then(function (updated) {
if (updated)
console.log("Success.");
else
console.log("Nothing was updated.");
})
.catch(function (err) { console.log(err); });
Any idea how can I accomplish that?
Thanks
Alex
You where right to use Table.update or Collection.modify. They should never delete other properties than the ones specified. Can you paste a jsitor.com or jsfiddle repro of that and someone may help you pinpoint why the code doesn't work as expected.
Now that you are saying I realised that company and contact stores are created dynamically and editedRecords store has the indexes explicitly declared therefore when update company or contact store, since dexie doesn't see the indexes will overwrite. I haven't tested it yet but I suspect this is the behaviour.
See the print screen below:
Dexie stores overview
Basically I have json raw data from db and in the browser I create the stores and stores data based on it, see code below:
function createDexieTables(jsonData) { //jsonData - array, is the json from db
const stores = {};
const editedRecordsTable = 'editedRecords';
jsonData.forEach((jsonPackage) => {
for (table in jsonPackage) {
if (_.find(dexieDB.tables, { 'name': table }) == undefined) {
stores[table] = 'dexieKey';
}
}
});
stores[editedRecordsTable] = 'dexieKey, table';
addDataToDexie(stores, jsonData);
}
function addDataToDexie(stores, jsonData) {
dbv1 = dexieDB.version(1);
if (jsonData.length > 0) {
dbv1.stores(stores);
jsonData.forEach((jsonPackage) => {
for (table in jsonPackage) {
jsonPackage[table].forEach((tableRow) => {
dexieDB.table(table).add(tableRow)
.then(function () {
console.log(tableRow, ' added to dexie db.');
})
.catch(function () {
console.log(tableRow, ' already exists.');
});
});
}
});
}
}
This is the json, which I convert to object and save to dexie in the value column and the key si "dexieKey":
[
{
"company": [
{
"dexieKey": "{1}",
"company": {
"com_pk": 1,
"com_name": "CloudFire",
"com_city": "Round Rock",
"serverLastEdit": [
{
"com_pk": "2021-06-02T11:30:24.774Z"
},
{
"com_name": "2021-06-02T11:30:24.774Z"
},
{
"com_city": "2021-06-02T11:30:24.774Z"
}
],
"userLastEdit": []
}
}
]
}
]
Any idea why indexes were not populated when generating them dynamically?
Given the JSON data, i understand what's going wrong.
Instead of passing the following to update():
{
company:
{
com_name: "TOP SERVE 2"
}
}
You probably meant to pass this:
{
"company.com_name": "TOP SERVE 2"
}
Another hint is to do the add within an rw transaction, or even better if you can use bulkAdd() instead to optimize the performance.

Updating Apollo Cache Without Variables

Originally, I make a GraphQL call as follows:
query getItems($filter_ids: [Int!], $filter: item_records_bool_exp) {
items(order_by: { negative: asc, parent: asc }, where: { level: { _in: [2, 3] } }) {
i18n {
value
}
id
parent
negative
}
filters(where: { category: { _eq: "PLAN" } }) {
id
value
}
}
Now, when I insert a new item, I update the cache using update function in the mutation options, and I'm supposed to use readQuery/readFragment and writeQuery/writeFragment to interact with the Apollo Cache as described here.
My question is, my readQuery calls always fail if I do not provide the exact same variables that I had previous provided to the original GraphQL query. Is there a way around this? In other words, can I just read objects from the cache by their ID irrespective of the original query that was used to fetch these objects?

Chat app list last messages of each peer using parse server

I am doing a chat app using parse server, everything is great but i tried make to list just last message for every remote peer. i didn't find any query limitation how to get just one message from every remote peer how can i make this ?
Query limitation with Parse SDK
To limit the number of object that you get from a query you use limit
Here is a little example:
const Messages = Parse.Object.extend("Messages");
const query = new Parse.Query(Messages);
query.descending("createdAt");
query.limit(1); // Get only one result
Get the first object of a query with Parse SDK
In you case as you really want only one result you can use Query.first.
Like Query.find the method Query.first make a query and will return only the first result of the Query
Here is an example:
const Messages = Parse.Object.extend("Messages");
const query = new Parse.Query(Messages);
query.descending("createdAt");
const message = await query.first();
I hope my answer help you 😊
If you want to do this using a single query, you will have to use aggregate:
https://docs.parseplatform.org/js/guide/#aggregate
Try something like this:
var query = new Parse.Query("Messages");
var pipeline = [
{ match: { local: '_User$' + userID } },
{ sort: { createdAt: 1 } },
{ group: { remote: '$remote', lastMessage: { $last: '$body' } } },
];
query.aggregate(pipeline)
.then(function(results) {
// results contains unique score values
})
.catch(function(error) {
// There was an error.
});

SuiteScript 2.0 Filter Operator AnyOf Not Working

I have a SuiteScript 2.0 that load and search transaction saved search with posting period filter. In my filter I am using 'anyof' operator which is not working for 'postingperiod' field
below is sample of my code:
function getTransactionData(datain)
{
try
{
var objSearch = search.load(
{
id: datain.savedsearchid
});
objSearch.filters.push(search.createFilter({ name: "postingperiod", operator: "ANYOF", values: ["42", "43"]}));
//above filter filters only record with internalid 42
result = readAllData(objSearch);
return result;
}
catch (ex)
{
log.error("getTransactionData", ex);
throw ex;
}
}
let me know if I am missing something here.
Please note above issue is occurring only for saved search, if I search other object for example 'account' object with internalid filter using 'anyof' operator, works fine.
Update: Today after more testing, found that its only happening for 'postingperiod' filter.
Try this code in Netsuite Debugger, Create a SavedSearch in Netsuite, don't add any filter in that , save that and get the id of saved search and use in below script against id value.
Also replace the Period Id with yours.
require(['N/runtime','N/search'],
function (runtime,search) {
var invoiceSearchObj = search.load({
type: "invoice",
id: '<your search id>',
columns:
[
search.createColumn({
name: "trandate",
sort: search.Sort.ASC,
label: "Date"
})
]
});
invoiceSearchObj.filters.push(search.createFilter({ name: "postingperiod", operator: "ANYOF", values: ["<your period value>"]}));
invoiceSearchObj.filters.push(search.createFilter({ name: "mainline", operator: "is", values : "T"}));
var searchResultCount = invoiceSearchObj.runPaged().count;
log.debug("invoiceSearchObj result count",searchResultCount);
invoiceSearchObj.run().each(function(result){
// .run().each has a limit of 4,000 results
return true;
});
});
var a=0;

ReQL Updating object inside an embedded array emits "create" and "delete" events when listening to changes

I am hitting the following problem: Suppose that I have the following structure:
{
"id": 1,
"data": {
"arr": [{"text":"item1"}]
}
}
And the following query:
r.db('test').table('test').get(1).update(function (item) {
return {
data: {
arr: item('data')('arr').map(function (row) {
return r.branch(
row('text').eq('item1'),
row.merge({updated:true}),
row
)
})
}
}
})
I am listening for changes in this specific array only, and when the item is updated both create and delete events are emitted. I really need to receive an update event, e.g. old_val is not null and new_val is not null.
Thanks in advance guys
After all, I decided to drop the embedded array and use table joins, this avoids all possible hacks.
You can use something like this
r.db('test').table('test')('data')('arr').changes()
.filter(function(doc) {
return doc('new_val').ne(null).and(doc('old_val').ne(null))
})
I'll only show update to array. If you need to get access to other document field, try this:
r.db('test').table('test').changes()
.filter(function(doc) {
return doc('new_val')('data')('arr').ne(null).and(doc('old_val')('data')('arr').ne(null))
})

Resources