Search code examples
primary-keyjsdatajs-data-angular

Does [js-data] support no primary key?


I have log data that I wish to manage with js-data (http://www.js-data.io/docs/dsdefaults#idattribute) that has no primary key.

Do I need to generate a key or can js-data be configured to allow access to the data without a pk?

Can I get js-data to generate a pk? I have no need to persist this data, just wanted to use the js-data capabilities to query it.

Example:

  $provide.factory('syslog', ['DS', function(DS) {
    return DS.defineResource({
      name: 'log'
    });
  }]);

  $provide.factory('LoggingServices', ['$q', '$filter', '$log', 'syslog', function($q, $filter, $log, syslog) {

    function injectMockLogs () {
      syslog.inject({
        'messages': [
          {
            'time':'2016-03-29 09:32:43',
            'severity':'INFO',
            'user':'carolyn',
            'auth_type':'RADIUS',
            'method':'UI',
            'event_id':1107,
            'message':'In make_radius_request: Making radius request for user carolyn',
            'full_message':'2016-03-29 09:32:44 "info" ns [1107]: RADIUS auth:In continue_radius_auth: Starting RADIUS authentication for user carolyn @ 10.217.22.20'
          },{
            'time':'2016-03-29 09:32:44',
            'severity':'INFO',
            'user':'carolyn',
            'auth_type':'RADIUS',
            'method':'UI',
            'event_id':1107,
            'message':'In make_radius_request: Making radius request for user carolyn',
            'full_message':'2016-03-29 09:32:44 "info" ns [1107]: RADIUS auth:In make_radius_request: Making radius request for user carolyn'
          },{...

Solution

  • In order for data to be injected into the store (which is an Identity Map), the data needs to have some sort of unique identifier. It's okay if you yourself don't need the items to have a primary key, but it is necessary for JSData.

    As of 2.9.0, passing the temporary: true option to inject will cause an id to be generated for each of the items being injected, though your current workaround is a fine approach.