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

id mapping does not work? #11

Closed
akralj opened this issue Jan 22, 2016 · 10 comments
Closed

id mapping does not work? #11

akralj opened this issue Jan 22, 2016 · 10 comments

Comments

@akralj
Copy link

akralj commented Jan 22, 2016

Hi,
my frontend requires a "id" to come from the rest service. I tried all sorts of different options to map _id to id, but no avail. i tried it with 1.3 and 2.0.1. I can do it with before and after hooks, but because you put it in the docs i'm assuming it should be handled by feathers-nedb. Is this assumption correct?
here is my code with feathers-nedb 1.3:

# app.js
feathers = require('feathers')
nedb = require('feathers-nedb')
bodyParser = require('body-parser')
hooks = require("feathers-hooks")
# Initialize NEDB CRUD database service
todos = nedb('todos', {id: "id"})
# A Feathers app is the same as an Express app
app = feathers().configure(hooks())
.use(feathers.static('./server/public')) # __dirname
# Add REST API support
app.configure feathers.rest()
# Configure Socket.io real-time APIs
app.configure feathers.socketio()
# Parse HTTP JSON bodies
app.use bodyParser.json()
# Register the todo service
app.use '/todos', todos
# Start the server
app.listen 3333

and the result of a get request http://localhost:3333/todos/EMVqs9KyTImCIKyE

{
"_id": "EMVqs9KyTImCIKyE",
"text": "A todo",
"complete": false
}

Thanks for your time.

@daffl
Copy link
Member

daffl commented Jan 22, 2016

That was definitely useless documentation on our part. We added it because all the other adapters allow it as well but it looks like NeDB does not support changing the id property name.

I removed it from the documentation and you are correct with your suggestion to use a hook to do the mapping from _id to id.

@akralj
Copy link
Author

akralj commented Jan 22, 2016

excellent. thanks for the clarification.

@sinedied
Copy link

sinedied commented Jan 2, 2019

Is is still true as of today? Because the id param is still in the docs, and not working 😕

@daffl
Copy link
Member

daffl commented Jan 2, 2019

The id option is working as documented and tested (here and here).

@sinedied
Copy link

sinedied commented Jan 2, 2019

I saw the unit tests, but I failed to make it working on a freshly generated project from the CLI:

  • setting id: 'playerId on my service does not make it work as expected:
    • I can create many objects with the same playerId value (not working with _id)
    • the _id property still exists and set/get along playerId
    • service.get() does not work with either playerId value nor _id value (works with _id without the id remap)

I'm not sure what I could have done wrong since it's pretty straightforward. I ended up remapping in pre/post hooks, but that's frustrating :/

@sinedied
Copy link

sinedied commented Jan 2, 2019

Just to complete with some code:

// Initializes the `scores` service on path `/scores`
const createService = require('feathers-nedb');
const createModel = require('../../models/scores.model');
const hooks = require('./scores.hooks');

module.exports = function (app) {
  const Model = createModel(app);

  const options = {
    Model,
    id: 'playerId'
  };

  // Initialize our service with any options it requires
  app.use('/scores', createService(options));

  // Get our initialized service so that we can register hooks
  const service = app.service('scores');

  service.hooks(hooks);
};

@daffl
Copy link
Member

daffl commented Jan 2, 2019

Just like MongoDB, NeDB needs the _id field and there is no way to remove it. Setting id will just let the service know to use that as the primary key and does not guarantee that it will be unique. To do that you will have to create a unique index in NeDB.

@sinedied
Copy link

sinedied commented Jan 3, 2019

Thanks for the clarification 👍 That might be a useful note on the docs regarding this section!
Will try that, the last remaining point is why service.get() wasn't working as expected, but I'll try to find out.

@sinedied
Copy link

sinedied commented Jan 3, 2019

Almost all good, except that trying to create a new entry using a PUT or PATCH method with the upsert option of NeDB enabled does not work anymore:

{
  "name": "BadRequest",
  "message": "You can not replace multiple instances. Did you mean 'patch'?",
  "code": 400,
  "className": "bad-request",
  "errors": {}
}

The request that was working fine before (by mapping playerId to _id with a hook)

PUT http://localhost:3030/scores
Content-Type: application/json

{
  "playerId": "Pierre",
  "score": 4000
}

Any ideas?

@daffl
Copy link
Member

daffl commented Jan 3, 2019

Please create a new issue with an example.

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

No branches or pull requests

3 participants