Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealing with MongoDB objectId primary keys? #363

Open
larifari opened this issue Jan 3, 2016 · 5 comments
Open

Dealing with MongoDB objectId primary keys? #363

larifari opened this issue Jan 3, 2016 · 5 comments

Comments

@larifari
Copy link

larifari commented Jan 3, 2016

Hi!

I'm stuck on dealing with primary keys delivered from the mongodb based backend.

The keys look like this:

[ {
  "_id" : {
    "$oid" : "56895f8265db7fcdd18c236e"
  },
  "birthdate" : "19.03.1931",
...
}]

I have tried various mappings, both global and local as mentioned on various parts of the website, issues, etc. But at best I can set $pk to a copy of _id. Savings fails, because _id is empty on PUT.

Thanks!

@gaboesquivel
Copy link

try this #86 (comment)

@larifari
Copy link
Author

larifari commented Jan 4, 2016

I did try, it didn't not work, at least not in the way I expected it to work. Maybe my expectations are wrong?

I expect:

  • a client in a client list view to have a $pk set to the $oid string of the ObjectId
  • a client in the single client view to have a $pk set to the $oid string of the ObjectId
  • the _id element to remain unchanged

What I get:

  • Each client in a list view has a $pk set to the ObjectID ( {$oid: '56895f8265db7fcdd18c236e'}
  • a client in the single client view has a $pk set to the $oid string
  • the contents of the _id element have been deleted

This is problematic because

  1. different format of $pk in list and single instance view means, my directive has to figure out if it was called from a list or single instance view
  2. saving to the backend fails, because the content of the _id is gone

I tried with "_id" and "_id.$oid". I even tried without any configuration at all, the result does not change.
Is there any way to stop it from deleting the contents of the _id?

I tried the following variants (separately, not together ;-)

angular
    .module('client')
    .factory('Client', Client)
    .factory('TestClient', TestClient)
    .factory('Clientsearch', Clientsearch)
    .factory('Clientcount', Clientcount)
    .factory('Clientpaged', Clientpaged)
    .config(config);


  config.$inject = ['restmodProvider'];


  // this is what i used before the backend started throwing ObjectIDs instead of strings
  // variant 0  ->  $pk = Objectid, _id is empty :-(
  function config(restmodProvider) {
    restmodProvider.rebase({
      $config: {
         primaryKey: '_id'
      }
    });
  }

/* 
  // variant 1  -> $pk = Objectid, _id is empty :-(
  function config(restmodProvider) {
    restmodProvider.rebase({
      $config: {
        primaryKey: '_id.$oid'
      }
    });
  }

  // variant 2  -> pushModelBase is not a function - has been renamed to rebase 
  function config(restmodProvider) {
    restmodProvider.pushModelBase(function() {
      this.setPrimaryKey('_id');
    });
  }

 // variant 2a -> $pk = Objectid, _id is empty :-(
  function config(restmodProvider) {
    restmodProvider.rebase(function () {
      this.setPrimaryKey('_id.$oid');
    });
  }

 // variant 2b -> $pk = Objectid, _id is empty :-(
  function config(restmodProvider) {
    restmodProvider.rebase(function () {
      this.setPrimaryKey('_id');
    });
  }

  // variant 3a  -> $pk = Objectid, _id is empty :-(
  function config(restmodProvider) {
    restmodProvider.rebase({
      $config: {
        PRIMARY_KEY: '_id'
      }
    });
  }

// variant 3b -> $pk = Objectid, _id is empty :-(
  function config(restmodProvider) {
    restmodProvider.rebase({
      $config: {
        PRIMARY_KEY: '_id.$oid'
      }
    });
  }
*/

@gaboesquivel
Copy link

lame, this is also stopping me from using angular-restmod

@larifari
Copy link
Author

larifari commented Jan 5, 2016

Well, it doesn't exactly stop me, it just leads to byzantine code I could do without. Just out of curiosity did anything you tried change anything on the outcome? What puzzles me most, is the fact, that whatever I tried it always turned out exactly the same.

@gaboesquivel
Copy link

I opted for js-data-angular

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants