Skip to content

Adding a record when jsonapi server does not accept client generated ids #516

@julfla

Description

@julfla

Hi,

I would like to use orbit with a rails backend using jsonapi-resources.
In order to created a new record, the api expects a POST request which does not include an id.

As explained on JSON api documentation, here is what should happen.

If a POST request did not include a Client-Generated ID and the requested resource has been created
successfully, the server MUST return a 201 Created status code.
The response SHOULD include a Location header identifying the location of the newly created
resource.
The response MUST also include a document that contains the primary resource created.

However, when I call addRecord, orbitjs is automatically generating a uuid and includes it to the post data.

Is there a way to prevent orbit from generating uuid for new records ?
I am getting confused with the remoteId, feature. Is it the way to go ?

Here is what I'm doing, its largely inspired by the Getting started in the documentation.

import Store from '@orbit/store';
import Orbit, { Schema } from '@orbit/data';
import Coordinator, { SyncStrategy, RequestStrategy } from '@orbit/coordinator';
import JSONAPISource from '@orbit/jsonapi';
import 'isomorphic-fetch';

Orbit.fetch = fetch;

const schema = new Schema({
  models: {
    customer: {
      attributes: {
        name: { type: 'string' },
      },
    },
  },
});

const store = new Store({ schema });

const remote = new JSONAPISource({
  schema,
  name: 'remote',
  host: 'http://localhost:3000/api/v1',
});

const coordinator = new Coordinator({
  sources: [store, remote],
});

// Query the remote server whenever the store is queried
coordinator.addStrategy(new RequestStrategy({
  source: 'store',
  on: 'beforeQuery',
  target: 'remote',
  action: 'pull',
  blocking: true,
}));

// Update the remote server whenever the store is updated
coordinator.addStrategy(new RequestStrategy({
  source: 'store',
  on: 'beforeUpdate',
  target: 'remote',
  action: 'push',
  blocking: true,
}));

// Sync all changes received from the remote server to the store
coordinator.addStrategy(new SyncStrategy({
  source: 'remote',
  target: 'store',
  blocking: true,
}));

test('store addRecord does not force remote id', async () => {
  const customer = {
    type: 'customer',
    attributes: {
      name: 'Bob',
    },
  };

  await coordinator.activate();

  // Add record make a post query to the api, with a id gererated by orbitjs.
  // My api expects no id to be present, the server will assign an id and
  // return the created object.
  const createdCustomer = await store.update(t => t.addRecord(customer));
  console.log(createdCustomer);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions