Giter Club home page Giter Club logo

express-nedb-rest's People

Contributors

andrenanninga avatar bi-tm avatar charlesnepote avatar markhoney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

express-nedb-rest's Issues

Post and Put not working

In this simple script:


const express = require('express')
const expressNedbRest = require('express-nedb-rest')
const Datastore = require('nedb')

// setup express app
const oApp = express()
// create NEDB datastore
//const nedb = require('nedb');

//const dbUsers = new Datastore({ filename: 'database/users.db', autoload: true })

const dbFruits = new Datastore({ filename: 'database/fruits.db', autoload: true })

// create rest api router and connect it to datastore
const restApi = expressNedbRest()
//restApi.addDatastore('test', dbUsers)
restApi.addDatastore('fruit', dbFruits)

// setup express server to serve rest service
oApp.use('/', restApi);

oApp.listen(8080, function () {
console.log('you may use nedb rest api at port 8080');
});

//http://localhost:8080/fruit


Gets work fine but when I test Puts and Posts with Postman, it only writes the _id field. Any help?

Problem with missing slashes in URLs

I've noticed that if a URL doesn't have a trailing slash, the resulting links generated for the base URL don't have a slash between the URL and the name. For example, if I mount my REST api at:

http://localhost:3000/api/rest

the JSON I get back looks like this:

[{"name":"path","link":"http://localhost:3000/api/restpath"},{"name":"otherpath","link":"http://localhost:3000/api/restotherpath"}]

Whereas if I add a trailing slash to my URL:

http://localhost:3000/api/rest/

I get the correct paths to my collections in the JSON:

[{"name":"path","link":"http://localhost:3000/api/rest/path"},{"name":"otherpath","link":"http://localhost:3000/api/rest/otherpath"}]

Looking at the code, the links are generated like this:

"link": fullUrl(req) + name

It looks simplest to update the fullURL function from this:

function fullUrl(req) {
return req.protocol + '://' + req.get('host') + req.originalUrl;
}

to this:

function fullUrl(req) {
return req.protocol + '://' + req.get('host') + req.originalUrl + (req.originalUrl.endsWith('/') ? '' : '/');
}

With the fix in place, the following line (which also uses fullURL) would need to be changed from:

res.append('Location', fullUrl(req) + '/' + doc._id);

to:

res.append('Location', fullUrl(req) + doc._id);

How to delete by field other than id

I'm wanting to use this module in a project but the items I'd want to delete I would not know the id of. A customer would insert the field and if present it should be deleted . I'm relatively new to rest and xmlhttprequest so not sure if there is a way to do this with your module. The field the customer would be entering would be unique so no concerns about too many or the wrong entry being deleted.

express-nedb-rest and NeDB timestampData option

I'm using the timestampData option of NeDB:

// ---------- create  NEDB datastore
// timestamData creates a "createdAt" field with ISO 8601 timestamp: 2017-04-06T08:39:44.016Z
var datastore = new nedb({ filename: "test.db",  autoload: true, timestampData: true });

But the field createdAt, created by NeDB for each object, is not searchable by express-nedb-rest. When I do http://127.0.0.1:8100/test?$filter=createdAt $regex 2017 the resulted answer is empty.

When I GET all the objects I can see "createdAt":"2017-04-06T08:39:44.016Z".

But when I open my NeDB test.db file, surprise!, the date is stored like that:
"createdAt":{"$$date":1491487725078}

But http://127.0.0.1:8100/test?$filter=createdAt $regex 14 doesn't work either (empty result).

This server side timestamp is very important for me (client side timestamps can be wrong). Any idea?

Routes and views

Maybe it's possible with Express but I don't know how to do that.

I would like to have particular views of the database based on the url. Example: see all the records but without the technical values such as _id and createdAt and updatedAt.

Other case: see and query all the records with { "status": "draft" } based on the url http://127.0.0.1:8100/collection/draft/. Example: http://127.0.0.1:8100/collection/draft?$filter=$author $eq John.

Maybe it's already possible and just a matter of documentation.

returning a single element

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.

There is an error while delete all doc

Note: express-nedb-rest is running on Electron main process.

when DELETE http://localhost:8080/test

Uncaught Exception:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.header (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/express/lib/response.js:719:10)
at ServerResponse.contentType (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/express/lib/response.js:552:15)
at ServerResponse.send (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/express/lib/response.js:139:14)
at /Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/express-nedb-rest/index.js:236:29
at newArguments.(anonymous function) (/Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/nedb/lib/executor.js:29:17)
at /Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/nedb/lib/datastore.js:693:14
at /Users/alexluoli/SVN/Medical/Electron/OT-Training/node_modules/nedb/lib/persistence.js:201:12
at FSReqWrap.oncomplete (fs.js:123:15)

filtering a complex string

Hi Torsten,
I'm trying to query (with my browser):
http://127.0.0.1:8100/test?$single&$filter=uri%20$eq%20https://gist.githubusercontent.com/CharlesNepote/26126a30adddc1781cb652b2af164ea8/raw/92ea63558c3d615ac21c5c31b286fa5f60cf7cd0/prenom-schema.json

the result is:

unvalid $filter Lexical error on line 1. Unrecognized text.
uri $eq https://gist.githubuserco
-------------^

I guess querying with an URL is for simple thing. "put the filter as JSON string into HTTP body" as you suggested earlier, might be a good idea -- would it also work with a browser?
Regards, Charles.

Select data to be retreived

Sometimes we don't want to retrieve all the data we stored in a record.

For example, sometimes we just want to know that a record exist: $filter=uri $eq http://example.com/thispage $select=uri should answer:
{"uri":"http://example.com/thispage"}

Sometimes we just want a few properties. $filter=ref $eq 4675 $select=size,price.euro should answer:
{"size": "38", "price": { "euros": "152"} }

We could even have a HTTP 200 without data when we have $filter=ref $eq 4675 $and size $eq 38 $select=ref.

That way, there would be less data exchanged between clients and server.

$filter issue

Based on this given json:

{
  "_id": "1",
  "datetime": "2017-04-06T08:39:44.016Z",
}

fruits?$filter=datetime $regex 2017 => ok

fruits?$filter=datetime $eq 2017-04-06T08:39:44.016Z => KO

unvalid $filter Lexical error on line 1. Unrecognized text.
datetime $regex 2017-04-06T08:39:44.016Z
--------------------^

fruits?$filter=datetime $regex 2017-04-06T08:39:44.016Z => KO

unvalid $filter Lexical error on line 1. Unrecognized text.
datetime $eq 2017-04-06T08:39:44.016Z
-----------------^

Is it filter.js definition of a word fault?
["\\w+\\b", "return 'WORD';"],

Is it possible to run rest api and serve static files at the same time?

The code below doesn't work for me... Rest API is working ok at 8100 port and "/" path but "/static/index.html" is not served. Could you give an example if it's possible?

var express = require('express'); // https://github.com/expressjs/express
var cors = require('cors');       // https://github.com/expressjs/cors
var nedb = require('nedb');       // https://github.com/louischatriot/nedb/
var expressNedbRest = require('express-nedb-rest'); // https://github.com/bi-tm/express-nedb-rest
var config = require('./config'); // file to store configuration variables

// setup express app
var app = express();

// Enable All CORS Requests (requests from other domains)
app.use(cors());

// ---------- create  NEDB datastore
// timestamData creates a "createdAt" field with ISO 8601 timestamp: 2017-04-06T08:39:44.016Z
var datastore = new nedb({ filename: "schemas.db",  autoload: true, timestampData: true });
// database automatic compaction interval, in millisecond (1000 * 60 * 60 * 24 = 24h)
datastore.persistence.setAutocompactionInterval(1000 * 60 * 60 * 24);

// setup express server to serve static files
app.use('/static', express.static('public'));

// ---------- create rest api router and connect it to datastore
var restApi = expressNedbRest();
restApi.addDatastore('schemas', datastore);

// setup express server to serve rest service
app.use(config.path, restApi);

app.listen(config.port, function () {
  console.log('you may use nedb rest api at port 8100');
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.