bi-tm / express-nedb-rest

rest api for nedb database, based on express http server
MIT License
24 stars 13 forks source link

returning a single element #8

Closed CharlesNepote closed 7 years ago

CharlesNepote commented 7 years ago

In NeDB, the function findOne allow to search a single document.

db.findOne({ _id: 'id1' }, function (err, doc) { });

This function is not only useful for the _id field but also for "createdAt" field. I would like to take "createdAt" as unique id because it's more meaningful than the generated "_id". By the time the user should choose its own unique id to retrieve his documents (see below). Also the function is returning a JSON object and not an array. So a function that retrieve a unique element should be very useful.

Some examples to be discussed:

http://127.0.0.1:8100/collection/2017-04-06T08:39:44.016Z # the unique id might be configured in the express-nedb-rest configuration?

http://127.0.0.1:8100/collection/createdAt/2017-04-06T08:39:44.016Z

http://127.0.0.1:8100/collection/document/2017-04-06T08:39:44.016Z # an alias could be configured in the express-nedb-rest configuration : document=createdAt property?

http://127.0.0.1:8100/collection/document/title-chosen-by-user # an alias could be configured in the express-nedb-rest configuration : document=uniqTitle property + a server side control to be sure uniqTitle is always unique

It could be something like that to keep it simple:

// [...]
// create rest api router and connect it to datastote  
var restApi = expressNedbRest();
restApi.addDatastore('test', datastore);
restAPI.options('{
  "uniqueIdAlias": {
     "document": "createdAt"
   }
}');

// or
restAPI.options('{
  "uniqueIdProperty": "title",
  "uniqueIdAlias": {
     "document": "title"
   }
}');

It's just some ideas to be discussed further.

(Torsten I'll try to publish my app on github to let you see my work with express-nedb-rest (which I found great :) Charles.

bi-tm commented 7 years ago

Hello Charles, great ideas, thank you a lot!

I would prefer a solution, which works with the URL, and not with server sided API. I like your suggestion, to append the search field, like .../createAt/2017-04-07T20:05:00Z. But this notation does not fit to the existing notations.

What do you think about $filter in combination with an additional flag i.e. $single? .../collection?$single&$filter=createAt $eq 2017-04-07T20:05:00Z

If you need to check for unique fields, you may use a validation function. I prepared an undocumented api call, with which you can register a callback function to express-nedb-rest. There you may validate the inserted document and throw an error. I will provide you an example, and add it to the documentation.

Thanks for your support Torsten

bi-tm commented 7 years ago

Hello Charles, thank you for your typo corrections. On npm you will find new version v1.2.2. I added the query parameter $single. If you use it, the rest service returns only one document as result, instead of an array. If no document is found, the service returns an error code. Does this meet your requirement? Regards Torsten