fortunejs / fortune-postgres

Postgres adapter for Fortune.
MIT License
23 stars 21 forks source link

How to specify the primary key of a type? #33

Open allthesignals opened 5 years ago

allthesignals commented 5 years ago

Hi, I'm using the json-api serializer with this adapter. JSON API serializer throws an error about toString of undefined— it's trying to stringify the id field received from Postgres, but that field is empty because it doesn't exist in the database.

In this adapter, I can get this to work by changing this and specifying my primary key: https://github.com/fortunejs/fortune-postgres/blob/046b49f0111f27b18e549921b75a2656c38f602c/lib/index.js#L36

I can't figure out how to do this through the public API. Do I need to extend the class to specify a primary key? Would that be the only way to do this?

allthesignals commented 5 years ago

I guess I also don't know where in the stack this should be a concern... do I need to configure my Postgres adapter to specify which column is the "id" column, or do I need to configure my json-api serializer to point to a specific identifier value?

allthesignals commented 5 years ago

Here is my options object... it doesn't appear that "query" or "generatePrimaryKey" ever get called:

  adapter: [
    PostgresAdapter, {
      url: process.env.DATABASE_URL,
      typeMap: {
        project: 'dcp_project',
      },
      generatePrimaryKey(type) {
        console.log('generate primary', type);
        return 'dcp_projectid';
      },
      query: function(query, params) {
        console.log(query, params);
        return query;
      },
    },
  ],
gr0uch commented 5 years ago

Hi @allthesignals,

I don't think I considered adding a customization option to map fields to database columns, which is what I think you are asking for. It seems you have a database table where the primary key isn't id and I didn't anticipate that.