Webix and mongodb, id vs _id, without proxy

Dear webix team, i’d like to have your feeling regarding that.

  1. In mongodb, the “_id” field is static, we can not rename it.
  2. In webix datastores, it"s the “id” field which is static.

The most common way to change “_id” into “id” is to use a custom proxy, see change id to _id for rest backend - #3 by mettaworks

The problem is when you use this method, you have to loop on each record.

That’s not a problem if you don’t have huge data. But it can be a problem if you have a huge amount of data, causing browser CPU jumps, UI freezes.

I tried to find another way to do that, and perhaps i found one. Seem’s it make the job.

In top of my code, i’m overriding a Datastore.id prototype function

webix.DataStore.prototype.id = function (data) {
  // let's see if we have a mongodb id
  data.id = data._id || data.id;

  // in the line below, uid() has been changed into webix.uid()
  // because of the scope
  return data.id || (data.id = webix.uid());
}

Below the original function we can find in the webix source code

webix.DataStore.prototype.id = function (data) {
  return data.id || (data.id = uid());
}

That’s all. Just 2 lines changed.

I no longer need a proxy :slight_smile:

Feedback from webix team, to ensure this hack as no effect side possible should be REALLY appreciated.

1 Like

I use the same method without any problem

1 Like

Hello @franck34 ,

As said above, there should be no problem.

1 Like

This is cool. Btw shall i made a suggestion ? put this somewhere in the documentation (in Datastores ?). It’s a win of time for people having to deal with this primary key deal.

Cheers, thank you

Thank you for the suggestion, @franck34 .
The documentation has been updated.

1 Like