platanus / angular-restmod

Rails inspired REST-API ORM for Angular
http://platanus.github.io/angular-restmod/
MIT License
1.18k stars 87 forks source link

Dealing with MongoDB objectId primary keys? #363

Open larifari opened 8 years ago

larifari commented 8 years ago

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 commented 8 years ago

try this https://github.com/platanus/angular-restmod/issues/86#issuecomment-52977524

larifari commented 8 years ago

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:

What I get:

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 commented 8 years ago

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

larifari commented 8 years ago

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 commented 8 years ago

I opted for js-data-angular